Solved Drop event - detect if the item was dropped by Q key or from inventory

Discussion in 'Spigot Plugin Development' started by BlackPhantom389, Aug 20, 2016.

  1. Hello,
    I have a drop event which I need to cancel when the player presses the Q key, but not when the item is dropped from the inventory. I read the Javadoc more than enough and I can't figure out how to detect if there's any difference.

    Any idea ?
    Thanks.
     
  2. You can check if the player's inventory is open by adding the player to a list on InventoryOpenEvent and removing them on InventoryCloseEvent. Then, when PlayerDropItemEvent is called, check if the player is in the list and cancel the event if they aren't.
    I don't think it's possible to detect pressing Q while hovering over an item in the inventory, though.
     
  3. Check if
    Code (Java):
    Player#getOpenInventory() == null
    when item is dropped.
     
  4. I already did it, it seems like "inventory.container.crafting" or something like that is open. The inventory is somehow never closed. I could not cancel the event if the inventory is null or the inventory is the latter, but would it be reliable ?

    That's a pretty good idea. Gonna try it.

    EDIT: the InventoryOpenEvent doesn't fire for player inventories, so it won't work.
     
    #4 BlackPhantom389, Aug 20, 2016
    Last edited: Aug 20, 2016
  5. I just looked it up and it seems like the client doesn't send a packet to the server when a player opens their own inventory and the event is only fired when opening chests, furnaces, etc.

    I think I have a solution though:
    A player has to click on an item first before dropping it while the inventory is open. So if you add the player to the list when InventoryClickEvent is called, then you would be sure that an inventory is open. Then remove them the same way as before, by using InventoryCloseEvent.

    Edit: I just tested it and InventoryClickEvent also gets called when a player hovers over an item and presses Q. So I believe that would be perfect for you
     
    • Agree Agree x 1
  6. Actually it doesn't. I've tested it.
     
  7. Okay, it worked like a charm. Pretty tricky, but by using your method I figured out how to make it work: when a player pickup an item from his inventory (so the action is a pickup action), I register him in a list, and he places an item I remove him from this list. So when he drops an item from his inventory, he will still be in the list and I can let him drop and at that point I also remove him.

    Many thanks to you !