Mod Server API: Difference between revisions

From Eco - English Wiki
[unchecked revision][checked revision]
Salbris (talk | contribs)
No edit summary
No edit summary
Tag: 2017 source edit
 
(One intermediate revision by one other user not shown)
Line 18: Line 18:
  user.Inventory.Toolbar
  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].
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]]
 
<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 26: Line 26:


<code>user.Inventory.Clothing</code> represents the items of clothing the player is wearing.
<code>user.Inventory.Clothing</code> represents the items of clothing the player is wearing.
[[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.


=== 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 ==
== Inventory ==

Latest revision as of 06:38, 28 April 2024

Introduction[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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.

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.

user.Inventory.Toolbar 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[edit | edit source]

Crafting tables, vehicles, stockpiles, and other placed structures are represented by the WorldObject 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 WorldObject contains components that represent their features. For example, the StorageComponent represents the ability for the object to store items as stacks.

Crafting Component[edit | edit source]

The crafting component represents a crafting tables work orders, recipes, etc.

Inventory[edit | edit source]

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[edit | edit source]

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.