Help With onEntityDeath()

Discussion in 'Plugin Development' started by azoundria, Feb 1, 2011.

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

    azoundria

    In my main plugin variables:

    Code:
        private final EntityListenerClient entityListener = new EntityListenerClient(this);
    In my onEnable():

    Code:
        public void onEnable() {
    
    Finally, in my EntityListenerClient (my implementation of EntityListener):
    
    [CODE]public class EntityListenerClient extends EntityListener {
    
    My code for entities attacking entities works fine, but nothing happens at all when an entity is killed (as tested by using the /kill command on my player and various other suicide attempts).
    
    Can anyone see what I have done wrong?
     
  2. Offline

    Timberjaw

    The code you've included here is missing:
    1. Event registration [ Like: pm.registerEvent(Event.Type.ENTITY_DEATH, entityListener, Priority.Normal, this); ]
    2. The event listener [ Like: public void onEntityDeath(EntityDeathEvent e) { /* Your code here */ } ]
     
  3. Offline

    Afforess

    Even in the very latest CraftBukkit, Entity_Death only fires for killed players. Nothing else is implemented yet.
     
  4. Offline

    Mixcoatl

    You'd probably have better luck handling the onEntityDamageByEntity and onEntityDamageByProjectile events and testing whether sufficient damage was done to kill the defender.
     
  5. Offline

    Snowl

    Not exactly ;) This works fine for me for a spider:

    Code:
       public void onEntityDeath(EntityDeathEvent event)
        {
            if((event.getEntity() instanceof CraftSpider))
            {
                if(event.getEntity() == null){
                    return;
                }
    
                    //code here
               }
         }
     
  6. Offline

    azoundria

    Okay I'm trying to make it so you spawn at a certain position after death, by teleporting you there. I would really love an onSpawn() but it doesn't exist yet. So I'm using onDeath().

    As for my code, I'll try again to include it. If it disappears again, I'm not sure what to do. This time, I'm using QUOTE instead of CODE.

    I'm not even seeing 'An entity died!' printed out to the command line when I kill myself with /kill. My code to stop attacks in towns works perfectly.
     
  7. Offline

    Timberjaw

    What CraftBukkit version are you using?
     
  8. Offline

    Snowl

    Do this:
    Code:
       public void onEntityDeath(EntityDeathEvent event)
        {
            if((event.getEntity() instanceof CraftPlayer))
            {
                if(event.getEntity() == null){
                    return;
                }
                 System.out.println("A player died :(");
               }
         }
     
  9. Offline

    azoundria

    My sincere apologies. I was using an outdated version of Bukkit. I was sure I had upgraded, however I may have misjudged how long ago that was.
    --- merged: Feb 2, 2011 6:41 AM ---
    However, I need the event to fire when you respawn instead. Right now it works well so you die then move quickly to your hometown, but when you hit the respawn button, of course you go back to the spawn point.
    --- merged: Feb 2, 2011 6:42 AM ---
    Does anyone have any genius ideas for me?
     
  10. Offline

    Timberjaw

    I've been trying to deal with the respawn issue myself. I haven't found a workaround yet, as the onPlayerTeleport event seems to be bugged (can't cancel/redirect the event, at least not if it's a respawn), and the death events are pre-respawn and as such too early to modify the player's destination.
     
  11. Offline

    Afforess

    Uh - I'm fairly sure null references can't also be type instances of an object in Java. So event.getEntity() will never BOTH be null AND a instance of CraftPlayer.

    Also - why use CraftPlayer? Unless he needs to something funny with obfuscated source, I don't see the need to use it over just regular Player.

    My only genius idea is to create a new thread at the moment of death, to teleport a player in 500 or so ms after death.
    Code:
      public void onEntityDeath(EntityDeathEvent event)
        {
            if((event.getEntity() instanceof Player))
            {
               final Player player = (Player)event.getEntity();
               new Thread
                        @Override
                       public void run() {
                           try{
                                sleep(1000); //waits for 1 second (1000ms = 1s)
                                player.teleportTo(//some location);
                            }catch(InterruptedException ex){}
                        }
                   }.start();
              }
        }
     
  12. Offline

    Mixcoatl

    That was a good catch. The specification indicates that instanceof must return false for a null reference. So we probably want something more like:
    Code:
    public void onEntityDeath(EntityDeathEvent event) {
        if (event.getEntity() instanceof Player) {
            System.out.println("A player died :(");
        }
    }
     
  13. Offline

    Timberjaw

    .getEntity() on an EntityDeathEvent returns an object of type CraftPlayer if the entity is a player.
     
  14. Offline

    azoundria

    You can use CraftPlayer in your code if you want as well. It also works. For me, I don't need any of the extra functionality, and I've always been using Player.

    I'm going to see if onPlayerMove() fires when you respawn.
     
  15. Offline

    Snowl

    That code works in my plugin. Player didnt work so I used CraftPlayer
     
  16. Offline

    Mixcoatl

    The CraftPlayer class implements the Player interface. It's generally regarded as inappropriate to refer to a concrete implementation when there is an appropriate interface. Unless there's a specific field in the CraftPlayer class that one needs and cannot access through the Player interface, one should use the Player interface to refer to their players.
     
  17. Offline

    Afforess

    Updated my post with my only real idea here - it assumes that Entity_Death even works at all though.
    --- merged: Feb 2, 2011 6:57 AM ---
    That's what I thought, and why I brought it up.
     
  18. Offline

    Mixcoatl

    Based upon the code on GitHub, it's not possible for x instanceof CraftPlayer to return true if x instanceof Player returns false.
     
  19. Offline

    Timberjaw

    I see. I was using CraftPlayer because that's what I saw .getEntity() returning. Good to know about Player.
     
  20. Offline

    Snowl

    Alright. I'm still learning java so yeah :p
    Entity_Death does work, it was implemented in something like #182
    --- merged: Feb 2, 2011 7:05 AM ---
    This.
     
  21. Offline

    Mixcoatl

    It doesn't break anything to test CraftPlayer. But if the underlying code changes, your dependent code may break. Testing the interface guarantees that your code won't break as long as the implementation adheres to the interface. Interfaces are really important in Java.
     
  22. Offline

    highz

    The Death Hook is disabled.
    I have faked the DeadHook and i am thinking of a way to share it :) (Importable JAR)
     
  23. Offline

    azoundria

    So the best results I've had so far come from creating a death_list, of all players who are dead. I add each player who dies to that list. When a player moves, if they are on the death_list, then they get teleported to their hometown (and removed from the list).

    Unfortunately, onPlayerMove() does not like this. I get [WARNING] azoundria moved wrongly if the new spawn is any significant distance from the real spawn. This happens regardless of whether I event.setCancelled(true); or not on the onPlayerMove(). If your hometown is near the real spawn it works quite nicely.

    So I need a way that onPlayerMove() will not get mad when I teleport the player inside it, or a way to execute a command right after onPlayerMove() is finished. Or something of this nature, because when onPlayerMove() reports that [WARNING] it cancels the move altogether, and so the teleport doesn't happen.
     
  24. Offline

    Snowl

    add this at the start of your teleport: event.setFrom(block.getLocation()); to stop those errors
     
Thread Status:
Not open for further replies.

Share This Page