api: remove PlayerMenuOptionClicked

There is only one legitimate use of this, which is in the hiscore
plugin, but it is simple to replace it with a normal menu option clicked
event. Additionally the world hopper was incorrectly using this event
for the Hop option, so it has been changed to use the menu option
clicked event.
This commit is contained in:
Adam
2021-01-25 19:27:15 -05:00
parent 426265c765
commit f6c68eefc8
5 changed files with 32 additions and 172 deletions

View File

@@ -1,44 +0,0 @@
/*
* Copyright (c) 2016-2017, Adam <Adam@sigterm.info>
* 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;
}

View File

@@ -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:
// "username<img=20>5<col=40ff00> (level-42)" -> "username<col=40ff00> (level-42)"
String target = BOUNTY_EMBLEM_TAG_AND_TIER_REGEXP.matcher(event.getMenuTarget()).replaceAll("");
// removes tags and level from player names for example:
// <col=ffffff>username<col=40ff00> (level-42) or <col=ffffff><img=2>username</col>
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)

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -1,95 +0,0 @@
/*
* Copyright (c) 2020, bfmoatbio <bfmoatbio@protonmail.com>
* Copyright (c) 2020, Jordan Atwood <nightfirecat@protonmail.com>
* 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<col=40ff00> (level-42)");
menuManager.onMenuOptionClicked(event);
ArgumentCaptor<PlayerMenuOptionClicked> 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("username<img=20>5<col=40ff00> (level-42)");
menuManager.onMenuOptionClicked(event);
ArgumentCaptor<PlayerMenuOptionClicked> captor = ArgumentCaptor.forClass(PlayerMenuOptionClicked.class);
verify(eventBus).post(captor.capture());
PlayerMenuOptionClicked clicked = captor.getValue();
assertEquals("username", clicked.getMenuTarget());
}
}