Endless loop when spawning entities

Hey there,

I am trying to create an event, where for every zombie spawning in the world there are spawning 1-5 extra. I am using this code

@EventHandler
    public void onSpawn(EntitySpawnEvent event) {
        Location location = event.getLocation();
        Random random = new Random();

        switch (event.getEntity().getType()) {
            case ZOMBIE:
                int r = random.nextInt(6);
                for(int i = r + 1; i > 0; i--) {
                    event.getEntity().getWorld().spawnEntity(location, EntityType.ZOMBIE);
                }
                break;
        }
    }

But the server is crashing everytime I try to run this. I think the extra spawning zombies also trigger the event therefore spawning thousands of them. How can I fix that?

Check the spawnreason.
Natural (the case you need) Vs. Custom(the case you trigger with you spawnEntity call)

1 Like

Thank you, I didnt knew about this feature