Getting when a player is damaged

Discussion in 'Resources' started by Jaker232, Feb 9, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Jaker232

    I have seen sometimes when people ask for code on how to find if the player is damaged. It's really easy. Just follow these steps.
    1. In your event listener class, implement Listener at (org.bukkit.event.Listener) and create a new constructor and make sure it looks something like this.
    Code:java
    1.  
    2. package me.Name.Plugin;
    3.  
    4. public class EventListener implements Listener {
    5.  
    6. public Plugin plugin;
    7.  
    8. public EventListener(Plugin instance) {
    9. plugin = instance;
    10. Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
    11. }
    12. }
    13.  

    This will create a new instance of EventListener connected to your main class and register the events. All you have to do is 'new EventListener(this)'. Change the class name to your main class. Using this outside your package, you'll need to import it.

    2. Now you'll want to create the event method, which will be called. Earlier in this year, Bukkit has done a major overhaul on the Event system, possibly breaking thousands of plugins. Good thing they let us have time to fix our plugins.
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.NORMAL)
    3.  

    This must be in the class before the event method. Now you'll want to do the following..
    Code:java
    1.  
    2. public void dmg(final EntityDamageEvent event) {
    3. }
    4.  

    This will register the EntityDamageEvent event onto that method. Now you'll want to catch the entity that has been damaged.

    3. So, you have setted up your class and your method, let's run down!
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.NORMAL)
    3. public void dmg(final EntityDamageEvent event) {
    4. Entity e = event.getEntity();
    5. }
    6.  

    This code will return a warning for the variable 'e' because it has been never used locally. Ignore this.
    You will want to do some checking. If you are using god mode for all players, I'd prefer doing this:
    Code:java
    1.  
    2. if(e instanceof Player) event.setCancelled(true);
    3.  

    The code above will save you some trouble.
    You'll want to do this then cast Player to Entity.
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.NORMAL)
    3. public void dmg(final EntityDamageEvent event) {
    4. Entity e = event.getEntity();
    5. if(e instanceof Player) {
    6. Player player = (Player)e;
    7. }
    8. }
    9.  

    Now you can mess around what you can do with it. Good luck! Think outside the 'block' and make wacky plugins.

    EDIT: thehutch was nice for us to provide some more specific causes of damage. It's the post directly under this one.
     
  2. Offline

    thehutch

    There are also sub events for this one eg:

    EntityDamageByEntityEvent (Entities)
    EntityDamageByProjectileEvent (Arrows/ idk about eggs/snowballs)
    EntityDamageByBlockEvent (Gravel/sand falling on player)

    To check if this event is used use:

    Code:
    if (event instanceof EntityDamageBy...Event) {
        // do stuff
    }
     
    ThatBox likes this.
  3. Offline

    Jaker232

    The developer could use cause of damage. I'll make sure I'll note you in the topic.
     
  4. Offline

    Ibix13

    Thank you :) helped me a lot
     
  5. Offline

    SgtPunishment

    Just wondering if there was a special one for Enderpearls?
     
  6. Offline

    Skyshayde

    No, closest thing you have is checking if the damage is fall damage . Now, if they take 2.5 hearts of damage right after throwing an enderpearl, which has ProjectileLaunchEvent...
     
  7. Offline

    chasechocolate

    Skyshayde or PlayerTeleportEvent and check if the event.getCause() is TeleportCause.ENDER_PEARL.
     
  8. Offline

    SgtPunishment

    Okay I'm checking the event, now I just need to disable the damage, any clues?
     
  9. Offline

    Jatoc

    To keep the hurt aanimation, use event.set damage(0).

    To do absolutely nothing (no hurt animation or damage)
    Use event.set cancelled(true)
     
  10. Offline

    SgtPunishment

    Thanks for that, I figured it out
     
Thread Status:
Not open for further replies.

Share This Page