Problem with InventoryClickEvent

Discussion in 'Plugin Development' started by Pingoo, Aug 25, 2014.

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

    Pingoo

    Hi, i have a problem with the bukkit InventoryClickEvent. My plugin add new type of items (all is managed with lore). When the player open the menu, he can click on specified item like the belt to equip it in the storage, and reverse, click on the equipped item to give it back in inventory.

    If i do like it :
    - Open menu
    - Click on MyBelt (like image)
    ==> Item was equiped
    - I press ESC (to close Inventory)
    - Open menu again
    - Click on MyBelt (like second image)
    - Item come back in my inventory
    ===> It's work

    But if i do like it (and i want to do like it) :
    - Open menu
    - Click on MyBelt
    ==> Item was equipped
    - Click on item to get it
    ==> NO WORK

    I've add some debug messages, to test InventoryClickEvent, and in second exemple (red line), the event was no call, i don't know why.
    I give you some parts of my code to understand, cause since few hours i don't know how to fix it. Sorry for my english, thank for your time, and i hope you can help me :)

    My code :
    Code:java
    1. @EventHandler
    2. public void onClick(InventoryClickEvent e)
    3. {
    4. Player p = (Player)e.getWhoClicked();
    5. ItemStack current = e.getCurrentItem();
    6. Inventory inv = e.getInventory();
    7. String invn = p.getOpenInventory().getTitle();
    8. int raw = e.getRawSlot();
    9.  
    10. if(inv.getTitle().equalsIgnoreCase("Menu du joueur") || invn.equalsIgnoreCase("Menu du joueur"))
    11. {
    12.  
    13. Equipement equip = EquipementStorage.getPlayerEquipement(p);
    14.  
    15. if(inv.getTitle().equalsIgnoreCase("Menu du joueur") && (raw >= 0 && raw <= 35))
    16. {
    17. e.setCancelled(true);
    18. updateInventoryLater(p);
    19.  
    20. if(current == null)
    21. return;
    22.  
    23. if(current.getItemMeta() == null)
    24. return;
    25.  
    26. if(current.getItemMeta().getDisplayName() == null)
    27. return;
    28.  
    29. int slot = e.getSlot();
    30.  
    31. if(slot == 8 || slot ==15 || slot == 24 || slot == 26 || slot == 35)
    32. {
    33.  
    34. if(current.isSimilar(ies1) || current.isSimilar(ies2) || current.isSimilar(ies3) || current.isSimilar(ies4) || current.isSimilar(ies5))
    35. {
    36. //do nothing
    37. }
    38. else
    39. {
    40. boolean full = true;
    41. for(ItemStack item : e.getView().getBottomInventory().getContents())
    42. {
    43. if(item == null)
    44. {
    45. full = false;
    46. break;
    47. }
    48. else if(item != null && item.getType() == Material.AIR)
    49. {
    50. full = false;
    51. break;
    52. }
    53. }
    54.  
    55. if(full)
    56. {
    57. //Inventaire plein
    58. p.sendMessage(ChatColor.RED+"Is full!");
    59. return;
    60. }
    61.  
    62. EquipementStorage.setPlayerEquipement(p, equip);
    63. /** Checkup**/
    64. EffectItems.checkEquipedPlayerStats(p);
    65. p.closeInventory();
    66. openPlayerMenu(p);
    67. updateInventoryLater(p);
    68. }
    69. return;
    70. }
    71. }
    72. }
    73.  
    74. //If click in my inventory (bottom)
    75. else if(raw >= 36 && raw <= 71)
    76. {
    77. if(e.getClick() == ClickType.RIGHT && LevelRestrict.hasLevelToUse(current, p))
    78. {
    79. if(current == null)
    80. return;
    81.  
    82. ItemMeta im = current.getItemMeta();
    83. if(!im.hasLore())
    84. return;
    85. List<String> lore = im.getLore();
    86.  
    87. if(lore != null && lore.size() >= 2)
    88. {
    89. ItemStack retitem = null;
    90. int copie = 0;
    91.  
    92. if(lore.get(0).contains("Belt") || lore.get(1).contains("Belt"))
    93. {
    94. //get bottom inventory
    95. Inventory binv = e.getView().getBottomInventory();
    96.  
    97. //Stack all copy of item in one stack and remove one amount to equip it
    98. for(int i = 0; i < binv.getSize(); i++)
    99. {
    100. if(binv.getItem(i) != null && binv.getItem(i).isSimilar(current))
    101. {
    102. copie = copie + binv.getItem(i).getAmount();
    103. binv.setItem(i, null);
    104. }
    105. }
    106.  
    107. if(copie > 1)
    108. {
    109. copie = copie - 1;
    110. ItemStack ic = current.clone();
    111. ic.setAmount(copie);
    112. binv.addItem(ic);
    113. }
    114. //end of remove one amount
    115.  
    116. //Place item in storage
    117. ItemStack newit = current.clone();
    118. newit.setAmount(1);
    119. retitem = equip.getBague();//
    120. equip.setItem(5, newit);//
    121. EquipementStorage.setPlayerEquipement(p, equip);
    122. binv.addItem(retitem);
    123. //add stack of item left in bottom inventory
    124. }
    125.  
    126. updateInventoryLater(p);
    127. p.closeInventory();
    128. openPlayerMenu(p);
    129. return;
    130. }
    131. }
    132. }
    133. }
     

    Attached Files:

  2. Try closing and opening the inventory l, and / or use getRawSlot instead of getSlot
     
  3. Offline

    Pingoo

    Thank for your reply, but i already close and open my inventory :
    Code:java
    1. p.closeInventory();
    2. openPlayerMenu(p);

    The problem is not the slot. When i click in inventory, anywhere, InventoryClickEvent is call, but after the close and open (openPlayerMenu), the opened inventory no longer call InventoryClickEvent, why, i don't know ...
     
  4. Offline

    ProMCKingz

    Pingoo
    So you mean, when you close and re-open the inventory, the plugin will not function the click events?
     
  5. Offline

    Pingoo

    Yes, is that. When i click on the new inventory, the InventoryClickEvent was no call. I'll try some ideas tonight :)
     
  6. Offline

    ProMCKingz

    Pingoo
    Instead of using

    Code:java
    1. if(inv.getTitle().equalsIgnoreCase("Menu du joueur") || invn.equalsIgnoreCase("Menu du joueur"))
    2. {


    try:
    Code:java
    1. if (event.getInventory().getName()
    2. .equals(InvClass.getCompassInventory().getName())) {


    Replace "InvClass" with your class name that you are calling the event for. E.g for the code I provided InvClass would be my class with the inventory.
     
Thread Status:
Not open for further replies.

Share This Page