Stop Baby Zombies Example

Discussion in 'Wiki Discussion' started by jflory7, May 22, 2015.

  1. jflory7

    jflory7 Retired Moderator
    Retired Benefactor

    Stop Baby Zombies Example
    This is an example of modifying a creature on spawn.
    In this case, if a zombie is spawned, you can either cancel it if it's a baby or change it to an adult.
    To make this code grow the babies, you need to remove the "setCancelled" call.
    Both are shown here.

    Code (Text):

    // When a creature spawns...
    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntitySpawn(CreatureSpawnEvent event) {
          if(event.getEntity().getType() == ZOMBIE) {
                Zombie zombie = (Zombie) event.getEntity();

                // Check for the zombie age.
                if(zombie.isBaby()) event.setCancelled(true);

                // Alternatively, cancel like this:
                event.setCancelled(zombie.isBaby());

               // Alternatively alternatively, make them grow up.
               if(zombie.isBaby()) zombie.setBaby(false);
       
            }
    }