ModAPI

ModAPI Allows a modder to add additional functionality to the dedicated server.

ModAPI mods are written in c# and compiled as a .dll which is then added to the Mods folder on the dedicated server alongside a ModInfo.xml just like other mods.

> **WARNING: Previous to A20, Mod API only works on dedicated server and will not run on the client either in single player or client hosted multiplayer**
>
>

### Tutorial

This is a quick start tutorial for getting started with ModAPI. Using Visual Studio

Create a new Class Library project targeting .NET framework 4.6.1 or 4.8 for A20 and above.

Then add the following references from your dedicated server '7DaysToDieServer\_Data/Managed' Folder

* Assembly-CSharp.dll
* Assembly-CSharp-firstpass.dll
* LogLibrary.dll
* mscorlib.dll
* System.dll
* System.Xml.dll
* UnityEngine.dll

Once you have added the references rename Class1.cs to API.cs this will be the start point for the mod.

Your API class needs to implement the IModApi interface and in the InitMod method we will register handlers for ModEvents.

```
public class API : IModApi
{
    private string ServerChatName = "Server";

    public void InitMod()
    {
       //This registers a handler for when the server handles a chat message.
       ModEvents.ChatMessage.RegisterHandler(ChatMessage);
    }

    //This method will then be called every time a ChatMessage is sent.
    private bool ChatMessage(ClientInfo clientInfo, EChatType _type, int _senderId, string message, string mainName, bool _localizeMain, List<int> _recipientEntityIds)
    {
         //We make sure there is an actual message and a client, and also ignore the message if it's from the server.
         if(!string.IsNullOrEmpty(message) && clientInfo != null && mainName != ServerChatName)
         {
             //We check to see if the message starts with a /
             if(message.StartsWith("/"))
             {
                  //we then remove that / to get the rest of the message.
                  message = message.Replace("/", "");

                  if(message == "hello")
                  {
                     ChatHook.ChatMessage(clientInfo, $"Hello {clientInfo.playerName}", -1, Config.Server_Response_Name, EChatType.Global, null);
                     //We return false to prevent any other listeners from processing this message.
                     return false;
                  }
             }
         }
         //Returning true allows other listeners to process this message.
         return true;
    }
}

```

As you can see from the above code, we register a handler for the ModEvent called ChatMessage which is then handled by our own method called ChatMessage. Inside this method we check to make sure the message is not blank and that we it starts with our command prefix "/". when they check to see if the message is hello and if so we have the server respond saying hello back. Note that we call a method called ChatMessage which handles sending message from the server to clients. This method has not be included here because it's part of the ServerTools mod by dmustanger. This is an extensive open source ModAPI mod which has some fantastic examples on how to do many things. it can be found here: https://github.com/dmustanger/7dtd-ServerTools. To add it to your project, download the latest release, and add the DLL from the downloaded .ZIP file to your project, like you did with the DLLs above. Don't forget to include the ServerTools using statement, ChatMessage is located under the ChatHook class.

Once you have completed the code, build the project and then create a new folder for your mod inside the dedicated server mods folder. Add a ModInfo.xml ( see Mod Structure for more information ) and drop your project .dll as well as UnityEngine.CoreModule.dll and UnityEngine.dll inside the mod folder. Once this is done simply start the server and your mod should be loaded.