Jump to content

Custom server commands


Dr_Cox1911

Recommended Posts

Greetings,

 

I'm trying to implement custom commands for my mods but it looks kinda tricky.

I was looking at the client/ISUI/ISChat.lua and there is the "onCommandEntered" which seems to execute whenever a input from a textbox is transmitted (here the textbox from the chat window). This also handles the commands shipped with the game.

 

The "easy" way would be to override some stuff from ISChat, but isn't there a better way to do this?

Link to comment
Share on other sites

I haven't looked to heavily into this one, but I did notice a while back the commands seem to be in the java without a simple lua function to add or remove additional commands.  Though in retrospect my initial investigations were at the beginning of my PZ modding attempts, and if i recall were limited to a quick 'find in files' to see if i could spot the built in commands in lua.

This one still interests me so I may do some poking around the java API later when I get the chance to find the actual functions that register commands. I'm pretty sure I spotted them the other week in my general browsing but didn't look to close.

 

Edit...I just looked at ISChat.lua,  it seems some of the commands are in the lua (shout, whisper etc) but i believe most of the actual commands are still in the java, searching the lua files for 'createhorde' returns nothing related to the actual processing of the command.

Edited by Fenris_Wolf
Link to comment
Share on other sites

I can put you in contact with someone to do this... however, at this time, he has managed this but it would be a client based thing only.  So each client would need to define a set of commands as if I remember right he mentioned something along the lines on how commands are handled locally or something like that. However, I think if you two worked together on this, we could both benefit from it. I will shoot him a link to this topic through discord. 

 

 

Link to comment
Share on other sites

19 hours ago, Fenris_Wolf said:

Edit...I just looked at ISChat.lua,  it seems some of the commands are in the lua (shout, whisper etc) but i believe most of the actual commands are still in the java, searching the lua files for 'createhorde' returns nothing related to the actual processing of the command.

There are a bunch more commands that are handled within lua (like the admin ones /teleport, /invisible, /reloadoptions, ...)

 

@FinestHops Don't quite get will this should only work on clients?

 

Current "easy" way of handling this: override the ISChat:onCommandEntered with my own function and check the beginning of the string for my command (e.g. /rules), if it matches I call my function to show my dialog (or even send a command to the server to refresh the rules and send it back to the client).

The problem with this: only one mod can add custom commands with this "solution".

Link to comment
Share on other sites

Just now, Dr_Cox1911 said:

There are a bunch more commands that are handled within lua (like the admin ones /teleport, /invisible, /reloadoptions, ...)

 

@FinestHops Don't quite get will this should only work on clients?

 

Current "easy" way of handling this: override the ISChat:onCommandEntered with my own function and check the beginning of the string for my command (e.g. /rules), if it matches I call my function to show my dialog (or even send a command to the server to refresh the rules and send it back to the client).

The problem with this: only one mod can add custom commands with this "solution".

 

Im going to be real with you.... when it comes to coding and all that, I know the basics and thats about it. However, the person I sent a link to for this topic is FAR better at coding than I am.  Hell... he made a mod for tire inflation speed modification as well as a mod for server owners to cap certain skills.... things I could never make. 

Link to comment
Share on other sites

16 minutes ago, Dr_Cox1911 said:

The problem with this: only one mod can add custom commands with this "solution".

thats  only if you have the commands hardcoded in the overwritten function with a if/elseif statement.

 

have the commands in a global table. Anything put in the global namespace can be accessed from any mod.

CommandTable = { ['command_name'] = callback_function }

 

with it in a global table, other mods could insert commands into the table, and your overwritten function can just do:

if CommandTable[command] then callback_function(args) end

 

This is a far better option then hardcoding in a if/else statement

 

edit: for best compatibility, and checking, you may want to create a registerCommand function

function registerCommand(name, callback)

   -- do some error checking making sure name is a string, and callback is actually a function

   -- plus whatever other checks, maybe have a additional argument to this function such as access level, etc

   CommandTable[name] = callback

end

 

 

Edited by Fenris_Wolf
Link to comment
Share on other sites

@FinestHops Sorry, shouldn't have come across as an insult or something, just curious how your guy tackled this and what his hurdles with the server-part where.

 

@Fenris_Wolf Thought about that too. Implementing this in CoxisUtil, but if another mod overrides the ISChat it's all gone if it get's loaded after CoxisUtil. And don't know how many modders will use a external lib like this. I personally don't have a problem with external libs, used LuaNet even before it was shipped with the game.

 

