Solved Gui getSlot?

Discussion in 'Plugin Development' started by YogurtSmudge, Dec 15, 2016.

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

    YogurtSmudge

    Alright, so in my gui plugin I have been working on, players are able to move items. So say that a cookie is in slot 3 in the gui, and a cake is in slot 5, and a player moves it to whatever slot they want in the Gui. How can I get the final slot that the items are in?

    I currently use this with InventoryClickEvent
    Code:
    if (event.getSlot(1).getType() == Material.COOKIE) {
    //do stuff
    ) else if (event.getSlot(2).getType() == Material.COOKIE) {
    
    }
    
    But this doesn't work right.
    Because it works with every slot not just 1 or 2

    Am I doing this right?

    Thanks.
     
  2. Offline

    FabeGabeMC

    If you want to get the last slot, you need to check for the size of the Inventory, then subtract 1 from it, as it counts from 0 to (last number - 1).
    Also, make sure the item is not null before checking its type. Otherwise, you will get a NullPointerException.
     
  3. Offline

    Sulphate

    The problem here is the event.getSlot() method returns the item slot that has been clicked, not an item at a slot in the inventory.

    Just simply get the inventory clicked using event.getClickedInventory(), and then get the item in a slot using inventory.getItem(int);

    Hope this helped :)
     
    ipodtouch0218 likes this.
Thread Status:
Not open for further replies.

Share This Page