Modding:Basic Inventory Handling: Difference between revisions

From Vintage Story Wiki
m
Updated to 1.19.3. Fixed torch ID being incorrect.
No edit summary
m (Updated to 1.19.3. Fixed torch ID being incorrect.)
Line 1: Line 1:
{{GameVersion|1.15}}
{{GameVersion|1.19.3}}
<languages/>
<languages/>
<translate>
<translate>
Line 12: Line 12:


<!--T:3-->
<!--T:3-->
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 <code>TryGiveItemStack</code>:
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 joining you could use <code>TryGiveItemStack</code>:
</translate>
</translate>
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
Line 20: Line 20:
   {
   {
     serverApi = api;
     serverApi = api;
     serverApi.Event.PlayerRespawn += EventOnPlayerRespawn;
     serverApi.Event.PlayerJoin += EventOnPlayerJoin;
   }
   }


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


<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-basic-lit-up")));
ItemSlot offhandSlot = player.Entity.LeftHandItemSlot;
ItemSlot offhandSlot = player.Entity.LeftHandItemSlot;
if (offhandSlot?.Empty == true)
if (offhandSlot?.Empty == true)
Line 49: Line 49:


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
Block torchBlock = _serverApi.World.GetBlock(new AssetLocation("torch-up"));
Block torchBlock = serverApi.World.GetBlock(new AssetLocation("torch-basic-lit-up"));
IInventory backpack = player.InventoryManager.GetOwnInventory(GlobalConstants.backpackInvClassName);
IInventory backpack = player.InventoryManager.GetOwnInventory(GlobalConstants.backpackInvClassName);
if (backpack != null)
if (backpack != null)
Confirmedusers
637

edits