]> git.lizzy.rs Git - worldedit.git/blob - worldedit_commands/cuboid.lua
Rewrite command registration
[worldedit.git] / worldedit_commands / cuboid.lua
1 worldedit.register_command("outset", {
2         params = "[h|v] <amount>",
3         description = "Outset the selected region.",
4         privs = {worldedit=true},
5         require_pos = 2,
6         parse = function(param)
7                 local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
8                 if find == nil then
9                         return false
10                 end
11
12                 local hv_test = dir:find("[^hv]+")
13                 if hv_test ~= nil then
14                         return false, "Invalid direction."
15                 end
16
17                 return true, dir, tonumber(amount)
18         end,
19         func = function(name, dir, amount)
20                 if dir == "" or dir == "hv" or dir == "vh" then
21                         assert(worldedit.cuboid_volumetric_expand(name, amount))
22                 elseif dir == "h" then
23                         assert(worldedit.cuboid_linear_expand(name, 'x', 1, amount))
24                         assert(worldedit.cuboid_linear_expand(name, 'x', -1, amount))
25                         assert(worldedit.cuboid_linear_expand(name, 'z', 1, amount))
26                         assert(worldedit.cuboid_linear_expand(name, 'z', -1, amount))
27                 elseif dir == "v" then
28                         assert(worldedit.cuboid_linear_expand(name, 'y', 1, amount))
29                         assert(worldedit.cuboid_linear_expand(name, 'y', -1, amount))
30                 else
31                         return false, "Invalid number of arguments"
32                 end
33
34                 worldedit.marker_update(name)
35                 return true, "Region outset by " .. amount .. " blocks"
36       end,
37 })
38
39
40 worldedit.register_command("inset", {
41         params = "[h|v] <amount>",
42         description = "Inset the selected region.",
43         privs = {worldedit=true},
44         require_pos = 2,
45         parse = function(param)
46                 local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
47                 if find == nil then
48                         return false
49                 end
50
51                 local hv_test = dir:find("[^hv]+")
52                 if hv_test ~= nil then
53                         return false, "Invalid direction."
54                 end
55
56                 return true, dir, tonumber(amount)
57         end,
58         func = function(name, dir, amount)
59                 if dir == "" or dir == "vh" or dir == "hv" then
60                         assert(worldedit.cuboid_volumetric_expand(name, -amount))
61                 elseif dir == "h" then
62                         assert(worldedit.cuboid_linear_expand(name, 'x', 1, -amount))
63                         assert(worldedit.cuboid_linear_expand(name, 'x', -1, -amount))
64                         assert(worldedit.cuboid_linear_expand(name, 'z', 1, -amount))
65                         assert(worldedit.cuboid_linear_expand(name, 'z', -1, -amount))
66                 elseif dir == "v" then
67                         assert(worldedit.cuboid_linear_expand(name, 'y', 1, -amount))
68                         assert(worldedit.cuboid_linear_expand(name, 'y', -1, -amount))
69                 else
70                         return false, "Invalid number of arguments"
71                 end
72
73                 worldedit.marker_update(name)
74                 return true, "Region inset by " .. amount .. " blocks"
75       end,
76 })
77
78
79 worldedit.register_command("shift", {
80         params = "[x|y|z|?|up|down|left|right|front|back] [+|-]<amount>",
81         description = "Moves the selection region. Does not move contents.",
82         privs = {worldedit=true},
83         require_pos = 2,
84         parse = function(param)
85                 local find, _, direction, amount = param:find("([%?%l]+)%s*([+-]?%d+)")
86                 if find == nil then
87                         return false
88                 end
89
90                 return true, direction, tonumber(amount)
91         end,
92         func = function(name, direction, amount)
93                 local axis, dir
94                 if direction == "x" or direction == "y" or direction == "z" then
95                         axis, dir = direction, 1
96                 elseif direction == "?" then
97                         axis, dir = worldedit.player_axis(name)
98                 else
99                         axis, dir = worldedit.translate_direction(name, direction)
100                 end
101
102                 if axis == nil or dir == nil then
103                         return false, "Invalid if looking straight up or down"
104                 end
105
106                 assert(worldedit.cuboid_shift(name, axis, amount * dir))
107                 worldedit.marker_update(name)
108
109                 return true, "Region shifted by " .. amount .. " nodes"
110       end,
111 })
112
113
114 worldedit.register_command("expand", {
115         params = "[+|-]<x|y|z|?|up|down|left|right|front|back> <amount> [reverse-amount]",
116         description = "expand the selection in one or two directions at once",
117         privs = {worldedit=true},
118         require_pos = 2,
119         parse = function(param)
120                 local find, _, sign, direction, amount,
121                                 rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
122                 if find == nil then
123                         return false
124                 end
125
126                 if rev_amount == "" then
127                         rev_amount = "0"
128                 end
129
130                 return true, sign, direction, tonumber(amount), tonumber(rev_amount)
131         end,
132         func = function(name, sign, direction, amount, rev_amount)
133                 local absolute = direction:find("[xyz?]")
134                 local dir, axis
135
136                 if absolute == nil then
137                         axis, dir = worldedit.translate_direction(name, direction)
138
139                         if axis == nil or dir == nil then
140                                 return false, "Invalid if looking straight up or down"
141                         end
142                 else
143                         if direction == "?" then
144                                 axis, dir = worldedit.player_axis(name)
145                         else
146                                 axis = direction
147                                 dir = 1
148                         end
149                 end
150
151                 if sign == "-" then
152                         dir = -dir
153                 end
154
155                 worldedit.cuboid_linear_expand(name, axis, dir, amount)
156                 worldedit.cuboid_linear_expand(name, axis, -dir, rev_amount)
157                 worldedit.marker_update(name)
158                 return true, "Region expanded by " .. (amount + rev_amount) .. " nodes"
159         end,
160 })
161
162
163 worldedit.register_command("contract", {
164         params = "[+|-]<x|y|z|?|up|down|left|right|front|back> <amount> [reverse-amount]",
165         description = "contract the selection in one or two directions at once",
166         privs = {worldedit=true},
167         require_pos = 2,
168         parse = function(param)
169                 local find, _, sign, direction, amount,
170                                 rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
171                 if find == nil then
172                         return false
173                 end
174
175                 if rev_amount == "" then
176                         rev_amount = "0"
177                 end
178
179                 return true, sign, direction, tonumber(amount), tonumber(rev_amount)
180         end,
181         func = function(name, sign, direction, amount, rev_amount)
182                 local absolute = direction:find("[xyz?]")
183                 local dir, axis
184
185                 if absolute == nil then
186                         axis, dir = worldedit.translate_direction(name, direction)
187
188                         if axis == nil or dir == nil then
189                                 return false, "Invalid if looking straight up or down"
190                         end
191                 else
192                         if direction == "?" then
193                                 axis, dir = worldedit.player_axis(name)
194                         else
195                                 axis = direction
196                                 dir = 1
197                         end
198                 end
199
200                 if sign == "-" then
201                         dir = -dir
202                 end
203
204                 worldedit.cuboid_linear_expand(name, axis, dir, -amount)
205                 worldedit.cuboid_linear_expand(name, axis, -dir, -rev_amount)
206                 worldedit.marker_update(name)
207                 return true, "Region contracted by " .. (amount + rev_amount) .. " nodes"
208         end,
209 })