From 5757ef892dbb340be6d281405ba389a5ab569d5b Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Wed, 28 Jul 2021 18:13:15 -0700 Subject: [PATCH] added //macro --- .../commands/meta/init.lua | 1 + .../commands/meta/macro.lua | 122 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 worldeditadditions_commands/commands/meta/macro.lua diff --git a/worldeditadditions_commands/commands/meta/init.lua b/worldeditadditions_commands/commands/meta/init.lua index 82b9a65..536fb43 100644 --- a/worldeditadditions_commands/commands/meta/init.lua +++ b/worldeditadditions_commands/commands/meta/init.lua @@ -11,6 +11,7 @@ local we_cm = worldeditadditions_commands.modpath .. "/commands/meta/" dofile(we_cm.."airapply.lua") dofile(we_cm.."ellipsoidapply.lua") dofile(we_cm.."for.lua") +dofile(we_cm.."macro.lua") dofile(we_cm.."many.lua") dofile(we_cm.."multi.lua") dofile(we_cm.."subdivide.lua") diff --git a/worldeditadditions_commands/commands/meta/macro.lua b/worldeditadditions_commands/commands/meta/macro.lua new file mode 100644 index 0000000..435d45a --- /dev/null +++ b/worldeditadditions_commands/commands/meta/macro.lua @@ -0,0 +1,122 @@ +-- ███ ███ █████ ██████ ██████ ██████ +-- ████ ████ ██ ██ ██ ██ ██ ██ ██ +-- ██ ████ ██ ███████ ██ ██████ ██ ██ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- ██ ██ ██ ██ ██████ ██ ██ ██████ +local wea = worldeditadditions +local v3 = worldeditadditions.Vector3 +local function step(params) + -- Initialize additional params on first call + if not params.first then + params.i = 1 -- Iteration number + params.time = 0 -- Total execution time + params.first = true + end + + -- Load current command string to use + local command, args = params.commands[params.i]:match("/([^%s]+)%s*(.*)$") + if not args then args = "" + else args = wea.trim(args) end + -- Get command and test privs + local cmd = minetest.chatcommands[command] + if not cmd then + return false, "Error: "..command.." isn't a valid command." + end + if not minetest.check_player_privs(params.player_name, cmd.privs) then + return false, "Your privileges are insufficient to run /\""..command.."\"." + end + + -- Start a timer + local start_time = wea.get_ms_time() + -- Execute command + cmd.func(params.player_name, args) + -- Finish timer and add to total + params.time = params.time + wea.get_ms_time() - start_time + -- Increment iteration state + params.i = params.i + 1 + + if params.i <= #params.commands then + -- If we haven't run out of values call function again + minetest.after(0, step, params) + else + worldedit.player_notify(params.player_name, "The macro \"".. + params.file.."\" was completed in " .. + wea.format.human_time(params.time)) + end +end + +worldedit.register_command("macro", { + params = "", + description = "Load commands from \"(world folder)/macros/[.weamac | .wmac]\" with position 1 of the current WorldEdit region as the origin", + privs = {worldedit=true}, + require_pos = 0, + parse = function(params_text) + if params_text == "" then + return false + end + if params_text:match("[!\"#%%&'%(%)%*%+,/:;<=>%?%[\\]%^`{|}]") then + return false, "Disallowed file name: " .. params_text + end + return true, wea.trim(params_text) + end, + func = function(name, file_name) + if not worldedit.pos1[name] then + worldedit.pos1[name] = v3.add(wea.player_vector(name), v3.new(0.5,-0.5,0.5)):floor() + worldedit.mark_pos1(name) + end + worldedit.pos2[name] = worldedit.pos1[name] + + -- Find the file in the world path + local testpaths = { + minetest.get_worldpath() .. "/macros/" .. file_name, + minetest.get_worldpath() .. "/macros/" .. file_name .. ".weamac", + minetest.get_worldpath() .. "/macros/" .. file_name .. ".wmac", + } + local file, err + for index, path in ipairs(testpaths) do + file, err = io.open(path, "rb") + if not err then break end + end + -- Check if file exists + if err then + return false, "Error: File \"" .. file_name .. "\" does not exist or is corrupt." + end + local value = file:read("*a") + file:close() + + step({ + player_name = name, + file = file_name:match("^[^%.]+"), + commands = wea.split(value,"[\n\r]+") + }) + + end, +}) + +-- Make default macro +local function default_macro() + local path = minetest.get_worldpath() .. "/macros" + -- Create directory if it does not already exist + minetest.mkdir(path) + local writer, err = io.open(path.."/fixlight.weamac", "ab") + if not writer then return false end + writer:write("//multi //1 //2 //outset 50 //fixlight //y") + writer:flush() + writer:close() + return true +end + +-- Check for default macro +local function chk_default_macro() + local path = minetest.get_worldpath() .. "/macros/fixlight.weamac" + local file, err = io.open(path, "rb") + if err then return false + else + file:close() + return true + end +end + +if not chk_default_macro() then + default_macro() +end