GridInventory isn’t an
InventoryProperty, so that won’t compile. But you’re on the right track.
First, it’s important to know what the context is behind what you’re doing - the methods for working with an inventory differ based on whether you have a chest, a player’s inventory, or some other implementation. An inventory is a combination of 0, 1, or more slots and contains multiple layers, so you need to specify what you’re looking for through queries.
For example, the player’s inventory (
player.getInventory()) contains multiple inventories - the main inventory, the hotbar, the player’s armor, and potentially more as a result of mods. To target a specific slot, you need to first query the part of inventory you’re interested in. For simplicities sake, I’m looking for the top left slot of the player’s main inventory. We can get that using:
Inventory main = player.getInventory().query(MainPlayerInventory.class);
At this point, the inventory we have could be one of three things:
- An empty inventory, if there was no match in the query (in this case, that shouldn’t happen - but you never know thanks to mods).
- A single
MainPlayerInventory object representing the player’s main inventory
InventoryAdaptor containing multiple
MainPlayerInventory objects, one of which is (hopefully) the player’s main inventory (also shouldn’t happen, but… mods )
We’re going to presume that nothing is interfering with the inventory structure and the second one occurred, but it’s important to note the possibility of other results (as we’ll be dealing with this later).
MainPlayerInventory contains the player’s hotbar and grid inventories. We can get the grid inventory from here, and then finally get the slot in column - row order (which places the top left slot at 0, 0). I’ll leave you to handle the returned
Optional as you see fit.
Optional<Slot> slot = ((MainPlayerInventory) main).getGrid().getSlot(0, 0);
As said above, a lot of this depends on what you’re trying to do and the context of the situation. It’s always better to include detail so people know exactly what you’re trying to accomplish and set you on the right path. Giving directions to a location without knowing where you’re starting from is a losing battle.