Skip to content

Commit 674d647

Browse files
committedDec 20, 2013
Improve node inspector to show player axis, replace //scale with //stretch, which supports per-axis stretching (full backwards compatibility retained), and secure schematic file loading functions.
1 parent 3767ea8 commit 674d647

File tree

6 files changed

+61
-30
lines changed

6 files changed

+61
-30
lines changed
 

‎Chat Commands.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Enable or disable node inspection.
2222
//inspect no
2323
//inspect enable
2424
//inspect disable
25+
//inspect
2526

2627
### //reset
2728

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ WorldEdit works primarily through the WorldEdit GUI and chat commands. Depending
3737

3838
WorldEdit has a huge potential for abuse by untrusted players. Therefore, users will not be able to use WorldEdit unless they have the `worldedit` privelege. This is available by default in single player, but in multiplayer the permission must be explicitly given by someone with the right credentials, using the follwoing chat command: `/grant <player name> worldedit`. This privelege can later be removed using the following chat command: `/revoke <player name> worldedit`.
3939

40-
Certain functions/commands such as WorldEdit GUI's "Run Lua" (equivalent to the `//lua` chat command) additionally require the `server` privilege. This is because it is extremely dangerous to give access to these commands to untrusted players, since they essentially are able to control the computer the server is running on. Give this privilege only to people you trust with your computer.
40+
Certain functions/commands such as WorldEdit GUI's "Run Lua" function (equivalent to the `//lua` and `//luatransform` chat command) additionally require the `server` privilege. This is because it is extremely dangerous to give access to these commands to untrusted players, since they essentially are able to control the computer the server is running on. Give this privilege only to people you trust with your computer.
4141

4242
For in-game information about these commands, type `/help <command name>` in the chat. For example, to learn more about the `//copy` command, simply type `/help /copy` to display information relevant to copying a region.
4343

‎WorldEdit API.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ Duplicates the region defined by positions `pos1` and `pos2` along the `axis` ax
5757

5858
Returns the number of nodes stacked.
5959

60-
### count, newpos1, newpos2 = worldedit.scale(pos1, pos2, factor)
60+
### count, newpos1, newpos2 = worldedit.stretch(pos1, pos2, stretchx, stretchy, stretchz)
6161

62-
Scales the region defined by positions `pos1` and `pos2` by an factor of positive integer `factor` with `pos1` as the origin.
62+
Stretches the region defined by positions `pos1` and `pos2` by an factor of positive integers `stretchx`, `stretchy`. and `stretchz` along the X, Y, and Z axes, respectively, with `pos1` as the origin.
6363

64-
Returns the number of nodes scaled, the new scaled position 1, and the new scaled position 2.
64+
Returns the number of nodes stretched, the new scaled position 1, and the new scaled position 2.
6565

6666
### count, newpos1, newpos2 = worldedit.transpose(pos1, pos2, axis1, axis2)
6767

‎worldedit/compatibility.lua

+3
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ worldedit.metaload = function(originpos, filename)
1818
local data = file:read("*a")
1919
return worldedit.deserialize(originpos, data)
2020
end
21+
worldedit.scale = function(pos1, pos2, factor)
22+
return worldedit.stretch(pos1, pos2, factor, factor, factor)
23+
end

‎worldedit/manipulations.lua

+20-14
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ worldedit.replaceinverse = function(pos1, pos2, searchnode, replacenode)
109109
return count
110110
end
111111

112-
worldedit.copy = function(pos1, pos2, axis, amount)
112+
worldedit.copy = function(pos1, pos2, axis, amount) --wip: replace the old version below
113113
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
114114

115115
if amount == 0 then
@@ -291,24 +291,28 @@ worldedit.stack = function(pos1, pos2, axis, count)
291291
return worldedit.volume(pos1, pos2) * count
292292
end
293293

294-
--scales the region defined by positions `pos1` and `pos2` by an factor of positive integer `factor` with `pos1` as the origin, returning the number of nodes scaled, the new scaled position 1, and the new scaled position 2
295-
worldedit.scale = function(pos1, pos2, factor)
294+
--stretches the region defined by positions `pos1` and `pos2` by an factor of positive integers `stretchx`, `stretchy`. and `stretchz` along the X, Y, and Z axes, respectively, with `pos1` as the origin, returning the number of nodes scaled, the new scaled position 1, and the new scaled position 2
295+
worldedit.stretch = function(pos1, pos2, stretchx, stretchy, stretchz) --wip: test this
296296
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
297297

