|
| 1 | +--- |
| 2 | +title: "Inventory Management" |
| 3 | +description: "Learn how to manage player inventories in your Hytale mod." |
| 4 | +authors: |
| 5 | + - name: "Neil Revin" |
| 6 | + link: "https://itsneil.dev" |
| 7 | +--- |
| 8 | + |
| 9 | +In this guide, you'll learn how to manage player inventories in your Hytale mod. |
| 10 | + |
| 11 | +## Accessing the Player Inventory |
| 12 | + |
| 13 | +To access the Inventory of a `Player`, you can use the `getInventory()` method which returns the `Inventory` object. |
| 14 | + |
| 15 | +```java |
| 16 | +Inventory inventory = player.getInventory(); |
| 17 | +``` |
| 18 | + |
| 19 | +## Opening Inventories |
| 20 | + |
| 21 | +You can open different types of inventories, these inventories are known as "Pages". You can also add custom pages, this will be covered in a guide later on. |
| 22 | + |
| 23 | +You can use the `Page` enum to reference existing inventories, currently the following pages are available: |
| 24 | + |
| 25 | +- `Page.None` |
| 26 | +- `Page.Bench` |
| 27 | +- `Page.Inventory` |
| 28 | +- `Page.ToolsSettings` |
| 29 | +- `Page.Map` |
| 30 | +- `Page.MachinimaEditor` |
| 31 | +- `Page.ContentCreation` |
| 32 | +- `Page.Custom` |
| 33 | + |
| 34 | +```java |
| 35 | +PageManager pageManager = player.getPageManager(); |
| 36 | +Store<EntityStore> store = player.getWorld().getEntityStore().getStore(); |
| 37 | +pageManager.setPage(player.getReference(), store, Page.Inventory); |
| 38 | +``` |
| 39 | + |
| 40 | + |
| 41 | +## `ItemStack` Class |
| 42 | + |
| 43 | +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. |
| 44 | + |
| 45 | +### Creating an ItemStack |
| 46 | + |
| 47 | +To create an `ItemStack`, you need to specify the material type and the quantity of items in the stack. |
| 48 | + |
| 49 | +```java |
| 50 | +ItemStack item = new ItemStack("Stone"); |
| 51 | +ItemStack withQuantity = new ItemStack("Stone", 64); |
| 52 | +``` |
| 53 | + |
| 54 | +#### Adding custom metadata |
| 55 | + |
| 56 | +You can also add custom metadata to a `ItemStack` by passing a `BsonDocument` when creating it. |
| 57 | + |
| 58 | +```java |
| 59 | +BsonDocument metadata = new BsonDocument(); |
| 60 | +metadata.append("customData", new BsonString("value")); |
| 61 | +ItemStack item = new ItemStack("Stone", 64, metadata); |
| 62 | +``` |
| 63 | + |
| 64 | +### Setting a durability |
| 65 | + |
| 66 | +```java |
| 67 | +ItemStack stackWithDurability = new ItemStack( |
| 68 | + "DiamondSword", // itemId |
| 69 | + 1, // quantity |
| 70 | + 100.0, // durability |
| 71 | + 100.0, // maxDurability |
| 72 | + metadata // metadata (optional) |
| 73 | +); |
| 74 | +``` |
| 75 | + |
| 76 | +## Getting the `ItemContainer` |
| 77 | + |
| 78 | +The `Inventory` class provides methods to get multiple `ItemContainer` objects, like: |
| 79 | + |
| 80 | +- `.getStorage()` |
| 81 | +- `.getArmor()` |
| 82 | +- `.getBackpack()` |
| 83 | +- `.getHotbar()` |
| 84 | +- `.getUtility()` |
| 85 | + |
| 86 | +There are also combined methods like: |
| 87 | + |
| 88 | +- `.getCombinedEverything()` |
| 89 | +- `.getCombinedArmorHotbarStorage()` |
| 90 | +- `.getCombinedBackpackStorageHotbar()` |
| 91 | +- `.getCombinedHotbarFirst()` |
| 92 | +- `.getCombinedStorageFirst()` |
| 93 | +- `.getCombinedArmorHotbarUtilityStorage()` |
| 94 | +- `.getCombinedHotbarUtilityConsumableStorage()` |
| 95 | + |
| 96 | +## Adding `ItemStack` objects to the Inventory |
| 97 | + |
| 98 | +To add an `ItemStack` to a player's inventory, you can use the `addItemStack()` method of the `Inventory` class. |
| 99 | + |
| 100 | +```java |
| 101 | +Inventory inventory = player.getInventory(); |
| 102 | +inventory.addItemStack(item); |
| 103 | +``` |
| 104 | + |
| 105 | +Or you can specify a certain slot to add it to: |
| 106 | + |
| 107 | +```java |
| 108 | +inventory.addItemStackToSlot((short) 4, stack) |
| 109 | +``` |
| 110 | + |
| 111 | +## Removing `ItemStack` objects from the Inventory |
| 112 | + |
| 113 | +To remove an `ItemStack` from a player's inventory, you can use the `removeItemStack()` method of the `Inventory` class. |
| 114 | + |
| 115 | +```java |
| 116 | +Inventory inventory = player.getInventory(); |
| 117 | +inventory.removeItemStack(item); |
| 118 | +``` |
| 119 | + |
| 120 | +Or you can specify a certain slot to remove it from: |
| 121 | + |
| 122 | +```java |
| 123 | +inventory.removeItemStackFromSlot((short) 4); |
| 124 | +``` |
0 commit comments