Introduce type parameter for worldedit.player_notify

This commit is contained in:
sfan5
2024-04-22 17:33:16 +02:00
parent d13ba673fe
commit 1d8d9a704f
12 changed files with 235 additions and 212 deletions

View File

@ -31,13 +31,13 @@ local function chatcommand_handler(cmd_name, name, param)
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, S("no region selected"))
worldedit.player_notify(name, S("no region selected"), "error")
return
end
elseif def.require_pos == 1 then
local pos1 = worldedit.pos1[name]
if pos1 == nil then
worldedit.player_notify(name, S("no position 1 selected"))
worldedit.player_notify(name, S("no position 1 selected"), "error")
return
end
end
@ -45,14 +45,14 @@ local function chatcommand_handler(cmd_name, name, param)
local parsed = {def.parse(param)}
local success = table.remove(parsed, 1)
if not success then
worldedit.player_notify(name, parsed[1] or S("invalid usage"))
worldedit.player_notify(name, parsed[1] or S("invalid usage"), "error")
return
end
local run = function()
local _, msg = def.func(name, unpack(parsed))
local ok, msg = def.func(name, unpack(parsed))
if msg then
minetest.chat_send_player(name, msg)
worldedit.player_notify(name, msg, ok and "ok" or "error")
end
end
@ -71,7 +71,7 @@ local function chatcommand_handler(cmd_name, name, param)
ok = ok and ( (v == nil and old_state[i] == nil) or vector.equals(v, old_state[i]) )
end
if not ok then
worldedit.player_notify(name, S("ERROR: the operation was cancelled because the region has changed."))
worldedit.player_notify(name, S("ERROR: the operation was cancelled because the region has changed."), "error")
return
end
@ -149,8 +149,27 @@ do
end
function worldedit.player_notify(name, message)
minetest.chat_send_player(name, "WorldEdit -!- " .. message)
-- Notifies a player of something related to WorldEdit.
-- Message types:
-- "error" = An operation did not work as expected.
-- "ok" = An operation completed successfully. Because notifications of this type
-- can be filtered, use this ONLY for generic messages like "1234 nodes set".
-- "info" = Other informational messages
-- @param name Name of player
-- @param message Message text
-- @param typ Type of message (optional but strongly recommend)
function worldedit.player_notify(name, message, typ)
local t = {
"WorldEdit",
"-!-",
tostring(message)
}
if typ == "error" then
t[2] = minetest.colorize("#f22", t[2])
elseif typ == "ok" then
t[2] = minetest.colorize("#2f2", t[2])
end
minetest.chat_send_player(name, table.concat(t, " "))
end
-- Determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1)
@ -178,11 +197,92 @@ worldedit.register_command("about", {
"commands, or find more information at @3",
worldedit.version_string, minetest.colorize("#00ffff", "//help"),
"https://github.com/Uberi/Minetest-WorldEdit"
))
), "info")
end,
})
-- initially copied from builtin/chatcommands.lua
local function help_command(name, param)
local function format_help_line(cmd, def, follow_alias)
local msg = minetest.colorize("#00ffff", "//"..cmd)
if def.name ~= cmd then
msg = msg .. ": " .. S("alias to @1",
minetest.colorize("#00ffff", "//"..def.name))
if follow_alias then
msg = msg .. "\n" .. format_help_line(def.name, def)
end
else
if def.params and def.params ~= "" then
msg = msg .. " " .. def.params
end
if def.description and def.description ~= "" then
msg = msg .. ": " .. def.description
end
end
return msg
end
-- @param cmds list of {cmd, def}
local function sort_cmds(cmds)
table.sort(cmds, function(c1, c2)
local cmd1, cmd2 = c1[1], c2[1]
local def1, def2 = c1[2], c2[2]
-- by category (this puts the empty category first)
if def1.category ~= def2.category then
return def1.category < def2.category
end
-- put aliases last
if (cmd1 ~= def1.name) ~= (cmd2 ~= def2.name) then
return cmd2 ~= def2.name
end
-- then by name
return c1[1] < c2[1]
end)
end
if not minetest.check_player_privs(name, "worldedit") then
return false, S("You are not allowed to use any WorldEdit commands.")
end
if param == "" then
local list = {}
for cmd, def in pairs(worldedit.registered_commands) do
if minetest.check_player_privs(name, def.privs) then
list[#list + 1] = cmd
end
end
table.sort(list)
local help = minetest.colorize("#00ffff", "//help")
return true, S("Available commands: @1@n"
.. "Use '@2' to get more information,"
.. " or '@3' to list everything.",
table.concat(list, " "), help .. " <cmd>", help .. " all")
elseif param == "all" then
local cmds = {}
for cmd, def in pairs(worldedit.registered_commands) do
if minetest.check_player_privs(name, def.privs) then
cmds[#cmds + 1] = {cmd, def}
end
end
sort_cmds(cmds)
local list = {}
local last_cat = ""
for _, e in ipairs(cmds) do
if e[2].category ~= last_cat then
last_cat = e[2].category
list[#list + 1] = "---- " .. last_cat
end
list[#list + 1] = format_help_line(e[1], e[2])
end
return true, S("Available commands:@n") .. table.concat(list, "\n")
else
local def = worldedit.registered_commands[param]
if not def then
return false, S("Command not available: ") .. param
else
return true, format_help_line(param, def, true)
end
end
end
worldedit.register_command("help", {
privs = {},
params = "[all/<cmd>]",
@ -191,83 +291,9 @@ worldedit.register_command("help", {
return true, param
end,
func = function(name, param)
local function format_help_line(cmd, def, follow_alias)
local msg = minetest.colorize("#00ffff", "//"..cmd)
if def.name ~= cmd then
msg = msg .. ": " .. S("alias to @1",
minetest.colorize("#00ffff", "//"..def.name))
if follow_alias then
msg = msg .. "\n" .. format_help_line(def.name, def)
end
else
if def.params and def.params ~= "" then
msg = msg .. " " .. def.params
end
if def.description and def.description ~= "" then
msg = msg .. ": " .. def.description
end
end
return msg
end
-- @param cmds list of {cmd, def}
local function sort_cmds(cmds)
table.sort(cmds, function(c1, c2)
local cmd1, cmd2 = c1[1], c2[1]
local def1, def2 = c1[2], c2[2]
-- by category (this puts the empty category first)
if def1.category ~= def2.category then
return def1.category < def2.category
end
-- put aliases last
if (cmd1 ~= def1.name) ~= (cmd2 ~= def2.name) then
return cmd2 ~= def2.name
end
-- then by name
return c1[1] < c2[1]
end)
end
if not minetest.check_player_privs(name, "worldedit") then
return false, S("You are not allowed to use any WorldEdit commands.")
end
if param == "" then
local list = {}
for cmd, def in pairs(worldedit.registered_commands) do
if minetest.check_player_privs(name, def.privs) then
list[#list + 1] = cmd
end
end
table.sort(list)
local help = minetest.colorize("#00ffff", "//help")
return true, S("Available commands: @1@n"
.. "Use '@2' to get more information,"
.. " or '@3' to list everything.",
table.concat(list, " "), help .. " <cmd>", help .. " all")
elseif param == "all" then
local cmds = {}
for cmd, def in pairs(worldedit.registered_commands) do
if minetest.check_player_privs(name, def.privs) then
cmds[#cmds + 1] = {cmd, def}
end
end
sort_cmds(cmds)
local list = {}
local last_cat = ""
for _, e in ipairs(cmds) do
if e[2].category ~= last_cat then
last_cat = e[2].category
list[#list + 1] = "---- " .. last_cat
end
list[#list + 1] = format_help_line(e[1], e[2])
end
return true, S("Available commands:@n") .. table.concat(list, "\n")
else
local def = worldedit.registered_commands[param]
if not def then
return false, S("Command not available: ") .. param
else
return true, format_help_line(param, def, true)
end
local ok, msg = help_command(name, param)
if msg then
worldedit.player_notify(name, msg, ok and "info" or "error")
end
end,
})
@ -285,7 +311,7 @@ worldedit.register_command("reset", {
worldedit.set_pos[name] = nil
--make sure the user does not try to confirm an operation after resetting pos:
reset_pending(name)
worldedit.player_notify(name, S("region reset"))
return true, S("region reset")
end,
})