Modding:Basic Inventory Handling: Difference between revisions

From Vintage Story Wiki
no edit summary
No edit summary
No edit summary
Line 13: Line 13:


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
private ICoreServerAPI _serverApi;
private ICoreServerAPI serverApi;


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


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


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
ItemStack torch = new ItemStack(_serverApi.World.GetBlock(new AssetLocation("torch-up")));
ItemStack torch = new ItemStack(serverApi.World.GetBlock(new AssetLocation("torch-up")));
ItemSlot offhandSlot = player.Entity.LeftHandItemSlot;
ItemSlot offhandSlot = player.Entity.LeftHandItemSlot;
if (offhandSlot?.Empty == true)
if (offhandSlot?.Empty == true)
Line 40: Line 40:
</syntaxhighlight>
</syntaxhighlight>


If you need access to other inventories like e.g. the backpack inventories, you can use the <code>InventoryManager.Inventories</code> 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:
If you need access to other inventories like e.g. the backpack inventories, you can use the <code>InventoryManager.Inventories</code> Property. This contains a dictionary with the name of the inventory (for example "hotbar-APlayerUID") and the corresponding inventory. Another cleaner looking way if you only need a single inventory would be the <code>InventoryManager.GetOwnInventory()</code> method. Also note that most of the string names are already in <code>GlobalConstants</code>.
A thing to remember is 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, you can use the <code>ItemSlot.CanHold</code> method for example.  
 
If for example you want to fill every empty slot in the backpack with single torches you could do the following:


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

edits