5

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.

Nic
  • 6,211
  • 10
  • 46
  • 69

1 Answers1

4

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);
    }
}
krock
  • 28,904
  • 13
  • 79
  • 85
  • Sorry, I forgot to include the first line. I do have @EventHandler, just copy/pasted it wrong. Thanks for the tip though, that should work. – Nic Nov 30 '12 at 14:48
  • @KrijnToet we do not modify events on that priority. Use HIGHEST. – Lucien Jan 16 '18 at 01:08