298298
--prepare schematic of large node
299299
local get_node, get_meta, place_schematic = minetest.get_node, minetest.get_meta, minetest.place_schematic
300300
local placeholder_node = {name="", param1=255, param2=0}
301301
local nodes = {}
302-
for i = 1, factor ^ 3 do
302+
for i = 1, stretchx * stretchy * stretchz do
303303
nodes[i] = placeholder_node
304304
end
305-
local schematic = {size={x=factor, y=factor, z=factor}, data=nodes}
305+
local schematic = {size={x=stretchx, y=stretchy, z=stretchz}, data=nodes}
306306

307-
local size = factor - 1
307+
local sizex, sizey, sizez = stretchx - 1, stretchy - 1, stretchz - 1
308308

309309
--make area stay loaded
310310
local manip = minetest.get_voxel_manip()
311-
local new_pos2 = {x=pos1.x + (pos2.x - pos1.x) * factor + size, y=pos1.y + (pos2.y - pos1.y) * factor + size, z=pos1.z + (pos2.z - pos1.z) * factor + size}
311+
local new_pos2 = {
312+
x=pos1.x + (pos2.x - pos1.x) * stretchx + sizex,
313+
y=pos1.y + (pos2.y - pos1.y) * stretchy + sizey,
314+
z=pos1.z + (pos2.z - pos1.z) * stretchz + sizez,
315+
}
312316
manip:read_from_map(pos1, new_pos2)
313317

314318
local pos = {x=pos2.x, y=0, z=0}
@@ -321,8 +325,10 @@ worldedit.scale = function(pos1, pos2, factor)
321325
local node = get_node(pos) --obtain current node
322326
local meta = get_meta(pos):to_table() --get meta of current node
323327

324-
local value = pos[axis] --store current position
325-
local posx, posy, posz = pos1.x + (pos.x - pos1.x) * factor, pos1.y + (pos.y - pos1.y) * factor, pos1.z + (pos.z - pos1.z) * factor
328+
--calculate far corner of the big node
329+
local posx = pos1.x + (pos.x - pos1.x) * stretchx
330+
local posy = pos1.y + (pos.y - pos1.y) * stretchy
331+
local posz = pos1.z + (pos.z - pos1.z) * stretchz
326332

327333
--create large node
328334
placeholder_node.name = node.name
@@ -331,10 +337,10 @@ worldedit.scale = function(pos1, pos2, factor)
331337
place_schematic(bigpos, schematic)
332338

333339
--fill in large node meta
334-
if next(meta.fields) ~= nil and next(meta.inventory) ~= nil then --node has meta fields
335-
for x = 0, size do
336-
for y = 0, size do
337-
for z = 0, size do
340+
if next(meta.fields) ~= nil or next(meta.inventory) ~= nil then --node has meta fields
341+
for x = 0, sizex do
342+
for y = 0, sizey do
343+
for z = 0, sizez do
338344
bigpos.x, bigpos.y, bigpos.z = posx + x, posy + y, posz + z
339345
get_meta(bigpos):from_table(meta) --set metadata of new node
340346
end
@@ -347,7 +353,7 @@ worldedit.scale = function(pos1, pos2, factor)
347353
end
348354
pos.x = pos.x - 1
349355
end
350-
return worldedit.volume(pos1, pos2) * (factor ^ 3), pos1, new_pos2
356+
return worldedit.volume(pos1, pos2) * stretchx * stretchy * stretchz, pos1, new_pos2
351357
end
352358

353359
--transposes a region defined by the positions `pos1` and `pos2` between the `axis1` and `axis2` axes, returning the number of nodes transposed, the new transposed position 1, and the new transposed position 2

‎worldedit_commands/init.lua

+33-12
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,18 @@ minetest.register_chatcommand("/about", {
6161
})
6262

