From 7dba7b58e6f5f732b2a9291112b17f7565e6833b Mon Sep 17 00:00:00 2001 From: bfmoatbio <64886905+bfmoatbio@users.noreply.github.com> Date: Wed, 13 May 2020 23:47:31 -0700 Subject: [PATCH] menu manager: Remove bounty hunter emblem text from player name (#11541) --- .../main/java/net/runelite/api/IconID.java | 3 +- .../runelite/client/menus/MenuManager.java | 12 ++- .../client/menus/MenuManagerTest.java | 95 +++++++++++++++++++ 3 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 runelite-client/src/test/java/net/runelite/client/menus/MenuManagerTest.java diff --git a/runelite-api/src/main/java/net/runelite/api/IconID.java b/runelite-api/src/main/java/net/runelite/api/IconID.java index 7874179a3f..cbc896ca4b 100644 --- a/runelite-api/src/main/java/net/runelite/api/IconID.java +++ b/runelite-api/src/main/java/net/runelite/api/IconID.java @@ -46,7 +46,8 @@ public enum IconID SKULL(9), HARDCORE_IRONMAN(10), NO_ENTRY(11), - CHAIN_LINK(12); + CHAIN_LINK(12), + BOUNTY_HUNTER_EMBLEM(20); private final int index; diff --git a/runelite-client/src/main/java/net/runelite/client/menus/MenuManager.java b/runelite-client/src/main/java/net/runelite/client/menus/MenuManager.java index 863e58ea10..5566e31247 100644 --- a/runelite-client/src/main/java/net/runelite/client/menus/MenuManager.java +++ b/runelite-client/src/main/java/net/runelite/client/menus/MenuManager.java @@ -24,6 +24,7 @@ */ package net.runelite.client.menus; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; @@ -33,10 +34,12 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.regex.Pattern; import javax.inject.Inject; import javax.inject.Singleton; import lombok.extern.slf4j.Slf4j; import net.runelite.api.Client; +import net.runelite.api.IconID; import net.runelite.api.MenuAction; import net.runelite.api.MenuEntry; import net.runelite.api.NPCComposition; @@ -61,6 +64,8 @@ public class MenuManager private static final int IDX_LOWER = 4; private static final int IDX_UPPER = 8; + private static final Pattern BOUNTY_EMBLEM_TAG_AND_TIER_REGEXP = Pattern.compile(String.format("%s[1-9]0?", IconID.BOUNTY_HUNTER_EMBLEM.toString())); + private final Client client; private final EventBus eventBus; @@ -71,7 +76,8 @@ public class MenuManager private final Set npcMenuOptions = new HashSet<>(); @Inject - private MenuManager(Client client, EventBus eventBus) + @VisibleForTesting + MenuManager(Client client, EventBus eventBus) { this.client = client; this.eventBus = eventBus; @@ -269,7 +275,9 @@ public class MenuManager } } - String target = event.getMenuTarget(); + // removes bounty hunter emblem tag and tier from player name, e.g: + // "username5 (level-42)" -> "username (level-42)" + String target = BOUNTY_EMBLEM_TAG_AND_TIER_REGEXP.matcher(event.getMenuTarget()).replaceAll(""); // removes tags and level from player names for example: // username (level-42) or username diff --git a/runelite-client/src/test/java/net/runelite/client/menus/MenuManagerTest.java b/runelite-client/src/test/java/net/runelite/client/menus/MenuManagerTest.java new file mode 100644 index 0000000000..f90cf99118 --- /dev/null +++ b/runelite-client/src/test/java/net/runelite/client/menus/MenuManagerTest.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2020, bfmoatbio + * Copyright (c) 2020, Jordan Atwood + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.menus; + +import com.google.inject.Guice; +import com.google.inject.Inject; +import com.google.inject.testing.fieldbinder.Bind; +import com.google.inject.testing.fieldbinder.BoundFieldModule; +import net.runelite.api.Client; +import net.runelite.api.MenuAction; +import net.runelite.api.events.MenuOptionClicked; +import net.runelite.api.events.PlayerMenuOptionClicked; +import net.runelite.client.eventbus.EventBus; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import static org.mockito.Mockito.verify; +import org.mockito.junit.MockitoJUnitRunner; +import static org.junit.Assert.assertEquals; + +@RunWith(MockitoJUnitRunner.class) +public class MenuManagerTest +{ + @Inject + private MenuManager menuManager; + + @Mock + @Bind + private Client client; + + @Mock + @Bind + private EventBus eventBus; + + @Before + public void before() + { + Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this); + } + + @Test + public void testPlayerMenuOptionClicked() + { + MenuOptionClicked event = new MenuOptionClicked(); + event.setMenuAction(MenuAction.RUNELITE); + event.setMenuTarget("username (level-42)"); + + menuManager.onMenuOptionClicked(event); + + ArgumentCaptor captor = ArgumentCaptor.forClass(PlayerMenuOptionClicked.class); + verify(eventBus).post(captor.capture()); + PlayerMenuOptionClicked clicked = captor.getValue(); + assertEquals("username", clicked.getMenuTarget()); + } + + @Test + public void testPlayerMenuOptionWithBountyHunterEmblemClicked() + { + MenuOptionClicked event = new MenuOptionClicked(); + event.setMenuAction(MenuAction.RUNELITE); + event.setMenuTarget("username5 (level-42)"); + + menuManager.onMenuOptionClicked(event); + + ArgumentCaptor captor = ArgumentCaptor.forClass(PlayerMenuOptionClicked.class); + verify(eventBus).post(captor.capture()); + PlayerMenuOptionClicked clicked = captor.getValue(); + assertEquals("username", clicked.getMenuTarget()); + } +}