Merge pull request #316 from Lucwousin/jabbadabbadooooooooooooooooooooooo

Add custom menu swaps to menu entry swapper
This commit is contained in:
Tyler Bochard
2019-05-20 14:21:42 -04:00
committed by GitHub
7 changed files with 288 additions and 52 deletions

View File

@@ -1,12 +1,14 @@
package net.runelite.client.menus;
import joptsimple.internal.Strings;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import net.runelite.api.MenuEntry;
import static net.runelite.client.menus.MenuManager.LEVEL_PATTERN;
import net.runelite.client.util.Text;
class AbstractMenuEntry
@EqualsAndHashCode
public class AbstractMenuEntry
{
@Getter
private String option;
@@ -26,17 +28,17 @@ class AbstractMenuEntry
@Getter
private boolean strictTarget;
AbstractMenuEntry(String option, String target)
public AbstractMenuEntry(String option, String target)
{
this(option, target, -1, -1, true, true);
}
AbstractMenuEntry(String option, String target, boolean strictTarget)
public AbstractMenuEntry(String option, String target, boolean strictTarget)
{
this(option, target, -1, -1, true, strictTarget);
}
AbstractMenuEntry(String option, String target, int id, int type, boolean strictOption, boolean strictTarget)
public AbstractMenuEntry(String option, String target, int id, int type, boolean strictOption, boolean strictTarget)
{
this.option = option;
this.target = target;
@@ -88,7 +90,7 @@ class AbstractMenuEntry
return true;
}
boolean equals(AbstractMenuEntry other)
/*boolean equals(AbstractMenuEntry other)
{
return target.equals(other.getTarget())
&& option.equals(other.getOption())
@@ -96,5 +98,5 @@ class AbstractMenuEntry
&& type == other.getType()
&& strictOption == other.isStrictOption()
&& strictTarget == other.isStrictTarget();
}
}*/
}

View File

@@ -26,6 +26,7 @@ package net.runelite.client.menus;
import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.Arrays;
@@ -36,7 +37,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
@@ -168,7 +168,15 @@ public class MenuManager
MenuEntry newestEntry = menuEntries[menuEntries.length - 1];
boolean isPrio = priorityEntries.stream().anyMatch(p -> p.matches(newestEntry));
boolean isPrio = false;
for (AbstractMenuEntry p : priorityEntries)
{
if (p.matches(newestEntry))
{
isPrio = true;
break;
}
}
// If the last entry was a priority entry, keep track of it
if (isPrio)
@@ -187,26 +195,42 @@ public class MenuManager
copy.add(CANCEL());
}
/*// Find the current entry in the swaps map
Optional<AbstractMenuEntry> swapEntry = swaps.keySet().stream().filter(e -> e.matches(newestEntry)).findFirst();
if (swapEntry.isPresent())
// Find the current entry in the swaps map
AbstractMenuEntry swapEntry = null;
for (AbstractMenuEntry e : swaps.keySet())
{
AbstractMenuEntry swap = swapEntry.get();
AbstractMenuEntry swapTarget = swaps.get(swap);
if (e.matches(newestEntry))
{
swapEntry = e;
break;
}
}
if (swapEntry != null)
{
AbstractMenuEntry swapTarget = swaps.get(swapEntry);
// Find the target for the swap in current menu entries
Optional<MenuEntry> foundSwap = Lists.reverse(copy).stream().filter(swapTarget::matches).findFirst();
MenuEntry foundSwap = null;
for (MenuEntry entry : Lists.reverse(copy))
{
if (swapTarget.matches(entry))
{
foundSwap = entry;
break;
}
}
if (foundSwap.isPresent())
if (foundSwap != null)
{
// Swap
int index = copy.indexOf(foundSwap.get());
int index = copy.indexOf(foundSwap);
int newIndex = copy.indexOf(newestEntry);
copy.set(index, newestEntry);
copy.set(copy.size() - 1, foundSwap.get());
copy.set(newIndex, foundSwap);
}
}*/
}
client.setMenuEntries(copy.toArray(new MenuEntry[0]));
}
@@ -401,8 +425,121 @@ public class MenuManager
AbstractMenuEntry entry = new AbstractMenuEntry(option, target);
Set<AbstractMenuEntry> toRemove = priorityEntries.stream().filter(entry::equals).collect(Collectors.toSet());
Set<AbstractMenuEntry> toRemove = new HashSet<>();
for (AbstractMenuEntry priorityEntry : priorityEntries)
{
if (entry.equals(priorityEntry))
{
toRemove.add(entry);
}
}
priorityEntries.removeAll(toRemove);
for (AbstractMenuEntry e : toRemove)
{
priorityEntries.remove(e);
}
}
/**
* Adds to the set of menu entries which when present, will remove all entries except for this one
* This method will add one with strict option, but not-strict target (contains for target, equals for option)
*/
public void addPriorityEntry(String option)
{
option = Text.standardize(option);
AbstractMenuEntry entry = new AbstractMenuEntry(option, "", false);
priorityEntries.add(entry);
}
public void removePriorityEntry(String option)
{
option = Text.standardize(option);
AbstractMenuEntry entry = new AbstractMenuEntry(option, "", false);
Set<AbstractMenuEntry> toRemove = new HashSet<>();
for (AbstractMenuEntry priorityEntry : priorityEntries)
{
if (entry.equals(priorityEntry))
{
toRemove.add(entry);
}
}
for (AbstractMenuEntry e : toRemove)
{
priorityEntries.remove(e);
}
}
/**
* Adds to the map of swaps. - Strict option + target
*/
public void addSwap(String option, String target, String option2, String target2)
{
option = Text.standardize(option);
target = Text.standardize(target);
option2 = Text.standardize(option2);
target2 = Text.standardize(target2);
AbstractMenuEntry swapFrom = new AbstractMenuEntry(option, target);
AbstractMenuEntry swapTo = new AbstractMenuEntry(option2, target2);
if (swapTo.equals(swapFrom))
{
log.warn("You shouldn't try swapping an entry for itself");
return;
}
swaps.put(swapFrom, swapTo);
}
/**
* Adds to the map of swaps - Pre-baked Abstract entry
*/
public void addSwap(AbstractMenuEntry swapFrom, AbstractMenuEntry swapTo)
{
if (swapTo.equals(swapFrom))
{
log.warn("You shouldn't try swapping an entry for itself");
return;
}
swaps.put(swapFrom, swapTo);
}
public void removeSwap(String option, String target, String option2, String target2)
{
option = Text.standardize(option);
target = Text.standardize(target);
option2 = Text.standardize(option2);
target2 = Text.standardize(target2);
AbstractMenuEntry swapFrom = new AbstractMenuEntry(option, target);
AbstractMenuEntry swapTo = new AbstractMenuEntry(option2, target2);
removeSwap(swapFrom, swapTo);
}
public void removeSwap(AbstractMenuEntry swapFrom, AbstractMenuEntry swapTo)
{
Set<AbstractMenuEntry> toRemove = new HashSet<>();
for (Map.Entry<AbstractMenuEntry, AbstractMenuEntry> e : swaps.entrySet())
{
if (e.getKey().equals(swapFrom) && e.getValue().equals(swapTo))
{
toRemove.add(e.getKey());
}
}
for (AbstractMenuEntry entry : toRemove)
{
swaps.remove(entry);
}
}
}