My ultimate wish: More accessibility for this stuff for mods to easily hook into. Same goes with all the menus and dialogs in the game. So much tables/variables are locally handled.

Link to comment
Share on other sites

@Dr_Cox1911 you can always use the trick of having your overwrite done on one of the events such as OnGameBoot so its less likely to be over written or have the file that does the overwrite use a filename starting with 'z' to ensure its loaded last. 

With ORGM I've been paying close attention to how PZ loads mod files and the loading order.  It seems all client/shared/server directories from all mods are merged, then the files loaded in alphabetical order, first loading shared, then client, then server.  The next ORGM version contains all functions and tables in a single global table, this table is defined in a file in shared/1LoadOrder/ORGMCore.lua. By having it in a subfolder starting with the 1 prefex, its pretty much guaranteed it will be loaded before any other files from other mods, ensuring those mods can access this global ORGM table.

 

If I wanted to ensure 100% it was loaded before anything else, shared/1/1.lua is probably a pretty safe bet. I have yet to check if the loadorder is case sensitive (ie: if Ab.lua will get loaded before aa.lua), but having your overwriting file named zzzzzzzz.lua is probably going to ensure its loaded last.

 

If you wanted to see if some other mod has overwriten the function, in a file named 1.lua do something like:

originalCommandEntered = ISChat.onCommandEntered

 

then in z.lua

if ISChat.onCommandEntered ~= originalCommandEntered then .... end

 

though best bet is probably in your overwrite function to immediately check if your mod should handle the command. If your not going to handle it, call the originalCommandEntered  (or other mods function if something else overwrote it)

 

 

 

Edited by Fenris_Wolf
Link to comment
Share on other sites

50 minutes ago, Dr_Cox1911 said:

@FinestHops Sorry, shouldn't have come across as an insult or something, just curious how your guy tackled this and what his hurdles with the server-part where.

 

@Fenris_Wolf Thought about that too. Implementing this in CoxisUtil, but if another mod overrides the ISChat it's all gone if it get's loaded after CoxisUtil. And don't know how many modders will use a external lib like this. I personally don't have a problem with external libs, used LuaNet even before it was shipped with the game.

 

My ultimate wish: More accessibility for this stuff for mods to easily hook into. Same goes with all the menus and dialogs in the game. So much tables/variables are locally handled.

 

I didnt take it as an insult at all. I thought you were asking ME that question and was just informing you I am not the one who knows those things. lol

Link to comment
Share on other sites

Tldr at the bottom.

 

Edit: There's a new update the ISChat again and thus well it broke a little but I fix it eventually.

 

What I did was to write on top of the original ISChat.

 

https://steamcommunity.com/sharedfiles/filedetails/?id=1265709605

 

How do I do this?

I have injected one more event that take place before the command processing. It will first do all the command processing for the mods before doing the vanilla commands.

 

What is the problem?

This mod is still quite experimental and I just started doing something ambitious hence it may not work quite well and optimized.

 

There is also chance that other mod that tries to override ISChat would fail to work as I have updated it to always load last but just before any event triggers. (Appending ~ to file name does that)

 

It becomes mod specific and everyone had to use that specific mod api to add more functions....

 

Legion was talking about me :) I did custom mods for them. (Sux to say that my mod had a dependency and doesn't work on its own when I ship them individually as a module)

 

In fact it is still early stage how it would work. I only did this for capping skills mod that has custom commands on it.

 

It does not work on server side command prompt and will never work anyway.

 

Tldr: My solution was same as the above discussion. It is by overwritting a global function and ensures it always load last.

Edited by jianmingyong
Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...

I have bundled a new clone (with additional features like colors and custom commands)

https://github.com/The-Dialga-Team-Project-Zomboid-Mods/The-Dialga-Team-Mods-Pack/blob/master/media/lua/client/TheDialgaTeam/TDTModChatBoxAPI/ChatBoxAPI.lua

 

I made a placeholder repo to hold my mods for now. Though all these code are align closely to our own server, I am making it customizable via setting files.

(I can bundle all the mods into one which allows easier usage)

 

I ship my own chatbox in addition to the default chatbox. As default chatbox differs from version to version, it may break anytime.

I am still looking for ways to make it simpler to inject custom command.

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...