]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Lua API: Add register_on_chatcommand to SSM and CSM (#7862)
authorElijah Duffy <enduffy2014@outlook.com>
Sat, 3 Oct 2020 16:38:08 +0000 (09:38 -0700)
committerGitHub <noreply@github.com>
Sat, 3 Oct 2020 16:38:08 +0000 (17:38 +0100)
Allows catching a chatcommand call just after the command and the
parameters are parsed but before its existence is checked and before the
corresponding function is run. Returning `true` from a callback function
will prevent default handling of the command leaving mods to handle the
command manually.

builtin/client/chatcommands.lua
builtin/client/register.lua
builtin/game/chat.lua
builtin/game/register.lua
clientmods/preview/init.lua
doc/client_lua_api.txt
doc/lua_api.txt
games/devtest/mods/experimental/commands.lua

index 5cb1b40bb34858a10dbaae94d9e1c70dfb7bc65f..0e8d4dd030cbbc3e4df9269162f0f74c6ceb5eae 100644 (file)
@@ -23,6 +23,11 @@ core.register_on_sending_chat_message(function(message)
                return true
        end
 
+       -- Run core.registered_on_chatcommand callbacks.
+       if core.run_callbacks(core.registered_on_chatcommand, 5, cmd, param) then
+               return true
+       end
+
        local cmd_def = core.registered_chatcommands[cmd]
        if cmd_def then
                core.set_last_run_mod(cmd_def.mod_origin)
index c1b4965c142c6ba0b3e4fd2ce9a54267fb42042e..acd36ab36fb7e02884cd03f899576320bb430342 100644 (file)
@@ -63,6 +63,7 @@ core.registered_on_mods_loaded, core.register_on_mods_loaded = make_registration
 core.registered_on_shutdown, core.register_on_shutdown = make_registration()
 core.registered_on_receiving_chat_message, core.register_on_receiving_chat_message = make_registration()
 core.registered_on_sending_chat_message, core.register_on_sending_chat_message = make_registration()
+core.registered_on_chatcommand, core.register_on_chatcommand = make_registration()
 core.registered_on_death, core.register_on_death = make_registration()
 core.registered_on_hp_modification, core.register_on_hp_modification = make_registration()
 core.registered_on_damage_taken, core.register_on_damage_taken = make_registration()
index 1d277730a720ab868aeafc31910719bdf6c6c0c2..945707623bf216df5e1545393265ea078c89d5e0 100644 (file)
@@ -58,6 +58,11 @@ core.register_on_chat_message(function(name, message)
 
        param = param or ""
 
+       -- Run core.registered_on_chatcommands callbacks.
+       if core.run_callbacks(core.registered_on_chatcommands, 5, name, cmd, param) then
+               return true
+       end
+
        local cmd_def = core.registered_chatcommands[cmd]
        if not cmd_def then
                core.chat_send_player(name, "-!- Invalid command: " .. cmd)
index 1034d4f2bd3bf8ddffc779231400d5b31eab763e..3de67c04b29484843056c6698bbdec623fbb044a 100644 (file)
@@ -584,6 +584,7 @@ core.unregister_biome = make_wrap_deregistration(core.register_biome,
                core.clear_registered_biomes, core.registered_biomes)
 
 core.registered_on_chat_messages, core.register_on_chat_message = make_registration()
+core.registered_on_chatcommands, core.register_on_chatcommand = make_registration()
 core.registered_globalsteps, core.register_globalstep = make_registration()
 core.registered_playerevents, core.register_playerevent = make_registration()
 core.registered_on_mods_loaded, core.register_on_mods_loaded = make_registration()
index 089955d2f94c496ab09bf7d6a7edc4a9ceaf015b..977ed0ec3a8948216d27f30aa4dc5534982982cb 100644 (file)
@@ -109,6 +109,10 @@ core.register_on_sending_chat_message(function(message)
        return false
 end)
 
+core.register_on_chatcommand(function(command, params)
+       print("[PREVIEW] caught command '"..command.."'. Parameters: '"..params.."'")
+end)
+
 -- This is an example function to ensure it's working properly, should be removed before merge
 core.register_on_hp_modification(function(hp)
        print("[PREVIEW] HP modified " .. hp)
index 4c5231b79e8dbece2d50a35c4c7d1d888d66d796..32be2fabfd35edaff60335c65fd302d2d37f0f47 100644 (file)
@@ -686,6 +686,11 @@ Call these functions only at load time!
     * Adds definition to minetest.registered_chatcommands
 * `minetest.unregister_chatcommand(name)`
     * Unregisters a chatcommands registered with register_chatcommand.
+* `minetest.register_on_chatcommand(function(command, params))`
+    * Called always when a chatcommand is triggered, before `minetest.registered_chatcommands`
+      is checked to see if that the command exists, but after the input is parsed.
+    * Return `true` to mark the command as handled, which means that the default
+      handlers will be prevented.
 * `minetest.register_on_death(function())`
     * Called when the local player dies
 * `minetest.register_on_hp_modification(function(hp))`
index edacfe05fe5e1e5b854f358134314cbd6f0feb5c..e8eb403c400afa0d4daa038a0d7eb861068413fa 100644 (file)
@@ -4606,6 +4606,11 @@ Call these functions only at load time!
     * Called always when a player says something
     * Return `true` to mark the message as handled, which means that it will
       not be sent to other players.
+* `minetest.register_on_chatcommand(function(name, command, params))`
+    * Called always when a chatcommand is triggered, before `minetest.registered_chatcommands`
+      is checked to see if the command exists, but after the input is parsed.
+    * Return `true` to mark the command as handled, which means that the default
+      handlers will be prevented.
 * `minetest.register_on_player_receive_fields(function(player, formname, fields))`
     * Called when the server received input from `player` in a formspec with
       the given `formname`. Specifically, this is called on any of the
index 158e5039de783eb71208c8243e4b355e14072f08..132b08b0b04e5e3646635c5eb357b354f7b540c3 100644 (file)
@@ -214,3 +214,6 @@ minetest.register_chatcommand("test_place_nodes", {
        end,
 })
 
+core.register_on_chatcommand(function(name, command, params)
+       minetest.log("caught command '"..command.."', issued by '"..name.."'. Parameters: '"..params.."'")
+end)