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.
This commit is contained in:
Starbeamrainbowlabs 2022-05-19 02:31:01 +01:00
parent c60b5c5bad
commit b816133716
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
11 changed files with 47 additions and 25 deletions

View file

@ -1,4 +1,4 @@
name = worldeditadditions name = worldeditadditions
description = Extra tools and commands to extend WorldEdit for Minetest description = Extra tools and commands to extend WorldEdit for Minetest
depends = worldedit depends = worldedit
optional_depends = bonemeal, cool_trees, default, moretrees, ethereal optional_depends = worldeditadditions_core, bonemeal, cool_trees, default, moretrees, ethereal

View file

@ -3,7 +3,7 @@
-- ███████ ██ ██████ ███████ ██████ ██████ ██ ████ -- ███████ ██ ██████ ███████ ██████ ██████ ██ ████
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ███████ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ███████ ██
local wea_c = worldeditadditions_core
worldedit.register_command("airapply", { worldedit.register_command("airapply", {
params = "<command_name> <args>", params = "<command_name> <args>",
@ -20,7 +20,7 @@ worldedit.register_command("airapply", {
end end
-- Note that we search the worldedit commands here, not the minetest ones -- 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 if cmd_we == nil then
return false, "Error: "..cmd_name.." isn't a valid command." return false, "Error: "..cmd_name.." isn't a valid command."
end end

View file

@ -3,6 +3,7 @@
-- █████ ██ ██ ██ ██████ ███████ █████ ███████ ██████ ██████ ██ ████ -- █████ ██ ██ ██ ██████ ███████ █████ ███████ ██████ ██████ ██ ████
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ███████ ███████ ███████ ██ ██ ███████ ███████ ██ ██ ██ ██ ███████ ██ -- ███████ ███████ ███████ ██ ██ ███████ ███████ ██ ██ ██ ██ ███████ ██
local wea_c = worldeditadditions_core
worldedit.register_command("ellipsoidapply", { worldedit.register_command("ellipsoidapply", {
params = "<command_name> <args>", params = "<command_name> <args>",
@ -21,7 +22,7 @@ worldedit.register_command("ellipsoidapply", {
-- print("cmd_name", cmd_name, "args_text", args_text) -- print("cmd_name", cmd_name, "args_text", args_text)
-- Note that we search the worldedit commands here, not the minetest ones -- 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 if cmd_we == nil then
return false, "Error: "..cmd_name.." isn't a valid command." return false, "Error: "..cmd_name.." isn't a valid command."
end end

View file

@ -3,6 +3,7 @@
-- ██ ██ ██ ██ ██ ██ ███████ █████ ███████ ██████ ██████ ██ ████ █████ ██ ██ -- ██ ██ ██ ██ ██ ██ ███████ █████ ███████ ██████ ██████ ██ ████ █████ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ████ ██████ ██ ███████ ███████ ██ ██ ██ ██ ███████ ██ ███████ ██████ -- ██ ████ ██████ ██ ███████ ███████ ██ ██ ██ ██ ███████ ██ ███████ ██████
local wea_c = worldeditadditions_core
worldedit.register_command("noiseapply2d", { worldedit.register_command("noiseapply2d", {
@ -19,7 +20,7 @@ worldedit.register_command("noiseapply2d", {
end end
-- Note that we search the worldedit commands here, not the minetest ones -- 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 if cmd_we == nil then
return false, "Error: "..cmd_name.." isn't a valid command." return false, "Error: "..cmd_name.." isn't a valid command."
end end

View file

@ -1,11 +1,12 @@
local wea = worldeditadditions local wea = worldeditadditions
local wea_c = worldeditadditions_core
-- Test command: -- Test command:
-- //multi //fp set1 1330 60 5455 //fp set2 1355 35 5430 //subdivide 10 10 10 fixlight //y -- //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) 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 = wea_c.fetch_command_def(cmd_name)
local def = worldedit.registered_commands[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 if not def.parse then return nil, "Error: No parse method found (this is a bug)." end
local parsed = {def.parse(args)} local parsed = {def.parse(args)}
@ -57,7 +58,7 @@ worldedit.register_command("subdivide", {
local cmd_name = parts[4] 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)." return false, "Error: The worldedit command '"..parts[4].."' does not exist (try /help)."
end end
@ -73,7 +74,7 @@ worldedit.register_command("subdivide", {
local pos1, pos2 = worldedit.sort_pos(worldedit.pos1[name], worldedit.pos2[name]) local pos1, pos2 = worldedit.sort_pos(worldedit.pos1[name], worldedit.pos2[name])
local volume = worldedit.volume(pos1, pos2) 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 -- 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 if not minetest.check_player_privs(name, cmd.privs) then
return false, "Error: Your privileges are unsufficient to run '"..cmd_name.."'." return false, "Error: Your privileges are unsufficient to run '"..cmd_name.."'."

View file

@ -11,7 +11,7 @@
-- The strategy here is not to have duplicate content, but to pull data from -- The strategy here is not to have duplicate content, but to pull data from
-- existing sources. -- existing sources.
-- Long-form article descriptions: Chat-Command-Reference.md -- 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 = {} worldeditadditions.doc = {}

View file

@ -1,4 +1,4 @@
name = worldeditadditions_commands name = worldeditadditions_commands
description = worldeditadditions: chat command interfaces 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 optional_depends = worldeditdebug, bonemeal

View file

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

View file

@ -5,15 +5,14 @@
-- ██ ██ ███████ ██████ ██ ███████ ██ ███████ ██ ██ -- ██ ██ ███████ ██████ ██ ███████ ██ ███████ ██ ██
-- WorldEditAdditions chat command registration -- WorldEditAdditions chat command registration
local modpath = minetest.get_modpath("worldeditadditions_core") local wea_c = worldeditadditions_core
local run_command = dofile(modpath.."/core/run_command.lua") local run_command = dofile(wea_c.modpath.."/core/run_command.lua")
local function log_error(cmdname, error_message) local function log_error(cmdname, error_message)
minetest.log("error", "register_command("..cmdname..") error: "..error_message) minetest.log("error", "register_command("..cmdname..") error: "..error_message)
end end
local function register_command(cmdname, options) local function register_command(cmdname, options)
local we_c = worldeditadditions_core
--- ---
-- 1: Validation -- 1: Validation

View file

@ -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. -- 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 wea_c = worldeditadditions_core
local safe_region = dofile(modpath.."/core/safe_region.lua") local safe_region = dofile(wea_c.modpath.."/core/safe_region.lua")
local human_size = dofile(modpath.."/core/lib/human_size.lua") local human_size = dofile(wea_c.modpath.."/core/lib/human_size.lua")
-- TODO: Reimplement worldedit.player_notify(player_name, msg_text) -- TODO: Reimplement worldedit.player_notify(player_name, msg_text)
@ -14,7 +14,6 @@ local function run_command_stage2(player_name, func, parse_result)
end end
local function run_command(cmdname, options, player_name, paramtext) 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 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.") worldedit.player_notify(player_name, "Error: pos1 must be selected to use this command.")
return false return false
@ -34,9 +33,9 @@ local function run_command(cmdname, options, player_name, paramtext)
if options.nodes_needed then if options.nodes_needed then
local potential_changes = options.nodes_needed(player_name, unpack(parse_result)) local potential_changes = options.nodes_needed(player_name, unpack(parse_result))
local limit = we_c.safe_region_limit_default local limit = wea_c.safe_region_limit_default
if we_c.safe_region_limits[player_name] then if wea_c.safe_region_limits[player_name] then
limit = we_c.safe_region_limits[player_name] limit = wea_c.safe_region_limits[player_name]
end end
if potential_changes > limit then if potential_changes > limit then

View file

@ -20,15 +20,16 @@ worldeditadditions_core = {
safe_region_limits = {}, safe_region_limits = {},
-- The default limit for new players on the number of potential nodes changed before safe_region kicks in. -- The default limit for new players on the number of potential nodes changed before safe_region kicks in.
safe_region_limit_default = 100000, 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 -- Initialise WorldEdit stuff if the WorldEdit mod is not present
if minetest.global_exists("worldedit") then if minetest.global_exists("worldedit") then
dofile(we_c.modpath.."/core/integrations/worldedit.lua") dofile(wea_c.modpath.."/core/integrations/worldedit.lua")
else else
dofile(we_c.modpath.."/core/integrations/noworldedit.lua") dofile(wea_c.modpath.."/core/integrations/noworldedit.lua")
end end