From 9ad27404b0e41da6ac76e19be096864772b38199 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Wed, 30 Jun 2021 08:36:04 -0700 Subject: [PATCH] implemented chatcommand_handler --- worldeditadditions_core/register/check.lua | 2 +- worldeditadditions_core/register/handler.lua | 40 +++++++++++++++++++ worldeditadditions_core/register/init.lua | 1 + worldeditadditions_core/register/override.lua | 2 +- worldeditadditions_core/register/register.lua | 2 +- 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 worldeditadditions_core/register/handler.lua diff --git a/worldeditadditions_core/register/check.lua b/worldeditadditions_core/register/check.lua index a08a40d..6274a74 100644 --- a/worldeditadditions_core/register/check.lua +++ b/worldeditadditions_core/register/check.lua @@ -1,4 +1,4 @@ -function worldeditadditions.register_command(def) +function worldeditadditions_core.register_command(def) local def = table.copy(def) assert(name and #name > 0) assert(def.privs) diff --git a/worldeditadditions_core/register/handler.lua b/worldeditadditions_core/register/handler.lua new file mode 100644 index 0000000..b80c4a6 --- /dev/null +++ b/worldeditadditions_core/register/handler.lua @@ -0,0 +1,40 @@ +function worldeditadditions_core.chatcommand_handler(cmd_name, name, param) + local def = assert(worldedit.registered_commands[cmd_name]) + + if def.require_pos == 2 then + local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] + if pos1 == nil or pos2 == nil then + worldedit.player_notify(name, "no region selected") + return + end + elseif def.require_pos == 1 then + local pos1 = worldedit.pos1[name] + if pos1 == nil then + worldedit.player_notify(name, "no position 1 selected") + return + end + end + + local parsed = {def.parse(param)} + local success = table.remove(parsed, 1) + if not success then + worldedit.player_notify(name, parsed[1] or "invalid usage") + return + end + + if def.nodes_needed then + local count = def.nodes_needed(name, unpack(parsed)) + safe_region(name, count, function() + local success, msg = def.func(name, unpack(parsed)) + if msg then + minetest.chat_send_player(name, msg) + end + end) + else + -- no "safe region" check + local success, msg = def.func(name, unpack(parsed)) + if msg then + minetest.chat_send_player(name, msg) + end + end +end diff --git a/worldeditadditions_core/register/init.lua b/worldeditadditions_core/register/init.lua index ddf3499..50508e3 100644 --- a/worldeditadditions_core/register/init.lua +++ b/worldeditadditions_core/register/init.lua @@ -11,5 +11,6 @@ local we_cm = worldeditadditions_core.modpath .. "/register/" dofile(we_cm.."check.lua") +dofile(we_cm.."handler.lua") dofile(we_cm.."register.lua") dofile(we_cm.."override.lua") diff --git a/worldeditadditions_core/register/override.lua b/worldeditadditions_core/register/override.lua index e0d63db..bf7bb4e 100644 --- a/worldeditadditions_core/register/override.lua +++ b/worldeditadditions_core/register/override.lua @@ -11,7 +11,7 @@ function we_c.override_command(name, def) params = def.params, description = def.description, func = function(player_name, param) - return chatcommand_handler(name, player_name, param) + return we_c.chatcommand_handler(name, player_name, param) end, }) worldedit.registered_commands[name] = def diff --git a/worldeditadditions_core/register/register.lua b/worldeditadditions_core/register/register.lua index ebf8dfc..2a93120 100644 --- a/worldeditadditions_core/register/register.lua +++ b/worldeditadditions_core/register/register.lua @@ -11,7 +11,7 @@ function we_c.register_command(name, def) params = def.params, description = def.description, func = function(player_name, param) - return chatcommand_handler(name, player_name, param) + return we_c.chatcommand_handler(name, player_name, param) end, }) worldedit.registered_commands[name] = def