be3906e1fb
Fixes #11 |
||
---|---|---|
tests | ||
.gitignore | ||
.gitlab-ci.yml | ||
.luacheckrc | ||
LICENSE | ||
README.md | ||
chatcmdbuilder.lua | ||
init.lua | ||
mod.conf |
README.md
ChatCmdBuilder
Easily create complex chat commands with no pattern matching.
Created by rubenwardy.\ License: MIT
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: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