From 01c571bdc843eb574de0386b62573042c25a7389 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Wed, 3 Jul 2024 06:57:48 +0800 Subject: [PATCH 1/3] Allow relative coordinates in /area_pos[12] --- pos.lua | 56 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/pos.lua b/pos.lua index 7a9cbd2..e6e9d35 100644 --- a/pos.lua +++ b/pos.lua @@ -47,20 +47,28 @@ 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 = 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 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 + pos = { x = x, y = y, z = z } 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 +82,28 @@ 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 = 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 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 + pos = { x = x, y = y, z = z } 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", From 43f02ba6ddaa0f15bca35a14f833cc3cdee395d3 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Wed, 3 Jul 2024 07:08:29 +0800 Subject: [PATCH 2/3] Round player position --- pos.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pos.lua b/pos.lua index e6e9d35..0026869 100644 --- a/pos.lua +++ b/pos.lua @@ -49,7 +49,7 @@ minetest.register_chatcommand("area_pos1", { local pos local player = minetest.get_player_by_name(name) if player then - pos = player:get_pos() + pos = vector.round(player:get_pos()) end local found, _, x_str, y_str, z_str = param:find( "^(~?-?%d*)[, ](~?-?%d*)[, ](~?-?%d*)$") @@ -84,7 +84,7 @@ minetest.register_chatcommand("area_pos2", { local pos local player = minetest.get_player_by_name(name) if player then - pos = player:get_pos() + pos = vector.round(player:get_pos()) end local found, _, x_str, y_str, z_str = param:find( "^(~?-?%d*)[, ](~?-?%d*)[, ](~?-?%d*)$") From a9b2c7ef944f17d5538a7da7622ee686c55b945a Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Mon, 8 Jul 2024 06:46:11 +0800 Subject: [PATCH 3/3] Error message for < 5.7.0 --- locale/areas.fr.tr | 2 ++ locale/areas.it.tr | 2 ++ locale/areas.ru.tr | 2 ++ locale/areas.zh_CN.tr | 4 ++- locale/areas.zh_TW.tr | 2 ++ locale/template.txt | 2 ++ pos.lua | 57 +++++++++++++++++++++++++++++++------------ 7 files changed, 54 insertions(+), 17 deletions(-) diff --git a/locale/areas.fr.tr b/locale/areas.fr.tr index bbbc089..b60726c 100644 --- a/locale/areas.fr.tr +++ b/locale/areas.fr.tr @@ -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.= diff --git a/locale/areas.it.tr b/locale/areas.it.tr index 006b04d..7fe77da 100644 --- a/locale/areas.it.tr +++ b/locale/areas.it.tr @@ -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.= diff --git a/locale/areas.ru.tr b/locale/areas.ru.tr index 43d8634..11e8c74 100644 --- a/locale/areas.ru.tr +++ b/locale/areas.ru.tr @@ -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.= diff --git a/locale/areas.zh_CN.tr b/locale/areas.zh_CN.tr index 57f021b..2ac2be1 100644 --- a/locale/areas.zh_CN.tr +++ b/locale/areas.zh_CN.tr @@ -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 \ No newline at end of file +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或之后的版本。 \ No newline at end of file diff --git a/locale/areas.zh_TW.tr b/locale/areas.zh_TW.tr index e53ecbb..4a2c33f 100644 --- a/locale/areas.zh_TW.tr +++ b/locale/areas.zh_TW.tr @@ -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或之後的版本。 diff --git a/locale/template.txt b/locale/template.txt index 2026c5e..3508101 100644 --- a/locale/template.txt +++ b/locale/template.txt @@ -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.= diff --git a/pos.lua b/pos.lua index 0026869..f512eac 100644 --- a/pos.lua +++ b/pos.lua @@ -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(""), description = S("Select an area by ID."), @@ -54,14 +85,11 @@ minetest.register_chatcommand("area_pos1", { local found, _, x_str, y_str, z_str = param:find( "^(~?-?%d*)[, ](~?-?%d*)[, ](~?-?%d*)$") if found then - 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 - pos = { x = x, y = y, z = z } + 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 elseif param ~= "" then return false, S("Invalid usage, see /help @1.", "area_pos1") @@ -89,14 +117,11 @@ minetest.register_chatcommand("area_pos2", { local found, _, x_str, y_str, z_str = param:find( "^(~?-?%d*)[, ](~?-?%d*)[, ](~?-?%d*)$") if found then - 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 - pos = { x = x, y = y, z = z } + 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 elseif param ~= "" then return false, S("Invalid usage, see /help @1.", "area_pos2")