From ac05627d5377174b50861ad3da8bc89688b1b2c6 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 14 Oct 2018 00:19:32 +0100 Subject: [PATCH] Add `//multi` command. TODO: Documentation. Perhaps it should be it's own mod? --- README.md | 1 + worldeditadditions/overlay.lua | 2 +- worldeditadditions_commands/depends.txt | 1 + worldeditadditions_commands/init.lua | 2 + worldeditadditions_commands/multi.lua | 62 +++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 worldeditadditions_commands/multi.lua diff --git a/README.md b/README.md index 26768f7..c4c2068 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ If you can dream of it, it probably belongs here! - `//hollowellipsoid ` - `//torus ` - `//hollowtorus ` + - `//multi .....` ### `//floodfill [ []]` Floods all connected nodes of the same type starting at _pos1_ with (which defaults to `water_source`), in a sphere with a radius of (which defaults to 50). diff --git a/worldeditadditions/overlay.lua b/worldeditadditions/overlay.lua index 6b6a9e1..8916f45 100644 --- a/worldeditadditions/overlay.lua +++ b/worldeditadditions/overlay.lua @@ -1,4 +1,4 @@ ---- Overlap command. Places a specified node on top of +--- Overlap command. Places a specified node on top of each column. -- @module worldeditadditions.overlay function worldedit.overlay(pos1, pos2, target_node) diff --git a/worldeditadditions_commands/depends.txt b/worldeditadditions_commands/depends.txt index 776fdb5..a81bd47 100644 --- a/worldeditadditions_commands/depends.txt +++ b/worldeditadditions_commands/depends.txt @@ -1,2 +1,3 @@ worldeditadditions worldedit_commands +worldedit diff --git a/worldeditadditions_commands/init.lua b/worldeditadditions_commands/init.lua index 1f50a97..5aff416 100644 --- a/worldeditadditions_commands/init.lua +++ b/worldeditadditions_commands/init.lua @@ -5,6 +5,8 @@ -- @license Mozilla Public License, 2.0 -- @author Starbeamrainbowlabs +dofile(minetest.get_modpath("worldeditadditions_commands").."/multi.lua") + local safe_region, check_region, reset_pending = dofile(minetest.get_modpath("worldedit_commands") .. "/safe.lua") diff --git a/worldeditadditions_commands/multi.lua b/worldeditadditions_commands/multi.lua new file mode 100644 index 0000000..2cba990 --- /dev/null +++ b/worldeditadditions_commands/multi.lua @@ -0,0 +1,62 @@ +--- Executes multiple worldedit commands in sequence. +-- @module worldeditadditions.multi + +-- explode(seperator, string) +-- From http://lua-users.org/wiki/SplitJoin +function explode(delim, str) + local ll, is_done + local delim_length = string.len(delim) + ll = 0 + is_done = false + + return function() + if is_done then return end + + local result = nil + local loc = string.find(str, delim, ll, true) -- find the next d in the string + if loc ~= nil then -- if "not not" found then.. + result = string.sub(str, ll, loc - 1) -- Save it in our array. + ll = loc + delim_length -- save just after where we found it for searching next time. + else + result = string.sub(str, ll) -- Save what's left in our array. + is_done = true + end + + return result + end +end + +-- From http://lua-users.org/wiki/StringTrim +function trim(s) + return (s:gsub("^%s*(.-)%s*$", "%1")) +end + +minetest.register_chatcommand("/multi", { + params = "// // .....", + description = "Executes multiple chat commands in sequence.", + privs = { worldedit = true }, + func = function(name, params_text) + -- Things start at 1, not 0 in Lua :-( + local i = 0 + for command in explode(" /", string.sub(params_text, 2)) do + local found, _, command_name, args = command:find("^([^%s]+)%s(.+)$") + if not found then command_name = command end + command_name = trim(command_name) + + worldedit.player_notify(name, "Executing #"..i..": "..command.." ("..command_name..")") + + local cmd = minetest.chatcommands[command_name] + if not cmd then + return false, "Error: "..command_name.." isn't a valid command." + end + if not minetest.check_player_privs(name, cmd.privs) then + return false, "Your privileges are insufficient." + end + + minetest.log("action", name.." runs "..command) + cmd.func(name, args) + + i = i + 1 + end + end +})