local globalSavePlayers = GlobalEvent("GlobalSavePlayers")
function globalSavePlayers.onThink()
for _, player in ipairs(Game.getPlayers()) do
player:save()
end
print("All players have been automatically saved.")
return true
end
globalSavePlayers:interval(600000) -- Periodic global save every 10 minutes (600,000 milliseconds)
globalSavePlayers:register()
local CONTAINER_POSITION = 65535
local SAVE_COOLDOWN_STORAGE = 99999
local SAVE_COOLDOWN_TIME = 5 -- Cooldown in seconds to avoid frequent saves
local DEBUG_MODE = true -- false or true
local function delayedSave(playerId)
local player = Player(playerId)
if player then
player:save()
if DEBUG_MODE then
print(string.format("Player %s saved after item movement.", player:getName()))
end
end
end
local saveOnMoveItem = EventCallback
function saveOnMoveItem.onMoveItem(player, item, fromPosition, toPosition, fromCylinder, toCylinder)
if not player or not item or item:getId() <= 0 then
if DEBUG_MODE then
print("Invalid item or player detected during movement.")
end
return true
end
fromPosition = type(fromPosition) == "table" and fromPosition or {x = -1, y = -1, z = -1}
toPosition = type(toPosition) == "table" and toPosition or {x = -1, y = -1, z = -1}
local lastSaveTime = player:getStorageValue(SAVE_COOLDOWN_STORAGE)
if lastSaveTime ~= -1 and os.time() < lastSaveTime then
if DEBUG_MODE then
print(string.format("Skipping save for player %s due to cooldown. Item ID: %d, From: (%d, %d, %d), To: (%d, %d, %d).",
player:getName(), item:getId(), fromPosition.x, fromPosition.y, fromPosition.z, toPosition.x, toPosition.y, toPosition.z))
end
return true
end
player:setStorageValue(SAVE_COOLDOWN_STORAGE, os.time() + SAVE_COOLDOWN_TIME)
addEvent(delayedSave, 1000, player:getId())
return true
end
saveOnMoveItem:register()