Mod Server API: Difference between revisions

From Eco - English Wiki
[unchecked revision][unchecked revision]
Salbris (talk | contribs)
No edit summary
Salbris (talk | contribs)
No edit summary
Line 17: Line 17:
  user.Inventory.Clothing
  user.Inventory.Clothing
  user.Inventory.Toolbar
  user.Inventory.Toolbar
The items in these inventories can be accessed in a couple different ways as discussed in the Inventory Section. Each of these is an instance of [https://docs.play.eco/api/server/eco.gameplay/Eco.Gameplay.Items.Inventory.html Inventory].
The items in these inventories can be accessed in a couple different ways as discussed in the [[Mod Server API#Inventory|Inventory Section]]. Each of these is an instance of [https://docs.play.eco/api/server/eco.gameplay/Eco.Gameplay.Items.Inventory.html Inventory].
[[File:ActionBarScreenshot.png|thumb|user.Inventory.ActionBar]]
[[File:ActionBarScreenshot.png|thumb|user.Inventory.ActionBar]]
<code>user.Inventory.ActionBar</code> represents all the standard buttons used to open the backpack, economy viewer, etc. located next to the minimap.
<code>user.Inventory.ActionBar</code> represents all the standard buttons used to open the backpack, economy viewer, etc. located next to the minimap.
Line 28: Line 28:
[[File:ToolbarScreenshot.png|thumb|user.Inventory.Toolbar]]
[[File:ToolbarScreenshot.png|thumb|user.Inventory.Toolbar]]
<code>user.Inventory.Toolbar</code> represents the items on the player's toolbar where they put tools and other items they want quick access to.
<code>user.Inventory.Toolbar</code> represents the items on the player's toolbar where they put tools and other items they want quick access to.




== Inventory ==
== Inventory ==
An [https://docs.play.eco/api/server/eco.gameplay/Eco.Gameplay.Items.Inventory.html Inventory] is a collection of item stacks and is used to represent the items inside various things in Eco including the player's backpack, vehicles, storage chests, stockpiles, and even fuel slots. Each instance of Inventory has a common interface for getting access to the items and stacks inside it. Inventories are also a hierarchy of inventories. For example, the player has a root level inventory <code>user.Inventory</code> which represents all the inventories of the player's character (their backpack, toolbar and clothing). To access child inventories you can use <code>inventory.AllInventories</code> or just by accessing the same stacks <code>IEnumerable</code> properties mentioned below.
An [https://docs.play.eco/api/server/eco.gameplay/Eco.Gameplay.Items.Inventory.html Inventory] is a collection of item stacks and is used to represent the items inside various things in Eco including the player's backpack, vehicles, storage chests, stockpiles, and even fuel slots. Each instance of Inventory has a common interface for getting access to the items and stacks inside it. Inventories are also a hierarchy of inventories. For example, the player has a root level inventory <code>user.Inventory</code> which represents all the inventories of the player's character (their backpack, toolbar and clothing). To access child inventories you can use <code>inventory.AllInventories</code> or just by accessing the same stacks <code>IEnumerable</code> properties mentioned below.
=== Items/Stacks ===
=== Items/Stacks ===
Each slot of the inventory is represented by the [https://docs.play.eco/api/server/eco.gameplay/Eco.Gameplay.Items.ItemStack.html ItemStack] class whether it is empty or not. You can access the individual items through the inventory's stacks. Use <code>inventory.Stacks</code> to access all the stacks or <code>inventory.NonEmptyStacks</code> to access just the slots with any items in them. Both of these are <code>IEnumberable<ItemStack></code> so they can be used with [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/ Linq] or iterated through in a for-each loop:
Each slot of the inventory is represented by the [https://docs.play.eco/api/server/eco.gameplay/Eco.Gameplay.Items.ItemStack.html ItemStack] class whether it is empty or not. You can access the individual items through the inventory's stacks. Use <code>inventory.Stacks</code> to access all the stacks or <code>inventory.NonEmptyStacks</code> to access just the slots with any items in them. Both of these are <code>IEnumberable<ItemStack></code> so they can be used with [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/ Linq] or iterated through in a for-each loop:

Revision as of 19:50, 12 February 2022

Introduction

This page provides some tips for how to interact with the Server API when you create mod that needs to interact with the game's runtime state or behaviour. A full comprehensive list of the entire API can be found at https://docs.play.eco/api/server/index.html.

To make full use of these docs you will want to have a basic understanding of the C# language and related concepts.

Player

The User and Player class contain references to information about a specific player. The User class contains references to the player's owned objects, inventories, their stats, professions, etc. While the Player class has references to their location, technical server properties, player name, etc. If you have a User and need to get a Player instance it's as simple as:

user.Player

Or vice-versa:

player.User

Accessing the Player's Inventory

All of a player's inventory is referenced by the UserInventory class which is accessed by the Inventory property:

user.Inventory

The player's backpack, toolbar and clothing can be accessed through this object like this:

user.Inventory.ActionBar
user.Inventory.Backpack
user.Inventory.Carried
user.Inventory.Clothing
user.Inventory.Toolbar

The items in these inventories can be accessed in a couple different ways as discussed in the Inventory Section. Each of these is an instance of Inventory.

File:ActionBarScreenshot.png
user.Inventory.ActionBar

user.Inventory.ActionBar represents all the standard buttons used to open the backpack, economy viewer, etc. located next to the minimap.

user.Inventory.Backpack represents all the stacks of items inside the player's backpack but not their clothes or items on their hotbar.

user.Inventory.Carried represents the stack of items the player can carry around such as dirt, stone, or logs.

user.Inventory.Clothing represents the items of clothing the player is wearing.

File:ToolbarScreenshot.png
user.Inventory.Toolbar

user.Inventory.Toolbar represents the items on the player's toolbar where they put tools and other items they want quick access to.


Inventory

An Inventory is a collection of item stacks and is used to represent the items inside various things in Eco including the player's backpack, vehicles, storage chests, stockpiles, and even fuel slots. Each instance of Inventory has a common interface for getting access to the items and stacks inside it. Inventories are also a hierarchy of inventories. For example, the player has a root level inventory user.Inventory which represents all the inventories of the player's character (their backpack, toolbar and clothing). To access child inventories you can use inventory.AllInventories or just by accessing the same stacks IEnumerable properties mentioned below.

Items/Stacks

Each slot of the inventory is represented by the ItemStack class whether it is empty or not. You can access the individual items through the inventory's stacks. Use inventory.Stacks to access all the stacks or inventory.NonEmptyStacks to access just the slots with any items in them. Both of these are IEnumberable<ItemStack> so they can be used with Linq or iterated through in a for-each loop:

foreach (ItemStack stack in backpack.NonEmptyStacks)
{
   ...
}
IEnumerable<ItemStack> stacks = user.Inventory.AllInventories.SelectMany(i => i.Stacks);

To access the item in the stack use stack.Item and use stack.Quantity to get the amount of items in that stack.