Add chatcommand unregister and override API (#5076)
authorElijah Duffy <enduffy2014@outlook.com>
Fri, 20 Jan 2017 18:49:20 +0000 (10:49 -0800)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Fri, 20 Jan 2017 18:49:20 +0000 (19:49 +0100)
Introduces two functions to unregister and override chatcommands.
minetest.unregister_chatcommand("<name>") and
minetest.override_chatcommand("<name>", {<redifinition>})

builtin/game/chatcommands.lua
doc/lua_api.txt

index 199b9e964a7e07e79554522bf9568af50535c6fc..08dc1bb1d11053189507a926871dbb8e8460e279 100644 (file)
@@ -15,6 +15,24 @@ function core.register_chatcommand(cmd, def)
        core.registered_chatcommands[cmd] = def
 end
 
+function core.unregister_chatcommand(name)
+       if core.registered_chatcommands[name] then
+               core.registered_chatcommands[name] = nil
+       else
+               core.log("warning", "Not unregistering chatcommand " ..name..
+                       " because it doesn't exist.")
+       end
+end
+
+function core.override_chatcommand(name, redefinition)
+       local chatcommand = core.registered_chatcommands[name]
+       assert(chatcommand, "Attempt to override non-existent chatcommand "..name)
+       for k, v in pairs(redefinition) do
+               rawset(chatcommand, k, v)
+       end
+       core.registered_chatcommands[name] = chatcommand
+end
+
 core.register_on_chat_message(function(name, message)
        local cmd, param = string.match(message, "^/([^ ]+) *(.*)")
        if not param then
index 9bdc01c0795464632c2937ddd37da0454af472bb..aada851a1fb968a9a199c70c28642c01525bfc78 100644 (file)
@@ -2077,6 +2077,10 @@ Call these functions only at load time!
 ### Other registration functions
 * `minetest.register_chatcommand(cmd, chatcommand definition)`
     * Adds definition to minetest.registered_chatcommands
+* `minetest.override_chatcommand(name, redefinition)`
+    * Overrides fields of a chatcommand registered with register_chatcommand.
+* `minetest.unregister_chatcommand(name)`
+    * Unregisters a chatcommands registered with register_chatcommand.
 * `minetest.register_privilege(name, definition)`
     * `definition`: `"description text"`
     * `definition`: `{ description = "description text", give_to_singleplayer = boolean}`