Check if particular item in Inventory is clicked

I want to create something like a inventory navigation menu of sorts for players on the server.

I have a Inventory object shop, and players can access that custom inventory. I have already put the navigation item(s) into the inventory at particular locations.

How do I detect if one (or more) of (particular) inventory items were clicked?
I am currently looking at the ClickInventoryEvent, but I can find no clear way to identify the item(s).

Does this answer your question? As in getting Inventory Positon?

I wrote this as a test to see if SlotPos.getX() would give me a proper value:

@Listener
public void onInventoryClick(ClickInventoryEvent event){
   Logger.debug("ClickInventoryEvent run");
   for (SlotTransaction t: event.getTransactions()){
      Slot s = t.getSlot();
      SlotPos pos = s.getProperty(SlotPos.class, null).get();
      Logger.debug(pos.getX() + " " + pos.getY()); // Custom class, ignore.
   }
}

and it threw a java.util.NoSuchElementException: No value present exception.

Checking with if(...isPresent()) will just not run anything within the if statement.

With null that definitly wont work.

I reccommend using getInventoryProperty(SlotPos.class)

The other variant needs the lowerecased classname as string getProperty(SlotPos.class, "slotpos")

1 Like

Well i never really managed to get the SlotPos either so i get the SlotIndex and then convert it to SlotPos if i need it

Unfortunately, the same thing occurs…

Code I used:

for (SlotTransaction t: event.getTransactions()){
    Slot s = t.getSlot();
    SlotPos pos = s.getInventoryProperty(SlotPos.class).get();
    Logger.debug(pos.getX() + " " + pos.getY());
}

Well i also tried everything my self but i noticed unlike SlotPos you can always get SlotIndex so instead i made a really simple function to convert SlotIndex to SlotPos so if you really want that you could do the same i guess