Message sending more than once.

Discussion in 'Plugin Development' started by HeadGam3z, Jul 2, 2014.

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

    HeadGam3z

    Firstly, listener:
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(EntityDamageByEntityEvent e) {
    3. if (e.getEntity() instanceof Player) {
    4. if (e.getEntity().getLastDamageCause().getEntity() instanceof Player) {
    5. // if (e.getDamager() instanceof Player) { (remove later, not needed)
    6. Player p = (Player) e.getEntity();
    7. if (p.getLastDamageCause().getEntity() instanceof Player) {
    8. System.out.println("Entity was a player2");
    9. if (p.getHealth() - e.getDamage() <= 0) {
    10. p.sendMessage("send once, pl0x");
    11. System.out.println("test");
    12. return;
    13. } else {
    14. System.out.println("Player is not dead yet");
    15. }
    16. } else {
    17. System.out.println("The last damage cause is NOT a player!");
    18. }
    19. } else {
    20. System.out.println("The damager is NOT a player!");
    21. }
    22. // } else {
    23. // System.out.println("The entity damage is NOT a player!"); (remove later, not needed)
    24. // }
    25. } else {
    26. System.out.println("The entity is NOT a player!");
    27. }
    28. }


    When the player dies, it sometimes sends the player "send once, pl0x" more than once - but other times it doesn't; mainly just with swords/bows/any-ouchy-weapons.
    Is there a different condition I could use besides if (p.getHealth() - e.getDamage() <= 0)? Because I think this isn't the way to go...

    Thanks for the help! :)
     
  2. Offline

    xTigerRebornx

    HeadGam3z Why not just use Player/EntityDeathEvent? Damage events don't account for any damage resistance, so that code isn't very reliable (event could say 10 dmg while armor and resistance could knock it down to like 2)
     
    HeadGam3z likes this.
  3. Offline

    HeadGam3z

    xTigerRebornx
    Thanks for the info. I'll mess around with that event when I get home. :)

    xTigerRebornx
    Not sure if EntityDeathEvent is the way to go after-all, or maybe I'm misunderstanding this.

    So I did some work with the event, got some success, and then of course some failures. I did some debugging and stumbled upon e.getEntity() and e.getEntity().getLastDamageCause().getEntity() are the same when I killed my alt account with my main account.

    I.e, if HeadGam3z kills MindGam3z (my alt), the EntityDeathEvent thinks MindGam3z killed MindGam3z, which doesn't make a whole lotta sense to me. I know this because the code under the condition (if p.getUniqueId() != k.getUniqueId()) never has been ran yet, always just goes to the else statement.

    Take a look at this:
    Code:java
    1. public void onPlayerDeath(EntityDeathEvent e) {
    2. if (e.getEntity() instanceof Player) {
    3. if (e.getEntity().getLastDamageCause().getEntity() instanceof Monster) { // pretty sure this isn't doing anything...
    4. System.out.println("Died by a monster");
    5. return;
    6. }
    7. if (e.getEntity().getLastDamageCause().getEntity() instanceof Player) {
    8. Player p = (Player) e.getEntity();
    9. Player k = (Player) e.getEntity().getLastDamageCause().getEntity();
    10. System.out.println(p);
    11. System.out.println(k);
    12. // p and k both print e's name (in this case, CraftPlayer{name=MindGam3z})
    13. if (p.getUniqueId() != k.getUniqueId()) {
    14. System.out.println("test"); // hasn't showed up once.
    15. return;
    16. } else {
    17. System.out.print("Entity that died was a player, and killed theirself."); // 99.99% shows up.
    18. }
    19. } else {
    20. System.out.println("Killer is not a player");
    21. }
    22. } else {
    23. System.out.println("Entity that died is not a player, ignore this.");
    24. }
    25. }


    Shouldn't p's (the player entity that died) UUID be different from k's (the player who killed the player entity, p) UUID?

    I have, however, tried to use different events and just use getKiller(), but it returns a player (or null). Which isn't going to work for me, because if HeadGam3z hits MindGam3z, then a zombie gets the kill on MindGam3z, it still thinks HeadGam3z got the kill, even though the zombie got the kill. Different issue, though, lol.

    Any light that you can shed on this would be great, thanks. :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    Gater12

    HeadGam3z
    Tried using equals() method, yet?
     
  5. Offline

    HeadGam3z

    Gater12
    Yes, the UUID condition was once if (!p.getUniqueId().equals(k.getUniqueId())).
     
  6. Offline

    xTigerRebornx

    HeadGam3z EntityDamageEvent#getEntity() returns the damagee, and afaik, that is the Entity that got damaged. not the Entity that damaged. I believe the last damage cause can be an EntityDamageByEntityEvent, which you can get that 'damager' Entity.
     
  7. Offline

    HeadGam3z

    Okay, got it.
    Are you suggesting I change my event to the EntityDamageByEntityEvent?
     
  8. Offline

    xTigerRebornx

    HeadGam3z Not exactly, Entity#getLastDamageCause() returns a EntityDamageEvent, meaning it can be an EntityDamageEvent or any of its subclasses, which means it can possibly be an EntityDamageByEntityEvent, which has a way of getting the attacker.
     
  9. Offline

    HeadGam3z

Thread Status:
Not open for further replies.

Share This Page