From 17d704799a4dea91dab71e6d80303d5bbfaef3a8 Mon Sep 17 00:00:00 2001 From: HybridDog Date: Mon, 17 Feb 2020 12:42:41 +0100 Subject: [PATCH] Use chatcommand return values --- worldedit_brush/init.lua | 67 ++++++++++++++++++++++--------------- worldedit_commands/init.lua | 33 ++++++------------ worldedit_commands/safe.lua | 11 +++--- 3 files changed, 56 insertions(+), 55 deletions(-) diff --git a/worldedit_brush/init.lua b/worldedit_brush/init.lua index 56b8280..25e5ad3 100644 --- a/worldedit_brush/init.lua +++ b/worldedit_brush/init.lua @@ -54,10 +54,13 @@ local brush_on_use = function(itemstack, placer) minetest.log("action", string.format("%s uses WorldEdit brush (//%s) at %s", name, cmd, minetest.pos_to_string(pointed_thing.under))) - cmddef.func(name, unpack(parsed)) - + local success, msg = cmddef.func(name, unpack(parsed)) worldedit.player_notify = player_notify_old - return true + + if msg then + worldedit.player_notify(name, msg) + end + return success ~= false end minetest.register_tool(":worldedit:brush", { @@ -89,37 +92,47 @@ worldedit.register_command("brush", { func = function(name, cmd, params) local itemstack = minetest.get_player_by_name(name):get_wielded_item() if itemstack == nil or itemstack:get_name() ~= "worldedit:brush" then - worldedit.player_notify(name, "Not holding brush item.") - return + return false, worldedit.notify_form:format( + "Not holding brush item.") end cmd = cmd:lower() local meta = itemstack:get_meta() if cmd == "none" then meta:from_table(nil) - worldedit.player_notify(name, "Brush assignment cleared.") - else - local cmddef = worldedit.registered_commands[cmd] - if cmddef == nil or cmddef.require_pos ~= 1 then - worldedit.player_notify(name, "//" .. cmd .. " cannot be used with brushes") - return - end - - -- Try parsing command params so we can give the user feedback - local ok, err = cmddef.parse(params) - if not ok then - err = err or "invalid usage" - worldedit.player_notify(name, "Error with brush command: " .. err) - return - end - - meta:set_string("command", cmd) - meta:set_string("params", params) - local fullcmd = "//" .. cmd .. " " .. params - meta:set_string("description", - minetest.registered_tools["worldedit:brush"].description .. ": " .. fullcmd) - worldedit.player_notify(name, "Brush assigned to command: " .. fullcmd) + minetest.get_player_by_name(name):set_wielded_item(itemstack) + return true, worldedit.notify_form:format( + "Brush assignment cleared.") end + + local cmddef = worldedit.registered_commands[cmd] + if cmddef == nil or cmddef.require_pos ~= 1 then + return false, worldedit.notify_form:format("//" .. cmd .. + " cannot be used with brushes") + end + + -- Try parsing command params so we can give the user feedback + local ok, err = cmddef.parse(params) + if not ok then + err = err or "invalid usage" + return false, worldedit.notify_form:format( + "Error with brush command: " .. err) + end + + meta:set_string("command", cmd) + meta:set_string("params", params) + local fullcmd = "//" .. cmd .. " " .. params + meta:set_string("description", + minetest.registered_tools["worldedit:brush"].description .. ": " .. fullcmd) + worldedit.player_notify(name, "Brush assigned to command: " .. fullcmd) + + meta:set_string("command", cmd) + meta:set_string("params", params) + local fullcmd = "//" .. cmd .. " " .. params + meta:set_string("description", + minetest.registered_tools["worldedit:brush"].description .. ": " .. fullcmd) minetest.get_player_by_name(name):set_wielded_item(itemstack) + return true, worldedit.notify_form:format( + "Brush assigned to command: " .. fullcmd) end, }) diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index 8fac3b2..28f43a7 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -12,8 +12,10 @@ worldedit.prob_list = {} local safe_region, reset_pending = dofile(minetest.get_modpath("worldedit_commands") .. "/safe.lua") +-- The prefix avoids chat messages being mixed up with WorldEdit messages +worldedit.notify_form = minetest.colorize("#94ffcc", "WorldEdit -!- ") .. "%s" function worldedit.player_notify(name, message) - minetest.chat_send_player(name, "WorldEdit -!- " .. message, false) + minetest.chat_send_player(name, worldedit.notify_form:format(message)) end worldedit.registered_commands = {} @@ -22,40 +24,27 @@ local function 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 + if not worldedit.pos1[name] or not worldedit.pos2[name] then + return false, worldedit.notify_form:format("no region selected") end + elseif def.require_pos == 1 and not worldedit.pos1[name] then + return false, worldedit.notify_form:format("no position 1 selected") 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 + return false, worldedit.notify_form:format(parsed[1] or "invalid usage") 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 + return safe_region(name, count, function() + return def.func(name, unpack(parsed)) end) else -- no "safe region" check - local success, msg = def.func(name, unpack(parsed)) - if msg then - minetest.chat_send_player(name, msg) - end + return def.func(name, unpack(parsed)) end end diff --git a/worldedit_commands/safe.lua b/worldedit_commands/safe.lua index c83cac8..5b7c407 100644 --- a/worldedit_commands/safe.lua +++ b/worldedit_commands/safe.lua @@ -9,7 +9,8 @@ local function safe_region(name, count, callback) --save callback to call later safe_region_callback[name] = callback - worldedit.player_notify(name, "WARNING: this operation could affect up to " .. count .. " nodes; type //y to continue or //n to cancel") + return false, "WARNING: this operation could affect up to " .. count .. + " nodes; type //y to continue or //n to cancel" end local function reset_pending(name) @@ -22,12 +23,11 @@ minetest.register_chatcommand("/y", { func = function(name) local callback = safe_region_callback[name] if not callback then - worldedit.player_notify(name, "no operation pending") - return + return false, worldedit.notify_form:format("no operation pending") end reset_pending(name) - callback(name) + return callback(name) end, }) @@ -36,8 +36,7 @@ minetest.register_chatcommand("/n", { description = "Abort a pending operation", func = function(name) if not safe_region_callback[name] then - worldedit.player_notify(name, "no operation pending") - return + return false, worldedit.notify_form:format("no operation pending") end reset_pending(name)