local STORAGE = {
DEATH_X = 90000,
DEATH_Y = 90001,
DEATH_Z = 90002,
TP_DAY = 90003,
TP_COUNT = 90004
}
local DAILY_LIMIT = 2
-- Zapis pozycji śmierci
local deathEvent = CreatureEvent("StoreDeathPosition")
function deathEvent.onPrepareDeath(creature, killer)
local player = creature:getPlayer()
if not player then
return true
end
local pos = player:getPosition()
player:setStorageValue(STORAGE.DEATH_X, pos.x)
player:setStorageValue(STORAGE.DEATH_Y, pos.y)
player:setStorageValue(STORAGE.DEATH_Z, pos.z)
return true
end
deathEvent:register()
-- Action przedmiotu: teleport do ostatniej śmierci z limitem 2/dzień
local teleportAction = Action()
function teleportAction.onUse(player, item, fromPosition, target, toPosition, isHotkey)
local lastZ = player:getStorageValue(STORAGE.DEATH_Z)
if lastZ == -1 then
player:sendCancelMessage("Brak zapisanej pozycji śmierci.")
return true
end
local today = tonumber(os.date("%j"))
local usedDay = player:getStorageValue(STORAGE.TP_DAY)
local usedCount = player:getStorageValue(STORAGE.TP_COUNT)
if usedDay ~= today then
usedDay = today
usedCount = 0
end
if usedCount >= DAILY_LIMIT then
player:sendCancelMessage("Możesz użyć tego przedmiotu tylko dwa razy dziennie.")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
return true
end
local pos = Position(
player:getStorageValue(STORAGE.DEATH_X),
player:getStorageValue(STORAGE.DEATH_Y),
lastZ
)
if pos.x <= 0 or pos.y <= 0 then
player:sendCancelMessage("Nieprawidłowa zapisana pozycja.")
return true
end
local fromPos = player:getPosition()
player:teleportTo(pos)
pos:sendMagicEffect(CONST_ME_TELEPORT)
fromPos:sendMagicEffect(CONST_ME_TELEPORT)
player:setStorageValue(STORAGE.TP_DAY, today)
player:setStorageValue(STORAGE.TP_COUNT, usedCount + 1)
return true
end
teleportAction:id(12345) -- <= PODMIEŃ na ID swojego itemu
teleportAction:register()
-- Rejestracja eventu na logowaniu (dla wszystkich graczy)
local loginEvent = CreatureEvent("RegisterStoreDeathPosition")
function loginEvent.onLogin(player)
player:registerEvent("StoreDeathPosition")
return true
end
loginEvent:register()