-- WorldEdit for MinecraftPE, written against the Lua mod API (see ../README.md). -- Command names, arguments, flags and behaviour follow EngineHub's WorldEdit -- (https://worldedit.enginehub.org/en/latest/commands/). Everything is typed -- with the usual double slash: "//wand", "//set stone", "/undo", ... local W = require("lib") local floor, ceil, abs, min, max = math.floor, math.ceil, math.abs, math.min, math.max local get_block = mc.world.get_block local WAND = mc.item.wooden_axe local NAVWAND = mc.item.compass local PICKAXES = { [270] = true, [274] = true, [257] = true, [278] = true, [285] = true } -- ----------------------------------------------------------------- helpers -- Names starting with "/" are typed with two slashes (//set). Commands that WorldEdit also -- offers with a single slash (/undo, /tool, ...) are registered under both spellings. local SINGLE = {} for name in ("unstuck ! ascend asc descend desc ceil thru jumpto j up toggleeditwand undo redo clearhistory " .. "tool none mask material mat range superpickaxe pickaxe sp brush br fixlava fixwater removeabove " .. "removebelow removenear replacenear snow thaw green extinguish ex ext butcher toggleplace schematic " .. "schem clearclipboard ;"):gmatch("%S+") do SINGLE[name] = true end local function reg(names, desc, fn) if type(names) == "string" then names = { names } end for _, n in ipairs(names) do mc.command(n, { description = desc, op = true }, fn) local bare = n:match("^/(.+)$") if bare and SINGLE[bare] then mc.command(bare, { description = desc, op = true }, fn) end end end local function fmt(p) return string.format("(%d, %d, %d)", p[1], p[2], p[3]) end local function say(ctx, ...) ctx.player:say(...) end local function selection(ctx) local s = W.session(ctx.player) local r = W.region(s) if not r then say(ctx, "Make a region selection first (//wand, //pos1, //pos2).") return s, nil end return s, r end local function describe_pos(s, which, p) local msg = string.format("Position %d set to %s", which, fmt(p)) local r = W.region(s) if r then msg = msg .. " (" .. W.volume(r) .. " blocks)" end return msg .. "." end local function set_pos(player, which, p) local s = W.session(player) s[which == 1 and "pos1" or "pos2"] = p player:say(describe_pos(s, which, p)) end --- Runs `fn(edit)` as one undoable edit and reports the number of changed blocks. local function edit_session(ctx, fn, noun) local s = W.session(ctx.player) local edit = W.new_edit(s) local ok, err = pcall(fn, edit, s) local n = edit:commit() if not ok then say(ctx, "Error: " .. tostring(err)) return n end if edit.overflow then say(ctx, "Block change limit reached (" .. s.limit .. "), use //limit.") end say(ctx, n .. " " .. (noun or "blocks") .. " changed.") return n end local function pattern_or_err(ctx, text) local p, err = W.pattern(text) if not p then say(ctx, err) end return p end local function num(text, default) local n = tonumber(text) return n or default end local function player_block(ctx) return ctx.player:block_pos() end local function radii(text) local list = {} for part in tostring(text):gmatch("[^,]+") do list[#list + 1] = tonumber(part) end return list end -- ----------------------------------------------------------------- help local help_lines = { "Navigation: /unstuck /ascend /descend /ceil /thru /jumpto /up", "Selection: //wand //pos1 //pos2 //hpos1 //hpos2 //chunk //sel //expand //contract //shift //outset //inset //size //count //distr", "Region: //set //replace //overlay //center //naturalize //walls //faces //smooth //move //stack //hollow //line", "Generation: //sphere //hsphere //cyl //hcyl //cone //pyramid //hpyramid", "Clipboard: //copy //cut //paste //rotate //flip /clearclipboard /schematic", "History: /undo /redo /clearhistory", "Tools: /tool /brush /mask /material /range /size // (super pickaxe)", "Utility: //fill //fillr //drain /fixlava /fixwater /removeabove /removebelow /removenear /replacenear", " /snow /thaw /green /extinguish /butcher //limit /toggleplace /we", } reg({ "/help" }, "Shows the WorldEdit commands", function(ctx) for _, l in ipairs(help_lines) do say(ctx, l) end end) reg({ "we", "worldedit" }, "WorldEdit information", function(ctx) local sub = ctx.args[1] if sub == "version" or sub == "ver" or not sub then say(ctx, "WorldEdit (MinecraftPE Lua port) 1.0, based on the EngineHub WorldEdit command set.") else say(ctx, "Usage: /we version") end end) reg("/limit", "Sets the block change limit", function(ctx) local s = W.session(ctx.player) if ctx.args[1] then s.limit = tonumber(ctx.args[1]) or s.limit end say(ctx, "Block change limit: " .. (s.limit < 0 and "unlimited" or s.limit)) end) -- ------------------------------------------------------------- navigation local function free(x, y, z) local id = get_block(x, y, z) return id == 0 or id == 50 or id == 31 or id == 37 or id == 38 or id == 78 or id == 6 end local function standable(x, y, z) return not free(x, y - 1, z) and not W.is_liquid(get_block(x, y - 1, z)) and free(x, y, z) and free(x, y + 1, z) end reg({ "/unstuck", "/!" }, "Escape from being stuck inside a block", function(ctx) local x, y, z = player_block(ctx) for dy = 0, 127 - y do if free(x, y + dy, z) and free(x, y + dy + 1, z) then ctx.player:teleport(x + 0.5, y + dy, z + 0.5) return "There you go!" end end return "Nowhere to go." end) local function levels_arg(ctx) return max(1, floor(num(ctx.args[1], 1))) end reg({ "/ascend", "/asc" }, "Go up a floor", function(ctx) local x, y, z = player_block(ctx) local levels, found = levels_arg(ctx), 0 local cy, last = y, nil for yy = y + 1, 126 do if standable(x, yy, z) and not standable(x, yy - 1, z) and (not free(x, yy - 1, z) or yy > cy + 1) then found = found + 1 last = yy if found >= levels then break end end end if not last then return "No free spot above you found." end ctx.player:teleport(x + 0.5, last, z + 0.5) return "Ascended " .. found .. " level" .. (found > 1 and "s" or "") .. "." end) reg({ "/descend", "/desc" }, "Go down a floor", function(ctx) local x, y, z = player_block(ctx) local levels, found, last = levels_arg(ctx), 0, nil for yy = y - 1, 1, -1 do if standable(x, yy, z) then found = found + 1 last = yy if found >= levels then break end end end if not last then return "No free spot below you found." end ctx.player:teleport(x + 0.5, last, z + 0.5) return "Descended " .. found .. " level" .. (found > 1 and "s" or "") .. "." end) reg("/ceil", "Go to the ceiling", function(ctx) local flags, pos = W.parse(ctx.args) local clearance = max(0, floor(num(pos[1], 0))) local x, y, z = player_block(ctx) for yy = 126, y + 1, -1 do if not free(x, yy + 1, z) and free(x, yy, z) and free(x, yy - 1, z) then local ty = yy - 1 - clearance if ty > y then ctx.player:teleport(x + 0.5, ty, z + 0.5) if flags.g then mc.world.set_block(x, ty - 1, z, 20, 0) end return "Whoosh!" end end end return "No free spot above you found." end) reg("/thru", "Pass through walls", function(ctx) local x, y, z = ctx.player:pos() local dir, dx, dz = ctx.player:facing() local yaw, pitch = ctx.player:look() local vy = pitch > 50 and -1 or (pitch < -50 and 1 or 0) if vy ~= 0 then dx, dz = 0, 0 end local bx, by, bz = floor(x), floor(y), floor(z) local inWall = false for i = 1, 100 do local cx, cy, cz = bx + dx * i, by + vy * i, bz + dz * i if not free(cx, cy, cz) or not free(cx, cy + 1, cz) then inWall = true elseif inWall and free(cx, cy, cz) and free(cx, cy + 1, cz) then ctx.player:teleport(cx + 0.5, cy, cz + 0.5) return "Whoosh!" end end return "No free spot ahead of you found." end) reg({ "/jumpto", "/j" }, "Teleport to the targeted location", function(ctx) local x, y, z = ctx.player:target(300) if not x then return "No block in sight!" end ctx.player:teleport(x + 0.5, y + 1, z + 0.5) return "Poof!" end) reg("/up", "Go upwards some distance", function(ctx) local flags, pos = W.parse(ctx.args) local dist = floor(num(pos[1], 0)) if dist <= 0 then return "Usage: /up " end local x, y, z = player_block(ctx) local ty = min(126, y + dist) ctx.player:teleport(x + 0.5, ty, z + 0.5) mc.world.set_block(x, ty - 1, z, 20, 0) return "Whoosh!" end) -- --------------------------------------------------------------- selection reg("/wand", "Gives the wand item", function(ctx) local flags = W.parse(ctx.args) if flags.n then ctx.player:give(NAVWAND, 1) return "Navigation wand given (left click: /jumpto, right click: /thru)." end ctx.player:give(WAND, 1) return "Left click: select pos #1; Right click: select pos #2" end) reg("/toggleeditwand", "Toggles the wand function", function(ctx) local s = W.session(ctx.player) s.wand_off = not s.wand_off return "Edit wand " .. (s.wand_off and "disabled." or "enabled.") end) local function parse_coords(text) if not text then return nil end local c = {} for n in text:gmatch("[^,]+") do c[#c + 1] = tonumber(n) end if #c == 3 then return { floor(c[1]), floor(c[2]), floor(c[3]) } end end local function pos_cmd(which) return function(ctx) local p = parse_coords(ctx.args[1]) if not p then local x, y, z = player_block(ctx) p = { x, y, z } end set_pos(ctx.player, which, p) end end reg("/pos1", "Set position 1", pos_cmd(1)) reg("/pos2", "Set position 2", pos_cmd(2)) reg("/pos", "Set positions", function(ctx) local x, y, z = player_block(ctx) set_pos(ctx.player, 1, { x, y, z }) end) local function hpos_cmd(which) return function(ctx) local x, y, z = ctx.player:target(300) if not x then return "No block in sight!" end set_pos(ctx.player, which, { x, y, z }) end end reg("/hpos1", "Set position 1 to targeted block", hpos_cmd(1)) reg("/hpos2", "Set position 2 to targeted block", hpos_cmd(2)) reg("/chunk", "Select the chunk you are in", function(ctx) local flags, pos = W.parse(ctx.args) local s = W.session(ctx.player) local x, _, z = player_block(ctx) local cx, cz = floor(x / 16), floor(z / 16) if pos[1] then local c = radii(pos[1]) if c[1] and c[2] then if flags.c then cx, cz = c[1], c[2] else cx, cz = floor(c[1] / 16), floor(c[2] / 16) end end end s.pos1 = { cx * 16, 0, cz * 16 } s.pos2 = { cx * 16 + 15, 127, cz * 16 + 15 } return "Chunk selected: (" .. cx .. ", " .. cz .. ")" end) local function sel_apply(s, fn) local r = W.region(s) fn(r) s.pos1 = { r.x0, r.y0, r.z0 } s.pos2 = { r.x1, r.y1, r.z1 } return r end local function move_edge(r, dir, amount) if dir[1] > 0 then r.x1 = r.x1 + amount elseif dir[1] < 0 then r.x0 = r.x0 - amount end if dir[2] > 0 then r.y1 = r.y1 + amount elseif dir[2] < 0 then r.y0 = r.y0 - amount end if dir[3] > 0 then r.z1 = r.z1 + amount elseif dir[3] < 0 then r.z0 = r.z0 - amount end r.y0, r.y1 = max(0, r.y0), min(127, r.y1) end reg("/expand", "Expand the selection area", function(ctx) local s, r = selection(ctx) if not r then return end if ctx.args[1] == "vert" or ctx.args[1] == "vertical" then s.pos1[2], s.pos2[2] = 0, 127 return "Region expanded vertically." end local amount = floor(num(ctx.args[1], 0)) local rev, dirWord = nil, ctx.args[2] if tonumber(ctx.args[2]) then rev = floor(tonumber(ctx.args[2])); dirWord = ctx.args[3] end local dir = W.direction(ctx.player, dirWord) local before = W.volume(r) sel_apply(s, function(r2) move_edge(r2, dir, amount) if rev then move_edge(r2, { -dir[1], -dir[2], -dir[3] }, rev) end end) return "Region expanded " .. (W.volume(W.region(s)) - before) .. " blocks." end) reg("/contract", "Contract the selection area", function(ctx) local s, r = selection(ctx) if not r then return end local amount = floor(num(ctx.args[1], 0)) local rev, dirWord = nil, ctx.args[2] if tonumber(ctx.args[2]) then rev = floor(tonumber(ctx.args[2])); dirWord = ctx.args[3] end local dir = W.direction(ctx.player, dirWord) local before = W.volume(r) sel_apply(s, function(r2) -- contracting moves the far edge in the direction towards the opposite one move_edge(r2, dir, -amount) if rev then move_edge(r2, { -dir[1], -dir[2], -dir[3] }, -rev) end end) return "Region contracted " .. (before - W.volume(W.region(s))) .. " blocks." end) reg("/shift", "Shift the selection area", function(ctx) local s, r = selection(ctx) if not r then return end local amount = floor(num(ctx.args[1], 0)) local dir = W.direction(ctx.player, ctx.args[2]) for _, p in ipairs({ s.pos1, s.pos2 }) do p[1], p[2], p[3] = p[1] + dir[1] * amount, p[2] + dir[2] * amount, p[3] + dir[3] * amount end return "Region shifted." end) local function outset_inset(sign) return function(ctx) local s, r = selection(ctx) if not r then return end local flags, pos = W.parse(ctx.args) local a = floor(num(pos[1], 0)) * sign sel_apply(s, function(r2) if not flags.v then r2.x0, r2.x1, r2.z0, r2.z1 = r2.x0 - a, r2.x1 + a, r2.z0 - a, r2.z1 + a end if not flags.h then r2.y0, r2.y1 = max(0, r2.y0 - a), min(127, r2.y1 + a) end end) return "Region " .. (sign > 0 and "outset." or "inset.") end end reg("/outset", "Outset the selection area", outset_inset(1)) reg("/inset", "Inset the selection area", outset_inset(-1)) reg({ "/sel", "/;", "/desel", "/deselect" }, "Choose a region selector", function(ctx) local s = W.session(ctx.player) if not ctx.args[1] or ctx.args[1] == "clear" or ctx.name == "/desel" or ctx.name == "/deselect" then s.pos1, s.pos2 = nil, nil return "Selection cleared." end if ctx.args[1] == "cuboid" then return "Cuboid: left click for point 1, right click for point 2" end return "Only the cuboid selector is available." end) reg("/size", "Get information about the selection", function(ctx) local flags = W.parse(ctx.args) local s = W.session(ctx.player) if flags.c then local c = s.clipboard if not c then return "Your clipboard is empty. Use //copy first." end return string.format("Size: %d x %d x %d\nBlocks: %d", c.w, c.h, c.l, c.w * c.h * c.l) end local _, r = selection(ctx) if not r then return end say(ctx, "Type: cuboid") say(ctx, "Position 1: " .. fmt(s.pos1)) say(ctx, "Position 2: " .. fmt(s.pos2)) say(ctx, string.format("Size: %d x %d x %d", r.x1 - r.x0 + 1, r.y1 - r.y0 + 1, r.z1 - r.z0 + 1)) say(ctx, "Number of blocks: " .. W.volume(r)) end) reg("/count", "Counts the number of blocks matching a mask", function(ctx) local _, r = selection(ctx) if not r then return end local mask, err = W.mask(ctx.args[1]) if not mask then return err or "Usage: //count " end W.load_region(r) local n = 0 W.each(r, function(x, y, z) local id, d = get_block(x, y, z) if mask(id, d) then n = n + 1 end end) return "Counted: " .. n end) reg("/distr", "Get the distribution of blocks in the selection", function(ctx) local flags = W.parse(ctx.args) local _, r = selection(ctx) if not r then return end W.load_region(r) local counts, total = {}, 0 W.each(r, function(x, y, z) local id, d = get_block(x, y, z) local key = flags.d and (id * 16 + d) or id counts[key] = (counts[key] or 0) + 1 total = total + 1 end) local list = {} for k, v in pairs(counts) do list[#list + 1] = { k, v } end table.sort(list, function(a, b) return a[2] > b[2] end) say(ctx, "# total blocks: " .. total) for i = 1, min(#list, 12) do local k = list[i][1] local name = flags.d and (mc.block.name(floor(k / 16)) .. ":" .. (k % 16)) or mc.block.name(k) say(ctx, string.format("%-7d (%.3f%%) %s", list[i][2], list[i][2] * 100 / total, name)) end end) reg("/trim", "Shrink the selection to the blocks matching a mask", function(ctx) local s, r = selection(ctx) if not r then return end local mask = W.mask(ctx.args[1] or "#existing") local nx0, ny0, nz0, nx1, ny1, nz1 = 1e9, 1e9, 1e9, -1e9, -1e9, -1e9 W.each(r, function(x, y, z) local id, d = get_block(x, y, z) if mask(id, d) then nx0, ny0, nz0 = min(nx0, x), min(ny0, y), min(nz0, z) nx1, ny1, nz1 = max(nx1, x), max(ny1, y), max(nz1, z) end end) if nx0 == 1e9 then return "No blocks match." end s.pos1, s.pos2 = { nx0, ny0, nz0 }, { nx1, ny1, nz1 } return "Region trimmed." end) -- ----------------------------------------------------------------- region reg("/set", "Set all the blocks inside the selection to a pattern", function(ctx) local _, r = selection(ctx) if not r then return end local pattern = pattern_or_err(ctx, ctx.args[1]) if not pattern then return end W.load_region(r) edit_session(ctx, function(e) W.each(r, function(x, y, z) e:place(x, y, z, pattern) end) end) end) reg({ "/replace", "/re", "/rep" }, "Replace all blocks in the selection with another", function(ctx) local _, r = selection(ctx) if not r then return end local from, to = ctx.args[1], ctx.args[2] if not to then from, to = "#existing", ctx.args[1] end local mask, err = W.mask(from) if not mask then return err or "Usage: //replace [from] " end local pattern = pattern_or_err(ctx, to) if not pattern then return end W.load_region(r) edit_session(ctx, function(e) W.each(r, function(x, y, z) local id, d = get_block(x, y, z) if mask(id, d) then e:place(x, y, z, pattern) end end) end) end) reg("/overlay", "Set a block on top of blocks in the region", function(ctx) local _, r = selection(ctx) if not r then return end local pattern = pattern_or_err(ctx, ctx.args[1]) if not pattern then return end W.load_region(r) edit_session(ctx, function(e) for x = r.x0, r.x1 do for z = r.z0, r.z1 do local top = W.top_existing(x, z, r.y0, r.y1) if top and top < 127 then e:place(x, top + 1, z, pattern) end end end end) end) reg({ "/center", "/middle" }, "Set the center block(s)", function(ctx) local _, r = selection(ctx) if not r then return end local pattern = pattern_or_err(ctx, ctx.args[1]) if not pattern then return end edit_session(ctx, function(e) local cx0, cx1 = floor((r.x0 + r.x1) / 2), ceil((r.x0 + r.x1) / 2) local cy0, cy1 = floor((r.y0 + r.y1) / 2), ceil((r.y0 + r.y1) / 2) local cz0, cz1 = floor((r.z0 + r.z1) / 2), ceil((r.z0 + r.z1) / 2) for x = cx0, cx1 do for y = cy0, cy1 do for z = cz0, cz1 do e:place(x, y, z, pattern) end end end end) end) reg("/naturalize", "3 layers of dirt on top then rock below", function(ctx) local _, r = selection(ctx) if not r then return end W.load_region(r) edit_session(ctx, function(e) for x = r.x0, r.x1 do for z = r.z0, r.z1 do local depth = 0 for y = r.y1, r.y0, -1 do local id = get_block(x, y, z) if id ~= 0 and not W.is_liquid(id) then if depth == 0 then e:set(x, y, z, 2, 0) elseif depth <= 3 then e:set(x, y, z, 3, 0) else e:set(x, y, z, 1, 0) end depth = depth + 1 else depth = 0 end end end end end) end) reg("/walls", "Build the four sides of the selection", function(ctx) local _, r = selection(ctx) if not r then return end local pattern = pattern_or_err(ctx, ctx.args[1]) if not pattern then return end W.load_region(r) edit_session(ctx, function(e) W.walls_and_faces(r, e, pattern, false) end) end) reg({ "/faces", "/outline" }, "Build the walls, ceiling, and floor of a selection", function(ctx) local _, r = selection(ctx) if not r then return end local pattern = pattern_or_err(ctx, ctx.args[1]) if not pattern then return end W.load_region(r) edit_session(ctx, function(e) W.walls_and_faces(r, e, pattern, true) end) end) reg("/hollow", "Hollows out the object contained in this selection", function(ctx) local _, r = selection(ctx) if not r then return end local flags, pos = W.parse(ctx.args) local thickness = max(1, floor(num(pos[1], 1))) local fill = pattern_or_err(ctx, pos[2] or "air") if not fill then return end W.load_region(r) local function inside(x, y, z) return x >= r.x0 and x <= r.x1 and y >= r.y0 and y <= r.y1 and z >= r.z0 and z <= r.z1 end local N = { { 1, 0, 0 }, { -1, 0, 0 }, { 0, 1, 0 }, { 0, -1, 0 }, { 0, 0, 1 }, { 0, 0, -1 } } local keep, frontier = {}, {} local function key(x, y, z) return (x - r.x0) + (z - r.z0) * 4096 + (y * 16777216) end W.each(r, function(x, y, z) if get_block(x, y, z) ~= 0 then for _, n in ipairs(N) do local nx, ny, nz = x + n[1], y + n[2], z + n[3] if not inside(nx, ny, nz) or get_block(nx, ny, nz) == 0 then keep[key(x, y, z)] = true frontier[#frontier + 1] = { x, y, z } break end end end end) for _ = 2, thickness do local nextFrontier = {} for _, p in ipairs(frontier) do for _, n in ipairs(N) do local nx, ny, nz = p[1] + n[1], p[2] + n[2], p[3] + n[3] if inside(nx, ny, nz) and get_block(nx, ny, nz) ~= 0 and not keep[key(nx, ny, nz)] then keep[key(nx, ny, nz)] = true nextFrontier[#nextFrontier + 1] = { nx, ny, nz } end end end frontier = nextFrontier end edit_session(ctx, function(e) W.each(r, function(x, y, z) if get_block(x, y, z) ~= 0 and not keep[key(x, y, z)] then e:place(x, y, z, fill) end end) end) end) -- Gaussian terrain smoothing, like the WorldEdit height map filter (5x5 kernel) local KERNEL, KSUM = {}, 0 for dx = -2, 2 do for dz = -2, 2 do local w = math.exp(-(dx * dx + dz * dz) / 2) KERNEL[#KERNEL + 1] = { dx, dz, w } KSUM = KSUM + w end end local function smooth_region(e, r, iterations, mask) local w, l = r.x1 - r.x0 + 1, r.z1 - r.z0 + 1 local function heights() local h, types = {}, {} for x = r.x0, r.x1 do for z = r.z0, r.z1 do local top for y = r.y1, r.y0, -1 do local id, d = get_block(x, y, z) if id ~= 0 and not W.is_liquid(id) and (not mask or mask(id, d)) then top = y; types[#types + 1] = nil; break end end h[(x - r.x0) * l + (z - r.z0)] = top or (r.y0 - 1) end end return h end for _ = 1, iterations do local h = heights() local newH = {} for x = 0, w - 1 do for z = 0, l - 1 do local sum, wsum = 0, 0 for _, k in ipairs(KERNEL) do local nx, nz = x + k[1], z + k[2] if nx >= 0 and nx < w and nz >= 0 and nz < l then sum = sum + h[nx * l + nz] * k[3] wsum = wsum + k[3] end end newH[x * l + z] = floor(sum / wsum + 0.5) end end for x = 0, w - 1 do for z = 0, l - 1 do local wx, wz = r.x0 + x, r.z0 + z local oh, nh = h[x * l + z], newH[x * l + z] if nh > oh then local id, d = get_block(wx, max(oh, r.y0), wz) if oh < r.y0 then id, d = 3, 0 end for y = oh + 1, min(nh, r.y1) do e:set(wx, y, wz, id, d) end elseif nh < oh then for y = nh + 1, oh do e:set(wx, y, wz, 0, 0) end end end end end end reg("/smooth", "Smooth the elevation in the selection", function(ctx) local _, r = selection(ctx) if not r then return end local iterations = max(1, floor(num(ctx.args[1], 1))) local mask = ctx.args[2] and W.mask(ctx.args[2]) W.load_region(r) edit_session(ctx, function(e) smooth_region(e, r, iterations, mask) end) end) -- ------------------------------------------------------- move / stack local function read_region(r, skipAir) local blocks = {} local n = 0 for x = r.x0, r.x1 do for y = r.y0, r.y1 do for z = r.z0, r.z1 do local id, d = get_block(x, y, z) if not (skipAir and id == 0) then n = n + 1 blocks[n] = { x - r.x0, y - r.y0, z - r.z0, id, d } end end end end return blocks end reg("/move", "Move the contents of the selection", function(ctx) local s, r = selection(ctx) if not r then return end local flags, pos = W.parse(ctx.args, { m = true }) local count = max(1, floor(num(pos[1], 1))) local dir = W.direction(ctx.player, pos[2]) local replace = pattern_or_err(ctx, pos[3] or "air") if not replace then return end local mask = flags.m and W.mask(flags.m) local dx, dy, dz = dir[1] * count, dir[2] * count, dir[3] * count local target = { x0 = r.x0 + dx, x1 = r.x1 + dx, y0 = r.y0 + dy, y1 = r.y1 + dy, z0 = r.z0 + dz, z1 = r.z1 + dz } W.load_region(r); W.load_region(target) local blocks = read_region(r, flags.a) edit_session(ctx, function(e) for _, b in ipairs(blocks) do if not mask or mask(b[4], b[5]) then e:place(r.x0 + b[1], r.y0 + b[2], r.z0 + b[3], replace) end end for _, b in ipairs(blocks) do if not mask or mask(b[4], b[5]) then e:set(target.x0 + b[1], target.y0 + b[2], target.z0 + b[3], b[4], b[5]) end end end) if flags.s then s.pos1 = { target.x0, target.y0, target.z0 } s.pos2 = { target.x1, target.y1, target.z1 } end end) reg("/stack", "Repeat the contents of the selection", function(ctx) local s, r = selection(ctx) if not r then return end local flags, pos = W.parse(ctx.args, { m = true }) local count = max(1, floor(num(pos[1], 1))) local dir = W.direction(ctx.player, pos[2]) local sx = (r.x1 - r.x0 + 1) * dir[1] local sy = (r.y1 - r.y0 + 1) * dir[2] local sz = (r.z1 - r.z0 + 1) * dir[3] local mask = flags.m and W.mask(flags.m) W.load_region(r) local blocks = read_region(r, flags.a) edit_session(ctx, function(e) for i = 1, count do W.load_region({ x0 = r.x0 + sx * i, x1 = r.x1 + sx * i, z0 = r.z0 + sz * i, z1 = r.z1 + sz * i, y0 = 0, y1 = 0 }) for _, b in ipairs(blocks) do if not mask or mask(b[4], b[5]) then e:set(r.x0 + b[1] + sx * i, r.y0 + b[2] + sy * i, r.z0 + b[3] + sz * i, b[4], b[5]) end end end end) if flags.s then s.pos2 = { r.x1 + sx * count, r.y1 + sy * count, r.z1 + sz * count } end end) -- ------------------------------------------------------------- generation local function gen_origin(ctx, flags, raise) local x, y, z = player_block(ctx) if flags.r then y = y + raise end return x, y, z end local function sphere_cmd(hollowDefault) return function(ctx) local flags, pos = W.parse(ctx.args) local pattern = pattern_or_err(ctx, pos[1]) if not pattern then return end local rad = radii(pos[2] or "") if not rad[1] then return "Usage: //sphere [-hr] " end local rx, ry, rz = rad[1], rad[2] or rad[1], rad[3] or rad[1] local x, y, z = gen_origin(ctx, flags, ry) local filled = not (flags.h or hollowDefault) W.load_region({ x0 = x - rx, x1 = x + rx, z0 = z - rz, z1 = z + rz, y0 = 0, y1 = 0 }) edit_session(ctx, function(e) W.sphere(e, x, y, z, pattern, rx, ry, rz, filled) end) end end reg("/sphere", "Generate a filled sphere", sphere_cmd(false)) reg("/hsphere", "Generate a hollow sphere", sphere_cmd(true)) local function cyl_cmd(hollowDefault) return function(ctx) local flags, pos = W.parse(ctx.args) local pattern = pattern_or_err(ctx, pos[1]) if not pattern then return end local rad = radii(pos[2] or "") if not rad[1] then return "Usage: //cyl [-h] [height]" end local rx, rz = rad[1], rad[2] or rad[1] local height = floor(num(pos[3], 1)) local x, y, z = player_block(ctx) W.load_region({ x0 = x - rx, x1 = x + rx, z0 = z - rz, z1 = z + rz, y0 = 0, y1 = 0 }) edit_session(ctx, function(e) W.cylinder(e, x, y, z, pattern, rx, rz, height, not (flags.h or hollowDefault)) end) end end reg("/cyl", "Generate a cylinder", cyl_cmd(false)) reg("/hcyl", "Generate a hollow cylinder", cyl_cmd(true)) reg("/cone", "Generate a cone", function(ctx) local flags, pos = W.parse(ctx.args) local pattern = pattern_or_err(ctx, pos[1]) if not pattern then return end local rad = radii(pos[2] or "") if not rad[1] then return "Usage: //cone [-h] [height]" end local rx, rz = rad[1], rad[2] or rad[1] local height = floor(num(pos[3], 1)) local x, y, z = player_block(ctx) W.load_region({ x0 = x - rx, x1 = x + rx, z0 = z - rz, z1 = z + rz, y0 = 0, y1 = 0 }) edit_session(ctx, function(e) W.cone(e, x, y, z, pattern, rx, rz, height, not flags.h) end) end) local function pyramid_cmd(hollowDefault) return function(ctx) local flags, pos = W.parse(ctx.args) local pattern = pattern_or_err(ctx, pos[1]) if not pattern then return end local size = floor(num(pos[2], 0)) if size < 1 then return "Usage: //pyramid [-h] " end local x, y, z = player_block(ctx) W.load_region({ x0 = x - size, x1 = x + size, z0 = z - size, z1 = z + size, y0 = 0, y1 = 0 }) edit_session(ctx, function(e) W.pyramid(e, x, y, z, pattern, size, not (flags.h or hollowDefault)) end) end end reg("/pyramid", "Generate a filled pyramid", pyramid_cmd(false)) reg("/hpyramid", "Generate a hollow pyramid", pyramid_cmd(true)) reg("/line", "Draws a line segment between the cuboid selection corners", function(ctx) local s, r = selection(ctx) if not r then return end local flags, pos = W.parse(ctx.args) local pattern = pattern_or_err(ctx, pos[1]) if not pattern then return end local thickness = floor(num(pos[2], 0)) W.load_region(r) edit_session(ctx, function(e) W.line(e, s.pos1, s.pos2, pattern, thickness, not flags.h) end) end) -- -------------------------------------------------------------- clipboard local function take_clipboard(ctx, r, flags, cut, leave) local s = W.session(ctx.player) local px, py, pz = player_block(ctx) W.load_region(r) local mask = flags.m and W.mask(flags.m) local blocks = {} local n = 0 for x = r.x0, r.x1 do for y = r.y0, r.y1 do for z = r.z0, r.z1 do local id, d = get_block(x, y, z) if not mask or mask(id, d) then n = n + 1 blocks[n] = { x - r.x0, y - r.y0, z - r.z0, id, d } end end end end s.clipboard = { w = r.x1 - r.x0 + 1, h = r.y1 - r.y0 + 1, l = r.z1 - r.z0 + 1, ox = r.x0 - px, oy = r.y0 - py, oz = r.z0 - pz, -- offset from copy position to min corner wx = r.x0, wy = r.y0, wz = r.z0, -- original world position blocks = blocks, } say(ctx, n .. " block(s) were copied.") if cut then edit_session(ctx, function(e) for _, b in ipairs(blocks) do e:place(r.x0 + b[1], r.y0 + b[2], r.z0 + b[3], leave) end end) end end reg("/copy", "Copy the selection to the clipboard", function(ctx) local flags = W.parse(ctx.args, { m = true }) local _, r = selection(ctx) if r then take_clipboard(ctx, r, flags, false) end end) reg("/cut", "Cut the selection to the clipboard", function(ctx) local flags, pos = W.parse(ctx.args, { m = true }) local _, r = selection(ctx) if not r then return end local leave = pattern_or_err(ctx, pos[1] or "air") if leave then take_clipboard(ctx, r, flags, true, leave) end end) reg("/paste", "Paste the clipboard's contents", function(ctx) local flags = W.parse(ctx.args, { m = true }) local s = W.session(ctx.player) local c = s.clipboard if not c then return "Your clipboard is empty. Use //copy first." end local px, py, pz = player_block(ctx) local bx, by, bz if flags.o then bx, by, bz = c.wx, c.wy, c.wz else bx, by, bz = px + c.ox, py + c.oy, pz + c.oz end local mask = flags.m and W.mask(flags.m) W.load_region({ x0 = bx, x1 = bx + c.w, z0 = bz, z1 = bz + c.l, y0 = 0, y1 = 0 }) if not flags.n then edit_session(ctx, function(e) for _, b in ipairs(c.blocks) do if not (flags.a and b[4] == 0) and (not mask or mask(b[4], b[5])) then e:set(bx + b[1], by + b[2], bz + b[3], b[4], b[5]) end end end) end if flags.s or flags.n then s.pos1 = { bx, by, bz } s.pos2 = { bx + c.w - 1, by + c.h - 1, bz + c.l - 1 } end end) local function transform_clipboard(c, fn, newW, newH, newL, ox, oy, oz) local out = {} for i, b in ipairs(c.blocks) do out[i] = fn(b) end c.blocks = out c.w, c.h, c.l = newW or c.w, newH or c.h, newL or c.l if ox then c.ox, c.oy, c.oz = ox, oy, oz end end reg("/rotate", "Rotate the contents of the clipboard", function(ctx) local s = W.session(ctx.player) local c = s.clipboard if not c then return "Your clipboard is empty. Use //copy first." end local deg = floor(num(ctx.args[1], 0)) if deg % 90 ~= 0 then return "Angles must be multiples of 90 degrees." end local turns = (deg / 90) % 4 for _ = 1, turns do -- 90 degrees clockwise around y: (x, z) -> (l - 1 - z, x) local w, l = c.w, c.l transform_clipboard(c, function(b) return { l - 1 - b[3], b[2], b[1], b[4], W.rotate_data(b[4], b[5], 1) } end, l, c.h, w, -(c.oz + l - 1), c.oy, c.ox) end return "The clipboard copy has been rotated." end) reg({ "/flip", "/mirror" }, "Flip the contents of the clipboard", function(ctx) local s = W.session(ctx.player) local c = s.clipboard if not c then return "Your clipboard is empty. Use //copy first." end local dir = W.direction(ctx.player, ctx.args[1]) local w, h, l = c.w, c.h, c.l transform_clipboard(c, function(b) local x, y, z = b[1], b[2], b[3] if dir[1] ~= 0 then x = w - 1 - x end if dir[2] ~= 0 then y = h - 1 - y end if dir[3] ~= 0 then z = l - 1 - z end return { x, y, z, b[4], b[5] } end) if dir[1] ~= 0 then c.ox = -(c.ox + w - 1) end if dir[2] ~= 0 then c.oy = -(c.oy + h - 1) end if dir[3] ~= 0 then c.oz = -(c.oz + l - 1) end return "The clipboard copy has been flipped." end) reg("/clearclipboard", "Clear your clipboard", function(ctx) W.session(ctx.player).clipboard = nil return "Clipboard cleared." end) -- schematics are stored as Lua tables in schematics/.schem reg({ "/schematic", "/schem" }, "Schematic commands for saving/loading areas", function(ctx) local sub = ctx.args[1] local s = W.session(ctx.player) mc._mkdir("schematics") local function path(name) return "schematics/" .. name:gsub("[^%w_%-]", "_") .. ".schem" end if sub == "save" then local name = ctx.args[#ctx.args] if not s.clipboard or not name or name == "save" then return "Usage: /schematic save (copy something first)" end local ok, err = mc.save_table(path(name), s.clipboard) return ok and ("Clipboard saved as " .. name .. ".") or ("Could not save: " .. tostring(err)) elseif sub == "load" then local name = ctx.args[#ctx.args] local data = name and mc.load_table(path(name)) if not data then return "Schematic not found." end s.clipboard = data return "Schematic " .. name .. " loaded. Paste it with //paste" elseif sub == "list" or sub == "all" or sub == "ls" then local names = {} for _, e in ipairs(mc._listdir("schematics")) do local n = e.name:match("^(.+)%.schem$") if n then names[#names + 1] = n end end table.sort(names) return "Available schematics: " .. (#names > 0 and table.concat(names, ", ") or "(none)") elseif sub == "delete" or sub == "d" then local name = ctx.args[2] if not name then return "Usage: /schematic delete " end os.remove(path(name)) return "Deleted " .. name .. "." elseif sub == "formats" then return "Available formats: mcpe-lua (default)" end return "Usage: /schematic " end) -- ---------------------------------------------------------------- history reg({ "/undo" }, "Undo your last action", function(ctx) local times = max(1, floor(num(ctx.args[1], 1))) local s = W.session(ctx.player) local done = 0 for _ = 1, times do if not W.undo(s) then break end done = done + 1 end if done == 0 then return "Nothing left to undo." end return "Undo successful." end) reg({ "/redo" }, "Redo your last undone action", function(ctx) local times = max(1, floor(num(ctx.args[1], 1))) local s = W.session(ctx.player) local done = 0 for _ = 1, times do if not W.redo(s) then break end done = done + 1 end if done == 0 then return "Nothing left to redo." end return "Redo successful." end) reg("/clearhistory", "Clear your history", function(ctx) local s = W.session(ctx.player) s.undo, s.redo = {}, {} return "History cleared." end) -- ------------------------------------------------------------------ utility local function flood_fill(ctx, recursive) local flags, pos = W.parse(ctx.args) local pattern = pattern_or_err(ctx, pos[1]) if not pattern then return end local radius = floor(num(pos[2], 0)) if radius < 1 then return "Usage: //fill [depth]" end local depth = floor(num(pos[3], 1 / 0)) local ox, oy, oz = player_block(ctx) W.load_region({ x0 = ox - radius, x1 = ox + radius, z0 = oz - radius, z1 = oz + radius, y0 = 0, y1 = 0 }) edit_session(ctx, function(e) local seen = {} local queue, head = {}, 1 local function push(x, y, z) local k = (x * 4096 + z) * 256 + y if seen[k] then return end if (x - ox) ^ 2 + (z - oz) ^ 2 > radius * radius then return end if y > oy or y < oy - depth + 1 or y < 0 then return end if get_block(x, y, z) ~= 0 then return end seen[k] = true queue[#queue + 1] = { x, y, z } end push(ox, oy, oz) if recursive then push(ox, oy - 1, oz) end while head <= #queue do local p = queue[head]; head = head + 1 e:place(p[1], p[2], p[3], pattern) push(p[1] + 1, p[2], p[3]); push(p[1] - 1, p[2], p[3]) push(p[1], p[2], p[3] + 1); push(p[1], p[2], p[3] - 1) if recursive then push(p[1], p[2] - 1, p[3]) end end end) end reg("/fill", "Fill a hole", function(ctx) flood_fill(ctx, false) end) reg("/fillr", "Fill a hole recursively", function(ctx) flood_fill(ctx, true) end) reg("/drain", "Drain a pool", function(ctx) local flags, pos = W.parse(ctx.args) local radius = floor(num(pos[1], 0)) if radius < 1 then return "Usage: //drain [-w] " end local ox, oy, oz = player_block(ctx) W.load_region({ x0 = ox - radius, x1 = ox + radius, z0 = oz - radius, z1 = oz + radius, y0 = 0, y1 = 0 }) edit_session(ctx, function(e) local seen, queue, head = {}, {}, 1 local function push(x, y, z) if y < 0 or y > 127 then return end local k = (x * 4096 + z) * 256 + y if seen[k] or (x - ox) ^ 2 + (y - oy) ^ 2 + (z - oz) ^ 2 > radius * radius then return end if not W.is_liquid(get_block(x, y, z)) then return end seen[k] = true queue[#queue + 1] = { x, y, z } end for dx = -1, 1 do for dy = -1, 2 do for dz = -1, 1 do push(ox + dx, oy + dy, oz + dz) end end end while head <= #queue do local p = queue[head]; head = head + 1 e:set(p[1], p[2], p[3], 0, 0) push(p[1] + 1, p[2], p[3]); push(p[1] - 1, p[2], p[3]) push(p[1], p[2] + 1, p[3]); push(p[1], p[2] - 1, p[3]) push(p[1], p[2], p[3] + 1); push(p[1], p[2], p[3] - 1) end end) end) local function near_sphere(ctx, radius, fn) local ox, oy, oz = player_block(ctx) W.load_region({ x0 = ox - radius, x1 = ox + radius, z0 = oz - radius, z1 = oz + radius, y0 = 0, y1 = 0 }) for x = ox - radius, ox + radius do for y = max(0, oy - radius), min(127, oy + radius) do for z = oz - radius, oz + radius do if (x - ox) ^ 2 + (y - oy) ^ 2 + (z - oz) ^ 2 <= radius * radius then fn(x, y, z) end end end end end reg("/fixwater", "Level nearby water", function(ctx) local radius = floor(num(ctx.args[1], 0)) if radius < 1 then return "Usage: /fixwater " end edit_session(ctx, function(e) near_sphere(ctx, radius, function(x, y, z) local id = get_block(x, y, z) if id == 8 then e:set(x, y, z, 9, 0) end end) end) end) reg("/fixlava", "Level nearby lava", function(ctx) local radius = floor(num(ctx.args[1], 0)) if radius < 1 then return "Usage: /fixlava " end edit_session(ctx, function(e) near_sphere(ctx, radius, function(x, y, z) local id = get_block(x, y, z) if id == 10 then e:set(x, y, z, 11, 0) end end) end) end) local function column_remove(ctx, dirY) local size = max(1, floor(num(ctx.args[1], 1))) local height = floor(num(ctx.args[2], 127)) local ox, oy, oz = player_block(ctx) W.load_region({ x0 = ox - size, x1 = ox + size, z0 = oz - size, z1 = oz + size, y0 = 0, y1 = 0 }) edit_session(ctx, function(e) local y0, y1 if dirY > 0 then y0, y1 = oy + 2, min(127, oy + 1 + height) else y0, y1 = max(0, oy - height), oy - 1 end for x = ox - size, ox + size do for z = oz - size, oz + size do for y = y0, y1 do e:set(x, y, z, 0, 0) end end end end) end reg("/removeabove", "Remove blocks above your head", function(ctx) column_remove(ctx, 1) end) reg("/removebelow", "Remove blocks below you", function(ctx) column_remove(ctx, -1) end) reg("/removenear", "Remove blocks near you", function(ctx) local mask, err = W.mask(ctx.args[1]) if not mask then return err or "Usage: /removenear [size]" end local size = floor(num(ctx.args[2], 50)) edit_session(ctx, function(e) near_sphere(ctx, size, function(x, y, z) local id, d = get_block(x, y, z) if mask(id, d) then e:set(x, y, z, 0, 0) end end) end) end) reg("/replacenear", "Replace nearby blocks", function(ctx) local size = floor(num(ctx.args[1], 0)) if size < 1 then return "Usage: /replacenear [from] " end local from, to = ctx.args[2], ctx.args[3] if not to then from, to = "#existing", ctx.args[2] end local mask = W.mask(from) local pattern = pattern_or_err(ctx, to) if not mask or not pattern then return end edit_session(ctx, function(e) near_sphere(ctx, size, function(x, y, z) local id, d = get_block(x, y, z) if mask(id, d) then e:place(x, y, z, pattern) end end) end) end) reg("/snow", "Simulates snow", function(ctx) local flags, pos = W.parse(ctx.args) local size = max(1, floor(num(pos[1], 10))) local ox, _, oz = player_block(ctx) W.load_region({ x0 = ox - size, x1 = ox + size, z0 = oz - size, z1 = oz + size, y0 = 0, y1 = 0 }) edit_session(ctx, function(e) for x = ox - size, ox + size do for z = oz - size, oz + size do if (x - ox) ^ 2 + (z - oz) ^ 2 <= size * size then local top = W.top_existing(x, z, 0, 127) if top and top < 127 then local id = get_block(x, top, z) if id == 9 or id == 8 then e:set(x, top, z, 79, 0) elseif id ~= 78 and id ~= 80 and id ~= 79 then e:set(x, top + 1, z, 78, 0) end end end end end end) end) reg("/thaw", "Thaws the area", function(ctx) local size = max(1, floor(num(ctx.args[1], 10))) edit_session(ctx, function(e) near_sphere(ctx, size, function(x, y, z) local id = get_block(x, y, z) if id == 78 then e:set(x, y, z, 0, 0) elseif id == 79 then e:set(x, y, z, 9, 0) end end) end) end) reg("/green", "Converts dirt to grass blocks", function(ctx) local size = max(1, floor(num(ctx.args[1], 10))) local ox, _, oz = player_block(ctx) W.load_region({ x0 = ox - size, x1 = ox + size, z0 = oz - size, z1 = oz + size, y0 = 0, y1 = 0 }) edit_session(ctx, function(e) for x = ox - size, ox + size do for z = oz - size, oz + size do if (x - ox) ^ 2 + (z - oz) ^ 2 <= size * size then local top = W.top_existing(x, z, 0, 127) if top and get_block(x, top, z) == 3 then e:set(x, top, z, 2, 0) end end end end end) end) reg({ "/extinguish", "/ex", "/ext" }, "Extinguish nearby fire", function(ctx) local size = floor(num(ctx.args[1], 40)) edit_session(ctx, function(e) near_sphere(ctx, size, function(x, y, z) if get_block(x, y, z) == 51 then e:set(x, y, z, 0, 0) end end) end) end) reg("/butcher", "Kill all or nearby mobs", function(ctx) local flags, pos = W.parse(ctx.args) local radius = floor(num(pos[1], 0)) local x, y, z = ctx.player:pos() local n = mc.world.butcher(x, y, z, radius > 0 and radius or 0, flags.f or flags.a) return "Killed " .. n .. " mobs" .. (radius > 0 and (" in a radius of " .. radius) or "") .. "." end) reg("/toggleplace", "Switch between your position and pos1 for placement", function(ctx) local s = W.session(ctx.player) s.place_at_pos1 = not s.place_at_pos1 return "Now placing at " .. (s.place_at_pos1 and "pos #1." or "the block you stand in.") end) -- ------------------------------------------------------------------- tools local function held_id(player) local id = player:held(); return id end reg("/tool", "Bind a tool to the item in your hand", function(ctx) local s = W.session(ctx.player) local item = held_id(ctx.player) if item == 0 then return "Hold an item to bind the tool to." end local sub = ctx.args[1] if sub == "none" or sub == "unbind" then s.tools[item] = nil return "Tool unbound from your current item." elseif sub == "repl" then local p = pattern_or_err(ctx, ctx.args[2]) if not p then return end s.tools[item] = { type = "repl", pattern = p } return "Block replacer tool bound to " .. mc.block.name(item) .. "." elseif sub == "info" then s.tools[item] = { type = "info" } return "Block info tool bound." elseif sub == "farwand" then s.tools[item] = { type = "farwand" } return "Far wand tool bound." elseif sub == "navwand" then s.tools[item] = { type = "nav" } return "Navigation wand bound." elseif sub == "selwand" then s.tools[item] = { type = "wand" } return "Selection wand bound." elseif sub == "deltree" then s.tools[item] = { type = "deltree" } return "Floating tree remover bound." elseif sub == "floodfill" or sub == "flood" then local p = pattern_or_err(ctx, ctx.args[2]) if not p then return end s.tools[item] = { type = "flood", pattern = p, range = floor(num(ctx.args[3], 5)) } return "Block flood fill tool bound." end return "Usage: /tool " end) reg("/none", "Unbind the tool of your item", function(ctx) local s = W.session(ctx.player) s.tools[held_id(ctx.player)] = nil return "Tool unbound from your current item." end) reg("/mask", "Set the brush mask", function(ctx) local s = W.session(ctx.player) if not ctx.args[1] then s.brush_mask = nil; return "Brush mask disabled." end local m, err = W.mask(ctx.args[1]) if not m then return err end s.brush_mask = m return "Brush mask set." end) reg({ "/material", "/mat" }, "Set the brush material", function(ctx) local s = W.session(ctx.player) local p = pattern_or_err(ctx, ctx.args[1]) if not p then return end s.brush_material = p return "Brush material set." end) reg("/range", "Set the brush range", function(ctx) local s = W.session(ctx.player) s.brush_range = floor(num(ctx.args[1], 200)) return "Brush range set." end) reg("size", "Set the brush size", function(ctx) local s = W.session(ctx.player) s.brush_size = max(1, min(20, floor(num(ctx.args[1], 5)))) return "Brush size set to " .. s.brush_size .. "." end) reg({ "/superpickaxe", "/pickaxe", "/sp" }, "Select super pickaxe mode", function(ctx) local s = W.session(ctx.player) local mode = ctx.args[1] if mode == "single" or mode == "area" or mode == "recursive" or mode == "recur" then s.superpick = (mode == "recur") and "recursive" or mode s.superpick_range = floor(num(ctx.args[2], 3)) return "Mode changed. Left click with a pickaxe. // to disable." end return "Usage: /sp [range]" end) reg({ "/", ",", "/," }, "Toggle the super pickaxe pickaxe function", function(ctx) local s = W.session(ctx.player) if s.superpick then s.superpick = nil; return "Super pick axe disabled." end s.superpick = "single" return "Super pick axe enabled." end) -- brushes local BRUSHES = { sphere = true, cylinder = true, cyl = true, set = true, smooth = true, gravity = true, extinguish = true, ex = true, raise = true, lower = true, butcher = true, kill = true } reg({ "/brush", "/br" }, "Brush tool", function(ctx) local s = W.session(ctx.player) local item = held_id(ctx.player) if item == 0 then return "Hold an item to bind the brush to." end local flags, pos = W.parse(ctx.args) local kind = pos[1] if kind == "none" or kind == "unbind" then s.tools[item] = nil return "Brush unbound from your current item." end if kind == "s" then kind = "sphere" elseif kind == "c" then kind = "cylinder" elseif kind == "cyl" then kind = "cylinder" elseif kind == "ex" then kind = "extinguish" elseif kind == "kill" then kind = "butcher" end if not kind or not BRUSHES[kind] then return "Usage: /brush ..." end local brush = { type = "brush", kind = kind, hollow = flags.h } local i = 2 if kind == "sphere" or kind == "cylinder" then brush.pattern = pattern_or_err(ctx, pos[i]); if not brush.pattern then return end brush.radius = floor(num(pos[i + 1], 2)) if kind == "cylinder" then brush.height = floor(num(pos[i + 2], 1)) end elseif kind == "set" then brush.pattern = pattern_or_err(ctx, pos[i + 2] or pos[i]); if not brush.pattern then return end brush.radius = floor(num(pos[i + 1], 2)) else brush.radius = floor(num(pos[i], 2)) brush.iterations = floor(num(pos[i + 1], 4)) end s.tools[item] = brush return "Brush '" .. kind .. "' bound to your current item." end) local function use_brush(player, s, brush, x, y, z) if brush.kind == "extinguish" then local e = W.new_edit(s) for dx = -brush.radius, brush.radius do for dy = -brush.radius, brush.radius do for dz = -brush.radius, brush.radius do if get_block(x + dx, y + dy, z + dz) == 51 then e:set(x + dx, y + dy, z + dz, 0, 0) end end end end e:commit() return elseif brush.kind == "butcher" then mc.world.butcher(x, y, z, brush.radius, false) return end local edit = W.new_edit(s) local mask = s.brush_mask local r = brush.radius W.load_region({ x0 = x - r, x1 = x + r, z0 = z - r, z1 = z + r, y0 = 0, y1 = 0 }) if brush.kind == "sphere" then local inner = brush.pattern local pat = inner if mask then pat = function() return inner() end end local guarded = { set = function(_, bx, by, bz, id, d) if mask then local oid, od = get_block(bx, by, bz); if not mask(oid, od) then return end end edit:set(bx, by, bz, id, d) end, place = function(self, bx, by, bz, p) local id, d = p(); self.set(self, bx, by, bz, id, d) end, } W.sphere(guarded, x, y, z, pat, r, r, r, not brush.hollow) elseif brush.kind == "cylinder" then W.cylinder(edit, x, y, z, brush.pattern, r, r, brush.height, not brush.hollow) elseif brush.kind == "set" then for dx = -r, r do for dy = -r, r do for dz = -r, r do if dx * dx + dy * dy + dz * dz <= r * r + 1 then local oid, od = get_block(x + dx, y + dy, z + dz) if not mask or mask(oid, od) then edit:place(x + dx, y + dy, z + dz, brush.pattern) end end end end end elseif brush.kind == "smooth" then smooth_region(edit, { x0 = x - r, x1 = x + r, z0 = z - r, z1 = z + r, y0 = max(0, y - r), y1 = min(127, y + r) }, brush.iterations or 4, mask) elseif brush.kind == "raise" or brush.kind == "lower" then for dx = -r, r do for dz = -r, r do if dx * dx + dz * dz <= r * r then local top = W.top_ground(x + dx, z + dz, 0, 127) if top then if brush.kind == "raise" then local id, d = get_block(x + dx, top, z + dz) edit:set(x + dx, top + 1, z + dz, id, d) else edit:set(x + dx, top, z + dz, 0, 0) end end end end end elseif brush.kind == "gravity" then for dx = -r, r do for dz = -r, r do if dx * dx + dz * dz <= r * r then local bx, bz = x + dx, z + dz local y0 = max(0, y - r) local floorY = y0 for yy = y0, min(127, y + r) do local id, d = get_block(bx, yy, bz) if id ~= 0 then if yy ~= floorY then edit:set(bx, floorY, bz, id, d) edit:set(bx, yy, bz, 0, 0) end floorY = floorY + 1 end end end end end end edit:commit() end -- ------------------------------------------------------------ item events local function is_op(player) return player:is_op() end -- Adjacent block a right click would have placed (to refresh nothing here, server does it) mc.on("block_break", function(player, x, y, z, tile) if not is_op(player) then return end local s = W.session(player) local item = held_id(player) if item == WAND and not s.wand_off then set_pos(player, 1, { x, y, z }) return false end if item == NAVWAND and not s.wand_off then player:teleport(x + 0.5, y + 1, z + 0.5) return false end local tool = s.tools[item] if tool and tool.type == "wand" then set_pos(player, 1, { x, y, z }) return false elseif tool and tool.type == "nav" then player:teleport(x + 0.5, y + 1, z + 0.5) return false end -- super pickaxe if s.superpick and PICKAXES[item] then local edit = W.new_edit(s) if s.superpick == "single" then edit:set(x, y, z, 0, 0) elseif s.superpick == "area" then local r = s.superpick_range for dx = -r, r do for dy = -r, r do for dz = -r, r do if dx * dx + dy * dy + dz * dz <= r * r then edit:set(x + dx, y + dy, z + dz, 0, 0) end end end end else local r = s.superpick_range local seen, queue, head = {}, { { x, y, z } }, 1 seen[(x * 4096 + z) * 256 + y] = true while head <= #queue do local p = queue[head]; head = head + 1 if get_block(p[1], p[2], p[3]) == tile then edit:set(p[1], p[2], p[3], 0, 0) for _, n in ipairs({ { 1, 0, 0 }, { -1, 0, 0 }, { 0, 1, 0 }, { 0, -1, 0 }, { 0, 0, 1 }, { 0, 0, -1 } }) do local nx, ny, nz = p[1] + n[1], p[2] + n[2], p[3] + n[3] local k = (nx * 4096 + nz) * 256 + ny if not seen[k] and abs(nx - x) <= r and abs(ny - y) <= r and abs(nz - z) <= r then seen[k] = true queue[#queue + 1] = { nx, ny, nz } end end end end end edit:commit() return false end end) mc.on("use_item", function(player, x, y, z, face, item, aux) if not is_op(player) then return end local s = W.session(player) if item == WAND and not s.wand_off then set_pos(player, 2, { x, y, z }) return false end if item == NAVWAND and not s.wand_off then -- /thru local px, py, pz = player:pos() local _, dx, dz = player:facing() local bx, by, bz = floor(px), floor(py), floor(pz) local inWall = false for i = 1, 100 do local cx, cz = bx + dx * i, bz + dz * i if not free(cx, by, cz) or not free(cx, by + 1, cz) then inWall = true elseif inWall then player:teleport(cx + 0.5, by, cz + 0.5); break end end return false end local tool = s.tools[item] if not tool then return end if tool.type == "repl" then local e = W.new_edit(s) e:place(x, y, z, tool.pattern) e:commit() return false elseif tool.type == "info" then local id, d = get_block(x, y, z) player:say(string.format("%s (%d:%d) at (%d, %d, %d)", mc.block.name(id), id, d, x, y, z)) return false elseif tool.type == "wand" then set_pos(player, 2, { x, y, z }) return false elseif tool.type == "nav" then return false elseif tool.type == "flood" then local e = W.new_edit(s) local target = get_block(x, y, z) local seen, queue, head = {}, { { x, y, z } }, 1 seen[(x * 4096 + z) * 256 + y] = true while head <= #queue do local p = queue[head]; head = head + 1 e:place(p[1], p[2], p[3], tool.pattern) for _, n in ipairs({ { 1, 0, 0 }, { -1, 0, 0 }, { 0, 1, 0 }, { 0, -1, 0 }, { 0, 0, 1 }, { 0, 0, -1 } }) do local nx, ny, nz = p[1] + n[1], p[2] + n[2], p[3] + n[3] local k = (nx * 4096 + nz) * 256 + ny if not seen[k] and abs(nx - x) <= tool.range and abs(ny - y) <= tool.range and abs(nz - z) <= tool.range and get_block(nx, ny, nz) == target then seen[k] = true queue[#queue + 1] = { nx, ny, nz } end end end e:commit() return false elseif tool.type == "farwand" then set_pos(player, 2, { x, y, z }) return false elseif tool.type == "deltree" then local e = W.new_edit(s) local seen, queue, head = {}, { { x, y, z } }, 1 while head <= #queue and #queue < 4000 do local p = queue[head]; head = head + 1 local id = get_block(p[1], p[2], p[3]) if id == 17 or id == 18 then e:set(p[1], p[2], p[3], 0, 0) for _, n in ipairs({ { 1, 0, 0 }, { -1, 0, 0 }, { 0, 1, 0 }, { 0, -1, 0 }, { 0, 0, 1 }, { 0, 0, -1 } }) do local nx, ny, nz = p[1] + n[1], p[2] + n[2], p[3] + n[3] local k = (nx * 4096 + nz) * 256 + ny if not seen[k] then seen[k] = true; queue[#queue + 1] = { nx, ny, nz } end end end end e:commit() return false elseif tool.type == "brush" then use_brush(player, s, tool, x, y, z) return false end end) mc.log("WorldEdit loaded: " .. #mc._command_names() .. " commands registered")