From b8161337167de3c8ac9594723a0e4cfe6cfc00c6 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Thu, 19 May 2022 02:31:01 +0100 Subject: [PATCH] core: implement fetch_command_def This rovides an abstraction to fetch a worldedit command's definition, regardless of where it was registered. We would normally expect all commands to be registered in wea_c.registered_commands, but before we only do a one-off pass to import commands from worldedit should a new mod we aren't aware of register a command with worldedit and get loaded after us, we won't detect it - hencee the need for this function. --- worldeditadditions/mod.conf | 2 +- .../commands/meta/airapply.lua | 4 ++-- .../commands/meta/ellipsoidapply.lua | 3 ++- .../commands/meta/noiseapply2d.lua | 3 ++- .../commands/meta/subdivide.lua | 9 +++++---- worldeditadditions_commands/doc/init.lua | 2 +- worldeditadditions_commands/mod.conf | 2 +- .../core/fetch_command_def.lua | 20 +++++++++++++++++++ .../core/register_command.lua | 5 ++--- worldeditadditions_core/core/run_command.lua | 13 ++++++------ worldeditadditions_core/init.lua | 9 +++++---- 11 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 worldeditadditions_core/core/fetch_command_def.lua diff --git a/worldeditadditions/mod.conf b/worldeditadditions/mod.conf index ea02050..2d0ac3a 100644 --- a/worldeditadditions/mod.conf +++ b/worldeditadditions/mod.conf @@ -1,4 +1,4 @@ name = worldeditadditions description = Extra tools and commands to extend WorldEdit for Minetest depends = worldedit -optional_depends = bonemeal, cool_trees, default, moretrees, ethereal +optional_depends = worldeditadditions_core, bonemeal, cool_trees, default, moretrees, ethereal diff --git a/worldeditadditions_commands/commands/meta/airapply.lua b/worldeditadditions_commands/commands/meta/airapply.lua index bd0f548..7989f44 100644 --- a/worldeditadditions_commands/commands/meta/airapply.lua +++ b/worldeditadditions_commands/commands/meta/airapply.lua @@ -3,7 +3,7 @@ -- ███████ ██ ██████ ███████ ██████ ██████ ██ ████ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ███████ ██ - +local wea_c = worldeditadditions_core worldedit.register_command("airapply", { params = " ", @@ -20,7 +20,7 @@ worldedit.register_command("airapply", { end -- Note that we search the worldedit commands here, not the minetest ones - local cmd_we = worldedit.registered_commands[cmd_name] + local cmd_we = wea_c.fetch_command_def(cmd_name) if cmd_we == nil then return false, "Error: "..cmd_name.." isn't a valid command." end diff --git a/worldeditadditions_commands/commands/meta/ellipsoidapply.lua b/worldeditadditions_commands/commands/meta/ellipsoidapply.lua index 83f00a1..0489407 100644 --- a/worldeditadditions_commands/commands/meta/ellipsoidapply.lua +++ b/worldeditadditions_commands/commands/meta/ellipsoidapply.lua @@ -3,6 +3,7 @@ -- █████ ██ ██ ██ ██████ ███████ █████ ███████ ██████ ██████ ██ ████ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ███████ ███████ ███████ ██ ██ ███████ ███████ ██ ██ ██ ██ ███████ ██ +local wea_c = worldeditadditions_core worldedit.register_command("ellipsoidapply", { params = " ", @@ -21,7 +22,7 @@ worldedit.register_command("ellipsoidapply", { -- print("cmd_name", cmd_name, "args_text", args_text) -- Note that we search the worldedit commands here, not the minetest ones - local cmd_we = worldedit.registered_commands[cmd_name] + local cmd_we = wea_c.fetch_command_def(cmd_name) if cmd_we == nil then return false, "Error: "..cmd_name.." isn't a valid command." end diff --git a/worldeditadditions_commands/commands/meta/noiseapply2d.lua b/worldeditadditions_commands/commands/meta/noiseapply2d.lua index 14d127c..c7edb48 100644 --- a/worldeditadditions_commands/commands/meta/noiseapply2d.lua +++ b/worldeditadditions_commands/commands/meta/noiseapply2d.lua @@ -3,6 +3,7 @@ -- ██ ██ ██ ██ ██ ██ ███████ █████ ███████ ██████ ██████ ██ ████ █████ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ████ ██████ ██ ███████ ███████ ██ ██ ██ ██ ███████ ██ ███████ ██████ +local wea_c = worldeditadditions_core worldedit.register_command("noiseapply2d", { @@ -19,7 +20,7 @@ worldedit.register_command("noiseapply2d", { end -- Note that we search the worldedit commands here, not the minetest ones - local cmd_we = worldedit.registered_commands[cmd_name] + local cmd_we = wea_c.fetch_command_def(cmd_name) if cmd_we == nil then return false, "Error: "..cmd_name.." isn't a valid command." end diff --git a/worldeditadditions_commands/commands/meta/subdivide.lua b/worldeditadditions_commands/commands/meta/subdivide.lua index 5acf272..c291146 100644 --- a/worldeditadditions_commands/commands/meta/subdivide.lua +++ b/worldeditadditions_commands/commands/meta/subdivide.lua @@ -1,11 +1,12 @@ local wea = worldeditadditions +local wea_c = worldeditadditions_core -- Test command: -- //multi //fp set1 1330 60 5455 //fp set2 1355 35 5430 //subdivide 10 10 10 fixlight //y local function will_trigger_saferegion(name, cmd_name, args) - if not worldedit.registered_commands[cmd_name] then return nil, "Error: That worldedit command could not be found (perhaps it hasn't been upgraded to worldedit.register_command() yet?)" end - local def = worldedit.registered_commands[cmd_name] + local def = wea_c.fetch_command_def(cmd_name) + if not def then return nil, "Error: That worldedit command could not be found (perhaps it hasn't been upgraded to worldedit.register_command() yet?)" end if not def.parse then return nil, "Error: No parse method found (this is a bug)." end local parsed = {def.parse(args)} @@ -57,7 +58,7 @@ worldedit.register_command("subdivide", { local cmd_name = parts[4] - if not worldedit.registered_commands[cmd_name] then + if not wea_c.fetch_command_def(cmd_name) then return false, "Error: The worldedit command '"..parts[4].."' does not exist (try /help)." end @@ -73,7 +74,7 @@ worldedit.register_command("subdivide", { local pos1, pos2 = worldedit.sort_pos(worldedit.pos1[name], worldedit.pos2[name]) local volume = worldedit.volume(pos1, pos2) - local cmd = worldedit.registered_commands[cmd_name] + local cmd = wea_c.fetch_command_def(cmd_name) -- Note that we don't need to check for //multi privileges, as it does it at runtime if not minetest.check_player_privs(name, cmd.privs) then return false, "Error: Your privileges are unsufficient to run '"..cmd_name.."'." diff --git a/worldeditadditions_commands/doc/init.lua b/worldeditadditions_commands/doc/init.lua index 9ab9a55..f6b9f0d 100644 --- a/worldeditadditions_commands/doc/init.lua +++ b/worldeditadditions_commands/doc/init.lua @@ -11,7 +11,7 @@ -- The strategy here is not to have duplicate content, but to pull data from -- existing sources. -- Long-form article descriptions: Chat-Command-Reference.md --- Short descriptions: Undecided, but maybe worldedit.registered_commands +-- Short descriptions: Undecided, but maybe from the registered command definition? worldeditadditions.doc = {} diff --git a/worldeditadditions_commands/mod.conf b/worldeditadditions_commands/mod.conf index 3b12d2d..c21809d 100644 --- a/worldeditadditions_commands/mod.conf +++ b/worldeditadditions_commands/mod.conf @@ -1,4 +1,4 @@ name = worldeditadditions_commands description = worldeditadditions: chat command interfaces -depends = worldeditadditions, worldedit_commands, worldedit_shortcommands, worldedit +depends = worldeditadditions, worldeditadditions_core, worldedit_commands, worldedit_shortcommands, worldedit optional_depends = worldeditdebug, bonemeal diff --git a/worldeditadditions_core/core/fetch_command_def.lua b/worldeditadditions_core/core/fetch_command_def.lua new file mode 100644 index 0000000..d77da5b --- /dev/null +++ b/worldeditadditions_core/core/fetch_command_def.lua @@ -0,0 +1,20 @@ + + +--- Fetches the definition of a WorldEditAdditions or WorldEdit command +-- Does not support fetching generic Minetest commands - check +-- minetest.chatcommands for this. +-- @param cmdname string The name of the command to fetch the definition for. +local function fetch_command_def(cmdname) + local wea_c = worldeditadditions_core + + if wea_c.registered_commands[cmdname] then + return wea_c.registered_commands[cmdname] + end + if minetest.global_exists("worldedit") and worldedit.registered_commands and worldedit.registered_commands[cmdname] then + return worldedit.registered_commands[cmdname] + end + + return nil +end + +return fetch_command_def diff --git a/worldeditadditions_core/core/register_command.lua b/worldeditadditions_core/core/register_command.lua index e7525ae..e481848 100644 --- a/worldeditadditions_core/core/register_command.lua +++ b/worldeditadditions_core/core/register_command.lua @@ -5,15 +5,14 @@ -- ██ ██ ███████ ██████ ██ ███████ ██ ███████ ██ ██ -- WorldEditAdditions chat command registration -local modpath = minetest.get_modpath("worldeditadditions_core") -local run_command = dofile(modpath.."/core/run_command.lua") +local wea_c = worldeditadditions_core +local run_command = dofile(wea_c.modpath.."/core/run_command.lua") local function log_error(cmdname, error_message) minetest.log("error", "register_command("..cmdname..") error: "..error_message) end local function register_command(cmdname, options) - local we_c = worldeditadditions_core --- -- 1: Validation diff --git a/worldeditadditions_core/core/run_command.lua b/worldeditadditions_core/core/run_command.lua index 71dd4bc..f6d48e5 100644 --- a/worldeditadditions_core/core/run_command.lua +++ b/worldeditadditions_core/core/run_command.lua @@ -1,7 +1,7 @@ -- WARNING: safe_region MUST NOT be imported more than once, as it defines chat commands. If you want to import it again elsewhere, check first that multiple dofile() calls don't execute a file more than once. -local modpath = minetest.get_modpath("worldeditadditions_core") -local safe_region = dofile(modpath.."/core/safe_region.lua") -local human_size = dofile(modpath.."/core/lib/human_size.lua") +local wea_c = worldeditadditions_core +local safe_region = dofile(wea_c.modpath.."/core/safe_region.lua") +local human_size = dofile(wea_c.modpath.."/core/lib/human_size.lua") -- TODO: Reimplement worldedit.player_notify(player_name, msg_text) @@ -14,7 +14,6 @@ local function run_command_stage2(player_name, func, parse_result) end local function run_command(cmdname, options, player_name, paramtext) - local we_c = worldeditadditions_core if options.require_pos > 0 and not worldedit.pos1[player_name] then worldedit.player_notify(player_name, "Error: pos1 must be selected to use this command.") return false @@ -34,9 +33,9 @@ local function run_command(cmdname, options, player_name, paramtext) if options.nodes_needed then local potential_changes = options.nodes_needed(player_name, unpack(parse_result)) - local limit = we_c.safe_region_limit_default - if we_c.safe_region_limits[player_name] then - limit = we_c.safe_region_limits[player_name] + local limit = wea_c.safe_region_limit_default + if wea_c.safe_region_limits[player_name] then + limit = wea_c.safe_region_limits[player_name] end if potential_changes > limit then diff --git a/worldeditadditions_core/init.lua b/worldeditadditions_core/init.lua index 19938c2..9a77be7 100644 --- a/worldeditadditions_core/init.lua +++ b/worldeditadditions_core/init.lua @@ -20,15 +20,16 @@ worldeditadditions_core = { safe_region_limits = {}, -- The default limit for new players on the number of potential nodes changed before safe_region kicks in. safe_region_limit_default = 100000, - register_command = dofile(modpath.."/core/register_command.lua") } +local wea_c = worldeditadditions_core +wea_c.register_command = dofile(modpath.."/core/register_command.lua") +wea_c.fetch_command_def = dofile(modpath.."/core/fetch_command_def.lua") -local we_c = worldeditadditions_core -- Initialise WorldEdit stuff if the WorldEdit mod is not present if minetest.global_exists("worldedit") then - dofile(we_c.modpath.."/core/integrations/worldedit.lua") + dofile(wea_c.modpath.."/core/integrations/worldedit.lua") else - dofile(we_c.modpath.."/core/integrations/noworldedit.lua") + dofile(wea_c.modpath.."/core/integrations/noworldedit.lua") end