View File

@@ -32,6 +32,17 @@ import net.runelite.client.config.ConfigItem;
@ConfigGroup("menuentryswapper")
public interface MenuEntrySwapperConfig extends Config
{
@ConfigItem(
position = -3,
keyName = "customSwaps",
name = "Custom swaps",
description = "Add custom swaps here, 1 per line. Syntax: option, target : option, target<br>Note that the first entry should be the left click one!"
)
default String customSwaps()
{
return "";
}
@ConfigItem(
position = -2,
keyName = "shiftClickCustomization",

View File

@@ -25,10 +25,14 @@
*/
package net.runelite.client.plugins.menuentryswapper;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Provides;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import joptsimple.internal.Strings;
import lombok.Getter;
import lombok.Setter;
import net.runelite.api.Client;
@@ -51,6 +55,7 @@ import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.game.ItemManager;
import net.runelite.client.game.ItemVariationMapping;
import net.runelite.client.input.KeyManager;
import net.runelite.client.menus.AbstractMenuEntry;
import net.runelite.client.menus.MenuManager;
import net.runelite.client.menus.WidgetMenuOption;
import net.runelite.client.plugins.Plugin;
@@ -71,7 +76,6 @@ public class MenuEntrySwapperPlugin extends Plugin
private static final String SAVE = "Save";
private static final String RESET = "Reset";
private static final String MENU_TARGET = "Shift-click";
private static final String CONFIG_GROUP = "shiftclick";
private static final String ITEM_KEY_PREFIX = "item_";
@@ -101,6 +105,13 @@ public class MenuEntrySwapperPlugin extends Plugin
MenuAction.NPC_FIFTH_OPTION,
MenuAction.EXAMINE_NPC);
private static final Splitter NEWLINE_SPLITTER = Splitter
.on("\n")
.omitEmptyStrings()
.trimResults();
private final Map<AbstractMenuEntry, AbstractMenuEntry> customSwaps = new HashMap<>();
@Inject
private Client client;
@@ -131,9 +142,6 @@ public class MenuEntrySwapperPlugin extends Plugin
@Setter
private boolean shiftModifier = false;
@Setter
private boolean controlModifier = false;
@Provides
MenuEntrySwapperConfig provideConfig(ConfigManager configManager)
{
@@ -160,6 +168,11 @@ public class MenuEntrySwapperPlugin extends Plugin
{
if (!CONFIG_GROUP.equals(event.getGroup()))
{
if (event.getKey().equals("customSwaps"))
{
loadCustomSwaps(config.customSwaps());
}
return;
}
@@ -455,20 +468,6 @@ public class MenuEntrySwapperPlugin extends Plugin
}
}
else if (option.equalsIgnoreCase("climb") && config.swapClimbUpDown())
{
if (controlModifier ^ shiftModifier)
{
if (shiftModifier)
{
swap(client, "climb-up", option, target, true);
}
if (controlModifier)
{
swap(client, "climb-down", option, target, true);
}
}
}
else if (config.swapTravel() && option.equals("pass") && target.equals("energy barrier"))
{
swap(client, "pay-toll(2-ecto)", option, target, true);
@@ -667,4 +666,96 @@ public class MenuEntrySwapperPlugin extends Plugin
menuManager.addManagedCustomMenu(RESIZABLE_INVENTORY_TAB_CONFIGURE);
}
}
private void loadCustomSwaps(String config)
{
Map<AbstractMenuEntry, AbstractMenuEntry> tmp = new HashMap<>();
if (!Strings.isNullOrEmpty(config))
{
Map<String, String> split = NEWLINE_SPLITTER.withKeyValueSeparator(':').split(config);
for (Map.Entry<String, String> entry : split.entrySet())
{
String from = entry.getKey();
String to = entry.getValue();
String[] splitFrom = Text.standardize(from).split(",");
String optionFrom = splitFrom[0].trim();
String targetFrom;
if (splitFrom.length == 1)
{
targetFrom = "";
}
else
{
targetFrom = splitFrom[1].trim();
}
AbstractMenuEntry fromEntry = new AbstractMenuEntry(optionFrom, targetFrom);
String[] splitTo = Text.standardize(to).split(",");
String optionTo = splitTo[0].trim();
String targetTo;
if (splitTo.length == 1)
{
targetTo = "";
}
else
{
targetTo = splitTo[1].trim();
}
AbstractMenuEntry toEntry = new AbstractMenuEntry(optionTo, targetTo);
tmp.put(fromEntry, toEntry);
}
}
for (Map.Entry<AbstractMenuEntry, AbstractMenuEntry> e : customSwaps.entrySet())
{
AbstractMenuEntry key = e.getKey();
AbstractMenuEntry value = e.getValue();
menuManager.removeSwap(key, value);
}
customSwaps.clear();
customSwaps.putAll(tmp);
for (Map.Entry<AbstractMenuEntry, AbstractMenuEntry> entry : customSwaps.entrySet())
{
AbstractMenuEntry a1 = entry.getKey();
AbstractMenuEntry a2 = entry.getValue();
menuManager.addSwap(a1, a2);
}
}
void startShift()
{
if (!config.swapClimbUpDown())
{
return;
}
menuManager.addPriorityEntry("climb-up");
}
void stopShift()
{
menuManager.removePriorityEntry("climb-up");
}
void startControl()
{
if (!config.swapClimbUpDown())
{
return;
}
menuManager.addPriorityEntry("climb-down");
}
void stopControl()
{
menuManager.removePriorityEntry("climb-down");
}
}

