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_name = nil local doneCallback = nil local state = 0 local timeout_timer = 0 local timeout_active = false local waiting_for_action = false local slots = { 10, 11, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 37, 38, 39, 40, 41, 42, 43, } local function stripMinecraftColors(text) return string_gsub(text, "ยง.", "") end local function findLoadoutSlot() local title = player.inventory.getChestTitle() if not title then return nil end local clean_title = stripMinecraftColors(title) if not string_find(clean_title, "Loadouts") then return nil end 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, require_loadout_name) then return slot end end end return nil 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 timeout_active = false waiting_for_action = false unregisterClientTick(tickerInventory) unregisterClientTick(timeoutTicker) require_loadout_name = nil if doneCallback then doneCallback(nil, false) end end end local function timeoutTicker() if timeout_active then timeout_timer = timeout_timer + 1 if timeout_timer >= 10 then timeoutCallback() end end end local function tickerInventory() local title = player.inventory.getChestTitle() if title and string_find(stripMinecraftColors(title), "Loadouts") then if not require_loadout_name then return end local target_slot = findLoadoutSlot() if target_slot then player.inventory.leftClick(target_slot) player.inventory.closeScreen() if not waiting_for_action then waiting_for_action = true if doneCallback then local num = tonumber(string_match(require_loadout_name, "%d+")) doneCallback(num, true) end end unregisterClientTick(tickerInventory) unregisterClientTick(timeoutTicker) timeout_active = false waiting_for_action = false require_loadout_name = nil return 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 waiting_for_action = false if loadout_num then require_loadout_name = "Loadout " .. tostring(loadout_num) player.sendCommand("/loadout") doneCallback = callback registerClientTick(tickerInventory) end end return loadout_switcher