Class PlayerDamageEvent

All Implemented Interfaces:
Cancellable

public final class PlayerDamageEvent extends PlayerEvent implements Cancellable
Called when a player takes damage (e.g fall damage).
Example: Prevent player from taking fall damage
1@EventMethod
2public void onPlayerDamage(PlayerDamageEvent evt){
3 //Check if the "cause" is fall damage
4 if(evt.getCause() == PlayerDamageEvent.Cause.FallDamage){
5 //Cancel the event, to the player won't take any damage
6 evt.setCancelled(true);
7 }
8}
  • Method Details

    • setDamage

      public void setDamage(short damage)
      Sets the amount of damage the player will receive.
      Parameters:
      damage - the new damage amount.
    • getDamage

      public short getDamage()
      Gets the amount of damage the player will receive.
      Returns:
      the amount of damage.
    • getPreviousHealth

      public int getPreviousHealth()
      Gets the previous amount of health.
      Returns:
      the old health of the player.
    • getCause

      public PlayerDamageEvent.Cause getCause()
      Gets the "cause" of this damage, i.e the reason why the player takes damage (or in other words, what's causing the damage)
      Returns:
      the reason why the player took damage.
      Example: While the player holds an apple in his hands, he should be immune to lava
      1@EventMethod
      2public void onPlayerDamage(PlayerDamageEvent event){
      3 //Get the currently equipped item (may be null!)
      4 Item item = event.getPlayer().getEquippedItem();
      5
      6 //Check if item is an apple (but first, check if it's not null)
      7 if(item != null && item.getName().equals("apple")){
      8 //Player holds apple in his hand, so we can cancel this event
      9 //(this means the player won't take any damage)
      10 event.setCancelled(true);
      11 }
      12}
    • isCancelled

      public boolean isCancelled()
      Description copied from interface: Cancellable
      Determines if the event is cancelled. If an event is cancelled, it will no longer be executed, but other plugins will still receive the event.

      Please note: If the event is threaded, cancellation has no effect, i.e the event will still be executed.
      Specified by:
      isCancelled in interface Cancellable
      Returns:
      true if the event is cancelled, or false if not.
    • setCancelled

      public void setCancelled(boolean cancel)
      Description copied from interface: Cancellable
      Cancels this event. This means it will no longer be executed, but other plugins will still receive the event.

      Specified by:
      setCancelled in interface Cancellable
      Parameters:
      cancel - set to true if you want to cancel this event.
      Example: Cancel "PlayerEnterAreaEvent", i.e prevent player from entering an area
      1//Listener class
      2public class PlayerListener implements Listener{
      3 @EventMethod
      4 public void onEnterArea(PlayerEnterAreaEvent evt){
      5 //Now the player will not be able to enter the area, i.e.
      6 //he will be teleported back to his old position (outside the area)
      7 evt.setCancelled(true);
      8 }
      9}