Skip to content

Commit edd85cd

Browse files
luk3yxMoNTE48
authored andcommitted
Add /move_area and /resize_area
1 parent e8ee792 commit edd85cd

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ Commands
101101

102102
* `/area_open <ID>` -- Toggle open/closed the specified area for everyone.
103103

104+
* `/move_area <ID> <X|Y|Z> <Amount>` -- Moves an area in the specified
105+
direction.
106+
For example, to move area 1 west by 10 nodes:
107+
108+
/move_area 1 X -10
109+
110+
* `/resize_area <ID> <X|Y|Z> <Amount>` -- Resizes an area.
111+
104112
License
105113
-------
106114

chatcommands.lua

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
local S = areas.S
2+
local posLimit = assert(areas.posLimit)
3+
areas.posLimit = nil
24

35
minetest.register_chatcommand("protect", {
46
params = S("<AreaName>"),
@@ -493,3 +495,61 @@ minetest.register_chatcommand("area_pvp", {
493495
not canPvP and S("enabled") or S("disabled"), id)
494496
end
495497
})
498+
499+
local function move_or_resize_area(name, param, resize)
500+
local id, dir, amount = param:match("^(%d+)%s([XYZxyz])%s([%-%d]+)$")
501+
amount = tonumber(amount)
502+
if not amount then
503+
return false, S("Invalid usage, see /help @1.", resize and "resize_area" or "move_area")
504+
end
505+
506+
id = tonumber(id)
507+
if not id then
508+
return false, S("That area doesn't exist.")
509+
end
510+
511+
local area = areas.areas[id]
512+
if not area or not areas:isAreaOwner(id, name) then
513+
return false, S("Area @1 does not exist or is not owned by you.", id)
514+
end
515+
516+
local delta = {x = 0, y = 0, z = 0}
517+
delta[dir:lower()] = amount
518+
local pos1, pos2 = vector.sort(area.pos1, area.pos2)
519+
if not resize then
520+
pos1 = posLimit(vector.add(pos1, delta))
521+
end
522+
523+
pos2 = posLimit(vector.add(pos2, delta))
524+
local ok, err = areas:canPlayerAddArea(pos1, pos2, name)
525+
if not ok then
526+
return false, S("You can't move that area there: @1", err)
527+
end
528+
529+
if pos2.x < pos1.x or pos2.y < pos1.y or pos2.z < pos1.z then
530+
return false, S("You can't make that area that small.")
531+
end
532+
533+
areas:move(id, area, pos1, pos2)
534+
535+
-- TODO: Consider only calling areas:save() here once every few seconds
536+
areas:save()
537+
538+
return true, resize and S("Area resized.") or S("Area moved.")
539+
end
540+
541+
minetest.register_chatcommand("move_area", {
542+
params = S("<ID>").." "..S("<X|Y|Z>").." "..S("<Amount>"),
543+
description = S("Moves an area"),
544+
func = function(name, param)
545+
return move_or_resize_area(name, param, false)
546+
end
547+
})
548+
549+
minetest.register_chatcommand("resize_area", {
550+
params = S("<ID>").." "..S("<X|Y|Z>").." "..S("<Amount>"),
551+
description = S("Resizes an area"),
552+
func = function(name, param)
553+
return move_or_resize_area(name, param, true)
554+
end
555+
})

init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ areas.modpath = minetest.get_modpath("areas")
1515
dofile(areas.modpath.."/settings.lua")
1616
dofile(areas.modpath.."/api.lua")
1717
dofile(areas.modpath.."/internal.lua")
18-
dofile(areas.modpath.."/chatcommands.lua")
1918
dofile(areas.modpath.."/pos.lua")
19+
dofile(areas.modpath.."/chatcommands.lua")
2020
dofile(areas.modpath.."/interact.lua")
2121
dofile(areas.modpath.."/hud.lua")
2222
dofile(areas.modpath.."/protector.lua")

pos.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ local function posLimit(pos)
2222
}
2323
end
2424

25+
-- For chatcommands.lua
26+
areas.posLimit = posLimit
27+
2528
minetest.register_chatcommand("select_area", {
2629
params = S("<ID>"),
2730
description = S("Select an area by ID."),

0 commit comments

Comments
 (0)