Inventory System Overview
Inventory System Overview
Section titled “Inventory System Overview”The Inventory System manages items, item stacks, and containers for players and other entities. It provides a flexible, transaction-based approach to item manipulation.
Package Location
Section titled “Package Location”- ItemStack:
com.hypixel.hytale.server.core.inventory.ItemStack - Containers:
com.hypixel.hytale.server.core.inventory.container - Transactions:
com.hypixel.hytale.server.core.inventory.transaction - Filters:
com.hypixel.hytale.server.core.inventory.container.filter
Core Classes
Section titled “Core Classes”| Class | Description |
|---|---|
ItemStack | Represents a stack of items |
ItemContainer | Base class for item storage |
SimpleItemContainer | Basic container implementation |
Transaction | Base for inventory operations |
Inventory | Player inventory component |
ItemStack
Section titled “ItemStack”ItemStack represents one or more items of the same type.
Creating Item Stacks
Section titled “Creating Item Stacks”// Single itemItemStack sword = new ItemStack("Sword_Iron");
// Stack of itemsItemStack arrows = new ItemStack("Arrow_Standard", 64);
// With metadataBsonDocument metadata = new BsonDocument();metadata.put("enchantment", new BsonString("fire"));ItemStack enchantedSword = new ItemStack("Sword_Iron", 1, metadata);
// With durabilityItemStack damagedSword = new ItemStack( "Sword_Iron", // item ID 1, // quantity 50.0, // current durability 100.0, // max durability null // metadata);ItemStack Properties
Section titled “ItemStack Properties”| Property | Type | Description |
|---|---|---|
itemId | String | Item type identifier |
quantity | int | Number of items in stack |
durability | double | Current durability |
maxDurability | double | Maximum durability |
metadata | BsonDocument | Custom item data |
ItemStack Methods
Section titled “ItemStack Methods”// Get propertiesString itemId = stack.getItemId();int quantity = stack.getQuantity();double durability = stack.getDurability();
// Get item definitionItem item = stack.getItem();
// Check stateboolean empty = stack.isEmpty();boolean broken = stack.isBroken();boolean unbreakable = stack.isUnbreakable();
// Check if can stack with anotherboolean stackable = stack.isStackableWith(otherStack);
// Get block key (for placeable items)String blockKey = stack.getBlockKey();
// CloneItemStack copy = stack.clone();Empty ItemStack
Section titled “Empty ItemStack”// The empty item stack singletonItemStack empty = ItemStack.EMPTY;
// Check if emptyif (stack.isEmpty()) { // No items}
// Static utilityboolean isEmpty = ItemStack.isEmpty(stack); // null-safeItemContainer
Section titled “ItemContainer”ItemContainer is the base class for all item storage.
Container Types
Section titled “Container Types”| Type | Description |
|---|---|
SimpleItemContainer | Basic fixed-size container |
ItemStackItemContainer | Single-slot container |
CombinedItemContainer | Multiple containers as one |
DelegateItemContainer | Wraps another container |
EmptyItemContainer | No storage (always empty) |
Container Properties
Section titled “Container Properties”// Get capacityshort capacity = container.getCapacity();
// Get slot contentsItemStack item = container.getSlot(slot);
// Set slot contentsItemStack previous = container.setSlot(slot, itemStack);
// Remove from slotItemStack removed = container.removeSlot(slot);
// Clear all slotsClearTransaction cleared = container.clear();Adding Items
Section titled “Adding Items”// Add item stack (returns what couldn't be added)ItemStackTransaction tx = container.addItemStack(itemStack);ItemStack remainder = tx.getRemainingStack();
// Add to specific slotItemStackSlotTransaction slotTx = container.addItemStackToSlot(slot, itemStack);
// Add with optionscontainer.addItemStack( itemStack, false, // allOrNothing - if true, fails if can't add all true // filter - respect slot filters);Removing Items
Section titled “Removing Items”// Remove specific itemItemStackTransaction tx = container.removeItemStack(itemStack);
// Remove from slotItemStackSlotTransaction slotTx = container.removeItemStackFromSlot(slot, quantity);
// Remove by materialMaterialTransaction materialTx = container.removeMaterial(materialId, quantity);
// Remove by tagTagTransaction tagTx = container.removeTag(tagId, quantity);Checking Contents
Section titled “Checking Contents”// Check if has itemboolean hasItem = container.hasItemStack(itemStack);
// Count items of typeint count = container.countMaterial(materialId);
// Check for spaceboolean canAdd = container.canAddItemStackToSlot(slot, itemStack, false, true);
// Find first empty slotshort emptySlot = container.getFirstEmptySlot();
// Iterate contentscontainer.forEach((slot, itemStack) -> { // Process each non-empty slot});Container Filters
Section titled “Container Filters”Filters control what items can go in which slots:
// Set global filtercontainer.setGlobalFilter(filterType);
// Set slot-specific filtercontainer.setSlotFilter(FilterActionType.ADD, slot, slotFilter);container.setSlotFilter(FilterActionType.REMOVE, slot, slotFilter);container.setSlotFilter(FilterActionType.DROP, slot, slotFilter);Built-in Filters
Section titled “Built-in Filters”| Filter | Description |
|---|---|
TagFilter | Filter by item tag |
ResourceFilter | Filter by resource type |
ArmorSlotAddFilter | Armor slot restrictions |
NoDuplicateFilter | Prevent duplicate items |
Container Events
Section titled “Container Events”// Register for change eventscontainer.registerChangeEvent(event -> { Transaction transaction = event.getTransaction(); // Handle changes});
// With prioritycontainer.registerChangeEvent(EventPriority.NORMAL, event -> { // Handle changes});Transactions
Section titled “Transactions”All inventory operations return a transaction object describing what happened.
Transaction Types
Section titled “Transaction Types”| Transaction | Description |
|---|---|
ItemStackTransaction | Add/remove item stack |
ItemStackSlotTransaction | Slot-specific item operation |
MaterialTransaction | Material-based operations |
TagTransaction | Tag-based operations |
ResourceTransaction | Resource-based operations |
MoveTransaction | Move between containers |
ClearTransaction | Clear container |
Transaction Results
Section titled “Transaction Results”// Item stack transactionItemStackTransaction tx = container.addItemStack(itemStack);int added = tx.getQuantityChange();ItemStack remainder = tx.getRemainingStack();List<SlotTransaction> slots = tx.getSlots();
// Check if completeif (tx.getRemainingStack().isEmpty()) { // All items were added}Moving Items
Section titled “Moving Items”Between Containers
Section titled “Between Containers”// Move item between containersMoveTransaction move = sourceContainer.moveToContainer( slot, // source slot targetContainer, MoveType.ALL // move type);
// Move typesMoveType.ALL // Move entire stackMoveType.HALF // Move half the stackMoveType.ONE // Move single itemSwap Containers
Section titled “Swap Containers”// Swap container contentsListTransaction swap = ItemContainerUtil.swap( containerA, containerB);Sorting
Section titled “Sorting”// Sort containercontainer.sort(SortType.ALPHABETICAL);
// Sort typesSortType.ALPHABETICAL // By item nameSortType.STACK_SIZE // By quantitySortType.MATERIAL // By material typePlayer Inventory
Section titled “Player Inventory”Player inventory has special sections:
// Access from playerInventory inventory = player.getInventory();
// Inventory sectionsItemContainer hotbar = inventory.getHotbar();ItemContainer main = inventory.getMain();ItemContainer armor = inventory.getArmor();ItemContainer offhand = inventory.getOffhand();
// Held itemItemStack heldItem = inventory.getHeldItem();short activeSlot = inventory.getActiveSlot();Item Context
Section titled “Item Context”ItemContext provides context for item operations:
// Create contextItemContext context = new ItemContext( playerRef, inventory, activeSlot);
// Get context infoPlayerRef player = context.getPlayerRef();ItemContainer container = context.getContainer();short slot = context.getSlot();ItemStack item = context.getItemStack();Serialization
Section titled “Serialization”ItemStack supports serialization:
// Codec for persistenceBuilderCodec<ItemStack> codec = ItemStack.CODEC;
// Convert to packet for networkItemWithAllMetadata packet = itemStack.toPacket();
// Container to protocolMap<Integer, ItemWithAllMetadata> protocolMap = container.toProtocolMap();InventorySection section = container.toPacket();Best Practices
Section titled “Best Practices”- Use transactions: Check transaction results to handle partial operations
- Respect filters: Pass
filter=trueunless you need to bypass restrictions - Validate slots: Check slot bounds before operations
- Handle empty stacks: Always check for
isEmpty()or useItemStack.isEmpty() - Clone when needed: Clone stacks when storing references outside transactions
Related
Section titled “Related”- Item Assets - Item type definitions
- Entity System - Player inventory access
- Events - Container events