Mob spawning

Discussion in 'Plugin Development' started by FozZeW, Nov 28, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    FozZeW

    What would be most efficient way to disable certain mobs from spawning? I only want zombies to spawn from all hostile mobs. I feel like doing

    Code:
    @EventHandler
    public void onSpawn(CreatureSpawnEvent e){
    EntityType type = e.getEntityType();
    if(type == EntityType.CREEPER ||
                        type == EntityType.SPIDER ||
                        type == EntityType.SKELETON ||
                        type == EntityType.ENDERMAN){
    e.setCancelled(true);
    }
    
    is kinda unefficient to server and may cause lag
     
  2. Offline

    Windy Day

    Something more like this?
    Code:
    @EventHandler
    public void onSpawn(CreatureSpawnEvent e){
    if(! (e instanceof Zombie)) {
    e.setCancelled(true);
    }
    }
    }
     
    Dr_Bunsen likes this.
  3. Offline

    xTrollxDudex

    FozZeW Windy Day
    Canceling mob spawns creates memory leaks, my suggestion is to kill the entity instead of canceling their spawn or removing them.
     
  4. Offline

    Windy Day

    I haven't done anything with mob spawning, I just whipped that up outside of an IDE for FozZeW but thats good to know, thanks.
     
  5. Offline

    xTrollxDudex

    Windy Day
    It's something more experienced secs know ;)
     
  6. Offline

    FozZeW

    Isnt cancelling event and removing entity on spawn same?
     
  7. Offline

    NathanWolf

    I'm curious about this, seems weird. Is there a thread or other reference with more information?
     
  8. Offline

    xTrollxDudex

  9. Offline

    NathanWolf

    Thank you! Very good to know.
     
  10. Offline

    FozZeW

  11. Offline

    xTrollxDudex

    FozZeW
    ??
     
  12. Offline

    bergerkiller

    Killing the Entity does not magically make the resources allocated for that Entity disappear. Using BKCommonLib it is easily possible to do this, but then again, you need to depend:

    Code:
    @EventHandler
    public void onCreaturePreSpawn(CreaturePreSpawnEvent event) {
        if (event.getEntityType() == EntityType.CREEPER) {
            event.setCancelled(true);
        }
    }
    Note how only the Entity Type is passed in with the event, and not the Entity instance itself. This is because this event is fired before actual spawning happens. It is also not accurate, since even though a prespawn might have fired, the creature spawn event is not actually fired in all cases afterwards. Some server logic decides what (allowed) entity to spawn.

    Note that the same event is re-used over and over again to save resources. That means storing the event or using it as final with a scheduled runnable code using it later on does not work.

    Link to the custom event

    The only other way to do this, as linked before, is to disable all mob spawning globally and only allow spawning every now and then, say, every 10 ticks you turn it on for one tick. Turn it on/off by changing the animal/monster spawn limits on the world to 0/nonzero.
     
Thread Status:
Not open for further replies.

Share This Page