rutgerkok / WorldGeneratorApi

Minecraft Spigot plugin that enables other plugins to customize world generation
MIT License
95 stars 9 forks source link

Bukkit CreatureSpawnEvent not fired when creatures spawn when chunks generated #29

Closed tastybento closed 3 years ago

tastybento commented 3 years ago

Describe the bug When generating chunks, animal spawns do not fire the Bukkit CreatureSpawnEvent, so they cannot be canceled. I'm trying to write a plugin that will limit animal (and monster) spawning to certain areas, but the animals spawned by WorldGeneratorAPI do not seem to be controllable. I don't want to prevent all spawning, just only allow it in a specific area.

To Reproduce Steps to reproduce the behavior:

This is my chunk generator:

        WorldRef wordRef = WorldRef.ofName(worldName);
        chunkGenerator = WorldGeneratorApi
                .getInstance(getPlugin(), 0, 5)
                 .createCustomGenerator(wordRef, generator -> {
                    // Set the noise generator
                    generator.setBaseNoiseGenerator(new YourNoiseGenerator(this, wordRef));
                    generator.getWorldDecorator().withoutDefaultDecorations(DecorationType.SURFACE_STRUCTURES);
                    generator.getWorldDecorator().withoutDefaultDecorations(DecorationType.STRONGHOLDS);
                });

This is used by the WorldCreator:

WorldCreator wc = WorldCreator.name(worldName).type(WorldType.FLAT).environment(env).generator(chunkGenerator).createWorld();

I'm using the YourNoiseGenerator class from your tutorial. The world generates nicely and starts to be populated by animals, etc. However, I have this listener running:

    @EventHandler
    public void onEntitySpawn(CreatureSpawnEvent e) {
        if (e.getEntity().getWorld().equals(world)) {
            plugin.logDebug(e.getEntity());
            e.setCancelled(true);
            e.remove(e.getEntity()); // Should not be required
        }
    }

This should prevent all entries from spawning, but it doesn't. There are some subsequent natural spawning events it blocks, but there are many animals everywhere.

Does the Decorator spawn entities? Is there a way to fire Bukkit events when these entities spawn?

Screenshots

Screen Shot 2021-01-04 at 10 09 13 PM
rutgerkok commented 3 years ago

This isn't a bug in WorldGeneratorApi, that's just how Spigot works nowadays. See the JavaDoc inside the CreatureSpawnEvent over here: https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/event/entity/CreatureSpawnEvent.java#55

        /**
         * When a creature spawns due to chunk generation
         *
         * @deprecated no longer called, chunks are generated with entities
         * already existing. Consider using {@link ChunkLoadEvent},
         * {@link ChunkLoadEvent#isNewChunk()} and {@link Chunk#getEntities()}
         * for similar effect.
         */
        @Deprecated
        CHUNK_GEN,
tastybento commented 3 years ago

Thanks, this helps.