Entity -> Player FAIL

Discussion in 'Plugin Development' started by LRFLEW, Jan 16, 2011.

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

    LRFLEW

    ok, there have been many threads about turning entities into players for the EntityDamageEvents, and the consensus is the way to see if an entity is a player is
    Code:
    (entity instanceof Player)
    and to get player is
    Code:
    ((Player)entity)
    problem is I've made a plugin that uses it, and it wasn't working. I tried debugging it with the following code:
    Code:
        @Override
        public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
            plugin.server.broadcastMessage("EntityDamageByEntityEvent called");
            Entity attacker = event.getDamager();
            Entity defender = event.getEntity();
    
            if ( (attacker instanceof Player) && (defender instanceof Player) ) {
                plugin.server.broadcastMessage(((Player)attacker).getDisplayName() + " attacked " + ((Player)defender.getDisplayName()));
                if ( !plugin.PvPstatus((Player)attacker) || !plugin.PvPstatus((Player)defender) ) {
                    plugin.server.broadcastMessage("Attack is bad");
                    event.setCancelled(true);
                } else {
                    plugin.server.broadcastMessage("Attack is good");
                }
            }
        }
    and when activated, attacking a player anounces:
    Code:
    EntityDamageByEntityEvent called
    and that's it.

    This suggests that everybody is wrong about entity to player conversion. I needed to throw that out there. If anybody sees a problem with my code, then tell me and I take it back, but for now, I say everybody (including myself) is wrong.
     
  2. Offline

    Afforess

    The problem could like in several areas's. First off, does the event even work the way it's supposed to? Bukkit is still early in development, both entity's could be null. Check for null and add a print statement there.

    Then, assuming the entities aren't null, they might be the wrong one's. Print each entity ID with your first print statement, and compare it to the entity id given by the console when a player logins to the server (each time a player logs on, the console prints "Afforess [some ip address] entity id [some number]. Check and see that they are the right ID's.

    There could be a whole host of other issues too.
     
  3. Offline

    LRFLEW

    ...or a player entity isn't an instance of a player
    Code:
    ("Hello World" instanceof string)
    returns true.
    Code:
    (entity instanceof Player)
    will never return true.

    (according to my understanding of instanceof)
     
  4. Offline

    Afforess

  5. Offline

    LRFLEW

    Ok, let's imply this statement is true. That would mean that the statement
    Code:
    (!plugin.PvPstatus((Player)attacker) || !plugin.PvPstatus((Player)defender)
    would return true, running the if statement. If the if statement ran and no other messages were announced, then something had to have happened between or during the next announcement. Since the next line is an announcement, any errors would occur here.
    Code:
    ((Player)attacker).getDisplayName() + " attacked " + ((Player)defender.getDisplayName())
    The only possible problem here would be with the
    Code:
    ((Player)attacker)
    and
    Code:
    ((Player)defender)
    which you said works. On top of that, the log returned ABSOLUTELY NO ERRORS, meaning if something is in there preventing this line from printing right, it wasn't called.

    I rest my case.
     
  6. Offline

    Reil

    It's MUCH more likely that the hook isn't triggering when a player is hurt. I've been trying things out. Punching sheep doesn't set things off, but the hook will go crazy sometimes, when I'm not doing anything. Maybe something somewhere else is getting hurt, but it won't register if I jump off something or punch an animal during a 'quiet' time.
     
  7. Offline

    Afforess

    See Reil's reply. Bukkit it still not 100% done, which is likely the source of your issues. As far as I'm aware, the damage hooks do not yet work reliably. Check the Bukkit roadmap.

    But feel free to keep up your vendetta.
     
  8. Offline

    barghest

    Why don't you post your source code, OP? If you are correct, it sounds like your code would be a fine test/example case; if you are messing up your code, someone will probably tell you how.
     
  9. Offline

    Plague

    I think the problem lies here: bug 188
     
  10. Offline

    LRFLEW

    see first post. If you want the whole project, I will put it up later tonight.
    --- merged: Jan 17, 2011 2:45 PM ---
    Hence the test to see if it is called. the whole
    was a way of testing it, which proved it ran. Next.
     
  11. Offline

    Vollch

    I think hooks just not ready yet. I have:
    Code:
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            if(event.getEntity() instanceof Player)
                log.info("Entity");
        }
    
    And it work, but only for creeper blow.
     
  12. Offline

    Reil

    Yeah, I did
    Yeah, I did the exact same thing. It would call in random huge chunks when I was doing nothing, and then very specifically not happen when I punched a sheep or jumped off a cliff. Next.

    Build 51, I am successfully registering "Player Pain" with the following code. But only when damaged by lava.
    Code:
        public void onEntityDamage(EntityDamageEvent event) {
            rTriggers.log.info("Pain.");
            if (!(event.getEntity() instanceof Player)) return;
            rTriggers.log.info("Player pain.");
            Player damaged = (Player) event.getEntity();
            if (!event.isCancelled() &&  event.getDamage() >= damaged.getHealth()){
                rTriggers.log.info("Player dead.");
                onEntityDeath(event);
            }
        }
    "Pain" is not displayed when jumping off cliffs, punching a monster, drowning, getting hit by creepers, getting hit by monsters, let alone "Player pain." "Pain" is, however, registered by monsters being set on fire/dying by lava. "Player pain" registers properly when I jump into a lava pool/touch fire.

    TLDR; My tests are more extensive, there are more points of possible failure in your test other than the entity passed failing to be a player.
     
  13. Offline

    LRFLEW

    I have experienced problems with the EntityDamageEvents with sheep and fall damage, but not with PvP. Plus, P v P, Mob v P, and Drowning don't call onEntityDamage, only their respective calls (onEntityDamageByEntity, onEntityDamageByBlock, ect.). My tests were specifically focused on PvP anyways, not all EntityDamageEvents. As for random messages appearing, it appears to be a mob being attacked by fire or cactus or lava. I doubt it's random.
    --- merged: Jan 17, 2011 7:55 PM ---
    That's what she said.
     
  14. Offline

    Reil

    Oh yeah, I figured that out a while ago (hence lack of mentioning previously). The map I was testing on had a convenient sea-level lava pool right by the spawn. I also find it silly that EntityDamage events don't get pushed the other damage events. It should be on all damage events, not just the 'leftovers.'
     
  15. Offline

    DjDCH

    Hum ... I think it still not ready yet because it doesn't work in my testing.
     
  16. Offline

    Plague

    instanceof works in damage events for a few releases now
     
  17. Offline

    DjDCH

    I use the EntityDamageByEntityEvent and it never get triggered when a player hit a mob.
     
  18. Offline

    Plague

    Code:
    public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
            Entity entity = event.getEntity();
            if (entity instanceof Player)
                event.setCancelled(true);
        }
    No player can hit me, I can hit mobs.
    CB 232
    I remember that in the past this got triggered, but since then I turned mobs off.
     
  19. Offline

    DjDCH

    No. I have test with different settings, with loggin any call to the onEntityDamageByEntity method, and it never get triggered.
     
  20. Offline

    fullwall

    Actually, my DeathStick plugin calls EntityDamageByEntity, and it works just fine :). Bit of lag between calls though.
     
  21. Offline

    DjDCH

    Strange. I will test your plugin and check out your code to see what doesn't work with mine.
     
  22. Offline

    Byteflux


    Based on some code I've written that relies on these events, I can confirm EntityDamageByEntityEvent works accurately as well (all of the EntityDamageEvent events work actually). Make sure you've got a recent bukkit/craftbukkit.

    Another good plugin to check out for examples would be the DeathTpPlus plugin, which makes use of all the entity damage events.
     
Thread Status:
Not open for further replies.

Share This Page