[Mod] Chatcommand Library [cmdlib]

Post Reply
User avatar
LMD
Member
Posts: 1400
Joined: Sat Apr 08, 2017 08:16
GitHub: appgurueu
IRC: appguru[eu]
In-game: LMD
Location: Germany
Contact:

[Mod] Chatcommand Library [cmdlib]

by LMD » Post

Chatcommand Library (cmdlib)

Making chatcommands a pleasure to use.

About

Adds a few features to chatcommands useful for devs (parsing, trees, forbidden privs, etc),
and other features useful for players, such as suggestions, command trees, and a better help command.
Note : Overrides /help chatcommand and builtin functions (minetest.register_chatcommand), replaces chatcommand handler.
Depends on modlib. IntelliJ IDEA with EmmyLua plugin project.
Code & media by Lars Mueller aka LMD or appguru(eu). Licensed under GPLv3.
Links : GitHub, Minetest Forum, Content DB

Screenshot

Image


API

A few API methods are listed below. Browse the code for more.
Three parts are provided by cmdlib :
  • Trie data structure
  • Help command
  • Chatcommand utils (main part)
cmdlib.register(name, def)

Name (name) : Chatcommand name, including whitespaces (such as mod command)
Definition (def) : Table with entries params, custom_syntax, implicit_call, description, privs, and func
  • Params: Argument syntax, string, format of [optional_param], or {param} for zero or more occurrences.
    Needs required params first, then optional ones, and finally, an optional list
  • Custom syntax: Flag, if set to true, func will be called with string params (empty string if none given). Automatically true if params string is invalid.
  • Implicit call: Metacommands only. If set to true, chatcommand will be called instead of proposing subcommand. Automatically true if params are specified.
  • Description: Text describing the usage of the chatcommand
  • Privileges: Table with privs which are required or should be missing, like {priv1=true, priv2=false}
  • Function: Function being invoked with sendername and a table of parameters ({param1="..."}), {params} will be supplied as tables
trie.new()

Creates (returns) a new trie (empty table).

trie.insert(trie, word, [value], [overwrite])

Inserts a word into a trie. value is optional (defaults to true).

overwrite is optional as well and defaults to false. Returns previous value.

trie.remove(trie, word)

Removes word from trie. Returns previous value.

trie.get(trie, word)

Check if trie contains word and return corresponding value, or nil.

trie.search(trie, word)

Search for word in trie. Return value if found, else (nil, closest word, value) or nil if no closest word exists.

Invocation

Invoke a chatcommand by giving the params separated by whitespaces (like /cmd subcmd param1 param2).

Help

Use /help [query] to open the extremely useful formspec shown above.
Last edited by LMD on Mon Dec 28, 2020 17:32, edited 17 times in total.
My stuff: Projects - Mods - Website

User avatar
Miniontoby
Member
Posts: 616
Joined: Fri Mar 01, 2019 19:25
GitHub: Miniontoby
IRC: Miniontoby
In-game: Miniontoby
Location: The Netherlands

