Allow relative coordinates in /area_pos[12] (#77)

This commit is contained in:
1F616EMO~nya 2024-08-08 03:33:36 +08:00 committed by GitHub
parent 9024be24eb
commit 9a5cdb2822
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 74 additions and 21 deletions

View File

@ -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 nexiste pas.
Unable to get position.=Impossible dobtenir 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.=

View File

@ -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.=

View File

@ -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.=

View File

@ -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或之后的版本。

View File

@ -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或之後的版本。

View File

@ -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
pos.lua
View File

@ -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."),
@ -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",
@ -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",