1.16.5 My code not working

Discussion in 'Spigot Plugin Development' started by Nikhil_11, Jul 13, 2021.

  1. Code (Java):
    public void onMobSpawn(CreatureSpawnEvent e) {
            if (mobPot) {
                LivingEntity mob = e.getEntity();
                Random rand = new Random();
                mob.addPotionEffect(new PotionEffect(potions[rand.nextInt(potions.length + 1)], Integer.MAX_VALUE, 5));
                System.out.println("Mob is potioned");
                Bukkit.getConsoleSender().sendMessage("A mob is potioned");
            } else {
                System.out.println("Mob not potioned");
                Bukkit.getConsoleSender().sendMessage("Mobs didn't get potioned");
            }
        }
    This is not working can anyone pls help....
    To be clear nothing is happening....

    Also
    Code (Java):
    Boolean mobPot = true;
        PotionEffectType[] potions = PotionEffectType.values();

    Thnx in advance:)
     
  2. Is it returning output to the console?
     
  3. Did you add @EventHandler before the event? It would look like this:
    Code (Text):
    @EventHandler
    public void onMobSpawn(CreatureSpawnEvent e) {
            if (mobPot) {
                LivingEntity mob = e.getEntity();
                Random rand = new Random();
                mob.addPotionEffect(new PotionEffect(potions[rand.nextInt(potions.length + 1)], Integer.MAX_VALUE, 5));
                System.out.println("Mob is potioned");
                Bukkit.getConsoleSender().sendMessage("A mob is potioned");
            } else {
                System.out.println("Mob not potioned");
                Bukkit.getConsoleSender().sendMessage("Mobs didn't get potioned");
            }
        }
    Also, you need to register the event.
     
  4. No..
     
  5. Lol... Noo i didnt it was a very small mistake was trying to figure out whats happening from so long...
    Thnx so much
     
  6. Ur welcome. Also, you need to make sure your event is registered.
     
  7. Also one doubt is can i get the potion effect a mob has when i hit it with a blaze rod using a onDamageEvent or something like that?
     
  8. You can get a list of potion effects using LivingEntity#getActivePotionEffects(). You might need to iterate over them using a foreach loop.
     
  9. Thnx..:)