Skip to content

Commit

Permalink
Allow relative coordinates in /area_pos[12] (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emojigit committed Aug 7, 2024
1 parent 9024be2 commit 9a5cdb2
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 21 deletions.
2 changes: 2 additions & 0 deletions locale/areas.fr.tr
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ Set area protection region, position 1, or position 2 by punching nodes, or disp
The area @1 does not exist.=La zone @1 n’existe pas.
Unable to get position.=Impossible d’obtenir la position.
Unknown subcommand: @1=Sous-commande inconnue : @1

Relative coordinates is not supported on this server. Please upgrade Minetest to 5.7.0 or newer versions.=
2 changes: 2 additions & 0 deletions locale/areas.it.tr
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ Set area protection region, position 1, or position 2 by punching nodes, or disp
The area @1 does not exist.=L'area @1 non esiste.
Unable to get position.=Impossibile ottenere la posizione.
Unknown subcommand: @1=Sotto-comando sconosciuto: @1

Relative coordinates is not supported on this server. Please upgrade Minetest to 5.7.0 or newer versions.=
2 changes: 2 additions & 0 deletions locale/areas.ru.tr
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ Set area protection region, position 1, or position 2 by punching nodes, or disp
The area @1 does not exist.=Территория @1 не существует.
Unable to get position.=Не удалось получить позицию.
Unknown subcommand: @1=Неизвестная под-команда/аргумент.

Relative coordinates is not supported on this server. Please upgrade Minetest to 5.7.0 or newer versions.=
4 changes: 3 additions & 1 deletion locale/areas.zh_CN.tr
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,6 @@ Set area protection region, position 1, or position 2 by punching nodes, or disp

The area @1 does not exist.=保护区 @1 不存在。
Unable to get position.=无法获得座标。
Unknown subcommand: @1=子指令不明:@1
Unknown subcommand: @1=子指令不明:@1

Relative coordinates is not supported on this server. Please upgrade Minetest to 5.7.0 or newer versions.=此服务器不支援相对座标。请更新Minetest至5.7.0或之后的版本。
2 changes: 2 additions & 0 deletions locale/areas.zh_TW.tr
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ Set area protection region, position 1, or position 2 by punching nodes, or disp
The area @1 does not exist.=保護區 @1 不存在。
Unable to get position.=無法獲得座標。
Unknown subcommand: @1=子指令不明:@1

Relative coordinates is not supported on this server. Please upgrade Minetest to 5.7.0 or newer versions.=此伺服器不支援相對座標。請更新Minetest至5.7.0或之後的版本。
2 changes: 2 additions & 0 deletions locale/template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ Set area protection region, position 1, or position 2 by punching nodes, or disp
The area @1 does not exist.=
Unable to get position.=
Unknown subcommand: @1=

Relative coordinates is not supported on this server. Please upgrade Minetest to 5.7.0 or newer versions.=
81 changes: 61 additions & 20 deletions pos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,37 @@ local function posLimit(pos)
}
end

local parse_relative_pos

if minetest.parse_relative_number then
parse_relative_pos = function(x_str, y_str, z_str, pos)

local x = pos and minetest.parse_relative_number(x_str, pos.x)
or tonumber(x_str)
local y = pos and minetest.parse_relative_number(y_str, pos.y)
or tonumber(y_str)
local z = pos and minetest.parse_relative_number(z_str, pos.z)
or tonumber(z_str)
if x and y and z then
return vector.new(x, y, z)
end
end
else
parse_relative_pos = function(x_str, y_str, z_str, pos)
local x = tonumber(x_str)
local y = tonumber(y_str)
local z = tonumber(z_str)
if x and y and z then
return vector.new(x, y, z)
elseif string.sub(x_str, 1, 1) == "~"
or string.sub(y_str, 1, 1) == "~"
or string.sub(z_str, 1, 1) == "~" then
return nil, S("Relative coordinates is not supported on this server. " ..
"Please upgrade Minetest to 5.7.0 or newer versions.")
end
end
end

minetest.register_chatcommand("select_area", {
params = S("<ID>"),
description = S("Select an area by ID."),
Expand All @@ -47,20 +78,25 @@ minetest.register_chatcommand("area_pos1", {
privs = {},
func = function(name, param)
local pos
local found, _, x, y, z = param:find(
"^(-?%d+)[, ](-?%d+)[, ](-?%d+)$")
local player = minetest.get_player_by_name(name)
if player then
pos = vector.round(player:get_pos())
end
local found, _, x_str, y_str, z_str = param:find(
"^(~?-?%d*)[, ](~?-?%d*)[, ](~?-?%d*)$")
if found then
pos = {x=tonumber(x), y=tonumber(y), z=tonumber(z)}
elseif param == "" then
local player = minetest.get_player_by_name(name)
if player then
pos = player:get_pos()
else
return false, S("Unable to get position.")
local get_pos, reason = parse_relative_pos(x_str, y_str, z_str, pos)
if get_pos then
pos = get_pos
elseif not get_pos and reason then
return false, reason
end
else
elseif param ~= "" then
return false, S("Invalid usage, see /help @1.", "area_pos1")
end
if not pos then
return false, S("Unable to get position.")
end
pos = posLimit(vector.round(pos))
areas:setPos1(name, pos)
return true, S("Area position @1 set to @2", "1",
Expand All @@ -74,20 +110,25 @@ minetest.register_chatcommand("area_pos2", {
.." location or the one specified", "2"),
func = function(name, param)
local pos
local found, _, x, y, z = param:find(
"^(-?%d+)[, ](-?%d+)[, ](-?%d+)$")
local player = minetest.get_player_by_name(name)
if player then
pos = vector.round(player:get_pos())
end
local found, _, x_str, y_str, z_str = param:find(
"^(~?-?%d*)[, ](~?-?%d*)[, ](~?-?%d*)$")
if found then
pos = {x=tonumber(x), y=tonumber(y), z=tonumber(z)}
elseif param == "" then
local player = minetest.get_player_by_name(name)
if player then
pos = player:get_pos()
else
return false, S("Unable to get position.")
local get_pos, reason = parse_relative_pos(x_str, y_str, z_str, pos)
if get_pos then
pos = get_pos
elseif not get_pos and reason then
return false, reason
end
else
elseif param ~= "" then
return false, S("Invalid usage, see /help @1.", "area_pos2")
end
if not pos then
return false, S("Unable to get position.")
end
pos = posLimit(vector.round(pos))
areas:setPos2(name, pos)
return true, S("Area position @1 set to @2", "2",
Expand Down

0 comments on commit 9a5cdb2

Please sign in to comment.