Solved Detect when a player moves an item to an inventory slot

Discussion in 'Plugin Development' started by MrBakbuki, May 19, 2021.

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

    MrBakbuki

    That might be a dumb question that may be very easy to solve, but I can't find the right event to listen for.
    I'm trying to detect when a player puts an item into a specific GUI (Inventory) slot, and then do something, but I can't find the right event.

    Any help appreciated
     
  2. Offline

    davidclue

    InventoryClickEvent
     
  3. Offline

    KarimAKL

    @MrBakbuki You should listen for both the InventoryClickEvent and the InventoryDragEvent.
     
  4. Offline

    MrBakbuki

    The event doesn't trigger all the time, only if I drag the item into the slot which is not exactly what I'm aiming for.
    Here's my testing and it doesn't broadcast the message all the time, only when I drag the item into the slot.
    Code:
    @EventHandler
        public void onInventoryDrag(InventoryDragEvent e){
            Bukkit.broadcastMessage("test");
        }
     
  5. Offline

    Xp10d3

    Try using InventoryClickEvent and InventoryDragEvent. You should be able to get the slot they clicked/dragged.
    Code:java
    1.  
    2. @EventHandler
    3. public void onClick(InventoryClickEvent event) {
    4. if (event.getSlot() == 1) {
    5. // do stuff
    6. }
    7. }
    8.  
    9. @EventHandler
    10. public void onDrag(InventoryDragEvent event) {
    11. // not as familiar with inventorydragevent but there is .getSlots
    12. if (event.getInventorySlots().contains("somearrayidek")) {
    13. // do stuff
    14. }
    15. }
    16.  


    yea ik that i did inventorydragevent wrong but we dont talk about that lmfao. you should use InventoryClickEvent i just gave an example for InventoryDragEvent as it might help
     
    darthvader1925 likes this.
  6. Offline

    darthvader1925

    The IntentoryDragEvent is only going to fire when you drag it, hence the name.
     
    Xp10d3 likes this.
  7. Offline

    MrBakbuki

    Then how can I check when a player puts an item into an inventory? I still cant find a way, and the InventoryClickEvent doesn't trigger when you put an item into an inventory, only when you take it out.
     
  8. Offline

    darthvader1925

    It triggers when you click an item, what do you mean "put item into an inventory"?? Swapping the slot?
     
  9. Offline

    MrBakbuki

    @darthvader1925 When a player does this:

    upload_2021-5-20_19-36-1.png


    upload_2021-5-20_19-36-14.png
     

    Attached Files:

  10. Offline

    KarimAKL

    @MrBakbuki
    InventoryClickEvent = player clicks an item already in the inventory or keeps the mouse still while putting an item from the cursor into the inventory.
    InventoryDragEvent = player drags an item from the cursor into the inventory.

    Note that the InventoryClickEvent also handles hotbar swaps.
     
    Xp10d3 and darthvader1925 like this.
  11. Offline

    darthvader1925

    Here is an example:
    Code:
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
        if (e.getSlot == 1) // Any Slot Number {
         e.getPlayer().sendMessage("Hello, World");
       }
    }
    This will fire when you click on slot 1, or whatever slot number u supply.
     
  12. Offline

    MrBakbuki

    Thanks for all the information, I think I solved it by checking if the player clicked/dragged on the slot I want them to put the item in.
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page