View File

@@ -26,18 +26,10 @@ package net.runelite.client.plugins.menuentryswapper;
import java.awt.event.KeyEvent;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.input.KeyListener;
public class ShiftClickInputListener implements KeyListener
{
@Inject
private ClientThread clientThread;
@Inject
private Client client;
@Inject
private MenuEntrySwapperPlugin plugin;
@@ -53,10 +45,11 @@ public class ShiftClickInputListener implements KeyListener
if (event.getKeyCode() == KeyEvent.VK_SHIFT)
{
plugin.setShiftModifier(true);
plugin.startShift();
}
if (event.getKeyCode() == KeyEvent.VK_CONTROL)
{
plugin.setControlModifier(true);
plugin.startControl();
}
}
@@ -66,10 +59,11 @@ public class ShiftClickInputListener implements KeyListener
if (event.getKeyCode() == KeyEvent.VK_SHIFT)
{
plugin.setShiftModifier(false);
plugin.stopShift();
}
if (event.getKeyCode() == KeyEvent.VK_CONTROL)
{
plugin.setControlModifier(false);
plugin.stopControl();
}
}
}

View File

@@ -48,7 +48,7 @@ import net.runelite.client.plugins.PluginType;
public class ShiftWalkerPlugin extends Plugin
{
private static final String WALK_HERE = "WALK HERE";
private static final String WALK_HERE = "Walk here";
@Inject
private ShiftWalkerConfig config;
@@ -91,11 +91,11 @@ public class ShiftWalkerPlugin extends Plugin
void startPrioritizing()
{
menuManager.addPriorityEntry(WALK_HERE, "");
menuManager.addPriorityEntry(WALK_HERE);
}
void stopPrioritizing()
{
menuManager.removePriorityEntry(WALK_HERE, "");
menuManager.removePriorityEntry(WALK_HERE);
}
}

View File

@@ -33,6 +33,7 @@ import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.MenuEntry;
@Deprecated
@Slf4j
public class MenuUtil
{