Problem with PlayerItemBreakEvent

Discussion in 'Plugin Development' started by FlaveDrake, Jul 2, 2019.

Thread Status:
Not open for further replies.
  1. Hey there,

    Let me explain my problem. Basically i want to Auto-Equip tools after they break if there is another tool with the same material.

    My Code only works when the tool breaks from destroying blocks not from hitting any entities.

    If the tool breaks from hitting an Entity it disappears and the other tool that has the same material as well, instead of replacing it.

    Btw there's no error Code, when the event triggers!

    Heres my code:

    Code:java
    1. @EventHandler
    2. public void onItemBreak(PlayerItemBreakEvent e) {
    3. Player p = e.getPlayer();
    4. ItemStack item = e.getBrokenItem();
    5. ArrayList<ItemStack> store = new ArrayList<ItemStack>();
    6.  
    7. store.add(item);
    8.  
    9. if (store.get(0).equals(p.getInventory().getItemInOffHand())) {
    10. //
    11. } else if (store.get(0).equals(p.getInventory().getHelmet())) {
    12. for (int i = 0; i <= p.getInventory().getSize(); i++) {
    13. ItemStack invitem = p.getInventory().getItem(i);
    14.  
    15. if (invitem != null) {
    16. if (invitem.getType().equals(store.get(0).getType())) {
    17. p.getInventory().remove(invitem);
    18. p.getInventory().setHelmet(invitem);
    19. store.remove(0);
    20.  
    21. break;
    22. }
    23. }
    24. }
    25. } else if (store.get(0).equals(p.getInventory().getChestplate())) {
    26. for (int i = 0; i <= p.getInventory().getSize(); i++) {
    27. ItemStack invitem = p.getInventory().getItem(i);
    28.  
    29. if (invitem != null) {
    30. if (invitem.getType().equals(store.get(0).getType())) {
    31. p.getInventory().remove(invitem);
    32. p.getInventory().setChestplate(invitem);
    33. store.remove(0);
    34.  
    35. break;
    36. }
    37. }
    38. }
    39. } else if (store.get(0).equals(p.getInventory().getLeggings())) {
    40. for (int i = 0; i <= p.getInventory().getSize(); i++) {
    41. ItemStack invitem = p.getInventory().getItem(i);
    42.  
    43. if (invitem != null) {
    44. if (invitem.getType().equals(store.get(0).getType())) {
    45. p.getInventory().remove(invitem);
    46. p.getInventory().setLeggings(invitem);
    47. store.remove(0);
    48.  
    49. break;
    50. }
    51. }
    52. }
    53. } else if (store.get(0).equals(p.getInventory().getBoots())) {
    54. for (int i = 0; i <= p.getInventory().getSize(); i++) {
    55. ItemStack invitem = p.getInventory().getItem(i);
    56.  
    57. if (invitem != null) {
    58. if (invitem.getType().equals(store.get(0).getType())) {
    59. p.getInventory().remove(invitem);
    60. p.getInventory().setBoots(invitem);
    61. store.remove(0);
    62.  
    63. break;
    64. }
    65. }
    66. }
    67. } else {
    68.  
    69. int slot = 0;
    70.  
    71. for (int i = 0; i <= p.getInventory().getSize(); i++) {
    72. ItemStack newitem = p.getInventory().getItem(i);
    73.  
    74. if (newitem != null) {
    75. if (newitem.equals(store.get(0))) {
    76. slot = i;
    77. break;
    78. }
    79. }
    80. }
    81.  
    82. for (int i = 0; i <= p.getInventory().getSize(); i++) {
    83. ItemStack invitem = p.getInventory().getItem(i);
    84.  
    85. if (invitem != null && i != slot) {
    86. if (invitem.getType().equals(store.get(0).getType())) {
    87.  
    88. p.getInventory().remove(invitem);
    89. p.getInventory().setItem(slot, invitem);
    90.  
    91. store.remove(0);
    92.  
    93. break;
    94. }
    95. }
    96. }
    97.  
    98. }
    99. }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @FlaveDrake Do you get the same result if you add the item a tick later?
     
    FlaveDrake likes this.
  3. Offline

    KarimAKL

    @FlaveDrake Are you sure the event is called when an item is broken from hitting an entity? I would imagine it is but just in case, you could try printing a message to the console when the event is fired, then test it in-game.
    Some things i wanna say:
    1. Why do you create the 'store' list just to add 1 item to it when you can just do 'item.equals()' instead of 'store.get(0).equals()'?
    2. It shouldn't make difference in the case of enums but it's preferred to use '==' to compare enums instead of '.equals' because of the null safety.
    3. You could create a method instead of repeating the same code over and over.
     
  4. @timtower Thanks that made it. Don't know why exactly but it works.

    @KarimAKL
    1. I don't know why, but when going into that for-loop the broken-item material changes to AIR. (Don't know if it's still so using the new code.)
    2. '==' didn't worked out.
    3. Yeah, that's true. I'm not into efficient coding yet, sorry.
     
  5. Offline

    KarimAKL

    I'm not quite sure what you mean; the material of 'e.getBrokenItem' shouldn't change.
    It should, did you compare the materials or the itemstacks? You have to compare the material, so something like this:
    Code:Java
    1. item.getType() == otherItem.getType()

    No need to apologise, i just told you that's a possibility.
     
  6. @KarimAKL

    1. The ArrayList is "useless" in this case. It was a fault when i began to write this code.

    2. '==' only works on Material not comparing ItemStacks


    3. That's my code now (It works perfectly fine):
    Code:java
    1.  
    2. @EventHandler
    3. public void onItemBreak(PlayerItemBreakEvent e) {
    4. Player p = e.getPlayer();
    5. ItemStack broken = e.getBrokenItem();
    6. PlayerInventory inv = p.getInventory();
    7.  
    8. for (int i = 0; i <= p.getInventory().getSize(); i++) {
    9. ItemStack currentitem = p.getInventory().getItem(i);
    10.  
    11. if (currentitem != null) {
    12. if (currentitem.equals(broken)) {
    13. slot = i;
    14. break;
    15. }
    16. }
    17. }
    18.  
    19. ItemStack newitem = reequip(p, broken, inv);
    20.  
    21. inv.remove(newitem);
    22.  
    23. Bukkit.getServer().getScheduler().runTaskLater(Main.getPlugin(), new Runnable() {
    24. public void run() {
    25. if (broken.equals(inv.getItemInOffHand())) {
    26. inv.setItemInOffHand(newitem);
    27. } else if (broken.equals(inv.getHelmet())) {
    28. inv.setHelmet(newitem);
    29. } else if (broken.equals(inv.getChestplate())) {
    30. inv.setChestplate(newitem);
    31. } else if (broken.equals(inv.getLeggings())) {
    32. inv.setLeggings(newitem);
    33. } else if (broken.equals(inv.getBoots())) {
    34. inv.setBoots(newitem);
    35. } else {
    36. inv.setItem(slot, newitem);
    37. }
    38. }
    39. }, 2L);
    40. }
    41.  
    42. public ItemStack reequip(Player p, ItemStack broken, PlayerInventory inv) {
    43. ItemStack newitem = null;
    44.  
    45. for (int i = 0; i <= inv.getSize(); i++) {
    46. ItemStack currentitem = inv.getItem(i);
    47.  
    48. if (currentitem != null && i != slot) {
    49. if (currentitem.getType() == broken.getType()) {
    50. newitem = currentitem;
    51. }
    52. }
    53. }
    54. return newitem;
    55. }
    56.  


    EDIT: slot is a private int in my Listener class.
     
  7. Offline

    KarimAKL

    I know, that's what i've been saying. It seems you've changed the enum comparing to '==' instead of '.equals', good. :)
    You aren't using the 'p' variable, you can remove it.
     
Thread Status:
Not open for further replies.

Share This Page