Rework clanmanmode attack option hiding (#746)

* A whole bunch of refactoring

* Api/mixins/injector additions for hiding attack options

* Rework clanmanmode attack hiding

* Update Client.java
This commit is contained in:
Lucwousin
2019-06-26 01:01:21 +02:00
committed by Kyleeld
parent ae6274a16f
commit 39a4cb2266
118 changed files with 2038 additions and 1955 deletions

View File

@@ -3,29 +3,22 @@ package net.runelite.client.plugins.clanmanmode;
import com.google.inject.Provides;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.MenuEntry;
import net.runelite.api.Player;
import net.runelite.api.Varbits;
import net.runelite.api.coords.WorldPoint;
import net.runelite.api.events.ConfigChanged;
import net.runelite.api.events.GameStateChanged;
import net.runelite.api.events.GameTick;
import net.runelite.api.events.MenuEntryAdded;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.game.ClanManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.PluginType;
import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.client.util.Text;
import org.apache.commons.lang3.ArrayUtils;
@PluginDescriptor(
name = "Clan Man Mode",
@@ -37,10 +30,6 @@ import org.apache.commons.lang3.ArrayUtils;
public class ClanManModePlugin extends Plugin
{
private static final String WALK_HERE = "WALK HERE";
private static final String CANCEL = "CANCEL";
@Inject
private OverlayManager overlayManager;
@@ -59,18 +48,6 @@ public class ClanManModePlugin extends Plugin
@Inject
private Client client;
@Inject
private ClanManager clanManager;
private static final String CAST = "CAST";
private static final String ATTACK_OPTIONS_ATTACK = "ATTACK";
public static final HashSet<String> ATTACK_OPTIONS_KEYWORDS = new HashSet<>();
static
{
ATTACK_OPTIONS_KEYWORDS.add(CAST);
ATTACK_OPTIONS_KEYWORDS.add(ATTACK_OPTIONS_ATTACK);
}
@Provides
ClanManModeConfig provideConfig(ConfigManager configManager)
{
@@ -90,6 +67,7 @@ public class ClanManModePlugin extends Plugin
overlayManager.add(ClanManModeOverlay);
overlayManager.add(ClanManModeTileOverlay);
overlayManager.add(ClanManModeMinimapOverlay);
client.setHideFriendAttackOptions(config.hideAtkOpt());
}
@Override
@@ -98,6 +76,7 @@ public class ClanManModePlugin extends Plugin
overlayManager.remove(ClanManModeOverlay);
overlayManager.remove(ClanManModeTileOverlay);
overlayManager.remove(ClanManModeMinimapOverlay);
client.setHideFriendAttackOptions(false);
clan.clear();
ticks = 0;
wildernessLevel = 0;
@@ -106,6 +85,17 @@ public class ClanManModePlugin extends Plugin
inwildy = 0;
}
@Subscribe
private void onConfigChanged(ConfigChanged event)
{
if (!"clanmanmode".equals(event.getGroup()))
{
return;
}
client.setHideFriendAttackOptions(config.hideAtkOpt());
}
@Subscribe
public void onGameStateChanged(GameStateChanged gameStateChanged)
{
@@ -135,111 +125,4 @@ public class ClanManModePlugin extends Plugin
clanmax = Collections.max(clan.values());
}
}
@Subscribe
public void onMenuEntryAdded(MenuEntryAdded event)
{
if (client.getGameState() != GameState.LOGGED_IN)
{
return;
}
Player[] players = client.getCachedPlayers();
Player player = null;
int identifier = event.getIdentifier();
if (identifier >= 0 && identifier < players.length)
{
player = players[identifier];
}
if (player == null)
{
return;
}
final String pOptionToReplace = Text.removeTags(event.getOption()).toUpperCase();
//If the option is already to walk there, or cancel we don't need to swap it with anything
if (pOptionToReplace.equals(CANCEL) || pOptionToReplace.equals(WALK_HERE))
{
return;
}
if (config.hideAtkOpt()
&& ATTACK_OPTIONS_KEYWORDS.contains(pOptionToReplace)
&& (player.isFriend() || player.isClanMember()))
{
swap(pOptionToReplace);
}
else if (!config.hideAtkOpt())
{
return;
}
final String option = Text.removeTags(event.getOption()).toLowerCase();
if (option.equals("attack"))
{
final Pattern ppattern = Pattern.compile("<col=ffffff>(.+?)<col=");
final Matcher pmatch = ppattern.matcher(event.getTarget());
if (pmatch.find() && pmatch.matches())
{
if (pmatch.group(1) != null)
{
if (clan.containsKey(pmatch.group(1).replace(" ", " ")))
{
MenuEntry[] entries = client.getMenuEntries();
entries = ArrayUtils.removeElement(entries, entries[entries.length - 1]);
client.setMenuEntries(entries);
}
}
}
}
}
/**
* Swaps menu entries if the entries could be found. This places Walk Here where the top level menu option was.
* @param pOptionToReplace The String containing the Menu Option that needs to be replaced. IE: "Attack", "Chop Down".
*/
private void swap(String pOptionToReplace)
{
MenuEntry[] entries = client.getMenuEntries();
Integer walkHereEntry = searchIndex(entries, WALK_HERE);
Integer entryToReplace = searchIndex(entries, pOptionToReplace);
if (walkHereEntry != null
&& entryToReplace != null)
{
MenuEntry walkHereMenuEntry = entries[walkHereEntry];
entries[walkHereEntry] = entries[entryToReplace];
entries[entryToReplace] = walkHereMenuEntry;
client.setMenuEntries(entries);
}
}
/**
* Finds the index of the menu that contains the verbiage we are looking for.
* @param pMenuEntries The list of {@link MenuEntry}s.
* @param pMenuEntryToSearchFor The Option in the menu to search for.
* @return The index location or null if it was not found.
*/
private Integer searchIndex(MenuEntry[] pMenuEntries, String pMenuEntryToSearchFor)
{
Integer indexLocation = 0;
for (MenuEntry menuEntry : pMenuEntries)
{
String entryOption = Text.removeTags(menuEntry.getOption()).toUpperCase();
if (entryOption.equals(pMenuEntryToSearchFor))
{
return indexLocation;
}
indexLocation++;
}
return null;
}
}