diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/chatcommands/ChatCommandsPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/chatcommands/ChatCommandsPlugin.java index ae704913b7..ad0e3a0727 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/chatcommands/ChatCommandsPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/chatcommands/ChatCommandsPlugin.java @@ -55,8 +55,8 @@ import net.runelite.api.events.WidgetLoaded; import net.runelite.api.vars.AccountType; import net.runelite.api.widgets.Widget; import static net.runelite.api.widgets.WidgetID.ADVENTURE_LOG_ID; -import static net.runelite.api.widgets.WidgetID.KILL_LOGS_GROUP_ID; import static net.runelite.api.widgets.WidgetID.COUNTERS_LOG_GROUP_ID; +import static net.runelite.api.widgets.WidgetID.KILL_LOGS_GROUP_ID; import net.runelite.api.widgets.WidgetInfo; import net.runelite.client.chat.ChatColorType; import net.runelite.client.chat.ChatCommandManager; @@ -119,7 +119,6 @@ public class ChatCommandsPlugin extends Plugin @VisibleForTesting static final int ADV_LOG_EXPLOITS_TEXT_INDEX = 1; - private final HiscoreClient hiscoreClient = new HiscoreClient(); private final ChatClient chatClient = new ChatClient(); private boolean bossLogLoaded; @@ -157,6 +156,9 @@ public class ChatCommandsPlugin extends Plugin @Inject private ChatKeyboardListener chatKeyboardListener; + @Inject + private HiscoreClient hiscoreClient; + @Override public void startUp() { @@ -1005,21 +1007,27 @@ public class ChatCommandsPlugin extends Plugin final Skill hiscoreSkill = result.getSkill(); - final String response = new ChatMessageBuilder() + ChatMessageBuilder chatMessageBuilder = new ChatMessageBuilder() .append(ChatColorType.NORMAL) .append("Level ") .append(ChatColorType.HIGHLIGHT) .append(skill.getName()).append(": ").append(String.valueOf(hiscoreSkill.getLevel())) - .append(ChatColorType.NORMAL) - .append(" Experience: ") - .append(ChatColorType.HIGHLIGHT) - .append(String.format("%,d", hiscoreSkill.getExperience())) - .append(ChatColorType.NORMAL) - .append(" Rank: ") - .append(ChatColorType.HIGHLIGHT) - .append(String.format("%,d", hiscoreSkill.getRank())) - .build(); + .append(ChatColorType.NORMAL); + if (hiscoreSkill.getExperience() != -1) + { + chatMessageBuilder.append(" Experience: ") + .append(ChatColorType.HIGHLIGHT) + .append(String.format("%,d", hiscoreSkill.getExperience())) + .append(ChatColorType.NORMAL); + } + if (hiscoreSkill.getRank() != -1) + { + chatMessageBuilder.append(" Rank: ") + .append(ChatColorType.HIGHLIGHT) + .append(String.format("%,d", hiscoreSkill.getRank())); + } + final String response = chatMessageBuilder.build(); log.debug("Setting response {}", response); final MessageNode messageNode = chatMessage.getMessageNode(); messageNode.setRuneLiteFormatMessage(response); diff --git a/runelite-client/src/test/java/net/runelite/client/plugins/chatcommands/ChatCommandsPluginTest.java b/runelite-client/src/test/java/net/runelite/client/plugins/chatcommands/ChatCommandsPluginTest.java index 27de559692..f0b3954bfa 100644 --- a/runelite-client/src/test/java/net/runelite/client/plugins/chatcommands/ChatCommandsPluginTest.java +++ b/runelite-client/src/test/java/net/runelite/client/plugins/chatcommands/ChatCommandsPluginTest.java @@ -27,32 +27,44 @@ package net.runelite.client.plugins.chatcommands; import com.google.inject.Guice; import com.google.inject.testing.fieldbinder.Bind; import com.google.inject.testing.fieldbinder.BoundFieldModule; +import java.io.IOException; import java.util.concurrent.ScheduledExecutorService; +import java.util.function.BiConsumer; import javax.inject.Inject; +import net.runelite.api.ChatMessageType; import static net.runelite.api.ChatMessageType.FRIENDSCHATNOTIFICATION; import static net.runelite.api.ChatMessageType.GAMEMESSAGE; import static net.runelite.api.ChatMessageType.TRADE; import net.runelite.api.Client; +import net.runelite.api.MessageNode; import net.runelite.api.Player; import net.runelite.api.events.ChatMessage; import net.runelite.api.events.GameTick; import net.runelite.api.events.WidgetLoaded; import net.runelite.api.widgets.Widget; -import net.runelite.api.widgets.WidgetInfo; -import net.runelite.client.config.ChatColorConfig; -import net.runelite.client.config.ConfigManager; import static net.runelite.api.widgets.WidgetID.ADVENTURE_LOG_ID; import static net.runelite.api.widgets.WidgetID.COUNTERS_LOG_GROUP_ID; +import net.runelite.api.widgets.WidgetInfo; +import net.runelite.client.chat.ChatCommandManager; +import net.runelite.client.chat.ChatMessageManager; +import net.runelite.client.config.ChatColorConfig; +import net.runelite.client.config.ConfigManager; +import net.runelite.http.api.hiscore.HiscoreClient; +import net.runelite.http.api.hiscore.HiscoreSkill; +import net.runelite.http.api.hiscore.SingleHiscoreSkillResult; +import net.runelite.http.api.hiscore.Skill; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import static org.mockito.ArgumentMatchers.eq; import org.mockito.Mock; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.anyString; -import static org.mockito.Mockito.never; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; @@ -79,6 +91,18 @@ public class ChatCommandsPluginTest @Bind ChatColorConfig chatColorConfig; + @Mock + @Bind + ChatCommandManager chatCommandManager; + + @Mock + @Bind + HiscoreClient hiscoreClient; + + @Mock + @Bind + ChatMessageManager chatMessageManager; + @Mock @Bind ChatCommandsConfig chatCommandsConfig; @@ -92,6 +116,14 @@ public class ChatCommandsPluginTest Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this); when(client.getUsername()).thenReturn(PLAYER_NAME); + + chatCommandsPlugin.startUp(); + } + + @After + public void after() + { + chatCommandsPlugin.shutDown(); } @Test @@ -380,22 +412,22 @@ public class ChatCommandsPluginTest chatCommandsPlugin.onGameTick(new GameTick()); String COUNTER_TEXT = "Duel Arena
Wins: 4
Losses: 2" + - "