6363
minetest.register_chatcommand("/inspect", {
64-
params = "on/off/1/0/true/false/yes/no/enable/disable",
64+
params = "on/off/1/0/true/false/yes/no/enable/disable/<blank>",
6565
description = "Enable or disable node inspection",
6666
privs = {worldedit=true},
6767
func = function(name, param)
68-
if param == "on" or param == "1" or param == "true" or param == "yes" or param == "enable" then
68+
if param == "on" or param == "1" or param == "true" or param == "yes" or param == "enable" or param == "" then
6969
worldedit.inspect[name] = true
70-
worldedit.player_notify(name, "node inspection enabled")
70+
local axis, sign = worldedit.player_axis(name)
71+
worldedit.player_notify(name, string.format("inspector: inspection enabled for %s, currently facing the %s axis",
72+
name, axis .. (sign > 0 and "+" or "-")))
7173
elseif param == "off" or param == "0" or param == "false" or param == "no" or param == "disable" then
7274
worldedit.inspect[name] = nil
73-
worldedit.player_notify(name, "node inspection disabled")
75+
worldedit.player_notify(name, "inspector: inspection disabled")
7476
else
7577
worldedit.player_notify(name, "invalid usage: " .. param)
7678
end
@@ -81,7 +83,9 @@ minetest.register_on_punchnode(function(pos, node, puncher)
8183
local name = puncher:get_player_name()
8284
if worldedit.inspect[name] then
8385
if minetest.check_player_privs(name, {worldedit=true}) then
84-
message = "inspector: " .. node.name .. " at " .. minetest.pos_to_string(pos) .. " (param1=" .. node.param1 .. ", param2=" .. node.param2 .. ")"
86+
local axis, sign = worldedit.player_axis(name)
87+
message = string.format("inspector: %s at %s (param1=%d, param2=%d) punched by %s facing the %s axis",
88+
node.name, minetest.pos_to_string(pos), node.param1, node.param2, name, axis .. (sign > 0 and "+" or "-"))
8589
else
8690
message = "inspector: worldedit privileges required"
8791
end
@@ -658,9 +662,9 @@ minetest.register_chatcommand("/stack", {
658662
end,
659663
})
660664

661-
minetest.register_chatcommand("/scale", {
662-
params = "<factor>",
663-
description = "Scale the current WorldEdit positions and region by a factor of positive integer <factor> with position 1 as the origin",
665+
minetest.register_chatcommand("/stretch", {
666+
params = "<stretchx> <stretchy> <stretchz>",
667+
description = "Scale the current WorldEdit positions and region by a factor of <stretchx>, <stretchy>, <stretchz> along the X, Y, and Z axes, repectively, with position 1 as the origin",
664668
privs = {worldedit=true},
665669
func = function(name, param)
666670
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
@@ -669,20 +673,25 @@ minetest.register_chatcommand("/scale", {
669673
return
670674
end
671675

672-
local factor = tonumber(param)
673-
if not factor or factor ~= math.floor(factor) or factor <= 0 then
676+
local found, _, stretchx, stretchy, stretchz = param:find("^(%d+)%s+(%d+)%s+(%d+)$")
677+
if found == nil then
678+
worldedit.player_notify(name, "invalid usage: " .. param)
679+
return
680+
end
681+
stretchx, stretchy, stretchz = tonumber(stretchx), tonumber(stretchy), tonumber(stretchz)
682+
if stretchx == 0 or stretchy == 0 or stretchz == 0 then
674683
worldedit.player_notify(name, "invalid scaling factor: " .. param)
675684
end
676685

677-
local count, pos1, pos2 = worldedit.scale(pos1, pos2, factor)
686+
local count, pos1, pos2 = worldedit.stretch(pos1, pos2, stretchx, stretchy, stretchz)
678687

679688
--reset markers to scaled positions
680689
worldedit.pos1[name] = pos1
681690
worldedit.pos2[name] = pos2
682691
worldedit.mark_pos1(name)
683692
worldedit.mark_pos2(name)
684693

685-
worldedit.player_notify(name, count .. " nodes scaled")
694+
worldedit.player_notify(name, count .. " nodes stretched")
686695
end,
687696
})
688697

@@ -919,6 +928,10 @@ minetest.register_chatcommand("/save", {
919928
worldedit.player_notify(name, "invalid usage: " .. param)
920929
return
921930
end
931+
if not string.find(param, "^[%w \t.,+-_=!@#$%%^&*()%[%]{};'\"]+$") then
932+
worldedit.player_notify(name, "invalid file name: " .. param)
933+
return
934+
end
922935

923936
local result, count = worldedit.serialize(pos1, pos2)
924937

@@ -953,6 +966,10 @@ minetest.register_chatcommand("/allocate", {
953966
worldedit.player_notify(name, "invalid usage: " .. param)
954967
return
955968
end
969+
if not string.find(param, "^[%w \t.,+-_=!@#$%%^&*()%[%]{};'\"]+$") then
970+
worldedit.player_notify(name, "invalid file name: " .. param)
971+
return
972+
end
956973

957974
local filename = minetest.get_worldpath() .. "/schems/" .. param .. ".we"
958975
local file, err = io.open(filename, "rb")
@@ -993,6 +1010,10 @@ minetest.register_chatcommand("/load", {
9931010
worldedit.player_notify(name, "invalid usage: " .. param)
9941011
return
9951012
end
1013+
if not string.find(param, "^[%w \t.,+-_=!@#$%%^&*()%[%]{};'\"]+$") then
1014+
worldedit.player_notify(name, "invalid file name: " .. param)
1015+
return
1016+
end
9961017

9971018
--find the file in the world path
9981019
local testpaths = {

0 commit comments

Comments
 (0)
Please sign in to comment.