Reacting to player events
Template:ModdingOutdated > Eco Version: 7.3.1
> Difficulty: Easy
Introduction[edit | edit source]
Hi, my name is Dirk and I have been trying to get into Eco modding by trial, error and DLL disassembly. Instead of keeping what I've learned to myself I decided to document my findings here so that the next person to come along can save some time. I will mark to which version of Eco this document applies to and keep it up to date. I realize that this document might contain some items not directly related to the title. In the future, it would make sense to me to split things off into their own document but for now, I will keep to this page. Please feel free to ask me to clarify anything that isn't clear in this write-up. You can hit me up on Discord as `Dirk#5346`.
Getting started[edit | edit source]
To start modding you will need:
- An Editor capable of handling c#
- The .NET SDK. The latest 4.x version is fine.
- The Eco DLL's
Now create a project (VS solution or msbuild based) for a library and create your root cs file. Let's set it up so that it will be recognized as a real plugin by Eco:
namespace MyModName { using System; using Eco.Core.Plugins.Interfaces; using Eco.Core.Utils; using Eco.Gameplay.Systems.Chat; public class MyModName : IModKitPlugin, IInitializablePlugin { public string GetStatus() { return String.Empty; } public void Initialize(TimedTask timer) { ChatManager.ServerMessageToAllLoc("My mod is ready to go!", false); } } }
The `IModKitPlugin` is a marker interface that allows Eco to pick this class up as a mod. `IInitializablePlugin` will require you to implement `GetStatus` and `Initialize`. Looking at examples it seems that `GetStatus` returning an empty string indicates it is ok. In the `Initialize` mod I am using `ChatManager.ServerMessageToAllLoc`. This is an invaluable method that will allow you to send yourself (and everyone else on the server) debugging notifications. Do make sure that you use the `false` options though otherwise your messages get persisted and you could slow down or mess up your world database if you flood it with messages.
Now go to your Eco root folder and navigate to `Eco_Data\Server\Mods`. To keep things tidy create a folder here named `MyModName`. Build your project as a library DLL and place it here. By using a DLL you will speed things up a bit because Eco will not have to compile your file. Additionally, you can make use of the latest c# features like string interpolation, which are not supported by Eco's c# compiler at present. Now start a local game. Every time you update your DLL you need to disconnect from the local game and reconnect to reload your mod.
Listening to craft events[edit | edit source]
Ok, so now the event handling part. Let's create a method to handle crafting events:
private void HandleCraftAction(CraftAction ca) { ChatManager.ServerMessageToAllLoc($"{ca.Username} just crafted a {ca.ItemTypeName} at {ca.X}, {ca.Y}, {ca.Z}", true); }
Now, something will need to call this method, right? Add the following to the bottom of your `Initialize` method:
PlayerActions.Craft.OnActionPerformed.Add(HandleCraftAction);
There you go! All set up now.