Get the player from EntityDeathEvent

Discussion in 'Plugin Development' started by adde, Jul 13, 2013.

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

    adde

    Code:
    Code:java
    1.  
    2. @SuppressWarnings("static-access")
    3. @EventHandler
    4. public void PlayerKilledZombie(EntityDeathEvent event)
    5. {
    6. Entity entity = event.getEntity();
    7. Player player = (Player) event.getEntity();
    8.  
    9. ItemStack[] isword = {new ItemStack(Material.IRON_SWORD)};
    10.  
    11. if(entity.getType().ZOMBIE != null){
    12. if(event.getEntity().getKiller().equals(player)){
    13. if(player.hasPermission("zday.isword.smitehp")){
    14. if(player.getItemInHand().equals(isword) && player.getItemInHand().containsEnchantment(Enchantment.DAMAGE_UNDEAD)){
    15. player.setHealth(player.getHealth() + 2);
    16. if(plugin.getConfig().getString("debug").equals("true")){
    17. player.sendMessage("You just killed a zombie.");
    18. }
    19. }
    20. }
    21. }else{
    22. player.sendMessage("" + event.getEntity().getKiller());
    23. }
    24. }
    25. }
    26.  

    My error is:

    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R2.entity.C
    raftBat cannot be cast to org.bukkit.entity.Player
    at com.zombie.test.ZombieSpawn.PlayerKilledZombie(ZombieSpawn.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
    ... 24 more
     
  2. Offline

    chasechocolate

    What line is the error on? Also, post your whole EventHandler method so we can see what each of your variables are.
     
  3. Offline

    xTrollxDudex

    adde
    event.getKiller() returns he killer. No need for getting the entities killer
     
  4. Offline

    adde

    "ply" is event.getDamager();


    Wops, sorry, didn't see that I used entity instead of event, that's why I got confused.
     
  5. Offline

    nitrousspark

    why are you using EntityDamageByEntityEvent? use EntityDeathEvent
     
    adde likes this.
  6. Offline

    adde

    Someone? Bump... :(
     
  7. check if the entity is not a player before you assume it is. that will give errors.
     
  8. Offline

    adde

    Well, how would I do this?
     
  9. well that is pretty basic stuff though, quick and easy: if(!event.getEntity() instanceof Player) return;
     
    adde likes this.
  10. Offline

    adde

    Okey, now what?
    Should I keep my player variable as it is and see if it works after I added the check?
     
  11. Offline

    xMrPoi

    If its not an instance of a plater, just end the bracket and then make an else statement with event.getKiller()
     
  12. Offline

    adde

    That didn't work :/

    edit:
    check first post, edited it.
     
  13. well now you changed the event... what is it that you want to accomplish exactly?
     
  14. Offline

    adde

    Lol I forgot to change the thread title.

    I want to give 2 hp to the player when they kill a zombie with the required stuff, it worked but printed errors, then I tried to change the code to not get any error and now it doesn't work at all.
     
  15. aha. then do event.getKiller and check if the killer is a player, then do your stuff
     
  16. Offline

    kreashenz

    Can you post your current code now, Adde? (If you remember me)
     
  17. Offline

    adde

    It is at the top.
     
  18. Offline

    kreashenz

    Re-wrote it for you, didn't test it, though
    Code:java
    1. @EventHandler
    2. public void PlayerKilledZombie(EntityDeathEvent event){
    3. Entity entity = (Entity)event.getEntity();
    4. if(entity.getLastDamageCause().getCause() != DamageCause.CUSTOM || entity.getLastDamageCause() instanceof Entity){
    5. Entity ent = (Entity) entity.getLastDamageCause();
    6. if(ent instanceof LivingEntity || ent instanceof Player){
    7. Player player = (Player)entity.getLastDamageCause();
    8. ItemStack isword = new ItemStack(Material.IRON_SWORD);
    9. if(entity instanceof Zombie){
    10. if(player.hasPermission("zday.isword.smitehp")){
    11. if(player.getItemInHand() == isword && player.getItemInHand().containsEnchantment(Enchantment.DAMAGE_UNDEAD)){
    12. player.setHealth(player.getHealth() + 2);
    13. if(plugin.getConfig().getBoolean("debug")){
    14. player.sendMessage("Killed a zombie");
    15. }
    16. }
    17. }
    18. }
    19. } else Bukkit.getLogger().log(Level.INFO, entity.getLastDamageCause().getEntity().toString());
    20. }
    21. }


    Reckon that's fixed to xTrollxDudex 's liking!
     
    adde likes this.
  19. Offline

    xTrollxDudex

    kreashenz
    The last damage casue isnt always and entity :3
    (event.getLastDamageCause().getEntity())
    What if the killer was /kill <player>?
     
  20. Offline

    kreashenz

    xTrollxDudex likes this.
  21. Offline

    xTrollxDudex

    Its mediocre....
    PHP:
    //entity death event
    //whoops, need to check the javadocs again! Its event.getEntity().getKiller()
    if(event.getEntity().getKiller() instanceof Player && event.getEntity() instanceof Zombie){

    Player killer = (Playerevent.getEntity().getKiller();
    ItemStack item = new ItemStack(Material.IRON_SWORD);
    ItemStack inhand killer.getItemInHand();

    if(
    killer.hasPermission("zday.issword.smitehp") && inhand.equals(item) && inhand.containsEnchantment(Enchantment.DAMAGE_UNDEAD) && plugin.getConfig().getBoolean("debug")){

    killer.setHealth(killer.getHealth() + 2);
    killer.sendMessage("Killed a Zombie");

    }}
     
    kreashenz likes this.
  22. Offline

    adde

    Didn't work.
    No error but doesn't print the message or giving me 2 hearts, also tried to remove the getConfig thingy.
     
  23. Offline

    xTrollxDudex

    adde
    Either:
    You didn't register correctly, didn't make the listener correctly
    Didn't op yourself or give permissions
    Didn't move some brackets around
     
  24. Offline

    Zethariel

    xTrollxDudex adde
    I discovered that getKiller has a problem with projectiles. Namely that the killer is the arrow or potion, not the player. This needs much more IF statements to account for player arrows and potions.
     
    adde likes this.
  25. Offline

    xTrollxDudex

    Zethariel
    Interesting! I'll look into that.
     
  26. Offline

    adde

    1. Yes, I did register as I did get an error last time so the listener "worked". I have other methods in same class that works.
    2. I did OP me.
    3. I did move brackets, putting them in if statements each of them but you did &&
     
  27. Offline

    kreashenz

    xTrollxDudex likes this.
  28. The entire code you need:
    Code:java
    1. @EventHandler
    2. public void onDeath(EntityDeathEvent event){
    3. if(event.getEntity().getKiller() instanceof Arrow){
    4. Arrow arrow = (Arrow) event.getEntity().getKiller();
    5. if(arrow.getShooter() instanceof Player && event.getEntity() instanceof Zombie){
    6. giveHealth(arrow.getShooter());
    7. }
    8. }else if(event.getEntity().getKiller() instanceof Player && event.getEntity() instanceof Zombie){
    9. giveHealth(event.getEntity().getKiller());
    10. }
    11. }
    12.  
    13. public void giveHealth(LivingEntity player){
    14. ((Player)player).setMaxHealth(player.getMaxHealth()+2);
    15. }


    well almost, except the health and permission part :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
    adde likes this.
  29. Offline

    adde

    Nvm. Sccch.
     
Thread Status:
Not open for further replies.

Share This Page