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"))); | ||
ItemSlot offhandSlot = player.Entity.LeftHandItemSlot; | |||
if (offhandSlot?.Empty == true) | if (offhandSlot?.Empty == true) | ||
{ | { | ||
Line 43: | Line 43: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Block torchBlock = _serverApi.World.GetBlock(new AssetLocation("torch-up")); | |||
IInventory backpack = player.InventoryManager.Inventories.FirstOrDefault(i => i.Key.StartsWith("backpack")).Value; | |||
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 | |||
foreach ( | foreach (ItemSlot bag in backpack.Where(b => b.CanHold(dummySlot) && b.Empty)) | ||
{ | { | ||
bag.Itemstack = new ItemStack(torchBlock); | bag.Itemstack = new ItemStack(torchBlock); |