6 min

Inventory & Items

Manage player inventories, create items, and handle item transactions.

Accessing Inventory

Get a player's inventory directly from the Player object:

1Inventory inv = player.getInventory();
2
3// Inventory sections
4ItemContainer hotbar = inv.getHotbar();
5ItemContainer storage = inv.getStorage();
6ItemContainer armor = inv.getArmor();
7ItemContainer tools = inv.getTools();
8ItemContainer utility = inv.getUtility();
9ItemContainer backpack = inv.getBackpack();

Inventory Sections

sectionmethoddescription
HotbargetHotbar()Main hotbar slots
StoragegetStorage()Main inventory grid
ArmorgetArmor()Armor slots
ToolsgetTools()Tool belt
UtilitygetUtility()Utility items
BackpackgetBackpack()Extended storage

ItemStack

Items are represented as ItemStack objects:

1// Create items
2ItemStack sword = new ItemStack("iron_sword");
3ItemStack stack = new ItemStack("cobblestone", 64);
4
5// Item properties
6String id = stack.getItemId();
7int quantity = stack.getQuantity();
8boolean empty = stack.isEmpty();
9
10// Durability
11double durability = stack.getDurability();
12double maxDurability = stack.getMaxDurability();
13boolean broken = stack.isBroken();
14
15// Modify (returns new ItemStack - immutable)
16ItemStack more = stack.withQuantity(32);
17ItemStack damaged = sword.withDurability(50.0);
18ItemStack repaired = sword.withRestoredDurability(10.0);
immutable
ItemStack is immutable. Methods like withQuantity() return a new ItemStack.

Common Operations

Get Active Item

1// Get item in hand
2ItemStack inHand = inv.getItemInHand();
3
4// Get specific active slots
5ItemStack hotbarItem = inv.getActiveHotbarItem();
6ItemStack toolItem = inv.getActiveToolItem();
7ItemStack utilityItem = inv.getUtilityItem();
8
9// Get/set active slot index
10byte activeSlot = inv.getActiveHotbarSlot();
11inv.setActiveHotbarSlot((byte) 0);

Add Items

1ItemContainer storage = inv.getStorage();
2
3// Add single item
4ItemStack item = new ItemStack("diamond", 5);
5storage.addItemStack(item);
6
7// Add to specific slot
8storage.addItemStackToSlot((short) 0, item);
9
10// Check if can add first
11if (storage.canAddItemStack(item)) {
12 storage.addItemStack(item);
13}

Remove Items

1ItemContainer storage = inv.getStorage();
2
3// Remove from slot
4storage.removeItemStackFromSlot((short) 0);
5
6// Remove specific quantity
7storage.removeItemStackFromSlot((short) 0, 10);
8
9// Remove item type from anywhere
10ItemStack toRemove = new ItemStack("cobblestone", 32);
11if (storage.canRemoveItemStack(toRemove)) {
12 storage.removeItemStack(toRemove);
13}

Move Items

1ItemContainer hotbar = inv.getHotbar();
2ItemContainer storage = inv.getStorage();
3
4// Move from slot to another container
5hotbar.moveItemStackFromSlot((short) 0, storage);
6
7// Move to specific slot
8hotbar.moveItemStackFromSlotToSlot(
9 (short) 0, // from slot
10 10, // quantity
11 storage, // to container
12 (short) 5 // to slot
13);

Checking Contents

1ItemContainer storage = inv.getStorage();
2
3// Check if empty
4boolean empty = storage.isEmpty();
5
6// Get capacity
7short capacity = storage.getCapacity();
8
9// Get item at slot
10ItemStack item = storage.getItemStack((short) 0);
11
12// Count items matching condition
13int diamonds = storage.countItemStacks(
14 stack -> stack.getItemId().equals("diamond")
15);
16
17// Iterate all items
18storage.forEach((slot, stack) -> {
19 if (!stack.isEmpty()) {
20 // process item
21 }
22});

Clear & Drop

1Inventory inv = player.getInventory();
2
3// Clear entire inventory
4inv.clear();
5
6// Clear specific section
7inv.getStorage().clear();
8
9// Drop all items (returns list of dropped items)
10List dropped = inv.dropAllItemStacks();
11
12// Sort storage
13inv.sortStorage(SortType.BY_NAME);

Listen to Changes

1ItemContainer storage = inv.getStorage();
2
3// Listen to any change
4storage.registerChangeEvent(event -> {
5 short slot = event.getSlot();
6 ItemStack oldItem = event.getOldItemStack();
7 ItemStack newItem = event.getNewItemStack();
8
9 getLogger().at(Level.INFO).log("Slot %d changed", slot);
10});
11
12// Listen to specific slot
13storage.registerChangeEvent((short) 0, event -> {
14 // Only fires for slot 0
15});
thread safety
Inventory modifications should be done in world.execute() to be safe.