Last Man Standing
Rank: 0" + - "

Treasure Trails
Beginner: 0
Easy: 7" + - "
Medium: 28
Hard: 108
Elite: 15" + - "
Master: 27
Rank: Novice" + - "

Chompy Hunting
Kills: 1,000
Rank: Ogre Expert" + - "

Order of the White Knights
Rank: Master
with a kill score of 1,300" + - "

TzHaar Fight Cave
Fastest run: 38:10" + - "

Inferno
Fastest run: -

Zulrah
" + - "Fastest kill: 5:48

Vorkath
Fastest kill: 1:21" + - "

Galvek
Fastest kill: -

Grotesque Guardians
" + - "Fastest kill: 2:49

Alchemical Hydra
Fastest kill: -" + - "

Hespori
Fastest kill: 0:57

Nightmare
" + - "Fastest kill: 3:30

The Gauntlet
Fastest run: -" + - "

The Corrupted Gauntlet
Fastest run: -

Fragment of Seren
" + - "Fastest kill: -

Barbarian Assault
High-level gambles: " + - "15

Fremennik spirits rested: 0"; + "

Last Man Standing
Rank: 0" + + "

Treasure Trails
Beginner: 0
Easy: 7" + + "
Medium: 28
Hard: 108
Elite: 15" + + "
Master: 27
Rank: Novice" + + "

Chompy Hunting
Kills: 1,000
Rank: Ogre Expert" + + "

Order of the White Knights
Rank: Master
with a kill score of 1,300" + + "

TzHaar Fight Cave
Fastest run: 38:10" + + "

Inferno
Fastest run: -

Zulrah
" + + "Fastest kill: 5:48

Vorkath
Fastest kill: 1:21" + + "

Galvek
Fastest kill: -

Grotesque Guardians
" + + "Fastest kill: 2:49

Alchemical Hydra
Fastest kill: -" + + "

Hespori
Fastest kill: 0:57

Nightmare
" + + "Fastest kill: 3:30

The Gauntlet
Fastest run: -" + + "

The Corrupted Gauntlet
Fastest run: -

Fragment of Seren
" + + "Fastest kill: -

Barbarian Assault
High-level gambles: " + + "15

Fremennik spirits rested: 0"; Widget countersPage = mock(Widget.class); when(countersPage.getText()).thenReturn(COUNTER_TEXT); @@ -411,7 +443,7 @@ public class ChatCommandsPluginTest verify(configManager).setConfiguration(eq("personalbest.adam"), eq("vorkath"), eq(1 * 60 + 21)); verify(configManager).setConfiguration(eq("personalbest.adam"), eq("grotesque guardians"), eq(2 * 60 + 49)); verify(configManager).setConfiguration(eq("personalbest.adam"), eq("hespori"), eq(57)); - verify(configManager).setConfiguration(eq("personalbest.adam"), eq("nightmare"), eq( 3 * 60 + 30)); + verify(configManager).setConfiguration(eq("personalbest.adam"), eq("nightmare"), eq(3 * 60 + 30)); } @Test @@ -490,4 +522,33 @@ public class ChatCommandsPluginTest verifyNoMoreInteractions(configManager); } + + @Test + public void testPlayerSkillLookup() throws IOException + { + Player player = mock(Player.class); + when(player.getName()).thenReturn(PLAYER_NAME); + when(client.getLocalPlayer()).thenReturn(player); + + when(chatCommandsConfig.lvl()).thenReturn(true); + ArgumentCaptor> captor = ArgumentCaptor.forClass(BiConsumer.class); + verify(chatCommandManager).registerCommandAsync(eq("!lvl"), captor.capture()); + BiConsumer value = captor.getValue(); + + SingleHiscoreSkillResult skillResult = new SingleHiscoreSkillResult(); + skillResult.setPlayer(PLAYER_NAME); + skillResult.setSkill(new Skill(10, 1000, -1)); + + when(hiscoreClient.lookup(PLAYER_NAME, HiscoreSkill.ZULRAH, null)).thenReturn(skillResult); + + MessageNode messageNode = mock(MessageNode.class); + + ChatMessage chatMessage = new ChatMessage(); + chatMessage.setType(ChatMessageType.PUBLICCHAT); + chatMessage.setName(PLAYER_NAME); + chatMessage.setMessageNode(messageNode); + value.accept(chatMessage, "!lvl zulrah"); + + verify(messageNode).setRuneLiteFormatMessage("Level Zulrah: 1000 Rank: 10"); + } }