Detect When User Moves An Item To A Different Inventory By Pressing (1-9)

Discussion in 'Plugin Development' started by MaxTheGinus, May 8, 2020.

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

    MaxTheGinus

    I'm preventing players on my server from moving an item into a chest or similar inventory using the
    InventoryClickEvent. However, some sneaky players have figured out that they can put the item in their Hotbar, then while hovering over a slot in a chest or similar inventory, press the number on their keyboard that corresponds to the Hotbar slot, moving the item into the chest.

    I've looked through the JavaDoc and can't seem to find a similar event that I can use to prevent this. Any help would be greatly appreciated.
     
  2. Offline

    wand555

    I'm pretty sure this calls the event with #getCurrentItem().
     
  3. Offline

    MaxTheGinus

    @wand555 I'm using getCurrentItem() and it's not detecting the item when they use this technique. Do you know of another event?
     
  4. Offline

    Tango_

    If you use event.setCancelled(true) in the right place, you can prevent this easily in InventoryClickEvent
     
  5. Offline

    KarimAKL

    @MaxTheGinus Use InventoryClickEvent#getHotbarButton() to get the button they clicked from 0-8, you can use this together with Player#getInventory()#getItem(int) to get the item they want to move.

    Note: InventoryClickEvent#getHotbarButton() returns -1 if no button was clicked.
     
  6. Offline

    wand555

    Print the current item to the console and replicate the 'bug'. I didn't test your exact case, but a few days ago I had to prevent any item interaction with another inventory and using getCurrentItem() was sufficient for me.
     
  7. Offline

    KarimAKL

    InventoryClickEvent#getCurrentItem() gets the item in the slot where your cursor is.
    InventoryClickEvent#getCursor() gets the item you're moving around with the cursor after clicking normally once.
    InventoryClickEvent#getHotbarButton() gets the button you pressed using the hotbar keys 1 - 9. (returns 0 - 8)
     
    MaxTheGinus likes this.
  8. Offline

    MaxTheGinus

    @KarimAKL Thanks! For anyone wondering, this is my code:

    Code:
      // Player clicked an item in an inventory
      @EventHandler
      public void onInventoryClick(InventoryClickEvent event) {
    
          // If inventory other than the player's inventory (ex. a chest) and item claim tool, prevent them from moving it
          if(!event.getInventory().getType().equals(InventoryType.CRAFTING) &&
          (event.getCurrentItem().equals(claimViewTool) ||
          event.getCurrentItem().equals(claimEditTool) ||
          event.getWhoClicked().getInventory().getItem(event.getHotbarButton()).equals(claimViewTool) ||
          event.getWhoClicked().getInventory().getItem(event.getHotbarButton()).equals(claimEditTool))) {
            
              event.setCancelled(true);
          }
      }
     
Thread Status:
Not open for further replies.

Share This Page