Skip to content

Commit

Permalink
menu swapper: perform shift click swaps in plugin
Browse files Browse the repository at this point in the history
This has been using the base client shift click feature to perform shift
click swaps, however the client behavior changed in the Apr 2022 if3
update. Previously the client would add the menu ops in the correct
order based on the shift key state when ticking the interface. Now
interface tick queues a script event to update the ui ops later.

This causes a one tick delay between the shift keypress and then
component ops being updated, which you can observe if you set the left
click swap to Drop, keep the shift click swap at Drop (vanilla) and
toggle shift. MES will not perform the left-click swap due to shift
being held, but vanilla will not perform the shift-click swap due to the
delay, leaving a tick with the wrong menu option.

Defer the shift check by 1 tick to match the vanilla behavior and
perform the previous swap for one more tick.
  • Loading branch information
Adam- committed May 1, 2024
1 parent e17d5fd commit a95a26f
Showing 1 changed file with 7 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import net.runelite.api.events.ClientTick;
import net.runelite.api.events.MenuEntryAdded;
import net.runelite.api.events.MenuOpened;
import net.runelite.api.events.PostItemComposition;
import net.runelite.api.events.PostMenuSort;
import net.runelite.api.widgets.ComponentID;
import net.runelite.api.widgets.InterfaceID;
Expand All @@ -78,7 +77,6 @@
import net.runelite.client.chat.QueuedMessage;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ConfigChanged;
import net.runelite.client.game.ItemManager;
import net.runelite.client.game.ItemVariationMapping;
import net.runelite.client.game.NpcUtil;
Expand Down Expand Up @@ -166,6 +164,7 @@ public class MenuEntrySwapperPlugin extends Plugin
private final Multimap<String, Swap> swaps = LinkedHashMultimap.create();
private final ArrayListMultimap<String, Integer> optionIndexes = ArrayListMultimap.create();
private final Multimap<Integer, TeleportSwap> teleportSwaps = HashMultimap.create();
private boolean lastShift, curShift;

@Provides
MenuEntrySwapperConfig provideConfig(ConfigManager configManager)
Expand Down Expand Up @@ -422,24 +421,6 @@ private void swapContains(String option, Predicate<String> targetPredicate, Stri
swaps.put(option, new Swap(alwaysTrue(), targetPredicate, swappedOption, enabled, false));
}

@Subscribe
public void onConfigChanged(ConfigChanged event)
{
if (event.getGroup().equals(MenuEntrySwapperConfig.GROUP) && event.getKey().equals("shiftClickCustomization"))
{
clientThread.invoke(this::resetItemCompositionCache);
}
else if (event.getGroup().equals(SHIFTCLICK_CONFIG_GROUP) && event.getKey().startsWith(ITEM_KEY_PREFIX))
{
clientThread.invoke(this::resetItemCompositionCache);
}
}

private void resetItemCompositionCache()
{
client.getItemCompositionCache().reset();
}

private Integer getItemSwapConfig(boolean shift, int itemId)
{
itemId = ItemVariationMapping.map(itemId);
Expand Down Expand Up @@ -1480,36 +1461,11 @@ private void swapMenuEntry(MenuEntry[] menuEntries, int index, MenuEntry menuEnt
final String target = Text.removeTags(menuEntry.getTarget()).toLowerCase();

final Widget w = menuEntry.getParent() != null ? menuEntry.getParent().getWidget() : menuEntry.getWidget();
// Custom shift-click item swap
if (shiftModifier() && w != null && WidgetUtil.componentToInterface(w.getId()) == InterfaceID.INVENTORY)
{
if (config.shiftClickCustomization() && !option.equals("use"))
{
Integer customOption = getItemSwapConfig(true, w.getItemId());

// Special case use shift click due to items not actually containing a "Use" option, making
// the client unable to perform the swap itself.
if (customOption != null && customOption == -1)
{
swap(menuEntries, "use", target, index, true);
}
else if (customOption != null && menuEntry.getParent() != null && menuEntry.getOption().hashCode() == customOption)
{
swap(optionIndexes, menuEntries, index, menuEntries.length - 1);
menuEntry.setParent(null);
}
}

// don't perform swaps on items when shift is held; instead prefer the client menu swap, which
// we may have overwrote
return;
}

// Custom left-click item swap
// Custom item swap
if (w != null && WidgetUtil.componentToInterface(w.getId()) == InterfaceID.INVENTORY
&& config.leftClickCustomization())
&& (lastShift ? config.shiftClickCustomization() : config.leftClickCustomization()))
{
Integer swapIndex = getItemSwapConfig(false, w.getItemId());
Integer swapIndex = getItemSwapConfig(lastShift, w.getItemId());
if (swapIndex != null)
{
if (swapIndex == -1)
Expand Down Expand Up @@ -1649,6 +1605,9 @@ else if (swapIndex + 1 == menuEntry.getItemOp()
@Subscribe
public void onClientTick(ClientTick clientTick)
{
lastShift = curShift;
curShift = shiftModifier();

if (client.isMenuOpen())
{
return;
Expand Down Expand Up @@ -1764,25 +1723,6 @@ private void removeDeadNpcs()
}
}

@Subscribe
public void onPostItemComposition(PostItemComposition event)
{
if (!config.shiftClickCustomization())
{
// since shift-click is done by the client we have to check if our shift click customization is on
// prior to altering the item shift click action index.
return;
}

ItemComposition itemComposition = event.getItemComposition();
Integer option = getItemSwapConfig(true, itemComposition.getId());

if (option != null && option >= 0 && option < itemComposition.getInventoryActions().length)
{
itemComposition.setShiftClickActionIndex(option);
}
}

private boolean swap(MenuEntry[] menuEntries, String option, String target, int index, boolean strict)
{
// find option to swap with
Expand Down

0 comments on commit a95a26f

Please sign in to comment.