Editing Mod Server API

From Eco - English Wiki

Your changes will be displayed to readers once an authorized user accepts them. (help)

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
== Introduction ==
== 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.
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 ==
== Player ==
The <code>User</code> and <code>Player</code> class contain references to information about a specific player. The <code>User</code> class contains references to the player's owned objects, inventories, their stats, professions, etc. While the <code>Player</code> class has references to their location, technical server properties, player name, etc. If you have a <code>User</code> and need to get a <code>Player</code> instance it's as simple as:
The <code>User</code> and <code>Player</code> class contain references to information about a specific player. The <code>User</code> class contains references to the player's owned objects, inventories, their stats, professions, etc. While the <code>Player</code> class has references to their location, technical server properties, player name, etc. If you have a <code>User</code> and need to get a <code>Player</code> instance it's as simple as:
Line 8: Line 6:
Or vice-versa:
Or vice-versa:
  player.User
  player.User
=== Accessing the Player's Inventory ===
=== Accessing the Player's Inventory ===
All of a player's inventory is referenced by the <code>UserInventory</code> class which is accessed by the Inventory property:
All of a player's inventory is referenced by the <code>UserInventory</code> class which is accessed by the Inventory property:
  user.Inventory
  user.Inventory
The player's backpack, toolbar and clothing can be accessed through this object like this:
The player's backpack, action bar, carried items and clothing can be accessed through this object:
user.Inventory.Backpack
  user.Inventory.ActionBar
  user.Inventory.ActionBar
user.Inventory.Backpack
  user.Inventory.Carried
  user.Inventory.Carried
  user.Inventory.Clothing
  user.Inventory.Clothing
user.Inventory.Toolbar
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].
<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.Backpack</code> represents all the stacks of items inside the player's backpack but not their clothes or items on their hotbar.
<code>user.Inventory.Carried</code> represents the stack of items the player can carry around such as dirt, stone, or logs.
<code>user.Inventory.Clothing</code> represents the items of clothing the player is wearing.
<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.
=== World Objects - Crafting Tables, Storage, and Vehicles ===
Crafting tables, vehicles, stockpiles, and other placed structures are represented by the <code>WorldObject</code> class. The walls, roof, roads, etc are not considered world objects. To list all the world objects owned by the player use the following static method:
WorldObject.GetOwnedBy(user) //IEnumerable<WorldObject>
Each <code>WorldObject</code> contains components that represent their features. For example, the <code>StorageComponent</code> represents the ability for the object to store items as stacks.
==== Crafting Component ====
The crafting component represents a crafting tables work orders, recipes, etc.
== 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.
=== 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:
foreach (ItemStack stack in backpack.NonEmptyStacks)
{
    ...
}
IEnumerable<ItemStack> stacks = user.Inventory.AllInventories.SelectMany(i => i.Stacks);
To access the item in the stack use <code>stack.Item</code> and use <code>stack.Quantity</code> to get the amount of items in that stack.
[[Category:Modding]]
Please note that all contributions to Eco - English Wiki are considered to be released under the CC BY-NC-SA 4.0 (see Eco:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following hCaptcha:

Cancel Editing help (opens in new window)