InventoryClickEvent Help

Discussion in 'Spigot Plugin Development' started by Dartanman, Jun 28, 2016.

  1. Hey guys! I've been working on a plugin, and I can't figure out how to make this work!

    Code (Text):

    public class OnArmorEquip implements Listener{
    @EventHandler
    public void onEquip(InventoryClickEvent event){
      Player player = (Player) event.getWhoClicked();
      ItemStack item1 = player.getInventory().getBoots();
      ItemStack dBoots = new ItemStack(Material.DIAMOND_BOOTS);
    if(item1 == dBoots){
    player.sendMessage("You have or just equipped Diamond Boots!");
    // This will do something else, I just have player.sendMessage since I don't want to give out my code.
    }
    }
    }
    The problem is that it doesn't send the message when they equip the diamond boots, but only if they click somewhere else, AFTER the diamond boots are on. I want it to do both, not just one. Thanks!
     
  2. I would suggest:
    Code (Java):
    @EventHandler
    private void invClick(InventoryClickEvent event) {
      if (event.getClickedInventory() != null) {
        if (event.getClickedInventory().getType() == InventoryType.PLAYER) {
          if (event.getSlotType() == SlotType.ARMOR) {
            if (event.getSlot() == 36) { // 36 -> armor boots slot for 1.8
              if (event.getCursor().getType() == Material.DIAMOND_BOOTS) {
                // finally do something
              }
            }
          }
        }
      }
    }
    EDIT: Updated null check and slot number.
     
    #2 Michel_0, Jun 28, 2016
    Last edited: Jun 28, 2016
  3. That should more or less be the right way, althought slot 8 should not be right, if you see the slots on that picture, you will see that the actual inventory is completely wrong, the normal inventory on the bottom already starts from 0. I guess the ARMOR slots have numbers from 0 - 3 or 4, if shield is counted too.
    But I've once tried something similar with the Crafting GUI, I managed to check for the click event and could cancel it, but there was no way for me to compare the item. (I think, there was some problem with it, it lacked something that the normal InventoryClickEvent had :D)
     
  4. Should definitely have some null checks in there.
     
    • "event" can't be null
    • "event.getClickedInventory()" can't return null
    • "event.getCursor()" can't return null (but might return air)
    So where you want null checks?
     
  5. As far as I remember, clicked inventory returns null if you click outside of the inventory itself. But that might just be my oldschool bukkit knowledge.

    https://hub.spigotmc.org/javadocs/s...nventoryClickEvent.html#getClickedInventory()
     
  6. Just tested it out on Spigot 1.8.8:
    • InventoryClickEvent is not triggered by a click outside of the inventory in creative mode, so ClickedInventory can't be null, so it can return null when clicking outside of the inventory in survival mode.
    • Armor slots where 36 until 39 (boots: 36, helmet: 39)
    • Hotbar slots where 0 until 8
    • Inventory slots where 9 until 35 (counted left to right, top to bottom)
    • Crafting field slots where 0 until 5 (0: result, 1 - 4 ingredients counted left to right and top to bottom)
      But the crafting field is no more the player inventory, it's the crafting inventory
    • If you want to know, when a player PLACES (equips) an item on the armor slot, you need to check the "cursor" item, because the Event is triggered before the item is placed into the slot. "Current" item is the item in the slot (normally air, before equipping).
    Code (Java):
    @EventHandler
    private void invClick(InventoryClickEvent event) {
      event.getWhoClicked().sendMessage("InventoryClickEvent triggered:");
      if (event.getClickedInventory() == null) {
        event.getWhoClicked().sendMessage("There is no inventory!");
      } else {
        event.getWhoClicked().sendMessage(event.getClickedInventory().getType().toString() + ", Slot: " + Integer.toString(event.getSlot()) + ", Current item: " + event.getCurrentItem().getType().toString() + ", Cursor Item: " + event.getCursor().getType());
      }
    }
    Strange behaviour... it only gets triggered in survival mode outside of the inventory, updated my prior posts.
     
    #7 Michel_0, Jun 28, 2016
    Last edited: Jun 28, 2016