1.16.5 Giving potion effect to mob spawns 2?

Discussion in 'Spigot Plugin Development' started by Bluehawk409, Jul 30, 2021.

  1. Hey, I'm trying to give a potion effect to a spawned mob but it seems to spawn 1, then spawn another with the potion effect, instead of just spawning 1 with the potion effect. How do I fix this? Code:
    Code (Text):
    LivingEntity golem = (LivingEntity) p.getWorld().spawnEntity(newlocation, EntityType.IRON_GOLEM);
    golem.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 9999999, 4), false);
     
  2. If you want to add this potion effect to every newly spawned mob. Than you can use CreatureSpawnEvent and then get the entity with Event#getEntity. Then you can also check the spawnReason if you want to affect only natural spawned mobs. (not spawn eggs or plugins) And if you want to affect only specific type of mob, you can check it with Entity#getType.
    So if you want to add weakness 4 to all naturally spawned iron golems, your code would be:
    Code (Java):
    if (event.getSpawnReason() != CreatureSpawnEvent.SpawnReason.NATURAL) // Doesn't affect built golems, you can use BUILD_IRONGOLEM for that.
      return!
    if (event.getEntity().getType() != EntityType.IRON_GOLEM)
      return!

    IronGolem golem = (IronGolem) event.getEntity();

    golem.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, Integer.MAX_VALUE, 4), false);
     
  3. But the thing is, I'm spawning this golem in a block break event, I still want all iron golems to be normal except ones spawned specifically here.
     
  4. Doesn't make sense, could you please explain more, what mob should the potion be added to?
     
  5. an iron golem that is spawned during a block break event.
     
  6. Iron golems don't spawn upon breaking a block. Do you have some other plugin, that spawns iron golem, when you destroy a block?
     
  7. Yes, this is in my Break Listener code. that's why I said "block break event".
     
  8. Show all your listeners whole codes please.