Get GridInventory of chest player is currently viewing

So I have a DoubleChest custom inventory that I’m displaying to the player, and I’m trying to get that inventory from Player#getOpenInventory but I’m having some trouble figuring out how I’m supposed to distinguish between the three GridInventory's that result from querying the player’s open inventory. I know one is the chest, one is the player’s grid, and one is the player’s grid plus hotbar, but is there a simple and concise way of fetching the chest? Is it guaranteed to be the first one?

My current method is ultra janky but works well enough to let me continue testing other aspects of my custom inventory system.

Current method:

    static Optional<GridInventory> searchForSizedGrid(Player player, Vector2i size) {
        return player.getOpenInventory().map(c -> {
            Inventory inventory = c.query(QueryOperationTypes.INVENTORY_TYPE.of(GridInventory.class));

            for (Inventory inv : inventory) {
                if (inv instanceof GridInventory) {
                    GridInventory gridInventory = (GridInventory) inv;
                    if (gridInventory.getDimensions().equals(size)) {
                        return gridInventory;
                    }
                }
            }
           return null;
        });
    }
1 Like

Take a look at inventory archetype

The following does not work

    static Optional<GridInventory> searchForMain(Player player){
        return player.getOpenInventory().map(c -> {
            Inventory inventory = c.query(QueryOperationTypes.INVENTORY_TYPE.of(GridInventory.class));

            for (Inventory inv : inventory) {
                if (inv instanceof GridInventory) {
                    GridInventory gridInventory = (GridInventory) inv;
                    if (gridInventory.getArchetype() == InventoryArchetypes.DOUBLE_CHEST) {
                        return gridInventory;
                    }
                }
            }
            return null;
        });
    }

Sorry i meant from the main inventory. From your code it doesnt look like its possible

This is very hacky and do not use it for any reason once there is a better way.

Inventory topHalf = inventory.<Slot>slots().iterator().next().transform().root();