EntityDeathEvent Need Help - No Errors In Console

Discussion in 'Plugin Development' started by CrazymanJR, Sep 6, 2014.

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

    CrazymanJR

    For some weird reason, i'm having some problems with my EntityDeathEvent class. I want to make it so when you kill either a Chicken, pig, cow, sheep, and ocelot it will send you a message saying you have 2 chances left. If you kill another animal, then you get a msg saying you have one chance left, then if you kill another animal then it should send you a msg saying you have 0 chances left and it should kill you.

    For some reason, it only sends out the first message(2 chances left) all the time whenever i kill like 10 mobs. It would be great if somebody could help me out here.

    My Code:
    Code:java
    1. package me.crazymanjr.animalhunt.listeners.player;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.entity.Chicken;
    7. import org.bukkit.entity.Cow;
    8. import org.bukkit.entity.Ocelot;
    9. import org.bukkit.entity.Pig;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.entity.Sheep;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.EntityDeathEvent;
    15.  
    16. public class PlayerEntityKill implements Listener {
    17.  
    18.  
    19.  
    20. @SuppressWarnings("deprecation")
    21. @EventHandler
    22. public void onKill(EntityDeathEvent e) {
    23.  
    24. ArrayList<String> entitykill1 = new ArrayList<String>();
    25. ArrayList<String> entitykill2 = new ArrayList<String>();
    26.  
    27.  
    28. if (e.getEntity() instanceof Chicken) {
    29. Chicken c = (Chicken) e.getEntity();
    30. if (c.getKiller() instanceof Player) {
    31. Player p = c.getKiller();
    32.  
    33. if(!entitykill1.contains(p.getName()) && !entitykill2.contains(p.getName())){
    34.  
    35. e.getDrops().clear();
    36. entitykill1.add(p.getName());
    37. p.sendMessage(ChatColor.YELLOW + "Looks like you killed the wrong mob. You have 2 more chances left!");
    38.  
    39. }else{
    40.  
    41. if(entitykill1.contains(p.getName())) {
    42. p.sendMessage(ChatColor.YELLOW + "Looks like you killed the wrong mob. You have 1 more chance left!");
    43. e.getDrops().clear();
    44. entitykill1.remove(p.getName());
    45. entitykill2.add(p.getName());
    46.  
    47. }else {
    48. if(entitykill2.contains(p.getName())) {
    49. p.sendMessage(ChatColor.YELLOW + "Looks like you've killed the wrong mob. You have no more chances left!");
    50. p.setHealth(0);
    51. e.getDrops().clear();
    52. entitykill2.remove(p.getName());
    53. }
    54. }
    55. }
    56. }
    57. }else if (e.getEntity() instanceof Cow) {
    58. Cow co = (Cow) e.getEntity();
    59. if (co.getKiller() instanceof Player) {
    60. Player p = co.getKiller();
    61.  
    62. if(!entitykill1.contains(p.getName()) && !entitykill2.contains(p.getName())){
    63.  
    64. e.getDrops().clear();
    65. entitykill1.add(p.getName());
    66. p.sendMessage(ChatColor.YELLOW + "Looks like you killed the wrong mob. You have 2 more chances left!");
    67.  
    68. }else{
    69.  
    70. if(entitykill1.contains(p.getName())) {
    71. p.sendMessage(ChatColor.YELLOW + "Looks like you killed the wrong mob. You have 1 more chance left!");
    72. e.getDrops().clear();
    73. entitykill1.remove(p.getName());
    74. entitykill2.add(p.getName());
    75.  
    76. }else {
    77. if(entitykill2.contains(p.getName())) {
    78. p.sendMessage(ChatColor.YELLOW + "Looks like you've killed the wrong mob. You have no more chances left!");
    79. p.setHealth(0);
    80. e.getDrops().clear();
    81. entitykill2.remove(p.getName());
    82. }
    83. }
    84. }
    85. }
    86. }else if (e.getEntity() instanceof Pig) {
    87. Pig pig = (Pig) e.getEntity();
    88. if (pig.getKiller() instanceof Player) {
    89. Player p = pig.getKiller();
    90.  
    91. if(!entitykill1.contains(p.getName()) && !entitykill2.contains(p.getName())){
    92.  
    93. e.getDrops().clear();
    94. entitykill1.add(p.getName());
    95. p.sendMessage(ChatColor.YELLOW + "Looks like you killed the wrong mob. You have 2 more chances left!");
    96.  
    97. }else{
    98.  
    99. if(entitykill1.contains(p.getName())) {
    100. p.sendMessage(ChatColor.YELLOW + "Looks like you killed the wrong mob. You have 1 more chance left!");
    101. e.getDrops().clear();
    102. entitykill1.remove(p.getName());
    103. entitykill2.add(p.getName());
    104.  
    105. }else {
    106. if(entitykill2.contains(p.getName())) {
    107. p.sendMessage(ChatColor.YELLOW + "Looks like you've killed the wrong mob. You have no more chances left!");
    108. p.setHealth(0);
    109. e.getDrops().clear();
    110. entitykill2.remove(p.getName());
    111. }
    112. }
    113. }
    114. }
    115. }else if (e.getEntity() instanceof Ocelot) {
    116. Ocelot oc = (Ocelot) e.getEntity();
    117. if (oc.getKiller() instanceof Player) {
    118. Player p = oc.getKiller();
    119.  
    120. if(!entitykill1.contains(p.getName()) && !entitykill2.contains(p.getName())){
    121.  
    122. e.getDrops().clear();
    123. entitykill1.add(p.getName());
    124. p.sendMessage(ChatColor.YELLOW + "Looks like you killed the wrong mob. You have 2 more chances left!");
    125.  
    126. }else{
    127.  
    128. if(entitykill1.contains(p.getName())) {
    129. p.sendMessage(ChatColor.YELLOW + "Looks like you killed the wrong mob. You have 1 more chance left!");
    130. e.getDrops().clear();
    131. entitykill1.remove(p.getName());
    132. entitykill2.add(p.getName());
    133.  
    134. }else {
    135. if(entitykill2.contains(p.getName())) {
    136. p.sendMessage(ChatColor.YELLOW + "Looks like you've killed the wrong mob. You have no more chances left!");
    137. p.setHealth(0);
    138. e.getDrops().clear();
    139. entitykill2.remove(p.getName());
    140. }
    141. }
    142. }
    143. }
    144. }else if (e.getEntity() instanceof Sheep) {
    145. Sheep sh = (Sheep) e.getEntity();
    146. if (sh.getKiller() instanceof Player) {
    147. Player p = sh.getKiller();
    148.  
    149. if(!entitykill1.contains(p.getName()) && !entitykill2.contains(p.getName())){
    150.  
    151. e.getDrops().clear();
    152. entitykill1.add(p.getName());
    153. p.sendMessage(ChatColor.YELLOW + "Looks like you killed the wrong mob. You have 2 more chances left!");
    154.  
    155. }else{
    156.  
    157. if(entitykill1.contains(p.getName())) {
    158. p.sendMessage(ChatColor.YELLOW + "Looks like you killed the wrong mob. You have 1 more chance left!");
    159. e.getDrops().clear();
    160. entitykill1.remove(p.getName());
    161. entitykill2.add(p.getName());
    162.  
    163. }else {
    164. if(entitykill2.contains(p.getName())) {
    165. p.sendMessage(ChatColor.YELLOW + "Looks like you've killed the wrong mob. You have no more chances left!");
    166. p.setHealth(0);
    167. e.getDrops().clear();
    168. entitykill2.remove(p.getName());
    169. }
    170. }
    171. }
    172. }
    173. }
    174. }
    175. }
     
  2. Offline

    coldandtired

    You create the list anew every time the event is raised, so it will always be empty when the player gets added. You need to move the list outside the event and make it an instance variable.
     
  3. Offline

    CrazymanJR

  4. Offline

    coldandtired

    When a mob dies it creates a new EntityDeathEvent and when that happens your two lists will be created. You need to move them outside
    Code:java
    1. public class YourClass implements Listener
    2. {
    3. private final List<String> list1 = new ArrayList<>();
    4. private final List<String> list2 = new ArrayList<>();
    5.  
    6. @EventHandler
    7. public void Event(EntityDeathEvent event)
    8. {
    9. //check the type of mob, check whether the player is in the lists, etc.
    10. }
    11. }

    This way the lists will only be created once.
     
  5. Offline

    CrazymanJR

    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page