Modding:Basic Inventory Handling: Difference between revisions
No edit summary |
m (Updated navbox to new code navbox.) |
||
(11 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{ | {{GameVersion|1.19.3}} | ||
<languages/> | |||
== Overview == | <translate> | ||
== Overview == <!--T:1--> | |||
The most important classes for inventory management are: | 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. | * InventoryManager: Contains multiple inventories (every player has one with different inventories). Also helps with common tasks like putting items into a valid inventory. | ||
Line 8: | Line 9: | ||
* ItemStack: Contains the Item and amount of items (called StackSize), e.g. "block torch-up" and StackSize of 2, for two Torches. | * 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 == | == Basic Inventory Handling == <!--T:2--> | ||
<!--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 joining you could use <code>TryGiveItemStack</code>: | |||
</translate> | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
private ICoreServerAPI | private ICoreServerAPI serverApi; | ||
public override void StartServerSide(ICoreServerAPI api) | public override void StartServerSide(ICoreServerAPI api) | ||
{ | { | ||
serverApi = api; | |||
serverApi.Event.PlayerJoin += EventOnPlayerJoin; | |||
} | } | ||
private void | private void EventOnPlayerJoin(IServerPlayer player) | ||
{ | { | ||
ItemStack torch = new ItemStack( | ItemStack torch = new ItemStack(serverApi.World.GetBlock(new AssetLocation("torch-basic-lit-up"))); | ||
player.InventoryManager.TryGiveItemstack(torch); | player.InventoryManager.TryGiveItemstack(torch); | ||
} | } | ||
Line 31: | Line 33: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
ItemStack torch = new ItemStack( | 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 40: | Line 42: | ||
</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. | 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, which appends the player uid to the classname and returns the inventory. To avoid errors when typing the classnames, check first if the classname is already in the <code>GlobalConstants</code>. | ||
Because backpack inventory contains the bags as well as the slots, you have to check if it is valid to put the item there. For this you can use the <code>ItemSlot.CanHold()</code> method. | |||
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 = | Block torchBlock = serverApi.World.GetBlock(new AssetLocation("torch-basic-lit-up")); | ||
IInventory backpack = player.InventoryManager. | IInventory backpack = player.InventoryManager.GetOwnInventory(GlobalConstants.backpackInvClassName); | ||
if (backpack != null) | if (backpack != null) | ||
{ | { | ||
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)) | ||
{ | { | ||
Line 55: | Line 61: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{Navbox/codemodding}} |
Latest revision as of 17:03, 27 March 2024
This page was last verified for Vintage Story version 1.19.3.
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 joining you could use TryGiveItemStack
:
private ICoreServerAPI serverApi;
public override void StartServerSide(ICoreServerAPI api)
{
serverApi = api;
serverApi.Event.PlayerJoin += EventOnPlayerJoin;
}
private void EventOnPlayerJoin(IServerPlayer player)
{
ItemStack torch = new ItemStack(serverApi.World.GetBlock(new AssetLocation("torch-basic-lit-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-basic-lit-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. Another cleaner looking way if you only need a single inventory would be the InventoryManager.GetOwnInventory()
method, which appends the player uid to the classname and returns the inventory. To avoid errors when typing the classnames, check first if the classname is already in the GlobalConstants
.
Because backpack inventory contains the bags as well as the slots, you have to check if it is valid to put the item there. For this you can use the ItemSlot.CanHold()
method.
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-basic-lit-up"));
IInventory backpack = player.InventoryManager.GetOwnInventory(GlobalConstants.backpackInvClassName);
if (backpack != null)
{
var dummySlot = new ItemSlot(null) { Itemstack = new ItemStack(torchBlock) };
foreach (ItemSlot bag in backpack.Where(b => b.CanHold(dummySlot) && b.Empty))
{
bag.Itemstack = new ItemStack(torchBlock);
bag.MarkDirty();
}
}
Code Modding | |||||||
---|---|---|---|---|---|---|---|
Basics | Code Mods • Preparing For Code Mods • Creating A Code Mod | ||||||
Tutorials |
|
||||||
Advanced | Server-Client Considerations • Setting up your Development Environment • Advanced Blocks • Advanced Items • Block and Item Interactions • Block Behavior • Block Entity • Particle Effects • World Access • Inventory Handling • Commands • GUIs • Network API • Monkey patching (Harmony) | ||||||
Data Management | Savegame Data Storage • ModConfig File • Chunk Data Storage • TreeAttribute | ||||||
Worldgen | WorldGen API • NatFloat • EvolvingNatFloat | ||||||
Rendering | Shaders and Renderers |
Wondering where some links have gone?
The modding navbox is going through some changes! Check out Navigation Box Updates for more info and help finding specific pages.
Modding | |
---|---|
Modding Introduction | Getting Started • Theme Pack |
Content Modding | Content Mods • Developing a Content Mod • Basic Tutorials • Intermediate Tutorials • Advanced Tutorials • Content Mod Concepts |
Code Modding | Code Mods • Setting up your Development Environment |
Property Overview | Item • Entity • Block • Block Behaviors • Block Classes • Block Entities • Block Entity Behaviors • World properties |
Workflows & Infrastructure | Modding Efficiency Tips • Mod-engine compatibility • Mod Extensibility • VS Engine |
Additional Resources | Community Resources • Modding API Updates • Programming Languages • List of server commands • List of client commands • Client startup parameters • Server startup parameters Example Mods • API Docs • GitHub Repository |