From 34679611427b49b9e182958102781ce84ed2bef4 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Sat, 25 Nov 2023 16:20:49 -0300 Subject: [PATCH] improve: '/pos' command parsing for multiple formats Refactored the '/pos' command in the game's scripting to support multiple input formats more robustly. The command now accurately interprets positions from table format '{x = ..., y = ..., z = ...}', the 'Position(..., ..., ...)' format, and the standard 'x, y, z' coordinate format. The update includes improved error handling and clearer user feedback for invalid inputs. --- data/scripts/talkactions/gm/position.lua | 51 +++++++++++++++++------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/data/scripts/talkactions/gm/position.lua b/data/scripts/talkactions/gm/position.lua index f68b4210996..ed43f1070dc 100644 --- a/data/scripts/talkactions/gm/position.lua +++ b/data/scripts/talkactions/gm/position.lua @@ -1,22 +1,45 @@ local position = TalkAction("/pos", "!pos") +local function extractCoordinates(input) + local patterns = { + "{%s*x%s*=%s*(%d+)%s*,%s*y%s*=%s*(%d+)%s*,%s*z%s*=%s*(%d+)%s*}", -- table format + "Position%s*%((%d+)%s*,%s*(%d+)%s*,%s*(%d+)%s*%)", -- Position format + "(%d+)%s*,%s*(%d+)%s*,%s*(%d+)" -- x, y, z format + } + + for _, pattern in ipairs(patterns) do + local x, y, z = string.match(input, pattern) + if x and y and z then + return tonumber(x), tonumber(y), tonumber(z) + end + end +end + function position.onSay(player, words, param) - -- create log - logCommand(player, words, param) + logCommand(player, words, param) -- create log + + if param == "" then + local pos = player:getPosition() + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your current position is: " .. + pos.x .. ", " .. pos.y .. ", " .. pos.z .. ".") + return true + end + + local x, y, z = extractCoordinates(param) + if x and y and z then + local position = Position(x, y, z) + local tile = Tile(position) + if not tile then + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Invalid tile or position. Send a valid position.") + return true + end - local param = string.gsub(param, "%s+", "") - local tile = load("return " .. param)() - local split = param:split(",") - if type(tile) == "table" and tile.x and tile.y and tile.z then - player:teleportTo(Position(tile.x, tile.y, tile.z)) - elseif split and param ~= "" then - player:teleportTo(Position(split[1], split[2], split[3])) - elseif param == "" then - local playerPosition = player:getPosition() - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your current position is: \z - " .. playerPosition.x .. ", " .. playerPosition.y .. ", " .. playerPosition.z .. ".") + player:teleportTo(position) + return true + else + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Invalid position format. Use one of the following formats: \n/pos {x = ..., y = ..., z = ...}\n/pos Position(..., ..., ...)\n/pos x, y, z.") + return true end - return true end position:separator(" ")