InventoryClickEvent - get the slot index of Shift + LeftClick

Discussion in 'Plugin Development' started by Father Of Time, May 29, 2012.

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

    Father Of Time

    Good afternoon all,

    I have a quick question about inventory events. I need to know what slot the ItemStack will be moved to when doing a Shift+Left click in an inventory view. So to break this question down a bit more:

    InventoryView = PlayerInventory & ChestInventory

    inside the InventoryView is the PlayerInventory (the bottom half of the view) and the ChestInventory (the top half of the view). Inside the player inventory is a stack of 32 glowstone dust. when I hold shift and left click the glowstone dust the entire stack will be moved from the player inventory to the first empty slot in the chest inventory.

    So my question is, how do I obtain the slot to which the glowstone dust will be moved to. Say the ChestInventory has cobble in the first slot, wheat in the second and iron ingots in the third; when shift clicking the glowstone in the player inventory the glowstone dust will be automatically moved to the 4th inventory slot, or index 3.

    So my question is how can I determine what slot the object will be moved to during a shift + left click.

    Thank you in advance to all who assist me with this inquiry, Have a wonderful day!
     
  2. Offline

    kumpelblase2

    Short answer: you can't, since it might get stored in more than one slot.
    Long answer:
    You need to go through each slot and check if the item you clicked can be added to this slot, if so remove the amount that can be added from the original stack and save the slot number. Do this until the end of the inventory or until the amount of the original stack is 0. I have done this already in Dragons Lair to be able to log such changes in a chest.

    Edit: You probably want to check all slots with the same item type as the item you clicked first. After that, if the amount of the original itemstack is greater than 0 you'd need to get the first empty itemslot because the item would technically get filled in there as well.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  3. Offline

    Father Of Time

    You are the north star by which the night sky is lit, thanks Kumpel.

    I should have assumed this as I've already tackled this exact problem with shopkeeper (you know what I am referring to), but I figured I would ask to make sure no new method of obtaining this information existed with the new events.

    Keep on rockin Kumpel, and congrats on the successful conversations webinar; I look forward to re-watching it after work. :D
     
  4. Offline

    kumpelblase2

    For future reference, this returns a list of the changed slots in the inventory.
    Parameters: inInv, the inventory you want to add the itemstack to
    inNew: itemstack that should be added
    Code:
    public static List<Integer> getChangedSlots(Inventory inInv, ItemStack inNew)
        {
            List<Integer> changed = new ArrayList<Integer>();
            if(inInv.contains(inNew.getType()))
            {
                int amount = inNew.getAmount();
                HashMap<Integer, ? extends ItemStack> items = inInv.all(inNew.getType());
                for(int i = 0; i < inInv.getSize(); i++)
                {
                    if(!items.containsKey((Integer)i))
                        continue;
                   
                    ItemStack item = items.get((Integer)i);
                    int slotamount = item.getMaxStackSize() - item.getAmount();
                    if(slotamount > 1)
                    {
                        if(amount > slotamount)
                        {
                            int toAdd = slotamount - amount;
                            amount = amount - toAdd;
                            changed.add(i);
                        }
                        else
                        {
                            changed.add(i);
                            amount = 0;
                            break;
                        }
                    }
                }
               
                if(amount > 0)
                {
                    if(inInv.firstEmpty() != -1)
                        changed.add(inInv.firstEmpty());
                }
            }
            else
            {
                if(inInv.firstEmpty() != -1)
                    changed.add(inInv.firstEmpty());
            }
            return changed;
        }
    (Note: this does not add the item to the inventory)
    This should probably work since I only changed it from the code that I use in my plugin.
     
    Badeye likes this.
Thread Status:
Not open for further replies.

Share This Page