From edd85cd71ef33712ae75f13cd66d730d12162992 Mon Sep 17 00:00:00 2001 From: luk3yx Date: Tue, 9 Aug 2022 17:10:34 +1200 Subject: [PATCH 1/3] Add /move_area and /resize_area --- README.md | 8 +++++++ chatcommands.lua | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 2 +- pos.lua | 3 +++ 4 files changed, 72 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c9523c4..c2348ee 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,14 @@ Commands * `/area_open ` -- Toggle open/closed the specified area for everyone. + * `/move_area ` -- Moves an area in the specified + direction. + For example, to move area 1 west by 10 nodes: + + /move_area 1 X -10 + + * `/resize_area ` -- Resizes an area. + License ------- diff --git a/chatcommands.lua b/chatcommands.lua index 2dddb7d..0649eae 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -1,4 +1,6 @@ local S = areas.S +local posLimit = assert(areas.posLimit) +areas.posLimit = nil minetest.register_chatcommand("protect", { params = S(""), @@ -493,3 +495,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") + 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", { + params = S("").." "..S("").." "..S(""), + 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("").." "..S("").." "..S(""), + description = S("Resizes an area"), + func = function(name, param) + return move_or_resize_area(name, param, true) + end +}) diff --git a/init.lua b/init.lua index cd57284..97e7496 100644 --- a/init.lua +++ b/init.lua @@ -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") diff --git a/pos.lua b/pos.lua index 3330ea2..d81d8ca 100644 --- a/pos.lua +++ b/pos.lua @@ -22,6 +22,9 @@ local function posLimit(pos) } end +-- For chatcommands.lua +areas.posLimit = posLimit + minetest.register_chatcommand("select_area", { params = S(""), description = S("Select an area by ID."), From 5862e71d73a32cbc8c6517747c31164f29a2a4f1 Mon Sep 17 00:00:00 2001 From: luk3yx Date: Fri, 26 Aug 2022 10:16:54 +1200 Subject: [PATCH 2/3] Rename /move_area to /move_area_by so it doesn't conflict with another command --- README.md | 2 +- chatcommands.lua | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c2348ee..b99f3b1 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ Commands * `/area_open ` -- Toggle open/closed the specified area for everyone. - * `/move_area ` -- Moves an area in the specified + * `/move_area_rel ` -- Moves an area in the specified direction. For example, to move area 1 west by 10 nodes: diff --git a/chatcommands.lua b/chatcommands.lua index 0649eae..9a79068 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -500,7 +500,7 @@ 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") + return false, S("Invalid usage, see /help @1.", resize and "resize_area" or "move_area_by") end id = tonumber(id) @@ -538,7 +538,7 @@ local function move_or_resize_area(name, param, resize) return true, resize and S("Area resized.") or S("Area moved.") end -minetest.register_chatcommand("move_area", { +minetest.register_chatcommand("move_area_by", { params = S("").." "..S("").." "..S(""), description = S("Moves an area"), func = function(name, param) From e4607e4eb4d8ec6563185e6b41317a698615ac30 Mon Sep 17 00:00:00 2001 From: Maksym H Date: Fri, 26 Aug 2022 23:40:03 +0300 Subject: [PATCH 3/3] Use the HUD API directly --- hud.lua | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/hud.lua b/hud.lua index 136ecd4..271bfbf 100644 --- a/hud.lua +++ b/hud.lua @@ -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:") @@ -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