I am currently making a plugin and would like to give the player some potion effects such as slowness when they use a totem of undying.
This is what I've tried so far. It runs when an entity dies without a totem, but when an entity dies with a totem, it will send the message "Totem has been Used" but will not give the effects.
@EventHandler
public void OnUseTotem(EntityResurrectEvent event)
{
Bukkit.getLogger().info("TOTEM USED");
event.getEntity().sendMessage("TOTEM HAS BEEN USED");
Entity entity = event.getEntity();
entity.sendMessage("Activate");
entity.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 200,10),true);
}
This type of issue appear because the entity isn't ready yet to respawn (as you can cancel event for example. It's like "pre" event.
And as when you're dead/respawning, you can't have potion effect, you don't have one)
To fix this, you can apply this but few times after thanks to BukkitScheduler
:
@EventHandler
public void OnUseTotem(EntityResurrectEvent event) {
Bukkit.getLogger().info("TOTEM USED");
Entity entity = event.getEntity();
entity.sendMessage("TOTEM HAS BEEN USED");
entity.sendMessage("Activate");
Bukkit.getScheduler().runTaskLater(myPlugin, () -> {
entity.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 200,10),true);
}, 1);
}
Wait 1 tick should be enough. You can set "0", it will just wait for next tick. You can set more to be sure (as 20 tick, so 1 second)