Solved Getting the Killer (Creature)

Discussion in 'Plugin Development' started by FabeGabeMC, Mar 15, 2014.

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

    FabeGabeMC

    Hi. I have been trying to fix this but it won't work.
    I want it to say a player got killed by a specific mob. But with color.

    Here's my code:

    Code:java
    1. //starting the event
    2. public void onEntityDeath(EntityDeathEvent e){
    3. Player p = e.getEntity();
    4. Entity k = p.getKiller();
    5. EntityDamageEvent c = p.getLastDamageCause();
    6. DamageCause dc = c.getCause();
    7.  
    8. if(dc == DamageCause.ENTITY_ATTACK && p instanceof Player){
    9. //Just an example
    10. if(k.getClass() == Zombie.class){
    11. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by a" + ChatColor.DARK_AQUA + "Zombie" + ChatColor.BLUE + ".");
    12. }
    13. }
    14. }


    And this is my console error:

    Code:
    [15:55:01 ERROR]: Could not pass event PlayerDeathEvent to Plugin v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:486) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:471) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:349) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityPlayer.die(EntityPlayer.java:368) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityLiving.damageEntity(EntityLiving.java:732) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityHuman.damageEntity(EntityHuman.java:746) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityPlayer.damageEntity(EntityPlayer.java:446) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityMonster.m(EntityMonster.java:99) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityZombie.m(EntityZombie.java:218) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PathfinderGoalMeleeAttack.e(PathfinderGoalMeleeAttack.java:100) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PathfinderGoalSelector.a(PathfinderGoalSelector.java:108) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityInsentient.bn(EntityInsentient.java:427) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityLiving.e(EntityLiving.java:1436) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityInsentient.e(EntityInsentient.java:306) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityMonster.e(EntityMonster.java:20) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityZombie.e(EntityZombie.java:152) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityLiving.h(EntityLiving.java:1299) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityInsentient.h(EntityInsentient.java:150) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityMonster.h(EntityMonster.java:24) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.EntityZombie.h(EntityZombie.java:214) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.World.entityJoinedWorld(World.java:1348) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.World.playerJoinedWorld(World.java:1329) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.World.tickEntities(World.java:1217) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.WorldServer.tickEntities(WorldServer.java:480) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:637) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
        at me.FabeGabe.Plugin.PlayerListener.onPlayerDeath(PlayerListener.java:127) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.6.0_65]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[?:1.6.0_65]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[?:1.6.0_65]
        at java.lang.reflect.Method.invoke(Method.java:597) ~[?:1.6.0_65]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[CraftBukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 30 more
    [15:55:01 INFO]: FabeGabe was slain by Zombie
    Can someone tell me what the problem is?

    Thanks,

    Gabe
     
  2. Offline

    SuperOmegaCow

    FabeGabeMC
    What is on line 127? Whatever it is, it is null.
     
  3. Offline

    ShadowLAX

    FabeGabeMC You are making it more complicated than necessary.

    Code:java
    1. if (e.getEntity() instanceof Player && e.getKiller() instanceof Zombie) {
    2. e.setDeathMessage("");
    3. }
     
  4. Offline

    FabeGabeMC

    SuperOmegaCow
    this is line 127:
    if(k.getClass() == Spider.class){
     
  5. Offline

    SuperOmegaCow

    FabeGabeMC
    do this:
    if(k != null && k instanceof Spider) {}
     
  6. Offline

    FabeGabeMC

  7. Offline

    Heirteir

    FabeGabeMC
    just do
    Code:java
    1. e.setDeathMessage(player + " Was Killed By a " + e.getEntity().getKiller().getType().toString().toLowerCase());

    should do it
     
  8. Offline

    FabeGabeMC

    SuperOmegaCow It doesn't show any errors but it doesn't send the message.
     
  9. Offline

    Heirteir

  10. Offline

    FabeGabeMC

    Heirteir not looking for that
    Looking for a unique color for each mob.
    Purple : Enderman
    Dark Aqua : Zombie
    Green : Slime
     
  11. Offline

    Heirteir

    FabeGabeMC
    then just do if
    Code:java
    1. (e.getEntity().getKiller() instanceof Skeleton){
    2. e.setDeathMessage(//Message);
    3. return;
    4. }
    5. [syntax=java](e.getEntity().getKiller() instanceof Zombie){
    6. e.setDeathMessage(//Message);
    7. return;
    8. }
    9. //etc[/syntax]
     
  12. Offline

    FabeGabeMC

    Heirteir
    I have already tried that exact code. Doesn't work. My question to you is, Have you tested it out?
     
  13. Offline

    Heirteir

    FabeGabeMC
    yes
    did you make sure to do Bukkit.getServer().getPluginManager().registerevents(this, this);
    in your main file I am assuming that's where your code is for the listener
    in the onEnable() method
     
  14. Offline

    FabeGabeMC

    Heirteir
    Why wouldn't I? Look, I did not. *sarcasm*

    ShadowLAX
    e.getKiller() would be p.getKiller() or e.getEntity().getKiller()

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

    ShadowLAX

    FabeGabeMC Assuming p is a variable for the object e.getEntity(), they would both have the same results.
     
  16. Offline

    FabeGabeMC

  17. Offline

    ShadowLAX

    FabeGabeMC I don't understand what you are asking then. Care to elaborate a bit more?
     
  18. Offline

    AoH_Ruthless

    Those are literally all the exact same.

    If your dead 'entity' is a player, why aren't you using a PlayerDeathEvent? That way you don't have to check if the Entity's instance is a player.

    Just check if the killer (e.getKiller())'s instance is a Zombie, then run your code? It's not as complicated as you are making it out to be.
     
  19. Offline

    FabeGabeMC

  20. Offline

    AoH_Ruthless

    FabeGabeMC
    Well I mean you don't have @EventHandler.


    Edit: On a completely unrelated note that I felt like I had to point out: 1000 messages on these forums! [pig]
     
  21. Offline

    FabeGabeMC

    AoH_Ruthless
    I do have @EventHandler but not in this topic.
     
  22. Offline

    Garris0n

    Post the main class and listener class you're using. And any stack traces you're getting. And explain what happens when the relevant event is fired.
     
  23. Offline

    FabeGabe

  24. Offline

    SuperOmegaCow

    Code:java
    1. @EventHandler
    2. public void entityDeath(EntityDeathEvent event) {
    3. if(event.getEntity() instanceof Player) {
    4. Player p = (Player) event.getEntity();
    5.  
    6. if(p.getKiller() instanceof Player) {
    7. Player killer = (Player) p.getKiller();
    8. }
    9.  
    10. }
    11. }
     
  25. Offline

    FabeGabe

  26. Offline

    MrInspector

     
  27. Offline

    FabeGabe

    ShadowLAX I want to set the death message to a custom death message with the mob's type and everything.
    Garris0n Here it is:

    Main class:
    I only post onEnable() since there is nothing regarding the event that I want to trigger.
    Code:java
    1. public void onEnable() {
    2. plugin = this;
    3. Freeze = false;
    4. PVP = false;
    5. headRecipe();
    6. registerCommands();
    7. getServer().getPluginManager().registerEvents(new PlayerListener(plugin), this);
    8.  
    9. }


    PlayerListener class:
    Code:java
    1. //Player Death Check
    2. @EventHandler
    3. public void onPlayerDeath(PlayerDeathEvent e){
    4. final Player p = e.getEntity();
    5. for(Player allP : Bukkit.getOnlinePlayers()){
    6. allP.playSound(allP.getLocation(), Sound.AMBIENCE_THUNDER, 1, 10);
    7. }
    8. dtask = UHC.plugin.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this, new Runnable(){
    9. @SuppressWarnings("deprecation")
    10. public void run(){
    11. int number = 1800;
    12. if(number != -1){
    13. if(number != 0){
    14. number--;
    15. }
    16. if(number == 0){
    17. p.kickPlayer("60 seconds have passed since you died. "
    18. + "You have been removed.");
    19. p.setBanned(true);
    20. }
    21. }
    22. }
    23. }, 0L, 20L);
    24. Firework fw = (Firework) p.getWorld().spawn(p.getLocation(),
    25. Firework.class);
    26. FireworkMeta fm = fw.getFireworkMeta();
    27. fm.addEffect(FireworkEffect.builder()
    28. .flicker(false)
    29. .trail(true)
    30. .with(Type.BALL_LARGE)
    31. .withColor(Color.RED)
    32. .withFade(Color.ORANGE)
    33. .build());
    34. fm.setPower(3);
    35. fw.setFireworkMeta(fm);
    36. Player k = p.getKiller();
    37. int lvl = p.getLevel();
    38. EntityDamageEvent c = p.getLastDamageCause();
    39. DamageCause dc = c.getCause();
    40. EntityType kmt = c.getEntity().getType();
    41. ItemStack is = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
    42. SkullMeta sm = (SkullMeta) is.getItemMeta();
    43. sm.setOwner(p.getName());
    44. sm.setDisplayName(ChatColor.WHITE + p.getName() + "'s Head");
    45. is.setItemMeta(sm);
    46. p.getWorld().dropItemNaturally(p.getLocation(), is);
    47. if(p instanceof Player){
    48. if(dc == DamageCause.FALL){
    49. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by" + ChatColor.GOLD + " Fall Damage" + ChatColor.BLUE + ".");
    50. }else
    51. if(dc == DamageCause.VOID){
    52. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by" + ChatColor.LIGHT_PURPLE + " Void Damage" + ChatColor.BLUE + ".");
    53. }else
    54. if(dc == DamageCause.CONTACT){
    55. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by" + ChatColor.GREEN + " Cactus" + ChatColor.BLUE + ".");
    56. }else
    57. if(dc == DamageCause.BLOCK_EXPLOSION){
    58. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by an" + ChatColor.RED + " Explosion" + ChatColor.BLUE + ".");
    59. }else
    60. if(dc == DamageCause.DROWNING){
    61. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by" + ChatColor.AQUA + " Drowning" + ChatColor.BLUE + ".");
    62. }else
    63. if(dc == DamageCause.STARVATION){
    64. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by" + ChatColor.YELLOW + " Starvation" + ChatColor.BLUE + ".");
    65. }else
    66. if(dc == DamageCause.LAVA){
    67. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by" + ChatColor.RED + " Lava" + ChatColor.BLUE + ".");
    68. }else
    69. if(dc == DamageCause.PROJECTILE && !(kmt == EntityType.PLAYER)){
    70. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been shot by a" + ChatColor.GRAY + "Skeleton" + ChatColor.BLUE + ".");
    71. }else
    72. if(dc == DamageCause.PROJECTILE && kmt == EntityType.PLAYER){
    73. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been shot by" + ChatColor.YELLOW + k.getName() + ChatColor.BLUE + ".");
    74. k.giveExpLevels(lvl);
    75. k.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10, 2));
    76. }else
    77. if(dc == DamageCause.LIGHTNING){
    78. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by" + ChatColor.YELLOW + " Lightning" + ChatColor.BLUE + ".");
    79. }else
    80. if(dc == DamageCause.MAGIC){
    81. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by" + ChatColor.LIGHT_PURPLE + " Magic" + ChatColor.BLUE + ".");
    82. }else
    83. if(dc == DamageCause.ENTITY_EXPLOSION){
    84. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by an" + ChatColor.RED + " Explosion" + ChatColor.BLUE + "."); }else
    85. if(dc == DamageCause.WITHER){
    86. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by" + ChatColor.DARK_PURPLE + " Wither Effect" + ChatColor.BLUE + ".");
    87. }else
    88. if(dc == DamageCause.SUICIDE){
    89. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by" + ChatColor.RED + " Suicide" + ChatColor.BLUE + ".");
    90. }else
    91. if(dc == DamageCause.FALLING_BLOCK){
    92. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by a" + ChatColor.GOLD + " Falling Block" + ChatColor.BLUE + ".");
    93. }else
    94. if(dc == DamageCause.ENTITY_ATTACK && (kmt != null || k != null)){
    95. switch(kmt){
    96. case BLAZE:
    97. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by a" + ChatColor.RED + " Blaze" + ChatColor.BLUE + ".");
    98. break;
    99. case ZOMBIE:
    100. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by a" + ChatColor.DARK_AQUA + " Zombie" + ChatColor.BLUE + ".");
    101. break;
    102. case CAVE_SPIDER:
    103. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by a" + ChatColor.DARK_AQUA + " Cave Spider" + ChatColor.BLUE + ".");
    104. break;
    105. case MAGMA_CUBE:
    106. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by a" + ChatColor.RED + " Magma Cube" + ChatColor.BLUE + ".");
    107. break;
    108. case ENDERMAN:
    109. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by a" + ChatColor.DARK_PURPLE + " Enderman" + ChatColor.BLUE + ".");
    110. break;
    111. case PIG_ZOMBIE:
    112. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by a" + ChatColor.LIGHT_PURPLE + " Zombie Pigman" + ChatColor.BLUE + ".");
    113. break;
    114. case PLAYER:
    115. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by" + ChatColor.YELLOW + k.getName() + ChatColor.BLUE + ".");
    116. k.giveExpLevels(lvl);
    117. k.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10, 2));
    118. break;
    119. case SILVERFISH:
    120. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by a" + ChatColor.GRAY + " Silverfish" + ChatColor.BLUE + ".");
    121. break;
    122. case SKELETON:
    123. Skeleton skeleton = (Skeleton) p.getKiller();
    124. if(skeleton.getSkeletonType() == SkeletonType.WITHER){
    125. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by a" + ChatColor.DARK_PURPLE + " Wither Skeleton" + ChatColor.BLUE + ".");
    126. }
    127. break;
    128. default:
    129. e.setDeathMessage(ChatColor.YELLOW + p.getName() + ChatColor.BLUE + " has been killed by an" + ChatColor.DARK_PURPLE + " Unknown Entity" + ChatColor.BLUE + ".");
    130. break;
    131. }
    132. }
    133. }
    134. }


    Please help dude I have tried everything. Also, Countdown doesn't trigger :/
    ~Gabe
     
  28. Offline

    ShadowLAX

    FabeGabe
    Code:java
    1. ((Plugin) this, new Runnable(){
    I don't even know where to start with that... You can't cast Plugin to this because this will always return a new instance of the class it is being used in... So you basically casted the main class to a class that will never be the main class because there can only be one main class... Man, that made my head hurt just thinking about it.
     
  29. Offline

    FabeGabeMC

    ShadowLAX okay.... How would I make it so the actual mob detector works?

    P.S. I never noticed I have two Bukkit accounts so I was like "Why is my name fabegabe instead of fabegabemc!?", and then I derped. Lol.
     
  30. Offline

    ShadowLAX

    FabeGabeMC It will work once you fix what I explained above, I can guarantee that what I said above in your code will throw an error every time, and thus stopping the code so it doesn't work.
     
Thread Status:
Not open for further replies.

Share This Page