HytaleModding
HytaleModding
Server Plugins

Inventory Management

Learn how to manage player inventories in your Hytale mod.

In this guide, you'll learn how to manage player inventories in your Hytale mod.

Accessing the Player Inventory

To access the Inventory of a Player, you can get the InventoryComponent. Before we can do that, you should know all the individual Component Types that come under this.

InventoryComponent has the following component types:

  • InventoryComponent.Backpack
  • InventoryComponent.Tool
  • InventoryComponent.Utility
  • InventoryComponent.Armor
  • InventoryComponent.Storage
  • InventoryComponent.Hotbar

Each of these have their own getComponentType() method that you can use to get the component type for that specific inventory. InventoryComponent does not have a getComponentType() method itself.

To get the inventory, you can do the following:

InventoryComponent armorComponent = store.getComponent(entityRef, InventoryComponent.Armor.getComponentType());
ItemContainer container = armorComponent.getInventory()

ItemStack Class

You can create and manipulate items in a player's inventory using the ItemStack class. This class represents a stack of items, and provides methods for managing the quantity and type of items in the stack.

Creating an ItemStack

To create an ItemStack, you need to specify the material type and the quantity of items in the stack.

ItemStack item = new ItemStack("Stone");
ItemStack withQuantity = new ItemStack("Stone", 64);

Adding custom metadata

You can also add custom metadata to a ItemStack by passing a BsonDocument when creating it.

BsonDocument metadata = new BsonDocument();
metadata.append("customData", new BsonString("value"));
ItemStack item = new ItemStack("Stone", 64, metadata);

Setting a durability

ItemStack stackWithDurability = new ItemStack(
    "DiamondSword", // itemId
    1,              // quantity
    100.0,          // durability
    100.0,          // maxDurability
    metadata        // metadata (optional)
);

Adding ItemStack objects to the Inventory

To add an ItemStack to a player's inventory, you can use the addItemStack() method of the ItemContainer class.

ItemContainer storageContainer = inventoryComponent.getInventory()
storageContainer.addItemStack(item);

Or you can specify a certain slot to add it to:

storageContainer.addItemStackToSlot((short) 4, stack)

Removing ItemStack objects from the Inventory

To remove an ItemStack from a player's inventory, you can use the removeItemStack() method of the ItemContainer class.

ItemContainer storageContainer = inventoryComponent.getInventory()
storageContainer.removeItemStack(item);

Or you can specify a certain slot to remove it from:

storageContainer.removeItemStackFromSlot((short) 4);