Modding:Basic Inventory Handling: Difference between revisions

From Vintage Story Wiki
(Created page with "{{GameVersion112}} == Overview == The most important classes for inventory management are: * InventoryManager: Contains multiple inventories (every player has one with diffe...")
 
No edit summary
Line 32: Line 32:
<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")));
var offhandSlot = player.Entity.LeftHandItemSlot;
ItemSlot offhandSlot = player.Entity.LeftHandItemSlot;
if (offhandSlot?.Empty == true)
if (offhandSlot?.Empty == true)
{
{
Line 43: Line 43:


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