EntityDamageByEntityEvent Help

Discussion in 'Plugin Development' started by Disturbed_Creator, Sep 26, 2013.

Thread Status:
Not open for further replies.
  1. So I am making a PvPLogger plugin, and I need a way to account for an instance where a player is attacked, but no damage is done.

    Code:java
    1. @EventHandler
    2. public void onPvP(EntityDamageByEntityEvent e)
    3. {
    4.  
    5. if(!(e.getEntity() instanceof Player))return;
    6. if(!(e.getDamager() instanceof Player))return;
    7.  
    8. final Player p = (Player) e.getEntity();
    9. final Player t = (Player) e.getDamager();
    10.  
    11. if(!combatList.contains(t.getName())){
    12.  
    13. t.sendMessage("§f[§bRadical§7Tag§f]§b "+"You are now in combat§f!");
    14.  
    15. }
    16.  
    17. if(!combatList.contains(p.getName())){
    18.  
    19. p.sendMessage("§f[§bRadical§7Tag§f]§b "+"You are now in combat§f!");
    20.  
    21. }
    22.  
    23. combatList.add(p.getName());
    24. combatList.add(t.getName());
    25.  
    26. new BukkitRunnable(){
    27.  
    28. @Override
    29. public void run()
    30. {
    31.  
    32. combatList.remove(p.getName());
    33. combatList.remove(t.getName());
    34.  
    35. if(!combatList.contains(t.getName())){
    36.  
    37. t.sendMessage("§f[§bRadical§7Tag§f]§c "+"You are no longer in combat§f!");
    38.  
    39. }
    40.  
    41. if(!combatList.contains(p.getName()))
    42. {
    43.  
    44. p.sendMessage("§f[§bRadical§7Tag§f]§c "+"You are no longer in combat§f!");
    45.  
    46. }
    47.  
    48. }
    49.  
    50. }.runTaskLater(plugin, 10 * 20L);
    51. return;
    52.  
    53. }


    Thanks :>
     
  2. Offline

    mydeblob

    Disturbed_Creator
    I don't know if this is the best way to do it, but you could check for individual entities. For example if you wanted to check if a player got hit by a snowball..
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    3. Entity entity = event.getDamager();
    4. if (entity instanceof Snowball) {
    5. //do stuff here
    6. }
    7. }
     
  3. mydeblob
    Thank you, however I don't think you understood me. With plugins like WorldGuard, you can have pvp free regions. In these regions, if you attack a player it will still act as if you did damage. I am looking to have a return statement if no damage is dealt.
     
  4. Offline

    1Achmed1

    I'm not in front of Eclipse right now but...
    Code:java
    1. if (p.health == 20) {
    2. return null;
    3. }
    4.  
     
    Disturbed_Creator likes this.
  5. 1Achmed1
    Yeah.. that works... but what if a player walks into a pvp free zone without having 20 health?
    Thank you though :>
     
  6. Offline

    1Achmed1

    I'm not sure on how to hook into world guard...
     
  7. Offline

    mattrick

    Maybe use => 20 health?
     
  8. Offline

    1Achmed1

    Not possible. The player has a maximum 20 health. Each heart is 2.
     
  9. 1Achmed1 mattrick16

    I would like to check if a player receives no damage and the event is triggered. Do you know of a way to do that?
     
  10. Yes, But you can change the max health of a player.
    Maybe do this?
    if(!e.isCancelled()){
    //rest of code
    }
     
  11. Offline

    1Achmed1

    Use This code, but put it in a method, like:
    Code:java
    1. public boolean checkHealth() {
    2. if (p.health == 20) {
    3. return false;
    4. }
    5. }
    6.  


    Then, in your listener call the method

    Please use
    [syntax=java]
    It helps :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  12. its 2 lines of code. rather not
     
  13. RealmsofValendor

    That code checks if the event wasn't cancelled. Isn't that pointless? Please tell me if I am missing something.
     
    1Achmed1 likes this.
  14. If I'm thinking correctly. WorldGuard cancels the event.. for worldguard, Not your plugin. So if you do that I'm pretty sure it checks if any plugins have cancelled the event before looking at your code any further. Idk, my current project is all 100% custom made.
     
  15. Offline

    1Achmed1

    I don't think you understand events. Or, frankly, the
    Code:java
    1. !


    We're talking about if the player enters a pvp free zone. We aren't checking for events happening in world guard.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  16. Btw,
    Code:java
    1.  
    2. @EventHandler
    3. public void onPvP(EntityDamageByEntityEvent e){
    4. if(e.getDamage() == 0)return;
    5. }
    6.  
     
    Disturbed_Creator likes this.
  17. RealmsofValendor
    If WorldGuard does cancel the event, wouldn't it mean that it shouldn't run the event at all?

    Nope.. that was the first thing I tried...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  18. I can't find any information on how cancelling events works. Why not try it?
     
Thread Status:
Not open for further replies.

Share This Page