Skip to content

Commit

Permalink
improve: '/pos' command parsing for multiple formats
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dudantas committed Nov 25, 2023
1 parent 1a8e295 commit 3467961
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions data/scripts/talkactions/gm/position.lua
Original file line number Diff line number Diff line change
@@ -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)

Check warning on line 30 in data/scripts/talkactions/gm/position.lua

View workflow job for this annotation

GitHub Actions / luacheck

[luacheck] data/scripts/talkactions/gm/position.lua#L30

shadowing upvalue 'position' on line 1
Raw output
data/scripts/talkactions/gm/position.lua:30:9: shadowing upvalue 'position' on line 1
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(" ")
Expand Down

0 comments on commit 3467961

Please sign in to comment.