How to check if dropped item was dropped from hand or the inventory?

Discussion in 'Plugin Development' started by Deathoffully, Jan 7, 2019.

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

    Deathoffully

    Title says all. I want to check if the dropped item from PlayerDropItemEvent was dropped from the mainhand, offhand or the inventory. How can I do this?
     
  2. Offline

    KarimAKL

    @Deathoffully I've never tried this so i'm not sure it'll work but have you tried something like this?
    Code:Java
    1. @EventHandler
    2. public void onDrop(PlayerDropItemEvent e) {
    3. for (int i = 0; i < e.getPlayer().getInventory().getContents().length; i++) {
    4. if (e.getPlayer().getInventory().getItem(i).equals(e.getItemDrop().getItemStack())) {
    5. if (i == e.getPlayer().getInventory().getHeldItemSlot()) {
    6. //Main hand
    7. } else if (i == 40) {
    8. //Off hand
    9. } else {
    10. //Is not main hand or off hand. (inventory)
    11. }
    12. }
    13. }
    14. }
     
  3. Offline

    Deathoffully

    @KarimAKL I tried something similar.

    On your suggestion:
    1. You need to check if the itemslot you want to check is null/empty before comparing with the dropped item
      Code:
      if(e.getPlayer().getInventory().getItem(i) != null)
    2. It checks if the player has another stack like the dropped itemstack in his inventory (even considering the stacksize) and uses this stack's index to determine where the dropped stack came from (which isn't what I need). I.e. When you throw 1 Dirt-Block from your mainhand and have another Dirt-Block in your inventory the alg will say that the dopped item was dropped from your inventory (same with hotbar and offhand).
    It would be awesome if there was a way to get the index the item was dropped from.

    I have a new idea which I'm ging to try tomorrow. You could check if the player that dropped the item has his inventory opened at the time he drops the item. Is there an easier way to check that than having a list of players with opened inventories and check if the player that dropped the item is in that list? Is there an event for when an entity closes his inventory (pretty sure there is one for when it gets opened)?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    EDIT : Couldn't create the list of player which got their inventory open 'cause the InventoryOpenEvent doesn't trigger when a player opens their inventory. (see: https://bukkit.org/threads/detect-player-opening-his-inventory.137228/)
     
    Last edited: Jan 8, 2019
  4. Offline

    KarimAKL

    @Deathoffully
    1. I didn't do a null check because it was just an example.
    2. I see, my bad.
    3. Yeah it would be awesome if there was a 'event.getSlot()' function in the PlayerDropItemEvent.
    4. Can't think of an easier way at the moment, though i just woke up.
    5. There is the InventoryCloseEvent.
     
Thread Status:
Not open for further replies.

Share This Page