Hytale Modding
Modding d'Hytale
Server Plugins

Gestion de l'inventaire

Apprenez à gérer les inventaires des joueurs dans votre mod Hytale.

Dans ce guide, vous apprendrez à gérer les inventaires des joueurs dans votre mod Hytale.

Accéder à l'inventaire du joueur

Vous pouvez accéder à l'inventaire d'un joueur de type Player avec la méthode getInventory() qui donnent un objet de type Inventory.

Ouverture des inventaires

Vous pouvez ouvrir différents types d'inventaires, ces inventaires sont appelés "Pages". Vous pouvez également ajouter des pages personnalisées, qui seront couvertes dans un guide plus tard.

Vous pouvez utiliser l'énumération Page pour référencer les inventaires existants, actuellement les pages suivantes sont disponibles:

Classe ItemStack

Vous pouvez créer et manipuler des objets dans l'inventaire d'un joueur en utilisant la classe ItemStack. Cette classe représente une pile d'objets, et fournit des méthodes pour gérer la quantité et le type des objets dans la pile.

Création d'un ItemStack

Pour créer un ItemStack, vous devez spécifier le type de matériau et la quantité d'éléments dans la pile.

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

Ajout de métadonnées personnalisées

Vous pouvez également ajouter des métadonnées personnalisées à un ItemStack en passant un BsonDocument lors de sa création.

Définir une durabilité

Obtention du ItemContainer

La classe Inventory fournit des méthodes pour obtenir plusieurs objets ItemContainer, comme:

Il existe également des méthodes combinées comme:

Ajout d'objets ItemStack à l'Inventaire

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

Inventory inventory = player.getInventory();
inventory.addItemStack(item);

Ou vous pouvez spécifier un certain emplacement à ajouter à :

inventory.addItemStackToSlot((short) 4, stack)

Suppression d'objets ItemStack de l'inventaire

Pour supprimer un ItemStack de l'inventaire d'un joueur, vous pouvez utiliser la méthode removeItemStack() de la classe Inventory.

Inventory inventory = player.getInventory();
inventory.removeItemStack(item);

Ou vous pouvez spécifier une case spécifique depuis laquelle faire la supression:

inventory.removeItemStackFromSlot((short) 4);

Custom Inventories (Item Containers)

Adding an inventory to an item

You can add a custom inventory by adding the following to the items json

"Container": {
    "Capacity": 9
},

And adding the following interaction allows opening the inventory UI Page by using a pre defined UI page. It opens the UI like how it would for a backpackpack. This allows the player to see their inventory along with the items.

"Interactions": {
    "Use": {
      "Interactions": [
        {
          "Type": "OpenItemStackContainer"
        }
      ]
    }
},

Plugin Interaction of Container Items

The items stored in an inventory are saved in the item BSON. Because of this they can not be access data through ECS when wanting to edit items. You will instead need to load the data from the BSON to interact with it and save it back to the BSON.

// Get held item that we know has a container field & its position in invenotory (Assume item its held for example)
ItemStack heldItemStack = player.getInventory().getActiveHotbarItem();
final short ACTIVE_HOTBAR_SLOT = player.getInventory().getActiveHotbarSlot();

// Get the container BSON of the item
BsonDocument containerBSON = heldItemStack.getFromMetadataOrNull(ItemStackItemContainer.CONTAINER_CODEC);

// Get the array of items in the container BSON
ItemStack[] containerItems = ItemStackItemContainer.ITEMS_CODEC.getOrNull(containerBSON, new ExtraInfo());

You can then modify the list of items however you want but this will not modify the item directly as item stacks on the player are immutable. To update the container the player is holding you will need to edit the item locally, then replace the held item with the newly made local copy. The following shows how to do that.

// For this example we are just removing the first item in the locally saved container (Continued from above)
containerItems[0] = null;

// Update the container fields in the BSON doc to have the removed item
ItemStackItemContainer.ITEMS_CODEC.put(containerBSON, containerItems, new ExtraInfo());

// Create a new version of the item
ItemStack updatedItem = heldItemStack.withMetadata(ItemStackItemContainer.CONTAINER_CODEC, containerBSON);

// Replace the equiped item in the players inventory with the new item as its not editable!
// - remove original item
player.getInventory().getHotbar().removeItemStackFromSlot(ACTIVE_HOTBAR_SLOT);
// - add new item
player.getInventory().getHotbar().setItemStackForSlot(ACTIVE_HOTBAR_SLOT, updatedItem);

Getting Components from Container Items

You can use a similar method to get any component from an item in a container

// Get the component data
CustomComponent customComponent = itemStack.getFromMetadataOrDefault(CustomComponent.CUSTOM_COMPONENT_ID, CustomComponent.CODEC);

// Modify component data here
customComponent.setSomeValue();

// Set the component data by creating new item with updated info
ItemStack newItem = itemStack.withMetadata(
        CustomComponent.CUSTOM_COMPONENT_ID,
        CustomComponent.CODEC,
        customComponent
    );

// Add updated item to the player
player.getInventory().getHotbar().setItemStackForSlot(0, newItem);