getKiller Player

Discussion in 'Plugin Development' started by Mother__, Mar 12, 2013.

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

    Mother__

    this code working but have conflict when the player and killed by another player like this
    Code:java
    1.  
    2.  
    3. Arrow arrow = (Arrow) damager;
    4. Entity killershooter = arrow.getShooter();
    5. if(killershooter instanceof Skeleton) {
    6. event.setDeathMessage("[NOTICE]" + ChatColor.RED + "[" + player + "]"
    7. + ChatColor.WHITE + " killed by skeleton ");
    8. }
    9.  
    10. if (playerInstance.getKiller() instanceof Player){
    11. event.setDeathMessage("[NOTICE]" + ChatColor.RED + "[" + player + "]"
    12. + ChatColor.WHITE + " killed by " + player.getKiller().getDisplayName() + " skeleton ");
    13. }
    14.  


    Can help me please?
     
  2. Offline

    Lolmewn Retired Staff

    You're casting the damager to Arrow before checking if it is one.
    If it's a player, errors.
     
  3. Offline

    Mother__

    Can help me show example ? i learn API bukkit
     
  4. Offline

    GodzOfMadness

  5. Offline

    beastman3226

    Try another "if" block to check if damager == Arrow.
     
  6. Offline

    Lolmewn Retired Staff

    if(killer instanceof Arrow){
    Arrow a = (Arrow)killer;
    }else if(killer instanceof Player){
    Player p = (Player)killer;
    }

    Get the point?
     
  7. Offline

    Mother__

    Dont working ={
     
  8. Offline

    Lolmewn Retired Staff

    Because...?
    Please give some more info, otherwise I simply cannot help you.
     
  9. Offline

    MrSnare

    here I did it for you

    Code:
      public void onPlayerDeath(PlayerDeathEvent  ev){
            Player player = (Player) ev.getEntity();
            if(player.getKiller() instanceof Arrow){
                LivingEntity shooter = ((Arrow)player.getKiller()).getShooter();
                if(shooter instanceof Skeleton){
                    ev.setDeathMessage("[NOTICE]" + ChatColor.RED + "[" + player + "]"
                            + ChatColor.WHITE + " killed by skeleton ");
                }else if(shooter instanceof Player){
                    ev.setDeathMessage("[NOTICE]" + ChatColor.RED + "[" + player + "]"
                            + ChatColor.WHITE + " killed by " + player.getKiller().getDisplayName());
                }
            }
        }
        
    Or maybe this works

    Code:
        public void onPlayerDeath2(PlayerDeathEvent  ev){
            Player player = (Player) ev.getEntity();
            if(player.getKiller() instanceof Player){
                ev.setDeathMessage("[NOTICE]" + ChatColor.RED + "[" + player + "]"
                        + ChatColor.WHITE + " killed by " + player.getKiller().getDisplayName());
            }else if(player.getKiller() instanceof Skeleton){
                ev.setDeathMessage("[NOTICE]" + ChatColor.RED + "[" + player + "]"
                        + ChatColor.WHITE + " killed by skeleton ");
            }
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    Mother__


    Dont working = (
     
  11. Offline

    ZeusAllMighty11 Retired Staff

    If you have essentials, make sure you have death messages enabled
     
  12. Offline

    Mother__

    i need when player dead by player say message but have conflict with player death by skeleton

    plz help me guys!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  13. Offline

    MCForger

    I'm sorry may you repeat this question?
     
  14. Offline

    Mother__

    i need when player dead by player say message killled by $nameplayer but have conflict with player death by skeleton

    please guys help me =(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  15. Offline

    sionzee

    debug ??

    Do you use ' @EventHandler ' and register events ?
     
  16. Offline

    AlexLeporiday

    Threads like this are very quickly identified as lost causes.
    To detect when a player is killed you need to listen to the EntityDamageByEntityEvent and then check the entities health, if it's 0 they're dead so you need to get the entity that damaged them. If the entity is a player then a player killed a player, else it was something else.

    With this information you should be able to do your own research and plugin development. If not then you need to understand either Java or Bukkit API better. :/
     
    sionzee likes this.
  17. Offline

    sionzee

    That's true.
     
  18. Offline

    Mother__


    this is my code but not working when i die by skeleton have error

    Code:java
    1.  
    2.  
    3. @EventHandler
    4. public void onPlayerDeath(PlayerDeathEvent event) {
    5. Player playerInstance = (Player) event.getEntity();
    6. EntityDamageEvent damageEvent = playerInstance.getLastDamageCause();
    7. if(!(damageEvent instanceof EntityDamageByEntityEvent))
    8. return;
    9.  
    10. Entity damager = ((EntityDamageByEntityEvent)damageEvent).getDamager();
    11. Arrow arrow = (Arrow) damager;
    12. Entity killershooter = arrow.getShooter();
    13. if(killershooter instanceof Skeleton) {
    14. event.setDeathMessage("[NOTICE]" + ChatColor.RED + "[" + player + "]"
    15. + ChatColor.WHITE + " killed by skeleton ");
    16. }
    17.  
    18. if (playerInstance.getKiller() instanceof Player){
    19. event.setDeathMessage("[NOTICE]" + ChatColor.RED + "[" + player + "]"
    20. + ChatColor.WHITE + " killed by " + player.getKiller().getDisplayName());
    21. }
    22. }
     
  19. Offline

    AlexLeporiday

    And the error is? Very few people will look at one part of your code and fix every issue with it. Tell us the error and then we might be able to help solve each individual issue at a time.
     
  20. Offline

    Mother__

    when i die by skeleton

    Code:java
    1.  
    2.  
    3. [SEVERE] Could not pass event PlayerDeathEvent to myPlugin v1.0
    4. org.bukkit.event.EventException
    5. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    6. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    7. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    8. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    9. at org.bukkit.craftbukkit.v1_4_R1.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:332)
    10. at net.minecraft.server.v1_4_R1.EntityPlayer.die(EntityPlayer.java:256)
    11. at net.minecraft.server.v1_4_R1.EntityLiving.damageEntity(EntityLiving.java:758)
    12. at net.minecraft.server.v1_4_R1.EntityHuman.damageEntity(EntityHuman.java:616)
    13. at net.minecraft.server.v1_4_R1.EntityPlayer.damageEntity(EntityPlayer.java:309)
    14. at net.minecraft.server.v1_4_R1.EntityArrow.j_(EntityArrow.java:226)
    15. at net.minecraft.server.v1_4_R1.World.entityJoinedWorld(World.java:1332)
    16. at net.minecraft.server.v1_4_R1.WorldServer.entityJoinedWorld(WorldServer.java:548)
    17. at net.minecraft.server.v1_4_R1.World.playerJoinedWorld(World.java:1313)
    18. at net.minecraft.server.v1_4_R1.World.tickEntities(World.java:1191)
    19. at net.minecraft.server.v1_4_R1.WorldServer.tickEntities(WorldServer.java:445)
    20. at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:580)
    21. at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:224)
    22. at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:494)
    23. at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:427)
    24. at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    25. Caused by: java.lang.NullPointerException
    26. at myPlugin.game.Main.onPlayerDeath(Main.java:1263)
    27. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    28. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    29. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    30. at java.lang.reflect.Method.invoke(Unknown Source)
    31. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    32. ... 19 more
    33. [INFO] Mother__ was shot by Skeleton
    34.  
    35.  


    when i die by player

    Code:java
    1.  
    2. [SEVERE] Could not pass event PlayerDeathEvent to myPlugin v1.0
    3. org.bukkit.event.EventException
    4. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    5. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    6. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    7. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    8. at org.bukkit.craftbukkit.v1_4_R1.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:332)
    9. at net.minecraft.server.v1_4_R1.EntityPlayer.die(EntityPlayer.java:256)
    10. at net.minecraft.server.v1_4_R1.EntityLiving.damageEntity(EntityLiving.java:758)
    11. at net.minecraft.server.v1_4_R1.EntityHuman.damageEntity(EntityHuman.java:616)
    12. at net.minecraft.server.v1_4_R1.EntityPlayer.damageEntity(EntityPlayer.java:309)
    13. at net.minecraft.server.v1_4_R1.EntityHuman.attack(EntityHuman.java:805)
    14. at net.minecraft.server.v1_4_R1.PlayerConnection.a(PlayerConnection.java:1116)
    15. at net.minecraft.server.v1_4_R1.Packet7UseEntity.handle(SourceFile:36)
    16. at net.minecraft.server.v1_4_R1.NetworkManager.b(NetworkManager.java:290)
    17. at net.minecraft.server.v1_4_R1.PlayerConnection.d(PlayerConnection.java:113)
    18. at net.minecraft.server.v1_4_R1.ServerConnection.b(SourceFile:39)
    19. at net.minecraft.server.v1_4_R1.DedicatedServerConnection.b(SourceFile:30)
    20. at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:598)
    21. at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:224)
    22. at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:494)
    23. at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:427)
    24. at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    25. Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_4_R1.entity.CraftPlayer cannot be cast to org.bukkit.entity.Arrow
    26. at myPlugin.game.Main.onPlayerDeath(Main.java:1280)
    27. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    28. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    29. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    30. at java.lang.reflect.Method.invoke(Unknown Source)
    31. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    32. ... 20 more
    33. [INFO] Mother__ was slain by Mother__2
    34.  
     
Thread Status:
Not open for further replies.

Share This Page