Solved PlayerDeathEvent on Wolf Kill Not Working

Discussion in 'Plugin Development' started by Mr_Gibbles, Mar 19, 2014.

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

    Mr_Gibbles

    I can't seem to find out what's wrong here, I want it to get the wolf owner and send them a message but it's not working.
    Code:java
    1. @EventHandler
    2. public void onWolfKill(PlayerDeathEvent e) {
    3. if (e.getEntity().getKiller() instanceof Wolf) {
    4. Wolf w = (Wolf) e.getEntity().getKiller();
    5. Player killer = (Player) w.getOwner();
    6. killer.sendMessage("It worked");
    7. }
    8. }

    Anyone know why it's not doing anything? I am registering the event and there is no error in console :/
     
  2. Offline

    pigeontroll

    You should probably do EntityDeathEvent rather than Player Death Event. I don't know if it changes anything, though.
     
  3. Offline

    Mr_Gibbles

    pigeontroll What difference would that make? I'll try it out though.

    pigeontroll Same result, nothing happens. No error in console.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    canemonster15

    You are trying to send a message to the owner of the wolf it looks like.
    Code:java
    1. @EventHandler
    2. public void onWolfKill(PlayerDeathEvent e) {
    3. if (e.getEntity().getKiller() instanceof Wolf) {
    4. Wolf w = (Wolf) e.getEntity();
    5. Player p = w.getKiller();
    6. p.sendMessage("It Worked!");
    7. }
    8. }

    Haven't tested it. I think it should work.
     
  5. Offline

    Mr_Gibbles

    canemonster15 No, I'm looking for a Wolf that kills a Player than sending the Wolf's owner a message saying that their wolf killed someone. Here's my full code:
    Code:java
    1. @EventHandler
    2. public void onWolfKill(PlayerDeathEvent e) {
    3. if (e.getEntity().getKiller() instanceof Wolf) {
    4. Player killed = (Player) e.getEntity().getPlayer();
    5. Wolf w = (Wolf) e.getEntity().getKiller();
    6. Player killer = (Player) w.getOwner();
    7. if (!Main.spawn.contains(killer.getName()) && !Main.spawn.contains(killed.getName()) && Abilities.artemis.contains(killer.getName())) {
    8. Yaml points = Main.getPlayerPoints(killer);
    9. points.increment("kills", 1);
    10. points.increment("points", 1);
    11. killer.sendMessage(Main.displayServer() + ChatColor.YELLOW + "Your dog killed " + killed.getName() + " and got you " + ChatColor.GOLD + "1" + ChatColor.YELLOW + " point!");
    12. killed.sendMessage(Main.displayServer() + ChatColor.YELLOW + "You were killed by " + killer.getName() + "'s dog!");
    13. }
    14. }
    15. }
     
  6. Offline

    xTigerRebornx

    Mr_Gibbles Print some debug before you do the check to see if they are in the lists, and use debug to see if any of them aren't giving the proper value
     
  7. Offline

    Mr_Gibbles

    xTigerRebornx What do you mean? I took out the lists from the code and nothing happened still, so it has nothing to do with them.
     
  8. Offline

    xTigerRebornx

    Mr_Gibbles Can we see the full class and where you are registering the event at?
     
  9. Offline

    Mr_Gibbles

    Full class:
    Code:java
    1. package me.Mr_Gibbles.McOitc;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.DyeColor;
    8. import org.bukkit.Location;
    9. import org.bukkit.entity.Arrow;
    10. import org.bukkit.entity.EntityType;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.entity.Wolf;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    16. import org.bukkit.event.entity.PlayerDeathEvent;
    17. import org.bukkit.event.entity.ProjectileLaunchEvent;
    18.  
    19. public class Artemis implements Listener {
    20.  
    21. Main main;
    22. public Artemis(Main main) {
    23. this.main = main;
    24. }
    25.  
    26. public static ArrayList<String> arrowcooldown = new ArrayList<String>();
    27.  
    28. @SuppressWarnings("deprecation")
    29. @EventHandler
    30. public void onProjectileShoot(ProjectileLaunchEvent e) {
    31. if (e.getEntity() instanceof Arrow && e.getEntity().getShooter() instanceof Player) {
    32. final Player p = (Player) e.getEntity().getShooter();
    33. if (Abilities.artemis.contains(p.getName()) && !Main.spawn.contains(p.getName())) {
    34. if (!arrowcooldown.contains(p.getName())) {
    35. arrowcooldown.add(p.getName());
    36. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    37. @Override
    38. public void run() {
    39. Arrow a = p.shootArrow();
    40. a.setVelocity(p.getLocation().getDirection().multiply(2));
    41. }
    42. }, 2L);
    43. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    44. @Override
    45. public void run() {
    46. arrowcooldown.remove(p.getName());
    47. }
    48. }, 5L);
    49. }
    50. }
    51. }
    52. }
    53.  
    54. @SuppressWarnings("deprecation")
    55. @EventHandler
    56. public void onProjectileHit(EntityDamageByEntityEvent e) {
    57. if (e.getEntity() instanceof Player && e.getDamager() instanceof Arrow) {
    58. Arrow a = (Arrow) e.getDamager();
    59. if (a.getShooter() instanceof Player) {
    60. Player p = (Player) a.getShooter();
    61. Player killed = (Player) e.getEntity();
    62. if (Abilities.artemis.contains(p.getName()) && !Main.spawn.contains(p.getName())) {
    63. Location loc = killed.getLocation();
    64. final Wolf wolf = (Wolf) loc.getWorld().spawnEntity(loc, EntityType.WOLF);
    65. wolf.setTamed(true);
    66. wolf.setOwner(p);
    67. wolf.setCollarColor(DyeColor.PINK);
    68. wolf.setCustomNameVisible(true);
    69. wolf.setCustomName(ChatColor.LIGHT_PURPLE + p.getName() + "'s Lil' Helper");
    70. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    71. @Override
    72. public void run() {
    73. if (wolf.isDead() == false) {
    74. wolf.damage(20D);
    75. }
    76. }
    77. }, 200L);
    78. }
    79. }
    80. }
    81. }
    82.  
    83. @EventHandler
    84. public void onWolfKill(PlayerDeathEvent e) {
    85. if (e.getEntity().getKiller() instanceof Wolf) {
    86. Player killed = (Player) e.getEntity().getPlayer();
    87. Wolf w = (Wolf) e.getEntity().getKiller();
    88. Player killer = (Player) w.getOwner();
    89. if (!Main.spawn.contains(killer.getName()) && !Main.spawn.contains(killed.getName()) && Abilities.artemis.contains(killer.getName())) {
    90. Yaml points = Main.getPlayerPoints(killer);
    91. points.increment("kills", 1);
    92. points.increment("points", 1);
    93. killer.sendMessage(Main.displayServer() + ChatColor.YELLOW + "Your dog killed " + killed.getName() + " and got you " + ChatColor.GOLD + "1" + ChatColor.YELLOW + " point!");
    94. killed.sendMessage(Main.displayServer() + ChatColor.YELLOW + "You were killed by " + killer.getName() + "'s dog!");
    95. }
    96. }
    97. }
    98. }


    Main class (where event is registered):
    Code:java
    1. public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
    2. for (Listener listener : listeners) {
    3. Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
    4. }
    5. }
    6. public void onEnable() {
    7. //blah blah blah
    8. registerEvents(this, new Artemis(this));
    9. //blah blah blah
    10. }


    bumpeee

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Offline

    agent6262

    Code:java
    1. if (e.getEntity().getKiller() instanceof Wolf) {
    2. }
    The reason why this code wont work is because the method getkiller() belongs to the class LivingEntity which returns a Player object. So therefore in your code you are asking if a Player is an instance of Wolf and the answer is no.
     
  11. Offline

    Mr_Gibbles

    agent6262 Hmm, than how could I get a player death from a wolf without using this?

    Ok, I fixed it using
    Code:java
    1. EntityDamageEvent ede = killed.getLastDamageCause();
    2. if(ede instanceof EntityDamageByEntityEvent){
    3. EntityDamageByEntityEvent edbe = (EntityDamageByEntityEvent) ede;
    4. Entity wolf = edbe.getDamager();
    5. if(wolf instanceof Wolf){

    instead of
    Code:java
    1. if (e.getEntity().getKiller() instanceof Wolf) {


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  12. Offline

    agent6262

    I have not tested this but this might work or something along this line
    Code:java
    1. if(e.getEntityType() instanceof EntityType.WOLF){
    2. }
     
Thread Status:
Not open for further replies.

Share This Page