Inside the for loop I aimed to damage other players that was not damaged. So, I made a new event that has a cause which is suicide so that when it damages the player this method will not be called all over again eventually killing the player. I did my research and found out about calling a new entity damage event EntityDamageEvent e = new EntityDamageEvent(p, DamageCause.SUICIDE,(int)event.getDamage()); Bukkit.getServer().getPluginManager().callEvent(e);
to damage the player but it does not seem to work. I need an alternative way to damage my player but not calling this event over and over.
p.Damage(event.getDamage());
When placed inside, the event loops.
@EventHandler
public void onPlayerDamage(EntityDamageEvent event) {
if(event.getEntity() instanceof Player && event.getCause() != DamageCause.SUICIDE ) {
Player damaged = (Player) event.getEntity();
for(Player p :Bukkit.getOnlinePlayers()) {
if(!p.getName().equalsIgnoreCase(damaged.getName())) {
EntityDamageEvent e = new EntityDamageEvent(p, DamageCause.SUICIDE,(int)event.getDamage());
Bukkit.getServer().getPluginManager().callEvent(e);
//double cHealth =p.getHealth()-event.getDamage();
//p.setHealth(cHealth);
}
}
}
The commented code works but when it reaches negative numbers it does not kill the player it only set the players health to a negative number.