local loadout_switcher = {} local string_find = string.find local string_gsub = string.gsub local string_match = string.match local player = require("player") local require_loadout_num = nil local doneCallback = nil local timeout_timer = 0 local timeout_active = false local slots = { 14, 15, 16, 23, 24, 25, 32, 33, 34, 41, 42, 43, } local function cleanup() timeout_active = false unregisterClientTick(tickerInventory) unregisterClientTick(timeoutTicker) require_loadout_num = nil end local function stripMinecraftColors(text) return string_gsub(text, "ยง.", "") end local function timeoutCallback() if timeout_active then local title = player.inventory.getChestTitle() if title and string_find(stripMinecraftColors(title), "Loadouts") then player.inventory.closeScreen() end if doneCallback then doneCallback(nil, false) end cleanup() end end local function timeoutTicker() if timeout_active then timeout_timer = timeout_timer + 1 if timeout_timer >= 30 then timeoutCallback() end end end local function tickerInventory() local title = player.inventory.getChestTitle() if not title then return end local clean_title = stripMinecraftColors(title) if not string_find(clean_title, "Loadouts") then return end if not require_loadout_num then return end local current_page = tonumber(string_match(clean_title, "(%d+)/")) local target_page = math.ceil(require_loadout_num / 12) if current_page and current_page ~= target_page then local search = current_page < target_page and "Next Page" or "Previous Page" local total_slots = player.inventory.getContainerSlots() for slot = 0, total_slots - 1 do local item = player.inventory.getStackFromContainer(slot) if item then local name = stripMinecraftColors(item.display_name) if string_find(name, search) then player.inventory.leftClick(slot) return end end end return end local target_name = "Loadout " .. tostring(require_loadout_num) for _, slot in ipairs(slots) do local item = player.inventory.getStackFromContainer(slot) if item then local name = stripMinecraftColors(item.display_name) if string_find(name, target_name) then player.inventory.leftClick(slot) player.inventory.closeScreen() if doneCallback then doneCallback(require_loadout_num, true) end cleanup() return end end end end --- Equip a loadout by number. --- @param loadout_num number Loadout number (1, 2, 3...). --- @param callback function Callback function(loadout_num, success). function loadout_switcher.equipLoadout(loadout_num, callback) unregisterClientTick(tickerInventory) unregisterClientTick(timeoutTicker) timeout_timer = 0 timeout_active = false if loadout_num then require_loadout_num = loadout_num player.sendCommand("/loadout") doneCallback = callback timeout_active = true registerClientTick(tickerInventory) registerClientTick(timeoutTicker) end end return loadout_switcher