Modding:Basic Inventory Handling

From Vintage Story Wiki
Revision as of 10:00, 13 April 2020 by P3t3rix (talk | contribs)

Template:GameVersion112

Overview

The most important classes for inventory management are:

  • InventoryManager: Contains multiple inventories (every player has one with different inventories). Also helps with common tasks like putting items into a valid inventory.
  • IInventory: Represents an item inventory (for example the players hotbar). Contains multiple ItemSlots.
  • ItemSlot: Represents a specific slot (for example the offhand slot) of an inventory. Contains the ItemStack of the slot and Methods to help transfer items to and from that slot.
  • ItemStack: Contains the Item and amount of items (called StackSize), e.g. "block torch-up" and StackSize of 2, for two Torches.

Basic Inventory Handling

To get access to a players inventory, you can use the InventoryManager of the IPlayer. For example, to simply give the player a torch when respawning you could use TryGiveItemStack:

private ICoreServerAPI _serverApi;

public override void StartServerSide(ICoreServerAPI api)
  {
    _serverApi = api;
    _serverApi.Event.PlayerRespawn += EventOnPlayerRespawn;
  }

private void EventOnPlayerRespawn(IServerPlayer player)
  {
    ItemStack torch = new ItemStack(_serverApi.World.GetBlock(new AssetLocation("torch-up")));
    player.InventoryManager.TryGiveItemstack(torch);
  }

If we want to put it directly into the offhand we can use player.Entity.LeftHandItemSlot:

ItemStack torch = new ItemStack(_serverApi.World.GetBlock(new AssetLocation("torch-up")));
ItemSlot offhandSlot = player.Entity.LeftHandItemSlot;
if (offhandSlot?.Empty == true)
{
  offhandSlot.Itemstack = torch;
  offhandSlot.MarkDirty(); //this is needed because otherwise the client does not get the update
}

If you need access to other inventories like e.g. the backpack inventories, you can use the InventoryManager.Inventories Property. This contains a dictionary with the name of the inventory (for example "hotbar-APlayerUID") and the corresponding inventory. Remember that the backpack inventory contains the bags as well as the slots, so you have to check if it is valid to put the item there. If for example you want to fill every empty slot in the backpack with single torches you could do the following:

Block torchBlock = _serverApi.World.GetBlock(new AssetLocation("torch-up"));
IInventory backpack = player.InventoryManager.Inventories.FirstOrDefault(i => i.Key.StartsWith("backpack")).Value;
if (backpack != null)
{
  ItemSlot dummySlot = new ItemSlot(null) { Itemstack = new ItemStack(torchBlock) }; //dummy slot to check if the slot would be a valid one
  foreach (ItemSlot bag in backpack.Where(b => b.CanHold(dummySlot) && b.Empty))
  {
    bag.Itemstack = new ItemStack(torchBlock);
    bag.MarkDirty();
  }
}