]> git.lizzy.rs Git - worldedit.git/commitdiff
Implement //cubeapply
authorsfan5 <sfan5@live.de>
Mon, 6 Apr 2020 21:32:11 +0000 (23:32 +0200)
committersfan5 <sfan5@live.de>
Mon, 6 Apr 2020 21:32:11 +0000 (23:32 +0200)
ChatCommands.md
worldedit_commands/cuboid.lua

index 528f2dfc02280c0974ea1a3b3b819e74202252fb..3cd349c86b434205abc2aa5f7a7b13ee938a31bf 100644 (file)
@@ -476,3 +476,12 @@ Note that this functionality requires the `worldedit_brush` mod enabled.
                //brush cube 8 8 8 Cobblestone\r
                //brush spr 12 glass\r
                //brush none\r
+\r
+### `//cubeapply <size> <command> [parameters]`\r
+\r
+Selects a cube with side length of `<size>` around the WorldEdit position 1 and runs the given `<command>` on the newly selected region.\r
+This is mostly useful for brushes since it allows commands such as `//replace` to be ran, but it can also be used standalone.\r
+\r
+               //cubeapply 10 replaceinverse air default:water_source\r
+               //brush cubeapply 15 drain\r
+               //brush cubeapply 1 deleteblocks\r
index d12ace8ff85a2f6059c57e582515e8c92d88bdd1..758ecd8e54a23672aa8abc0e23393ff4fa7fc70d 100644 (file)
@@ -207,3 +207,49 @@ worldedit.register_command("contract", {
                return true, "Region contracted by " .. (amount + rev_amount) .. " nodes"
        end,
 })
+
+worldedit.register_command("cubeapply", {
+       params = "<size> <command> [parameters]",
+       description = "Select a cube with side length <size> around position 1 and run <command> on region",
+       privs = {worldedit=true},
+       require_pos = 1,
+       parse = function(param)
+               local found, _, side_length, cmd, args = param:find("^(%d+)%s+([^%s]+)%s*(.*)$")
+               if found == nil then
+                       return false
+               end
+               local cmddef = worldedit.registered_commands[cmd]
+               if cmddef == nil or cmddef.require_pos ~= 2 then
+                       return false, "invalid usage: //" .. cmd .. " cannot be used with cubeapply"
+               end
+               -- run parsing of target command
+               local parsed = {cmddef.parse(args)}
+               if not table.remove(parsed, 1) then
+                       return false, parsed[1]
+               end
+               return true, tonumber(side_length), cmd, parsed
+       end,
+       nodes_needed = function(name, side_length, cmd, parsed)
+               -- its not possible to defer to the target command at this point
+               return side_length * side_length * side_length
+       end,
+       func = function(name, side_length, cmd, parsed)
+               local cmddef = assert(worldedit.registered_commands[cmd])
+               local success, missing_privs = minetest.check_player_privs(name, cmddef.privs)
+               if not success then
+                       worldedit.player_notify(name, "Missing privileges: " ..
+                               table.concat(missing_privs, ", "))
+                       return
+               end
+
+               -- update region to be the cube the user wanted
+               local sizea, sizeb = math.floor(side_length / 2), math.ceil(side_length / 2)
+               local center = worldedit.pos1[name]
+               worldedit.pos1[name] = vector.subtract(center, sizea)
+               worldedit.pos2[name] = vector.add(center, sizeb)
+               worldedit.marker_update(name)
+
+               -- actually run target command
+               return cmddef.func(name, unpack(parsed))
+       end,
+})