1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2025-07-02 08:00:40 +02:00

Use chatcommand return values

This commit is contained in:
HybridDog
2020-02-17 12:42:41 +01:00
parent bf55f52197
commit 17d704799a
3 changed files with 56 additions and 55 deletions

View File

@ -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

View File

@ -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)