crwdns156065:0crwdne156065:0

crwdns156067:0:javadoc:crwdne156067:0

crwdns156069:0crwdne156069:0

crwdns156071:0:javadoc:crwdne156071:0

// Returns the slot at a given index
Optional<Slot> slot = inventory.slot(index);
// Returns all the slots in the inventory in order
List<Slot> slots = inventory.slots();
// returns the number of slots in the inventory
int capacity = inventory.capacity();

crwdns156073:0crwdne156073:0

 // Returns the first non-empty ItemStack following the order of `slots()`
ItemStack firstStack = inventory.peek();
// Returns the ItemStack at a given index.
// Note that the result is `Optional.empty()` only when the slot does not exist.
// If the slot is empty the stack is `ItemStack.empty()`
Optional<ItemStack> stackAt = inventory.peekAt(index);

// sums total quantity of all stacks in the inventory.
// In combination with a query for a stack this can count the quantity of that stack in the inventory.
int quantity = inventory.totalQuantity();

// checks for an exact match in the inventory.
boolean contains = inventory.contains(stack/type);
// checks for a match ignoring quantity in the inventory.
boolean containsAny = inventory.containsAny(stack);

crwdns156075:0crwdne156075:0

crwdns156077:0crwdne156077:0

// Adds stacks to the inventory filling it up following the order of `slots()`
InventoryTransactionResult result1 = inventory.offer(stack);
// Adds a stack to the slot at given index.
InventoryTransactionResult result2 = inventory.offer(index, stack);

// Returns whether a `offer(...)` would succeed.
boolean canFit = inventory.canFit(stack);
// Returns the number of empty slots.
// If it is zero `offer(...)` could still succeed if it can stack with an existing stack.
int freeCapacity = inventory.freeCapacity();

// Returns the rejected items if there was not enough space to fit every item.
List<ItemStackSnapshot> rejected = result1.rejectedItems();
// Reverts the transaction
result1.revert();
// InventoryTransactionResults can be combined
InventoryTransactionResult combined = result1.and(result2);
// Reverts the transaction if any of the combined transaction was not successful
combined.revertOnFailure();

crwdns156079:0crwdne156079:0

crwdns156081:0crwdne156081:0

// Removes the first non-empty (analogous to `peek()`) stack in the inventory
InventoryTransactionResult.Poll result1 = inventory.poll()
// Removes the first non-empty stack in the inventory up the the given limit.
// If the limit is higher than what is in the first empty slot it will continue removing the same item from slots after it up to the limit.
InventoryTransactionResult.Poll result2 = inventory.poll(limit)
// Removes the stack from given index
InventoryTransactionResult.Poll result3 = inventory.pollFrom(index)
// Removes the stack from given index but only up to the given limit
InventoryTransactionResult.Poll result4 = inventory.pollFrom(index, limit)

// Returns the polled item
ItemStackSnapshot polledStack = result1.polledItem();
// InventoryTransactionResults can be combined
InventoryTransactionResult combined = result1.and(result2).and(result3).and(result4);
// Returns the list of polled items - useful when handling combined results
List<ItemStackSnapshot> polledStack = result.polledItems();

crwdns156083:0crwdne156083:0

crwdns156085:0crwdne156085:0

// Sets the content of a single slot at given index replacing the previous item.
inventory.set(index, stack);
// Sets the content of a slot
slot.set(stack);
// Sets all slots to ItemStack.empty()
inventory.clear()

Note

crwdns156087:0crwdne156087:0

crwdns156089:0crwdne156089:0

crwdns156091:0crwdne156091:0

crwdns156093:0crwdne156093:0

crwdns156095:0crwdne156095:0

crwdns156097:0crwdne156097:0

crwdns156099:0crwdne156099:0

crwdns156101:0crwdne156101:0

crwdns156103:0crwdne156103:0

crwdns156105:0crwdne156105:0

crwdns156107:0crwdne156107:0

public static void query() {
    TODO think up some nice query examples
}

crwdns156109:0crwdne156109:0

crwdns156111:0crwdne156111:0

crwdns156113:0crwdne156113:0

crwdns156115:0crwdne156115:0

crwdns156117:0:ref:crwdne156117:0

// Only ViewableInventory can be opened
Optional<Container> container1 = player.openInventory(inventory);
// Optionally provide a title for the container - not supported for all inventories
Optional<Container> container2 = player.openInventory(inventory, Component.text("My Title"));

crwdns156119:0:ref:crwdne156119:0

Note

crwdns156121:0crwdne156121:0