Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing damage to a specified player in Bukkit?

I'm trying to make a command that allows you to make any player invulnerable -- that is, god mode.

This is my code so far (though it's all boilerplate)

@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
    if(event.getEntity() instaceof Player) {
        if(godModed.containsKey(event.getPlayer())) {
            //This is where I need the code to go - something to cancel the damage.
        }
    }
}

godModed is a HashMap godModed which contains all the players who are currently godmoded. When they turn off godmode they are removed from the map.

The command itself is working fine - I currently have it send a message to the player who triggered it, and I also have it add the player to godModed if they are not already on. However, I can't figure out how to actually prevent the damage to the player. I want to stop it completely, not just heal them back afterwards; while the latter might work, it could also lead to unforeseen consequences if other mods look at onEntityDamage to trigger things which a godmoded player shouldn't encounter.

like image 588
Nic Avatar asked Oct 06 '22 19:10

Nic


1 Answers

You will want to use event.setCancelled(true).

If the code you have currently is working you must be using the old event API (and an old version of bukkit), I suggest you upgrade bukkit. Code using the new event API would look something like this:

@EventHandler
public void onPlayerDamage(EntityDamageEvent event) {
    if(godModed.containsKey(event.getEntity())) {
        event.setCancelled(true);
    }
}
like image 192
krock Avatar answered Oct 19 '22 07:10

krock