Solved EntityDeathListener

Discussion in 'Plugin Development' started by kmccmk9, Jul 4, 2013.

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

    kmccmk9

    Hello, I am trying to use this entity death listener, however it just does not work. Any idea why?

    Code:java
    1. package com.kmccmk9.ZombieSiege;
    2.  
    3. import java.sql.ResultSet;
    4.  
    5. import org.bukkit.Server;
    6. import org.bukkit.entity.Creature;
    7. import org.bukkit.entity.LivingEntity;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.EntityDeathEvent;
    10.  
    11. public class entityListener implements Listener {
    12. //Global Vars
    13. LivingEntity chicken;
    14. Server server2;
    15.  
    16. public entityListener(Server server,LivingEntity chick) {
    17. chicken = chick;
    18. server2 = server;
    19. }
    20. public void onEntityDeath(EntityDeathEvent event)
    21. {
    22. // Who died?
    23. System.out.println("Someone died");
    24. LivingEntity deadentity = event.getEntity();
    25. System.out.println(deadentity + " is dead");
    26. }
    27. }
    28.  
     
  2. Offline

    ERROR372

    One of the first things that pops out to me is that you are missing @EventHandler above your onEntityDeath function. Try that, and see if it works better.
     
  3. Offline

    kmccmk9

    Thank you! That worked so well. Don't know how I missed that. Also, would you know why a comparison of if (deadentity==chicken) would not work? Is it because one is a livingentity and one is a deadentity?
     
  4. Offline

    AmShaegar

  5. Offline

    ERROR372

    From what I can see, they are both of the LivingEntity type. Only thing I can assume is that chicken is not being set. and so it's checking if deadentity==null, which it most likely won't be.

    AmShaegar:

    Not exactly. He is checking to see if the two set entities are the same entity, so he can use ==.
     
  6. Offline

    kmccmk9

    Basically all I want to do is check if my spawned entity was killed.
     
  7. Offline

    ERROR372

    kmccmk9

    Well then you need to handle setting the LivingEntity chicken when you spawn it. I don't see that happening. If you initialized it in another class, you may need to make it public (and possibly also static).
     
  8. Offline

    kmccmk9

    Okay I did that here in my main class, which does work:
    Code:
    chicken = world.spawnCreature(tower, CreatureType.CHICKEN);
    The chicken is a global variable and I pass it to the listener like so:
    Code:
            entityListener eListener = new entityListener(server, chicken);
            Bukkit.getServer().getPluginManager().registerEvents(eListener , this);
     
  9. Offline

    ERROR372

    kmccmk9:

    I've looked up and down the code, not exactly sure where it's going wrong. Does it change/fix it when you use .equals? If not, I'll write up a quick test class and see if I get different results. Much easier to debug when actually running the code =P
     
  10. Offline

    kmccmk9

    Unfortunately it does not, it seems to have no effect. One thing I noticed though, is that when I first start the world, if a entity dies (like from sun) it throws an error dealing with the if statement. Is it possible that once it receives this error it won't work there on? I have had a plugin do that to me once before.

    Much agree btw :)
     
  11. Offline

    ERROR372

    The only reason the listener would stop working is if there was a fatal crash (aka server death). Instead, it would just keep throwing errors for each offending event that correlated to it. I'll keep looking at it. Have to go somewhere in an hour or so. Hopefully can whip up a test class for it quick enough.
     
  12. Offline

    kmccmk9

    Ok great, I appreciate it!
     
  13. Offline

    ERROR372

    kmccmk9

    Alright, so... after doing some quick testing, I'm not sure what's going wrong with your plugin. For me, it works perfectly fine...

    Code:

    Code:
    if(deadentity==chicken)
            {
                System.out.println("bye bye chicken =(");
            }
    Log:

    Code:
    17:10:27 [INFO] Someone died
    17:10:27 [INFO] CraftChicken is dead
    17:10:27 [INFO] bye bye chicken =(
    The only thing I can think is that spawnCreature, CreatureType are deprecated. I changed them all to Entity types from LivingEntity, and did spawnEntity(tower, EntityType.CHICKEN). Maybe try that and see?
     
  14. Offline

    kmccmk9

    Thanks for the response, but if I do this, I have no way of targeting another entity right?
    EDIT: This code makes the zombie target, but the death no longer works. Would it be help if I posted more of my code?

    Code:
    Entity zombie = world.spawnEntity(to, EntityType.PIG_ZOMBIE);
                                ((Creature) zombie).setTarget((LivingEntity) chicken);
     
  15. Offline

    ERROR372

    kmccmk9:

    Really? It works perfectly for me:

    Code:
            chicken = world.spawnEntity(tower, EntityType.CHICKEN);
            Entity zombie = world.spawnEntity(tower, EntityType.PIG_ZOMBIE);
            ((Creature) zombie).setTarget((LivingEntity) chicken);
     
     
    14:15:44 [INFO] ERROR372: Reload complete.
    14:15:44 [INFO] Someone died
    14:15:44 [INFO] CraftChicken is dead
    14:15:44 [INFO] bye bye chicken =(
    The pig zombie targets the chicken, and when the chicken dies, it displays that the chicken did in fact die, with the appropriate message (bye bye chicken =( )
     
  16. Offline

    kmccmk9

    I have no idea what's wrong with my code then. Its exactly the same. Mine sets the target but won't display the kill message. I guess here is my listener:
    Code:java
    1. package com.kmccmk9.ZombieSiege;
    2.  
    3. import java.sql.ResultSet;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Server;
    7. import org.bukkit.entity.Creature;
    8. import org.bukkit.entity.Entity;
    9. import org.bukkit.entity.LivingEntity;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDeathEvent;
    13.  
    14. public class entityListener implements Listener {
    15. //Global Vars
    16. Entity chicken;
    17. Server server2;
    18.  
    19. public entityListener(Server server,Entity chick) {
    20. chicken = chick;
    21. server2 = server;
    22. }
    23. @EventHandler
    24. public void onEntityDeath(EntityDeathEvent event)
    25. {
    26. Entity deadentity = event.getEntity();
    27. if (deadentity == chicken)
    28. {
    29. server2.broadcastMessage(ChatColor.RED + "You have failed! The tower has beenn lost!");
    30. }
    31. else {
    32. //nothing
    33. }
    34. }
    35. }
    36.  

    EDIT: I can post my main if that would help too.
     
Thread Status:
Not open for further replies.

Share This Page