--[[
orb_check_destroy_LEM.lua
LEM Condition Event: checks if "Orb of Mastery" has 0 charges, and if so,
destroys it.
Add this as a Condition Event in LEM:
1. LEM UI -> Condition Events -> Add Event...
2. Name it (e.g. "OrbOfMasteryDestroy")
3. Paste this code into the code section (replacing the template)
4. Enable the event with /lem cond OrbOfMasteryDestroy
LEM calls `condition()` on a loop. When it returns true, LEM calls `action()`.
DEBUGGING: this version prints status lines to the MQ console so you can
see exactly what happens on each cycle (search for "[OrbCheck]" in your
chat window). Remove the print() lines once it's confirmed working
reliably, if you want a quieter script.
]]
local mq = require('mq')
local ITEM_NAME = "Orb of Mastery"
-- Guard so action() doesn't refire every loop tick while the item is momentarily
-- mid-destroy or if FindItem lags a frame behind reality.
local alreadyHandled = false
local function log(msg)
print(string.format("\ay[OrbCheck]\ax %s", msg))
end
---@return boolean @Returns true if the condition is met, and the action should be executed.
local function condition()
local item = mq.TLO.FindItem(ITEM_NAME)
if not item() then
if alreadyHandled then
log("Item no longer found - resetting handled flag.")
end
alreadyHandled = false -- item's gone (already destroyed or never had one); reset guard
return false
end
local charges = item.Charges() or 0
-- A recharged/fresh orb (charges > 0) means a new item is in play - reset
-- the guard so its eventual 0-charge state gets caught. We can't rely only
-- on the "item missing" case above, because a fast resummon script can
-- swap in a new orb before FindItem ever sees a gap.
if charges > 0 then
if alreadyHandled then
log("Fresh orb with charges detected - resetting handled flag.")
end
alreadyHandled = false
return false
end
if charges == 0 and not alreadyHandled then
log(string.format("Charges at 0 (handled=false) - condition TRUE."))
return true
end
return false
end
local function action()
log("action() firing.")
-- Safety: if something is already stuck on the cursor (e.g. a previous
-- destroy attempt failed to complete), clear it first so we don't end up
-- grabbing/destroying the wrong item, and so future itemnotify calls
-- aren't blocked by an occupied cursor.
if mq.TLO.Cursor() and mq.TLO.Cursor.Name() then
log(string.format("Cursor already occupied by '%s' - stowing it first.", mq.TLO.Cursor.Name()))
mq.cmd('/autoinventory')
mq.delay(500)
end
local item = mq.TLO.FindItem(ITEM_NAME)
if not item() or (item.Charges() or 0) > 0 then
log("Item gone or charges > 0 by the time action() ran - aborting.")
return
end
alreadyHandled = true -- prevent re-triggering while we work
-- Notify by item name directly - MQ resolves the correct inventory slot itself,
-- avoiding the raw-slot-index math (which caused the earlier "pack30" error).
mq.cmdf('/itemnotify "%s" leftmouseup', ITEM_NAME)
mq.delay(500)
local cursorName = mq.TLO.Cursor() and mq.TLO.Cursor.Name()
log(string.format("After itemnotify, cursor holds: '%s'", tostring(cursorName)))
if cursorName == ITEM_NAME then
mq.cmd('/destroy')
mq.delay(500)
log("Destroy command sent.")
else
log("Item did NOT end up on cursor - destroy NOT sent. Will retry next cycle.")
alreadyHandled = false -- didn't actually consume it, allow retry
end
end
return {condfunc=condition, actionfunc=action}