diff --git a/runelite-api/src/main/java/net/runelite/api/events/PlayerMenuOptionClicked.java b/runelite-api/src/main/java/net/runelite/api/events/PlayerMenuOptionClicked.java deleted file mode 100644 index dcfb0421ca..0000000000 --- a/runelite-api/src/main/java/net/runelite/api/events/PlayerMenuOptionClicked.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2016-2017, Adam - * 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.api.events; - -import lombok.Data; - -/** - * An event where a player menu option that was added by RuneLite has - * been clicked (ie. HiScore Lookup). - */ -@Data -public class PlayerMenuOptionClicked -{ - /** - * The menu option clicked. - */ - private String menuOption; - /** - * The target player. - */ - private String menuTarget; -} 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 67129b0ba5..a597038628 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 @@ -34,25 +34,21 @@ 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; import net.runelite.api.events.MenuEntryAdded; import net.runelite.api.events.MenuOptionClicked; import net.runelite.api.events.NpcActionChanged; -import net.runelite.api.events.PlayerMenuOptionClicked; import net.runelite.api.events.PlayerMenuOptionsChanged; import net.runelite.api.events.WidgetMenuOptionClicked; import net.runelite.api.widgets.WidgetInfo; import net.runelite.client.eventbus.EventBus; import net.runelite.client.eventbus.Subscribe; -import net.runelite.client.util.Text; @Singleton @Slf4j @@ -64,8 +60,6 @@ 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; @@ -235,10 +229,9 @@ public class MenuManager @Subscribe public void onMenuOptionClicked(MenuOptionClicked event) { - if (event.getMenuAction() != MenuAction.RUNELITE - && event.getMenuAction() != MenuAction.RUNELITE_PLAYER) + if (event.getMenuAction() != MenuAction.RUNELITE) { - return; // not a managed widget option or custom player option + return; } int widgetId = event.getWidgetId(); @@ -254,23 +247,9 @@ public class MenuManager customMenu.setMenuTarget(event.getMenuTarget()); customMenu.setWidget(curMenuOption.getWidget()); eventBus.post(customMenu); - return; // don't continue because it's not a player option + return; } } - - // 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 - String username = Text.removeTags(target).split("[(]")[0].trim(); - - PlayerMenuOptionClicked playerMenuOptionClicked = new PlayerMenuOptionClicked(); - playerMenuOptionClicked.setMenuOption(event.getMenuOption()); - playerMenuOptionClicked.setMenuTarget(username); - - eventBus.post(playerMenuOptionClicked); } private void addPlayerMenuItem(int playerOptionIndex, String menuText) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/hiscore/HiscorePlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/hiscore/HiscorePlugin.java index edb450d61b..caf3020149 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/hiscore/HiscorePlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/hiscore/HiscorePlugin.java @@ -38,9 +38,10 @@ import net.runelite.api.ChatMessageType; import net.runelite.api.Client; import net.runelite.api.MenuAction; import net.runelite.api.MenuEntry; +import net.runelite.api.Player; import net.runelite.api.events.ChatMessage; import net.runelite.api.events.MenuEntryAdded; -import net.runelite.api.events.PlayerMenuOptionClicked; +import net.runelite.api.events.MenuOptionClicked; import net.runelite.api.widgets.WidgetInfo; import net.runelite.client.config.ConfigManager; import net.runelite.client.eventbus.Subscribe; @@ -174,11 +175,30 @@ public class HiscorePlugin extends Plugin } @Subscribe - public void onPlayerMenuOptionClicked(PlayerMenuOptionClicked event) + public void onMenuOptionClicked(MenuOptionClicked event) { - if (event.getMenuOption().equals(LOOKUP)) + if ((event.getMenuAction() == MenuAction.RUNELITE || event.getMenuAction() == MenuAction.RUNELITE_PLAYER) + && event.getMenuOption().equals(LOOKUP)) { - lookupPlayer(Text.removeTags(event.getMenuTarget())); + final String target; + if (event.getMenuAction() == MenuAction.RUNELITE_PLAYER) + { + // The player id is included in the event, so we can use that to get the player name, + // which avoids having to parse out the combat level and any icons preceding the name. + Player player = client.getCachedPlayers()[event.getId()]; + if (player == null) + { + return; + } + + target = player.getName(); + } + else + { + target = Text.removeTags(event.getMenuTarget()); + } + + lookupPlayer(target); } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldHopperPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldHopperPlugin.java index b10e2a0667..a283fe9d7f 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldHopperPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldHopperPlugin.java @@ -47,10 +47,10 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; import net.runelite.api.ChatMessageType; import net.runelite.api.ChatPlayer; -import net.runelite.api.FriendsChatMember; -import net.runelite.api.FriendsChatManager; import net.runelite.api.Client; import net.runelite.api.Friend; +import net.runelite.api.FriendsChatManager; +import net.runelite.api.FriendsChatMember; import net.runelite.api.GameState; import net.runelite.api.MenuAction; import net.runelite.api.MenuEntry; @@ -60,7 +60,7 @@ import net.runelite.api.events.ChatMessage; import net.runelite.api.events.GameStateChanged; import net.runelite.api.events.GameTick; import net.runelite.api.events.MenuEntryAdded; -import net.runelite.api.events.PlayerMenuOptionClicked; +import net.runelite.api.events.MenuOptionClicked; import net.runelite.api.events.VarbitChanged; import net.runelite.api.events.WorldListLoad; import net.runelite.api.widgets.WidgetInfo; @@ -408,9 +408,9 @@ public class WorldHopperPlugin extends Plugin } @Subscribe - public void onPlayerMenuOptionClicked(PlayerMenuOptionClicked event) + public void onMenuOptionClicked(MenuOptionClicked event) { - if (!event.getMenuOption().equals(HOP_TO)) + if (event.getMenuAction() != MenuAction.RUNELITE || !event.getMenuOption().equals(HOP_TO)) { return; } 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 deleted file mode 100644 index 4c378d48ce..0000000000 --- a/runelite-client/src/test/java/net/runelite/client/menus/MenuManagerTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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_PLAYER); - 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_PLAYER); - 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()); - } -}