Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add /move_area and /resize_area #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ Commands

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

* `/move_area_rel <ID> <X|Y|Z> <Amount>` -- Moves an area in the specified
direction.
For example, to move area 1 west by 10 nodes:

/move_area 1 X -10

* `/resize_area <ID> <X|Y|Z> <Amount>` -- Resizes an area.

License
-------

Expand Down
60 changes: 60 additions & 0 deletions chatcommands.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local S = areas.S
local posLimit = assert(areas.posLimit)
areas.posLimit = nil

minetest.register_chatcommand("protect", {
params = S("<AreaName>"),
Expand Down Expand Up @@ -494,3 +496,61 @@ minetest.register_chatcommand("area_pvp", {
not canPvP and S("enabled") or S("disabled"), id)
end
})

local function move_or_resize_area(name, param, resize)
local id, dir, amount = param:match("^(%d+)%s([XYZxyz])%s([%-%d]+)$")
amount = tonumber(amount)
if not amount then
return false, S("Invalid usage, see /help @1.", resize and "resize_area" or "move_area_by")
end

id = tonumber(id)
if not id then
return false, S("That area doesn't exist.")
end

local area = areas.areas[id]
if not area or not areas:isAreaOwner(id, name) then
return false, S("Area @1 does not exist or is not owned by you.", id)
end

local delta = {x = 0, y = 0, z = 0}
delta[dir:lower()] = amount
local pos1, pos2 = vector.sort(area.pos1, area.pos2)
if not resize then
pos1 = posLimit(vector.add(pos1, delta))
end

pos2 = posLimit(vector.add(pos2, delta))
local ok, err = areas:canPlayerAddArea(pos1, pos2, name)
if not ok then
return false, S("You can't move that area there: @1", err)
end

if pos2.x < pos1.x or pos2.y < pos1.y or pos2.z < pos1.z then
return false, S("You can't make that area that small.")
end

areas:move(id, area, pos1, pos2)

-- TODO: Consider only calling areas:save() here once every few seconds
areas:save()

return true, resize and S("Area resized.") or S("Area moved.")
end

minetest.register_chatcommand("move_area_by", {
params = S("<ID>").." "..S("<X|Y|Z>").." "..S("<Amount>"),
description = S("Moves an area"),
func = function(name, param)
return move_or_resize_area(name, param, false)
end
})

minetest.register_chatcommand("resize_area", {
params = S("<ID>").." "..S("<X|Y|Z>").." "..S("<Amount>"),
description = S("Resizes an area"),
func = function(name, param)
return move_or_resize_area(name, param, true)
end
})
32 changes: 18 additions & 14 deletions hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ areas.hud = {}
local vround = vector.round
local tconcat = table.concat

hud.register("areas", {
hud_elem_type = "text",
position = {x = 0, y = 1},
alignment = {x = 1, y = -1},
offset = {x = 8, y = -8},
scale = {x = 200, y = 60},
text = "",
number = 0xFFFFFF
})

local function update_hud(player, name, pos)
local areaStrings = {
S("Areas:")
Expand All @@ -40,10 +30,24 @@ local function update_hud(player, name, pos)
-- "Areas:" text has index 1
local areaString = #areaStrings > 1 and tconcat(areaStrings, "\n") or ""

local phud = areas.hud[name] or {}
if not phud.oldAreas or phud.oldAreas ~= areaString then
hud.change_item(player, "areas", {text = areaString})
phud.oldAreas = areaString
local hud = areas.hud[name]
if not hud then
hud = {}
areas.hud[name] = hud
hud.areasId = player:hud_add({
hud_elem_type = "text",
name = "Areas",
number = 0xFFFFFF,
position = {x = 0, y = 1},
offset = {x = 8, y = -8},
scale = {x = 200, y = 60},
alignment = {x = 1, y = -1},
text = areaString
})
hud.oldAreas = areaString
elseif hud.oldAreas ~= areaString then
player:hud_change(hud.areasId, "text", areaString)
hud.oldAreas = areaString
end
end

Expand Down
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ areas.modpath = minetest.get_modpath("areas")
dofile(areas.modpath.."/settings.lua")
dofile(areas.modpath.."/api.lua")
dofile(areas.modpath.."/internal.lua")
dofile(areas.modpath.."/chatcommands.lua")
dofile(areas.modpath.."/pos.lua")
dofile(areas.modpath.."/chatcommands.lua")
dofile(areas.modpath.."/interact.lua")
dofile(areas.modpath.."/hud.lua")
dofile(areas.modpath.."/protector.lua")
Expand Down
3 changes: 3 additions & 0 deletions pos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ local function posLimit(pos)
}
end

-- For chatcommands.lua
areas.posLimit = posLimit

minetest.register_chatcommand("select_area", {
params = S("<ID>"),
description = S("Select an area by ID."),
Expand Down