From 5ea1755e5511e1e5a7ccc4c063ce51682c7bcf52 Mon Sep 17 00:00:00 2001 From: FFNeko Date: Sun, 15 Nov 2026 09:05:35 -0500 Subject: [PATCH] ZoneTimer: Update to 2.0.0.0 - Add configurable HUD visibility, fades, and timed reminders - Add appearance commands for fonts, colors, alpha, and positioning - Implement independent per-character settings with legacy migration - Preserve zone elapsed time across eligible addon reloads - Add NPC interaction and zoning visibility handling - Reduce unnecessary rendering work and improve lifecycle cleanup - Update README with commands and configuration details --- addons/zonetimer/readme.md | 105 +++- addons/zonetimer/zonetimer.lua | 911 ++++++++++++++++++++++++++++++--- 2 files changed, 935 insertions(+), 81 deletions(-) diff --git a/addons/zonetimer/readme.md b/addons/zonetimer/readme.md index 30e736205e..5f07cb79de 100644 --- a/addons/zonetimer/readme.md +++ b/addons/zonetimer/readme.md @@ -1,15 +1,90 @@ -Commands: - -zonetimer posX # - change the x position - -zonetimer posY # - change the y position - -zonetimer fontsize # - change the fontsize - -zoentimer help - print these instructions in the log - -Original plugin by Krellion. +# ZoneTimer + +Version 2.0.0.0 displays the elapsed real-world time in the current zone. It can stay visible, remain hidden, or briefly appear with optional reminders. + +A fresh installation shows the HUD at X=400, Y=0, in Arial size 12. Reminders are disabled. Settings files are created automatically; do not install a preconfigured `data` directory. + +## Commands + +Use `//zt` or `//zonetimer` in game. In the Windower console, omit the leading `//`. + +| Command | Description | +| --- | --- | +| `//zt` | Print the current zone name and elapsed time to chat. | +| `//zt time` or `//zt t` | Same as `//zt`. | +| `//zt show` | Fade in and remain visible. Save this character's preference. | +| `//zt hide` | Fade out and remain hidden between temporary displays. Save this character's preference. | +| `//zt fade on` / `//zt fade off` | Enable or disable animations. Fades are enabled by default. | +| `//zt reminder 30` | Remind at each 30-minute interval since zone entry or timer reset. Use a positive whole number of minutes, including `1`. | +| `//zt reminder off` | Disable reminders. `0` also works. | +| `//zt reset` | Restart the current zone timer. | +| `//zt posX 400` / `//zt posY 0` | Set the horizontal or vertical position. | +| `//zt fontsize 12` | Set font size, from 1 to 100. Fractional sizes are supported. | +| `//zt font Arial Black` | Set the primary font. Names with spaces are supported; configured fallback fonts are retained. | +| `//zt color 255 255 255` | Set text RGB using three integers from 0 to 255. | +| `//zt bg_color 0 0 0` | Set background RGB using three integers from 0 to 255. | +| `//zt alpha 160` | Set text opacity from 0 (transparent) to 255 (opaque). `text_alpha` is an alias. | +| `//zt bg_alpha 120` | Set background opacity from 0 to 255. | +| `//zt bg show` / `//zt bg hide` | Show or hide the background independently of the text. `background` and `on`/`off` are also accepted. | +| `//zt padding 5` | Set background padding to an integer from 0 to 100 pixels. `pad` is an alias. | +| `//zt stroke 1` | Set outline width from 0 to 10. `off` removes it; `on` sets width 1. | +| `//zt bold on` / `//zt bold off` | Enable or disable bold text. | +| `//zt drag on` / `//zt drag off` | Enable or disable mouse dragging. The position saves on release. | +| `//zt help` | Display command help. | +| `//zt print` | Print entry and current Unix timestamps to the Windower console (legacy debug command). | + +Use `fade`, `reminder`, `bg`, `color`, `bg_color`, `alpha`, `bg_alpha`, `padding`, `stroke`, `bold`, or `drag` without a value to display that setting. Commands are case-insensitive. Chat output requires a logged-in character. + +## Appearance, previews, and dragging + +The default appearance is white text, a black one-pixel outline, and a visible black background with zero padding. Text and background alpha both start at 255. Bold is off and dragging is on. Appearance changes apply immediately and save for the active character. + +Changing an appearance setting while the HUD is hidden previews it for three seconds: one second fading in, one second holding, and one second fading out. Further changes extend the preview without flashing. Queries, timer commands, and reminder commands do not start previews. `show` and `hide` use one-second transitions and can reverse an active fade. + +Dragging during a preview or reminder holds the HUD at its configured opacity until the mouse is released. Release starts a fresh three-second preview, or a ten-second display if a reminder has priority. Dragging does not change the saved visibility preference. + +Text, outline, and background fade independently to their configured alpha values, not necessarily to 255. Changing text or background alpha during a fade updates its endpoint. Background visibility remains independent of its color and alpha. + +With `fade off`, manual show/hide is immediate. Previews and reminders still last three and ten seconds, but appear and disappear without animation. No `prerender` fade handler is registered. Changing the fade setting during a temporary display restarts that display using the new setting. + +NPC menus and cutscenes temporarily hide the HUD. Zoning also hides it and restores it on arrival, unless it is configured as hidden or an NPC event is still active. Fading out may not be visible if the game stops rendering during loading. Mog Houses sharing their parent zone ID do not reset elapsed time. Reminders continue counting. With `fade off`, these transitions are immediate. The timer and zone are checked once per second; no watchdog or packet injection is used. + +## Optional reminders + +For an OLED-friendly setup, use `//zt hide` followed by `//zt reminder 30`. The reminder interval is saved across addon reloads and game sessions. + +Each reminder prints the actual zone name and elapsed time to chat: a blue `[Zone timer]` prefix, white explanatory text, a green zone name, and yellow elapsed time. When the HUD is normally hidden, it appears for ten seconds: one second fading in, eight seconds holding, and one second fading out. When permanently shown, it remains visible and the reminder only writes to chat. + +Reminders take priority over appearance previews. Hiding the HUD does not disable reminders. Changing the interval selects the next interval boundary from zone entry or the last reset; it does not send missed reminders. + +## Configuration files and upgrades + +ZoneTimer uses Windower's `config` library to create and save settings. Shared defaults and character-owned data have separate files: + +```text +data/settings.xml Shared defaults and legacy character sections +data/alice.xml Alice's overrides and zone state +data/bob.xml Bob's overrides and zone state +``` + +`settings.xml` supplies shared `` defaults. Once this file exists, ZoneTimer reads it without rewriting it. Each lowercase character file stores `` and `` inside its own `` section. In a character file, `` is local to that file; it does not affect other characters. + +Commands save only the preferences they explicitly set. An explicit value remains a character override even when it currently matches a shared default. Other preferences continue to inherit shared defaults. Zone tracking and temporary fades do not create appearance overrides. Different character names write to different files, so they do not overwrite one another's settings. Filenames intentionally do not include a server ID: identical names on different servers share preferences when using the same addon directory. + +On the first load for a character without its own file, ZoneTimer imports that character's legacy section from `settings.xml`. Existing character files are not reimported. The shared file is preserved, and globally stored zone records are ignored. Migration cannot identify or repair values already changed by an older version. + +To edit shared defaults manually, unload ZoneTimer in every running FFXI instance, edit `data/settings.xml`, and load the addon again. To edit a character file, unload the addon for that character first. Changes made to a character file while it is loaded can be overwritten by the next save. Use commands for ordinary appearance changes. + +Existing standard `texts` properties remain supported, including italic, right justification, stroke color/alpha, and fallback fonts. Those advanced properties can be edited in XML. Padding belongs alongside `pos` and `text`, not inside `bg`. To resume inheritance for a preference, unload the addon and remove that preference from the character file's `` section. + +## Zone time and addon reloads + +The timer uses elapsed real-world seconds and does not wrap at 24 hours. Without a valid saved record, counting starts when the addon observes the current zone; it cannot recover an earlier entry that happened while it was not running. A different zone ID or `reset` starts a new timer. Entering or leaving a Mog House with the same parent zone ID does not reset it; a separate zone ID, such as Mog Garden's, does. + +A normal addon unload records a restoration timestamp in the character file. Loading again within **five minutes of that unload**, on the same character, server, and zone, resumes the previous timer. The five-minute window limits how long an unloaded timer remains eligible for restoration; it does not limit the duration of a running zone timer. Missing, mismatched, future-dated, or older records start a fresh timer. Player or zone information that arrives shortly after addon loading is checked before restoration is decided. + +Logout clears the zone record, and a new login starts fresh. Unexpected termination normally leaves no eligible clean-unload marker. A new session with an addon loaded only after login can still be indistinguishable from a recent addon reload when its saved identity, zone, and timestamps match. The timestamp check is a conservative restoration rule, not a session identifier. + +Appearance, visibility, fades, and reminder preferences survive these timer resets. Timer checks run once per second; hidden HUD text is not repeatedly rewritten. Frame-based updates are used only while fading. + +Original plugin by Krellion. Addon by Ihina. diff --git a/addons/zonetimer/zonetimer.lua b/addons/zonetimer/zonetimer.lua index e2f851d1b8..bdc1321023 100644 --- a/addons/zonetimer/zonetimer.lua +++ b/addons/zonetimer/zonetimer.lua @@ -1,69 +1,3 @@ -_addon.name = 'zonetimer' -_addon.author = 'Ihina' -_addon.version = '1.0.1.0' -_addon.command = 'zonetimer' - -require('logger') -config = require('config') -texts = require('texts') - -defaults = {} -defaults.pos = {} -defaults.pos.x = 400 -defaults.pos.y = 0 -defaults.text = {} -defaults.text.font = 'Arial' -defaults.text.size = 12 - -settings = config.load(defaults) -times = texts.new(settings) -start_time = os.time() - -windower.register_event('zone change', function(new_zone, old_zone) - start_time = os.time() -end) - -windower.register_event('prerender', function() - local info = windower.ffxi.get_info() - if not info.logged_in then - times:hide() - return - end - - seconds = os.time() - start_time - times:text(os.date('!%H:%M:%S', seconds)) - times:visible(true) -end) - -windower.register_event('addon command', function(...) - local param = L{...} - local command = param[1] - if command == 'help' then - log("'zonetimer fontsize #' to change the font size" ) - log("'zonetimer posX #' to change the x position") - log("'zonetimer posY #' to change the y position") - - elseif command == 'fontsize' or command == 'posX' or command == 'posY' then - - if command == 'fontsize' then - settings.text.size = param[2] - elseif command == 'posX' then - settings.pos.x = param[2] - elseif command == 'posY' then - settings.pos.y = param[2] - end - - config.save(settings, 'all') - times:visible(false) - times = texts.new(settings) - elseif command == 'print' then - print(start_time .. " " .. os.time()) - elseif command == 'reset' then - start_time = os.time() - - end -end) - --[[ Copyright (c) 2013, Ihina All rights reserved. @@ -91,3 +25,848 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ]] + +_addon.name = 'zonetimer' +_addon.author = 'Ihina' +_addon.version = '2.0.0.0' +_addon.commands = {'zonetimer', 'zt'} + +local config = require('config') +local texts = require('texts') +local res = require('resources') +local files = require('files') +local xml = require('xml') +require('lists') + +-- Defaults and runtime state +local defaults = {} +defaults.pos = {} +defaults.pos.x = 400 +defaults.pos.y = 0 +defaults.text = {} +defaults.text.font = 'Arial' +defaults.text.size = 12 +defaults.text.fonts = L{} +defaults.text.red = 255 +defaults.text.green = 255 +defaults.text.blue = 255 +defaults.text.alpha = 255 +defaults.text.stroke = {width = 1, red = 0, green = 0, blue = 0, alpha = 255} +defaults.flags = {bold = false, draggable = true, italic = false, right = false, bottom = false} +defaults.padding = 0 +defaults.bg = {} +defaults.bg.visible = true +defaults.bg.red = 0 +defaults.bg.green = 0 +defaults.bg.blue = 0 +defaults.bg.alpha = 255 +defaults.visible = true +defaults.fade_enabled = true +defaults.reminder_interval = 0 +-- The zone record is internal state, not a user-facing setting. +defaults.zone_state = {zone_id = 0, entered_at = 0, character = '', server = 0, resume_at = 0} + +local shared_defaults = table.copy(defaults) +local legacy_characters = {} +local settings = table.copy(defaults) +local character_settings +local active_character +local character_files = {} +local pending_resume = false +local in_event = false +local zoning = false +local timer +local zone_id = 0 +local entered_at = 0 +local next_reminder +local last_displayed +local running = false +local tick_task +local display_task +local fade_event +local mouse_release_event +local drag_reminder = false +local fade_to +local fade_opacity = 1 +local temporary_display = false +local temporary_reminder = false +local FADE_SECONDS = 1 +local REMINDER_SECONDS = 10 +local PREVIEW_SECONDS = 3 +local RESUME_SECONDS = 300 +local CHAT_COLOR = 207 +local CHAT_WHITE = string.char(0x1F, 1) +local CHAT_BLUE = string.char(0x1F, 207) +local CHAT_GREEN = string.char(0x1F, 158) +local CHAT_YELLOW = string.char(0x1F, 159) + +-- Configuration: read shared defaults; write only the active character file. +local function read_xml_values(node) + local result = {} + local text + for _, child in ipairs(node.children) do + if child.type == 'tag' then + result[child.name:lower()] = read_xml_values(child) + elseif child.type == 'text' then + text = child.value + end + end + if next(result) then return result end + if text and text:lower() == 'true' then return true end + if text and text:lower() == 'false' then return false end + return tonumber(text) or text or '' +end + +local function font_list(value) + if type(value) == 'table' then + return L(table.copy(value)) + end + local result = L{} + if type(value) == 'string' then + -- Windower stores fallback font lists as CSV, including quoted names. + local item, quoted, index = '', false, 1 + while index <= #value do + local char = value:sub(index, index) + if char == '"' then + if quoted and value:sub(index + 1, index + 1) == '"' then + item = item .. '"' + index = index + 1 + else + quoted = not quoted + end + elseif char == ',' and not quoted then + local name = item:match('^%s*(.-)%s*$') + if name ~= '' then result:append(name) end + item = '' + else + item = item .. char + end + index = index + 1 + end + local name = item:match('^%s*(.-)%s*$') + if name ~= '' then result:append(name) end + end + return result +end + +local function apply_values(target, source) + for key, value in pairs(source or {}) do + if key == 'fonts' then + target[key] = font_list(value) + elseif type(value) == 'table' and type(target[key]) == 'table' + and class(target[key]) ~= 'List' then + apply_values(target[key], value) + elseif value ~= '' or type(target[key]) ~= 'table' then + target[key] = value + end + end +end + +local function read_shared_settings() + local path = 'data/settings.xml' + local file = files.new(path) + if not file:exists() then + local global_defaults = table.copy(defaults) + global_defaults.zone_state = nil + config.load(path, global_defaults) + end + local root, err = xml.read(file) + if not root or root.name ~= 'settings' then + error('ZoneTimer: Could not read settings.xml: ' .. (err or 'Invalid root element.')) + end + for _, section in ipairs(root.children) do + if section.type == 'tag' then + local values = read_xml_values(section) + if type(values) ~= 'table' then values = {} end + if section.name:lower() == 'global' then + values.zone_state = nil + apply_values(shared_defaults, values) + else + legacy_characters[section.name:lower()] = values + end + end + end + settings = table.copy(shared_defaults) +end + +local function load_character(player) + local name = player.name:lower() + local stored = character_files[name] + if not stored then + local path = 'data/' .. name .. '.xml' + local migrate = not files.new(path):exists() and legacy_characters[name] + stored = config.load(path, {overrides = {}, zone_state = table.copy(defaults.zone_state)}) + if migrate then + -- Import once without rewriting the shared file. + local previous = legacy_characters[name] + stored.overrides = table.copy(previous) + stored.overrides.zone_state = nil + if type(previous.zone_state) == 'table' then + apply_values(stored.zone_state, previous.zone_state) + end + config.save(stored, 'all') + end + -- config retains every loaded table; reuse it when this character returns. + character_files[name] = stored + legacy_characters[name] = nil + end + character_settings = stored + active_character = name + settings = table.copy(shared_defaults) + apply_values(settings, stored.overrides) + settings.zone_state = table.copy(defaults.zone_state) + apply_values(settings.zone_state, stored.zone_state) +end + +local function save_settings(changes) + if character_settings then + -- Commands supply only the preferences they explicitly set. Unrelated + -- preferences keep inheriting shared defaults, even after zone saves. + apply_values(character_settings.overrides, changes) + character_settings.zone_state = table.copy(settings.zone_state) + config.save(character_settings, 'all') + end +end + +-- Timer text and chat output +local function chat(message) + if windower.ffxi.get_info().logged_in then + windower.add_to_chat(CHAT_COLOR, message) + end +end + +local function elapsed_seconds() + return math.max(0, os.time() - entered_at) +end + +local function format_time(seconds) + local hours = math.floor(seconds / 3600) + local minutes = math.floor(seconds / 60) % 60 + return ('%02d:%02d:%02d'):format(hours, minutes, seconds % 60) +end + +local function zone_name() + local zone = res.zones[zone_id] + return zone and zone.name or ('Zone %d'):format(zone_id) +end + +local function zone_message() + return CHAT_BLUE .. '[Zone timer]' .. CHAT_WHITE .. ' You have currently been in ' .. CHAT_GREEN + .. zone_name() .. CHAT_WHITE .. ' for ' .. CHAT_YELLOW .. format_time(elapsed_seconds()) .. CHAT_WHITE +end + +local function update_text() + if timer and zone_id ~= 0 then + local elapsed = elapsed_seconds() + if elapsed ~= last_displayed then + timer:text(format_time(elapsed)) + last_displayed = elapsed + end + end +end + +-- Visibility: one fade event and at most one temporary-display timeout. +local function set_opacity(opacity) + fade_opacity = opacity + timer:alpha(math.floor(settings.text.alpha * opacity + 0.5)) + timer:stroke_alpha(math.floor(settings.text.stroke.alpha * opacity + 0.5)) + timer:bg_alpha(math.floor(settings.bg.alpha * opacity + 0.5)) +end + +local function cancel_transition() + if display_task then + coroutine.close(display_task) + display_task = nil + end + temporary_display = false + temporary_reminder = false + drag_reminder = false + if fade_event then + windower.unregister_event(fade_event) + fade_event = nil + end + if mouse_release_event then + windower.unregister_event(mouse_release_event) + mouse_release_event = nil + -- A zone change or unload can interrupt a drag before mouse release. + save_settings({pos = {x = settings.pos.x, y = settings.pos.y}}) + end +end + +local function stop_fade() + cancel_transition() + if timer then + set_opacity(1) + timer:visible(settings.visible and zone_id ~= 0 and not in_event and not zoning) + end +end + +local function fade_to_opacity(target, after) + if fade_event then + windower.unregister_event(fade_event) + fade_event = nil + end + if not settings.fade_enabled then + set_opacity(target) + if after then + after() + end + return + end + local started = os.clock() + local from = fade_opacity + fade_to = target + fade_event = windower.register_event('prerender', function() + local progress = math.min(1, (os.clock() - started) / FADE_SECONDS) + set_opacity(from + (target - from) * progress) + if progress >= 1 then + windower.unregister_event(fade_event) + fade_event = nil + if after then + after() + end + end + end) +end + +local function fade_visibility(visible) + visible = visible and not in_event and not zoning + cancel_transition() + if zone_id == 0 then + set_opacity(1) + timer:hide() + elseif visible then + update_text() + if not timer:visible() then + set_opacity(0) + timer:show() + end + if fade_opacity < 1 then + fade_to_opacity(1) + end + elseif timer:visible() then + if fade_opacity > 0 then + fade_to_opacity(0, function() + timer:hide() + set_opacity(1) + end) + else + timer:hide() + set_opacity(1) + end + end +end + +local function show_temporarily(duration, reminder) + if settings.visible or zone_id == 0 or in_event or zoning or (temporary_reminder and not reminder) then + return + end + if mouse_release_event then + drag_reminder = drag_reminder or reminder == true + return + end + temporary_display = true + temporary_reminder = reminder == true + if display_task then + coroutine.close(display_task) + display_task = nil + end + update_text() + if not timer:visible() then + timer:show() + set_opacity(0) + fade_to_opacity(1) + elseif fade_event and fade_to == 0 then + -- Reverse an outgoing fade without blinking on repeated changes. + fade_to_opacity(1) + end + display_task = coroutine.schedule(function() + display_task = nil + fade_to_opacity(0, function() + timer:hide() + set_opacity(1) + temporary_display = false + temporary_reminder = false + end) + end, duration - (settings.fade_enabled and FADE_SECONDS or 0)) +end + +local function save_appearance(changes) + save_settings(changes) + show_temporarily(PREVIEW_SECONDS) +end + +local function schedule_reminder() + local interval = tonumber(settings.reminder_interval) or 0 + next_reminder = nil + if interval > 0 and zone_id ~= 0 then + local period = interval * 60 + next_reminder = (math.floor(elapsed_seconds() / period) + 1) * period + end +end + +local function apply_display_settings() + timer:pos(settings.pos.x, settings.pos.y) + timer:font(settings.text.font, unpack(settings.text.fonts)) + timer:size(settings.text.size) + timer:pad(settings.padding) + timer:stroke_width(settings.text.stroke.width) + timer:stroke_color(settings.text.stroke.red, settings.text.stroke.green, settings.text.stroke.blue) + timer:italic(settings.flags.italic == true) + timer:right_justified(settings.flags.right == true) + timer:bold(settings.flags.bold) + timer:draggable(settings.flags.draggable) + timer:bg_visible(settings.bg.visible) + timer:color(settings.text.red, settings.text.green, settings.text.blue) + timer:bg_color(settings.bg.red, settings.bg.green, settings.bg.blue) + set_opacity(fade_opacity) +end + +-- Zone lifecycle and the single once-per-second scheduler +local function save_zone(zone) + local player = windower.ffxi.get_player() + local info = windower.ffxi.get_info() + settings.zone_state.zone_id = zone + settings.zone_state.entered_at = entered_at + settings.zone_state.character = player and player.name or '' + settings.zone_state.server = info.server or 0 + settings.zone_state.resume_at = 0 + save_settings() +end + +local function start_zone(zone, fade_in) + stop_fade() + zone_id = zone + entered_at = os.time() + last_displayed = nil + save_zone(zone) + schedule_reminder() + update_text() + if fade_in and not zoning and settings.visible and not in_event then + timer:hide() + fade_visibility(true) + else + timer:visible(settings.visible and zone ~= 0 and not in_event and not zoning) + end +end + +local function clear_zone() + stop_fade() + zone_id = 0 + entered_at = 0 + next_reminder = nil + last_displayed = nil + settings.zone_state = table.copy(defaults.zone_state) + save_settings() + in_event = false + zoning = false + timer:hide() +end + +local function restore_zone(info, player) + local saved = settings.zone_state + local now = os.time() + local valid = saved.zone_id == info.zone + and saved.character == player.name + and saved.server == (info.server or 0) + and saved.entered_at > 0 and saved.entered_at <= now + and saved.resume_at > 0 and saved.resume_at <= now + and now - saved.resume_at <= RESUME_SECONDS + + if valid then + zone_id = info.zone + entered_at = saved.entered_at + schedule_reminder() + update_text() + timer:visible(settings.visible and not in_event) + saved.resume_at = 0 + save_settings() + else + start_zone(info.zone) + end +end + +local function activate_player(player, info, resume) + if active_character ~= player.name:lower() then + if zone_id ~= 0 then clear_zone() end + if player.status ~= nil then in_event = player.status == 4 end + load_character(player) + stop_fade() + apply_display_settings() + end + if info.zone ~= 0 then + if resume then + restore_zone(info, player) + else + start_zone(info.zone) + end + pending_resume = false + end +end + +local function tick() + tick_task = nil + if not running then + return + end + local info = windower.ffxi.get_info() + if not info.logged_in then + if zone_id ~= 0 then + clear_zone() + end + else + local player = windower.ffxi.get_player() + if player and (active_character ~= player.name:lower() or zone_id == 0) then + activate_player(player, info, pending_resume) + elseif player and info.zone ~= 0 and info.zone ~= zone_id then + start_zone(info.zone, true) + end + if player and zone_id ~= 0 then + if timer:visible() then + update_text() + end + if next_reminder and elapsed_seconds() >= next_reminder then + chat(zone_message()) + show_temporarily(REMINDER_SECONDS, true) + schedule_reminder() + end + end + end + tick_task = coroutine.schedule(tick, 1) +end + +-- Dragging and Windower event entry points +local function on_drag(_, x, y) + settings.pos.x = x + settings.pos.y = y + if not mouse_release_event then + if not settings.visible and zone_id ~= 0 then + local was_reminder = temporary_reminder + cancel_transition() + drag_reminder = was_reminder + set_opacity(1) + end + mouse_release_event = windower.register_event('mouse', function(event_type) + if event_type == 2 then + windower.unregister_event(mouse_release_event) + mouse_release_event = nil + save_settings({pos = {x = settings.pos.x, y = settings.pos.y}}) + local reminder = drag_reminder + drag_reminder = false + if running and not settings.visible and zone_id ~= 0 then + show_temporarily(reminder and REMINDER_SECONDS or PREVIEW_SECONDS, reminder) + end + end + end) + end +end + +local function on_load() + read_shared_settings() + -- Keep temporary fade alpha separate from persistent user settings. + local display_settings = table.copy(settings) + setmetatable(display_settings, nil) + timer = texts.new(display_settings) + timer:register_event('drag', on_drag) + + local info = windower.ffxi.get_info() + local player = windower.ffxi.get_player() + -- Preserve reload eligibility until both the player and zone are available. + pending_resume = info.logged_in + if info.logged_in and player then + activate_player(player, info, pending_resume) + else + timer:hide() + end + running = true + tick_task = coroutine.schedule(tick, 1) +end + +windower.register_event('load', on_load) +windower.register_event('login', function() + local info = windower.ffxi.get_info() + local player = windower.ffxi.get_player() + pending_resume = false + if player then + activate_player(player, info, false) + else + clear_zone() + end +end) + +windower.register_event('logout', function() + clear_zone() + -- The character's file is saved even after LuaCore marks logout complete. + character_settings = nil + active_character = nil + settings = table.copy(shared_defaults) + pending_resume = false +end) + +windower.register_event('status change', function(status) + if in_event ~= (status == 4) then + in_event = status == 4 + if timer then fade_visibility(settings.visible) end + end +end) + +-- Event status does not identify every zoning transition. +windower.register_event('incoming chunk', function(id, data, _, injected, blocked) + if injected or blocked or not running then return end + if id == 0x00B and zone_id ~= 0 then + zoning = true + fade_visibility(false) + elseif id == 0x00A and zoning then + local low, high = data:byte(0x31, 0x32) + local zone = low + 256 * high + if zone ~= zone_id then start_zone(zone) end + zoning = false + in_event = data:byte(0x20) == 4 -- Zone-in status at offset 0x1F. + fade_visibility(settings.visible) + end +end) + +windower.register_event('zone change', function(new_zone) + local player = windower.ffxi.get_player() + if windower.ffxi.get_info().logged_in and player and not pending_resume + and active_character == player.name:lower() and new_zone ~= 0 and new_zone ~= zone_id then + start_zone(new_zone, true) + end +end) + +windower.register_event('unload', function() + running = false + if tick_task then + coroutine.close(tick_task) + tick_task = nil + end + if zone_id ~= 0 and windower.ffxi.get_info().logged_in then + settings.zone_state.resume_at = os.time() + save_settings() + end + stop_fade() + if timer then + timer:destroy() + end +end) + +-- In-game commands +local function number_argument(value, min, max, whole) + local number = tonumber(value) + if number and number >= min and number <= max + and (not whole or number == math.floor(number)) then return number end +end + +windower.register_event('addon command', function(command, ...) + local args = {...} + command = command and command:lower() or '' + + if command == '' or command == 'time' or command == 't' then + if zone_id ~= 0 then + chat(zone_message()) + else + chat('ZoneTimer: No active zone timer.') + end + elseif command == 'help' then + chat('ZoneTimer: //zt [time|t], show, hide, fade , reset, reminder ') + chat('ZoneTimer: //zt posX , posY , fontsize , font , padding <0-100>') + chat('ZoneTimer: //zt bg , stroke , bold , drag ') + chat('ZoneTimer: //zt color , bg_color , alpha <0-255>, bg_alpha <0-255>') + chat('ZoneTimer: //zt reminder (show current interval), //zt print (debug)') + elseif command == 'show' or command == 'hide' then + settings.visible = command == 'show' + save_settings({visible = settings.visible}) + fade_visibility(settings.visible) + chat('ZoneTimer: HUD ' .. (settings.visible and 'shown.' or 'hidden.')) + elseif command == 'fade' then + local mode = args[1] and args[1]:lower() + if not mode then + chat('ZoneTimer: Fade ' .. (settings.fade_enabled and 'on.' or 'off.')) + return + end + if #args ~= 1 or (mode ~= 'on' and mode ~= 'off') then + chat('ZoneTimer: Use fade .') + return + end + local enabled = mode == 'on' + if settings.fade_enabled ~= enabled then + local keep_reminder = temporary_reminder + local keep_preview = temporary_display + settings.fade_enabled = enabled + if not mouse_release_event then + stop_fade() + if keep_preview then + show_temporarily(keep_reminder and REMINDER_SECONDS or PREVIEW_SECONDS, keep_reminder) + end + end + end + save_settings({fade_enabled = enabled}) + chat('ZoneTimer: Fade ' .. (settings.fade_enabled and 'on.' or 'off.')) + elseif command == 'bg' or command == 'background' then + local mode = args[1] and args[1]:lower() + if mode == 'show' or mode == 'on' or mode == 'hide' or mode == 'off' then + settings.bg.visible = mode == 'show' or mode == 'on' + timer:bg_visible(settings.bg.visible) + save_appearance({bg = {visible = settings.bg.visible}}) + elseif mode then + chat('ZoneTimer: Use bg .') + return + end + chat('ZoneTimer: Background ' .. (settings.bg.visible and 'shown.' or 'hidden.')) + elseif command == 'color' or command == 'bg_color' then + local is_background = command == 'bg_color' + local target = is_background and settings.bg or settings.text + local label = is_background and 'Background' or 'Text' + if #args == 0 then + chat(('ZoneTimer: %s RGB %d %d %d.'):format(label, target.red, target.green, target.blue)) + return + end + local red = number_argument(args[1], 0, 255, true) + local green = number_argument(args[2], 0, 255, true) + local blue = number_argument(args[3], 0, 255, true) + if #args ~= 3 or not red or not green or not blue then + chat(('ZoneTimer: Use %s (each 0-255).'):format(command)) + return + end + target.red, target.green, target.blue = red, green, blue + if is_background then + timer:bg_color(red, green, blue) + else + timer:color(red, green, blue) + end + save_appearance({[is_background and 'bg' or 'text'] = {red = red, green = green, blue = blue}}) + chat(('ZoneTimer: %s RGB set to %d %d %d.'):format(label, red, green, blue)) + elseif command == 'alpha' or command == 'text_alpha' or command == 'bg_alpha' then + local is_background = command == 'bg_alpha' + local label = is_background and 'Background' or 'Text' + local target = is_background and settings.bg or settings.text + if #args == 0 then + chat(('ZoneTimer: %s alpha %d.'):format(label, target.alpha)) + return + end + local alpha = number_argument(args[1], 0, 255, true) + if #args ~= 1 or not alpha then + chat(('ZoneTimer: Use %s <0-255>.'):format(command)) + return + end + target.alpha = alpha + set_opacity(fade_opacity) + save_appearance({[is_background and 'bg' or 'text'] = {alpha = alpha}}) + chat(('ZoneTimer: %s alpha set to %d.'):format(label, alpha)) + elseif command == 'stroke' then + local value = args[1] and args[1]:lower() + if not value then + chat(('ZoneTimer: Stroke width %s.'):format(settings.text.stroke.width)) + return + end + local width = value == 'off' and 0 or value == 'on' and 1 + or number_argument(value, 0, 10, false) + if not width then + chat('ZoneTimer: Use stroke .') + return + end + settings.text.stroke.width = width + timer:stroke_width(width) + save_appearance({text = {stroke = {width = width}}}) + chat(('ZoneTimer: Stroke width set to %s.'):format(width)) + elseif command == 'bold' or command == 'drag' then + local option = args[1] and args[1]:lower() + local key = command == 'bold' and 'bold' or 'draggable' + if option and option ~= 'on' and option ~= 'off' then + chat(('ZoneTimer: Use %s .'):format(command)) + return + end + if option then + settings.flags[key] = option == 'on' + if command == 'bold' then + timer:bold(settings.flags.bold) + else + timer:draggable(settings.flags.draggable) + end + save_appearance({flags = {[key] = settings.flags[key]}}) + end + chat(('ZoneTimer: %s %s.'):format(command, settings.flags[key] and 'on' or 'off')) + elseif command == 'reminder' then + local value = args[1] and args[1]:lower() + if not value then + chat(('ZoneTimer: Reminders %s.'):format(settings.reminder_interval == 0 + and 'disabled' or ('every %d minute(s)'):format(settings.reminder_interval))) + elseif value == 'off' or value == '0' then + settings.reminder_interval = 0 + save_settings({reminder_interval = 0}) + stop_fade() + schedule_reminder() + chat('ZoneTimer: Reminders disabled.') + else + local minutes = number_argument(value, 1, 1000000, true) + if not minutes then + chat('ZoneTimer: Use reminder (minimum 1).') + return + end + settings.reminder_interval = minutes + save_settings({reminder_interval = minutes}) + schedule_reminder() + chat(('ZoneTimer: Reminders every %d minute(s).'):format(minutes)) + end + elseif command == 'reset' then + if zone_id ~= 0 then + start_zone(zone_id) + chat('ZoneTimer: Timer reset.') + else + chat('ZoneTimer: No active zone timer.') + end + elseif command == 'posx' or command == 'posy' then + local position = number_argument(args[1], -100000, 100000, true) + if not position then + chat(('ZoneTimer: Use %s .'):format(command)) + return + end + if command == 'posx' then + settings.pos.x = position + timer:pos_x(position) + else + settings.pos.y = position + timer:pos_y(position) + end + save_appearance({pos = {[command == 'posx' and 'x' or 'y'] = position}}) + chat(('ZoneTimer: %s set to %d.'):format(command, position)) + elseif command == 'padding' or command == 'pad' then + if not args[1] then + chat(('ZoneTimer: Background padding %s.'):format(settings.padding)) + return + end + local padding = number_argument(args[1], 0, 100, true) + if #args ~= 1 or not padding then + chat('ZoneTimer: Use padding .') + return + end + settings.padding = padding + timer:pad(padding) + save_appearance({padding = padding}) + chat(('ZoneTimer: Background padding set to %d.'):format(padding)) + elseif command == 'fontsize' then + local size = number_argument(args[1], 1, 100, false) + if not size then + chat('ZoneTimer: Use fontsize (1-100).') + return + end + settings.text.size = size + timer:size(size) + save_appearance({text = {size = size}}) + chat(('ZoneTimer: Font size set to %s.'):format(size)) + elseif command == 'font' then + local font = table.concat(args, ' '):gsub('^"(.-)"$', '%1') + if font == '' then + chat('ZoneTimer: Use font .') + return + end + settings.text.font = font + timer:font(font, unpack(settings.text.fonts)) + save_appearance({text = {font = font}}) + chat(('ZoneTimer: Font set to %s.'):format(font)) + elseif command == 'print' then + print(entered_at .. ' ' .. os.time()) + else + chat('ZoneTimer: Unknown command. Use //zt help.') + end +end)