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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With