Accessing Inventory
Get a player's inventory directly from the Player object:
1Inventory inv = player.getInventory();23// Inventory sections4ItemContainer 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
| section | method | description |
|---|---|---|
| Hotbar | getHotbar() | Main hotbar slots |
| Storage | getStorage() | Main inventory grid |
| Armor | getArmor() | Armor slots |
| Tools | getTools() | Tool belt |
| Utility | getUtility() | Utility items |
| Backpack | getBackpack() | Extended storage |
ItemStack
Items are represented as ItemStack objects:
1// Create items2ItemStack sword = new ItemStack("iron_sword");3ItemStack stack = new ItemStack("cobblestone", 64);45// Item properties6String id = stack.getItemId();7int quantity = stack.getQuantity();8boolean empty = stack.isEmpty();910// Durability11double durability = stack.getDurability();12double maxDurability = stack.getMaxDurability();13boolean broken = stack.isBroken();1415// 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 hand2ItemStack inHand = inv.getItemInHand();34// Get specific active slots5ItemStack hotbarItem = inv.getActiveHotbarItem();6ItemStack toolItem = inv.getActiveToolItem();7ItemStack utilityItem = inv.getUtilityItem();89// Get/set active slot index10byte activeSlot = inv.getActiveHotbarSlot();11inv.setActiveHotbarSlot((byte) 0);Add Items
1ItemContainer storage = inv.getStorage();23// Add single item4ItemStack item = new ItemStack("diamond", 5);5storage.addItemStack(item);67// Add to specific slot8storage.addItemStackToSlot((short) 0, item);910// Check if can add first11if (storage.canAddItemStack(item)) {12 storage.addItemStack(item);13}Remove Items
1ItemContainer storage = inv.getStorage();23// Remove from slot4storage.removeItemStackFromSlot((short) 0);56// Remove specific quantity7storage.removeItemStackFromSlot((short) 0, 10);89// Remove item type from anywhere10ItemStack toRemove = new ItemStack("cobblestone", 32);11if (storage.canRemoveItemStack(toRemove)) {12 storage.removeItemStack(toRemove);13}Move Items
1ItemContainer hotbar = inv.getHotbar();2ItemContainer storage = inv.getStorage();34// Move from slot to another container5hotbar.moveItemStackFromSlot((short) 0, storage);67// Move to specific slot8hotbar.moveItemStackFromSlotToSlot(9 (short) 0, // from slot10 10, // quantity11 storage, // to container12 (short) 5 // to slot13);Checking Contents
1ItemContainer storage = inv.getStorage();23// Check if empty4boolean empty = storage.isEmpty();56// Get capacity7short capacity = storage.getCapacity();89// Get item at slot10ItemStack item = storage.getItemStack((short) 0);1112// Count items matching condition13int diamonds = storage.countItemStacks(14 stack -> stack.getItemId().equals("diamond")15);1617// Iterate all items18storage.forEach((slot, stack) -> {19 if (!stack.isEmpty()) {20 // process item21 }22});Clear & Drop
1Inventory inv = player.getInventory();23// Clear entire inventory4inv.clear();56// Clear specific section7inv.getStorage().clear();89// Drop all items (returns list of dropped items)10List dropped = inv.dropAllItemStacks(); 1112// Sort storage13inv.sortStorage(SortType.BY_NAME);Listen to Changes
1ItemContainer storage = inv.getStorage();23// Listen to any change4storage.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});1112// Listen to specific slot13storage.registerChangeEvent((short) 0, event -> {14 // Only fires for slot 015});thread safety
Inventory modifications should be done in
world.execute() to be safe.