- Lua 100%
| tests | Fix intermittent test failures | |
| .gitignore | Initial Commit | |
| .gitlab-ci.yml | Improve code quality | |
| .luacheckrc | Implement new API | |
| chatcmdbuilder.lua |
Replace Minetest with Luanti, minetest. with core.
|
|
| init.lua |
Replace Minetest with Luanti, minetest. with core.
|
|
| LICENSE | Improve code quality | |
| mod.conf | Improve code quality | |
| README.md |
Replace Minetest with Luanti, minetest. with core.
|
|
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 Luanti dependency system install it for you.
-
OR include the
chatcmdbuilder.luafile in your mod, and thendofileit like so:local modpath = core.get_modpath(core.get_current_modname()) local chatcmdbuilder = dofile(modpath.."/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 = core.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 = core.get_player_by_name(target)
if player then
player:setpos(pos)
return true, "Moved " .. target .. " to " .. core.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:
moveandtoare terminals. They need to be there in order to match.:targetand:pos:posare variables. They're passed to the function.- The second
posin:pos:posafter:is the param type.:targethas 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.Builderinstance 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