From e8c361c933a0c7eb0a88bfc99837a1c1fc62d70c Mon Sep 17 00:00:00 2001 From: Zirk Date: Tue, 20 Oct 2026 22:02:39 -0700 Subject: [PATCH] [GearSwap] equip_sets_exit pretarget cleanup part 2 The current code path is a bit tangled and not cohesive, as demonstrated by the "This case should not be hittable - 2" being hit for any item trades where the target is greater than 6 yalms away, which is a very real scenario. The overall code path should be pretty straightforward, either we succeeded in assembling a proposed packet and will proceed into the precast event, or we didn't get a proposed packet and will do some cleanup before bailing out and letting the client handle the action. Assigning nil to storedcommand is not necessary as part of cleanup, as any subsequent outgoing text trigger is guaranteed to assign a new value to storedcommand before storedcommand is ever used, so it is not possible for a stale value to persist from a previous trigger and be utilized. The same cleanup steps are added to outgoing text for consistency. We shouldn't be doing the val1.target.name check because filter_precast already validates the target by checking .target.id and .target.index, so inside filter_precast code block is where we have a valid target and outside filter_precast code block is where we don't have a valid target, we don't need to check for valid target in multiple places. The debug message about "Change target was used to pick an invalid target" was modified because that debug message lies most of the time, most often change target was not exercised and the original command contained without any target selected, for example. In order for an action to be a trade, the item must be in the inventory with a status of 0, so this find_inventory_item check should be baked into the trade determination. Before this commit, it was possible to have an item action_type labeled as a trade, but we still end up assembling an item use packet instead of a trade packet if the find_inventory_item check failed but the find_usable_item check succeeded. With this commit, we only set the action_type as trade if it really is a trade. As part of this commit's untangling of things, GearSwap will no longer fake out the "Target out of range" message for out of range trades, and will instead do the proper thing of letting the client handle that action and print out a native out of range message, along with the native feedback of echoing the input command that produced the out of range message. Outgoing text is also updated to no longer fake out these out of range trades to remain consistent. --- addons/GearSwap/flow.lua | 32 +++++++------------------------- addons/GearSwap/triggers.lua | 10 ++++------ 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/addons/GearSwap/flow.lua b/addons/GearSwap/flow.lua index 0ac688d83..16967de26 100644 --- a/addons/GearSwap/flow.lua +++ b/addons/GearSwap/flow.lua @@ -206,7 +206,6 @@ function equip_sets_exit(swap_type, ts, val1) if command_registry[ts].cancel_spell then msg.debugging('Action canceled (' .. storedcommand .. ' ' .. val1.target.raw .. ')') - storedcommand = nil command_registry:delete_entry(ts) return true elseif not ts or not command_registry[ts] or not storedcommand then @@ -218,7 +217,7 @@ function equip_sets_exit(swap_type, ts, val1) val1.target = command_registry[ts].new_target -- Switch target, if it is requested. if val1.prefix == '/item' then - if val1.target.type ~= 'NONE' and bit.band(val1.target.spawn_type, 2) == 2 then + if val1.target.type ~= 'NONE' and bit.band(val1.target.spawn_type, 2) == 2 and find_inventory_item(val1.id) then val1.action_type = 'Trade' else val1.action_type = 'Item' @@ -235,12 +234,10 @@ function equip_sets_exit(swap_type, ts, val1) if filter_precast(val1) then if val1.prefix == '/item' then -- Item use packet handling here - if val1.action_type == 'Trade' and find_inventory_item(val1.id) then + if val1.action_type == 'Trade' then -- 0x36 packet if val1.target.distance <= 6 then command_registry[ts].proposed_packet = assemble_menu_item_packet(val1.target.id, val1.target.index, val1.id) - else - windower.add_to_chat(67, 'Target out of range.') end elseif find_usable_item(val1.id) then -- 0x37 packet @@ -250,22 +247,7 @@ function equip_sets_exit(swap_type, ts, val1) command_registry[ts].proposed_packet = assemble_action_packet(val1.target.id, val1.target.index, outgoing_action_category_table[unify_prefix[val1.prefix]], val1.id, command_registry[ts].target_arrow) end - if not command_registry[ts].proposed_packet then - command_registry:delete_entry(ts) - end - end - - if ts and command_registry[ts] then - if not val1.target.name then - -- Spells with invalid pass_through_targs, like using without a target - command_registry:delete_entry(ts) - msg.debugging('Change target was used to pick an invalid target (' .. storedcommand .. ' ' .. val1.target.raw .. ')') - local ret = storedcommand .. ' ' .. val1.target.raw - storedcommand = nil - return ret - else - -- Spells with complete target information - -- command_registry[ts] is deleted for cancelled spells + if command_registry[ts].proposed_packet then if command_registry[ts].pretarget_cast_delay == 0 then equip_sets('precast', ts, val1) else @@ -273,11 +255,12 @@ function equip_sets_exit(swap_type, ts, val1) end return true end - elseif not ts or not command_registry[ts] then - msg.debugging('This case should not be hittable - 2') - return true end + msg.debugging('No valid target at end of pretarget (' .. storedcommand .. ' ' .. val1.target.raw .. ')') + command_registry:delete_entry(ts) + return storedcommand .. ' ' .. val1.target.raw + elseif swap_type == 'precast' then -- Update the target_arrow if val1.prefix ~= '/item' then @@ -285,7 +268,6 @@ function equip_sets_exit(swap_type, ts, val1) end return precast_send_check(ts) elseif swap_type == 'filtered_action' and command_registry[ts] and command_registry[ts].cancel_spell then - storedcommand = nil command_registry:delete_entry(ts) return true elseif swap_type == 'midcast' and _settings.demo_mode then diff --git a/addons/GearSwap/triggers.lua b/addons/GearSwap/triggers.lua index 32ed962ef..27bb062bb 100644 --- a/addons/GearSwap/triggers.lua +++ b/addons/GearSwap/triggers.lua @@ -118,7 +118,7 @@ windower.register_event('outgoing text', function(original, modified) spell.target = temp_mob_arr spell.action_type = action_type_map[command] - if spell.prefix == '/item' and spell.target.type ~= 'NONE' and bit.band(spell.target.spawn_type, 2) == 2 then + if spell.prefix == '/item' and spell.target.type ~= 'NONE' and bit.band(spell.target.spawn_type, 2) == 2 and find_inventory_item(spell.id) then spell.action_type = 'Trade' end @@ -135,13 +135,10 @@ windower.register_event('outgoing text', function(original, modified) if spell.prefix == '/item' then -- Item use packet handling here - if spell.action_type == 'Trade' and find_inventory_item(spell.id) then + if spell.action_type == 'Trade' then --0x36 packet if spell.target.distance <= 6 then command_registry[ts].proposed_packet = assemble_menu_item_packet(spell.target.id, spell.target.index, spell.id) - else - windower.add_to_chat(67, 'Target out of range.') - return true end elseif find_usable_item(spell.id) then --0x37 packet @@ -151,10 +148,11 @@ windower.register_event('outgoing text', function(original, modified) command_registry[ts].proposed_packet = assemble_action_packet(spell.target.id, spell.target.index, outgoing_action_category_table[unify_prefix[spell.prefix]], spell.id, initialize_arrow_offset(spell.target)) end - -- The packets created above should not be used. if command_registry[ts].proposed_packet then equip_sets('precast', ts, spell) return true + else + command_registry:delete_entry(ts) end end