ModAPI

From 7 Days to Die Wiki
Jump to: navigation, search
Some items' or groups' chance to drop changes according to the player's gamestage. Here you can see those values for any gamestage you want using the little box below. Just type the gamestage you want in the box and press the Apply!-button. The values should change immediately, if the chance is different.

Current game stage: 1

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: Mod API only works on dedicated server and will not run on the client either in single player or client hosted multiplayer

Tutorial[edit | edit source]

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

Create a new Class Library project targetting .net framework 4.6.1

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 _cInfo, EChatType _type, int _senderId, string _msg, 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")
                  {
                     SendChatMessage(clientInfo, $"Hello {clientInfo.playerName}", -1, LoadConfig.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 SendChatMessage 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

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.