Having trouble adding potion effects to mobs

Discussion in 'Plugin Development' started by Theztc, Feb 17, 2021.

  1. Offline

    Theztc

    Hi, I'm having trouble adding a potion effect to a zombie. After an hour of searching on Google, I still can't figure it out.

    Here's what I have
    Code:
    public void event(CreatureSpawnEvent event) {
            if (event.getEntityType() == EntityType.ZOMBIE) {
                LivingEntity zombie = event.getEntity();
                zombie.getEquipment().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
                zombie.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 999999, 1, true));
            }
        }
    All the zombies spawn with a diamond helmet, but I can't get the potion effect to apply.
     
  2. Offline

    Kars

    That is because the event code gets handled before the mob actually spawns.
    Schedule a task 1 tick in the future to add the effect to the mob, once it has spawned.
     
  3. Offline

    Theztc

    I just tried for an hour to get a runnable to work for this, do you have any examples I can work off of?
     
  4. Offline

    Kars

Share This Page