Jump to content

Looking for info on ModAPI. Multiple questions.

Recommended Posts

Is there any documentation for the ModAPI?  The only thing I've found is that one page on the fandom wiki (which is outdated).  I see that it only allows mods to respond to various events.  But there's no documentation on how to use it that I can find...and I have several questions:

  1. How do I send a message to a player?
  2. How do I get a player's information?  I'm assuming I should be catching join and leave events to acquire the IDs and be building a player list/dictionary inside the mod?
  3. Would it be possible with ModAPI to fix the code for Crafting Time when the player's craftingtime is negative?  For example, I have a faster forge on my server, as well as two equippable items that decrease crafting time.  When used together, there are some items that have a negative processing time (so they craft instantly).  I want to set the crafting time to 0.001 if it's determined to be negative.  Which should be a simple matter, provided I'm allowed to make that kind of alteration.
Link to comment
Share on other sites

On #1, this may not be exactly what you are looking for, but you can send a message to players on the server with a console command "Say" (see link here https://commands.gg/7dtd).  Maybe that "Say" command could be an action item?  Also if you have a quest you can send messages via Localization Tutorial tips etc.

Link to comment
Share on other sites

There is no documentation for ModAPI except for the stuff fellow modders have gathered and posted around. The official ModAPI is very barebones, there isn't a huge list of premade methods and capability built-in. It only provides a few ModEvents and a way to initialize custom code. The rest is left up to the modder to implement. There are several primary methods we use to add our custom functionality using the ModAPI, all through the provided InitMod entrypoint:

1. Register a handler for one of the build-in ModEvents.

2. Patch/hook a game method via the Harmony library.

3. Run custom code directly and/or add a new Unity GameObject to capitalize on its events.


Using a tool like dnSpy, we can decompile and read the game's code to determine how it works, and based on that, we can figure out how we need to adjust it in order to add our desired functionality.


For your questions:

  1. As commented previously, there is the "say" command built-in the game. We can take a look at how the game executes this command natively and use the same methodology to do it ourselves in a ModAPI mod. Open the Assembly-CSharp.dll using dnSpy and find the class that handles the "say" console command. It may take some digging to find it. In this case, I found it under the ConsoleCmdServerMessage class. Reading the code, the key line that stands out is:
    GameManager.Instance.ChatMessageServer(null, EChatType.Global, -1, msg, "Server", false, null);

    Taking a closer look at this method reveals the parameters:
    ChatMessageServer(ClientInfo _cInfo, EChatType _chatType, int _senderEntityId, string _msg, string _mainName, bool _localizeMain, List<int> _recipientEntityIds)

  2. The game already tracks clients/players. Depending on how you want player information you want, there are different ways to fetch it. Just browsing through the code a little gives me a few options, there are probably more.

    ServerTools mod already does a bunch of work with chat messages and fetching player information. I would recommend browsing its source https://github.com/dmustanger/7dtd-ServerTools/ and don't try reinventing the wheel.

  3. Absolutely, but it might not be as simple as you might hope. I would look into the XUiC_RecipeStack and XUiC_CraftingQueue classes. You might be able to do it by hooking (prefixing) the XUiC_RecipeStack.SetRecipe method and clamping the craftTime argument, but that's just a guess.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...