API
Installing
As a mod developer, you can:
-
Depend on lib_chatcmdbuilder mod and let the Minetest dependency system install it for you.
-
OR include the
chatcmdbuilder.lua
file in your mod, and thendofile
it like so:
lua
local chatcmdbuilder = dofile("chatcmdbuilder.lua")
It's important that you keep this as a local, to avoid conflict with the mod version if installed.
Registering Chat Commands
chatcmdbuilder.register(name, def)
registers a new chat command called name
. It returns an object to register subcommands.
Here is an example:
local cmd = chatcmdbuilder.register("admin", {
description = "Admin tools",
privs = {
kick = true,
ban = true
}
})
cmd:sub("kill :target", function(name, target)
local player = minetest.get_player_by_name(target)
if player then
player:set_hp(0)
return true, "Killed " .. target
else
return false, "Unable to find " .. target
end
end)
cmd:sub("move :target to :pos:pos", {
privs = {
teleport = true
}
func = function(name, target, pos)
local player = minetest.get_player_by_name(target)
if player then
player:setpos(pos)
return true, "Moved " .. target .. " to " .. minetest.pos_to_string(pos)
else
return false, "Unable to find " .. target
end
end,
})
A player could then do /admin kill player1
to kill player1,
or /admin move player1 to 0,0,0
to teleport a user.
Introduction to Routing
A route is a string. Let's look at move :target to :pos:pos
:
move
andto
are terminals. They need to be there in order to match.:target
and:pos:pos
are variables. They're passed to the function.- The second
pos
in:pos:pos
after:
is the param type.:target
has an implicit type ofword
.
Param Types
word
: default. Any string without spacesnumber
: Any number, including decimalsint
: Any integer, no decimalstext
: Any stringpos
: 1,2,3 or 1.1,2,3.4567 or (1,2,3) or 1.2, 2 ,3.2modname
: a mod namealpha
: upper or lower alphabetic characters (A-Za-z)alphascore
: above, but with underscoresalphanumeric
: upper or lower alphabetic characters and numbers (A-Za-z0-9)username
: a usernameitemname
: an item name
Registering new Param Types
-- Simple type for lowercase text
-- eg: `:param:lower`
chatcmdbuilder.register_type("lower", "([a-z]+)", function(pop)
return tonumber(pop())
end)
-- Position type
-- eg: `:param:pos`
chatcmdbuilder.register_type("pos", "%(? *(%-?[%d.]+) *, *(%-?[%d.]+) *, *(%-?[%d.]+) *%)?", function(pop)
return {
x = tonumber(pop()),
y = tonumber(pop()),
z = tonumber(pop())
}
end)
Reference
Functions
chatcmdbuilder.register(name, def)
: registers a chat command- Returns a
chatcmdbuilder.Builder
instance name
: chat command name.def
: chat command def, can contain everything inregister_chatcommand
, except forfunc
.
- Returns a
chatcmdbuilder.register_type(name, pattern, converter)
: register a param typename
: type name, used in routespattern
: A Lua patternconverter(pop)
: Optional, a function to convert text into the typepop
: function to return the next matched group.- returns the converted value.
class chatcmdbuilder.Builder
This is the class returned by chatcmdbuilder.register
.
Constructor:
chatcmdbuilder.Builder:new()
: returns new instance
Methods:
sub(path, func_or_def)
path
: a routefunc_or_def
: either a function or a def table containing:func
: functionprivs
: a list of required privs
run(name, params)
: Execute chat command- Returns same as
func
:boolean, message
. - Doesn't check chat command privs, but will check subcommand privs.
- Returns same as
Way easier commands
I've been using this library for months, since I read about it on the Modding Book. Definitely worth it
Easy to use, saves me a lot of time
If you're only making one relatively simple command for your mod you may want to just write everything yourself, but if you're doing lots of decently complex commands then I highly recommend this API
Time Saver!
This saves so much time when creating more complex commands, awesome!
Very necessary
Top API for modding