Re: [Mod] Command Lib - making them a pleasure to use [cmdli

by Miniontoby » Post

That's cool. Nice. Please put it on content.minetest.net
Working on mtctl ---- Check my mod "Doorbell" -- Stay safe

User avatar
LMD
Member
Posts: 1400
Joined: Sat Apr 08, 2017 08:16
GitHub: appgurueu
IRC: appguru[eu]
In-game: LMD
Location: Germany
Contact:

Re: [Mod] Command Lib - making them a pleasure to use [cmdli

by LMD » Post

Thanks ! Did it : https://content.minetest.net/packages/LMD/cmdlib/
I don't know when it will be approved, though.
My stuff: Projects - Mods - Website

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: [Mod] Command Lib - making them a pleasure to use [cmdli

by rubenwardy » Post

LMD wrote:Thanks ! Did it : https://content.minetest.net/packages/LMD/cmdlib/
I don't know when it will be approved, though.
Already is
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
Miniontoby
Member
Posts: 616
Joined: Fri Mar 01, 2019 19:25
GitHub: Miniontoby
IRC: Miniontoby
In-game: Miniontoby
Location: The Netherlands

Re: [Mod] Command Lib - making them a pleasure to use [cmdli

by Miniontoby » Post

And add "modlib" to content.minetest.net
Working on mtctl ---- Check my mod "Doorbell" -- Stay safe

User avatar
LMD
Member
Posts: 1400
Joined: Sat Apr 08, 2017 08:16
GitHub: appgurueu
IRC: appguru[eu]
In-game: LMD
Location: Germany
Contact:

Re: [Mod] Command Lib - making them a pleasure to use [cmdli

by LMD » Post

Thanks for the reminder. Submitted it : https://content.minetest.net/packages/LMD/modlib/
My stuff: Projects - Mods - Website

User avatar
Wuzzy
Member
Posts: 4804
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy
Contact:

Re: [Mod] Chatcommand Library [cmdlib]

by Wuzzy » Post

This looks interesting overall, but it has a bit of unneccessary complexity IMO.
It's basically one of the mods to overcome the limitations of the engine.

First, what looks good is the existence of a parameter parser. This is a glaring omission in builtin, and we're required to parse by hand.

What looks very good is that your mod apears to automatically construct the syntax string. This is always something that's very error-prone with builtin chat commands.

The new /help screen is an interesting concept, but I think it's pretty messy to read. The text doesn't properly align at all and goes all over the place. I have no idea what the colors are supposed to mean. I think what is needed is a per-parameter description. This is needed for the more complicated commands.
Anyway, I think the mod shouldn't touch the /help screen, it might be better to split off this functionality to a different mod.

I don't understand the purpose of “implicit call”.

What is really annoying that this mod has a dependency on a different mod, altough it's meant to be an “API mod” itself. Can't you just get rid of the dependency? Maybe just copy the features your need into cmblib.

Your mod is a wrapper around the current API, but in the long run, I think the API itself needs improvement. Having too many wrappers is also bad for your mental health. :D The obvious downside of your approach is that existing chat commands need to be completely rewritten to become compatible with cmdlib, and cmdlib has its completely own syntax. I don't like this, even if it works better.

Ultimately, I think the builtin chat command handling is kind of limited. OK, you can already do everything with it, but for parameter parsing, you're completely on your own. In the long run, I think builtin needs improvement, but changes need to be more lightweight to still be compatible.

User avatar
LMD
Member
Posts: 1400
Joined: Sat Apr 08, 2017 08:16
GitHub: appgurueu
IRC: appguru[eu]
In-game: LMD
Location: Germany
Contact:

Re: [Mod] Chatcommand Library [cmdlib]

by LMD » Post

I should probably have elaborated on this mods internals, my bad...
It's basically one of the mods to overcome the limitations of the engine.
Yes, sort of. It's main purpose is to make writing chatcommands easier, though.
What looks very good is that your mod apears to automatically construct the syntax string. This is always something that's very error-prone with builtin chat commands.
Actually not. It instead parses the given syntax.

Regarding the help screen: Maybe your font sizes are different, but it actually looks acceptable on my screens... I even implemented a workaround to ensure that descriptions are always legible...

Implicit call is for categories; if you have something like "/test say" and "/test shout", then "/test" is not a chatcommand, but a category. If you now try to execute "/test something", it will fail, because there is no subcommand called "something"; but with implicit call, "/test" will be called with "something" as param.

What is really annoying that this mod has a dependency on a different mod, altough it's meant to be an “API mod” itself. Can't you just get rid of the dependency? Maybe just copy the features your need into cmdlib.
True, I rather prefer "dependency hell" over bad conceptual design. Modlib is a basic dependency. I'd rather go with bundling modlib & cmdlib & other API mods in one "uber-API" modpack :P - I don't think code duplication is the way to go. I'd rather prefer an NPM-style approach...

On top, cmdlib is compatible with Minetest. Chatcommands don't need to be reworked. In fact, cmdlib replaces the builtin chatcommand handling using some sort of "hack". Cmdlib also overrides minetest.register_chatcommand, minetest.unregister_chatcommand and minetest.override_chatcommand, and it's working fine. No compatibility issues.

But you're right, cmdlib has it's own API (cmdlib.register_chatcommand) which can not be used interchangeably with minetest.register_chatcommand.
In the long run, I think builtin needs improvement, but changes need to be more lightweight to still be compatible.
I disagree. Builtin should never have implemented chatcommands, this should always have been left to mods in order to be up for customization (for example, chatcommand suggestions as cmdlib does).
My stuff: Projects - Mods - Website

Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot], Rayjack and 40 guests