From 294f93954924ee99d04233e795035545c5bf2237 Mon Sep 17 00:00:00 2001 From: iProdigy Date: Sun, 22 Dec 2024 22:38:24 -0600 Subject: [PATCH 1/2] chore: remove deprecated getCachedNPCs and getCachedPlayers calls --- .../dinkplugin/notifiers/DeathNotifier.java | 4 ++-- .../notifiers/DeathNotifierTest.java | 10 +++++----- .../notifiers/KillCountNotifierTest.java | 2 +- .../notifiers/MockedNotifierTest.java | 18 ++++++++++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main/java/dinkplugin/notifiers/DeathNotifier.java b/src/main/java/dinkplugin/notifiers/DeathNotifier.java index bb1a868c..9422f400 100644 --- a/src/main/java/dinkplugin/notifiers/DeathNotifier.java +++ b/src/main/java/dinkplugin/notifiers/DeathNotifier.java @@ -348,7 +348,7 @@ private Actor identifyKiller() { // find another player interacting with us (that is preferably not a friend or clan member) if (pvpEnabled) { - Optional pker = Arrays.stream(client.getCachedPlayers()) + Optional pker = client.getTopLevelWorldView().players().stream() .filter(interacting) .min(PK_COMPARATOR.apply(localPlayer)); // O(n) if (pker.isPresent()) @@ -356,7 +356,7 @@ private Actor identifyKiller() { } // otherwise search through NPCs interacting with us - return Arrays.stream(client.getCachedNPCs()) + return client.getTopLevelWorldView().npcs().stream() .filter(interacting) .filter(npc -> NPC_VALID.test(npc.getTransformedComposition())) .min(NPC_COMPARATOR.apply(npcManager, localPlayer)) // O(n) diff --git a/src/test/java/dinkplugin/notifiers/DeathNotifierTest.java b/src/test/java/dinkplugin/notifiers/DeathNotifierTest.java index 50517692..30a726f0 100644 --- a/src/test/java/dinkplugin/notifiers/DeathNotifierTest.java +++ b/src/test/java/dinkplugin/notifiers/DeathNotifierTest.java @@ -76,8 +76,8 @@ protected void setUp() { // init client mocks when(client.getVarbitValue(Varbits.IN_WILDERNESS)).thenReturn(1); - when(client.getCachedPlayers()).thenReturn(new Player[0]); - when(client.getCachedNPCs()).thenReturn(new NPC[0]); + mockNpcs(new NPC[0]); + mockPlayers(new Player[0]); WorldPoint location = new WorldPoint(0, 0, 0); when(localPlayer.getWorldLocation()).thenReturn(location); when(localPlayer.getLocalLocation()).thenReturn(new LocalPoint(0, 0)); @@ -165,7 +165,7 @@ void testNotifyPk() { when(other.getName()).thenReturn(pker); when(other.getInteracting()).thenReturn(localPlayer); Player[] candidates = { mock(Player.class), mock(Player.class), other, mock(Player.class) }; - when(client.getCachedPlayers()).thenReturn(candidates); + mockPlayers(candidates); // fire event plugin.onActorDeath(new ActorDeath(localPlayer)); @@ -215,7 +215,7 @@ void testNotifyNotPk() { Player other = mock(Player.class); when(other.getName()).thenReturn("Rasmus"); when(other.getInteracting()).thenReturn(localPlayer); - when(client.getCachedPlayers()).thenReturn(new Player[] { other }); + mockPlayers(new Player[] { other }); when(client.getVarbitValue(Varbits.IN_WILDERNESS)).thenReturn(0); // fire event @@ -258,7 +258,7 @@ void testNotifyNpc() { when(comp.getActions()).thenReturn(new String[] { "Pickpocket", "Attack", "Examine" }); when(npcManager.getHealth(NpcID.GUARD)).thenReturn(22); - when(client.getCachedNPCs()).thenReturn(new NPC[] { other }); + mockNpcs(new NPC[] { other }); when(config.deathNotifyMessage()).thenReturn("%USERNAME% has died to %NPC%"); // fire event diff --git a/src/test/java/dinkplugin/notifiers/KillCountNotifierTest.java b/src/test/java/dinkplugin/notifiers/KillCountNotifierTest.java index 4ee40e69..04f5930a 100644 --- a/src/test/java/dinkplugin/notifiers/KillCountNotifierTest.java +++ b/src/test/java/dinkplugin/notifiers/KillCountNotifierTest.java @@ -52,7 +52,7 @@ protected void setUp() { // init client mocks when(client.getVarbitValue(TimeUtils.ENABLE_PRECISE_TIMING)).thenReturn(1); - when(client.getCachedNPCs()).thenReturn(new NPC[0]); + mockNpcs(new NPC[0]); } @Test diff --git a/src/test/java/dinkplugin/notifiers/MockedNotifierTest.java b/src/test/java/dinkplugin/notifiers/MockedNotifierTest.java index d1be00cc..ada085ad 100644 --- a/src/test/java/dinkplugin/notifiers/MockedNotifierTest.java +++ b/src/test/java/dinkplugin/notifiers/MockedNotifierTest.java @@ -19,10 +19,13 @@ import lombok.SneakyThrows; import net.runelite.api.Client; import net.runelite.api.GameState; +import net.runelite.api.IndexedObjectSet; import net.runelite.api.ItemComposition; +import net.runelite.api.NPC; import net.runelite.api.Player; import net.runelite.api.Varbits; import net.runelite.api.WorldType; +import net.runelite.api.WorldView; import net.runelite.api.coords.WorldPoint; import net.runelite.client.callback.ClientThread; import net.runelite.client.chat.ChatMessageManager; @@ -44,6 +47,7 @@ import java.util.EnumSet; import java.util.concurrent.ScheduledExecutorService; import java.util.function.Consumer; +import java.util.stream.IntStream; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doAnswer; @@ -67,6 +71,9 @@ abstract class MockedNotifierTest extends MockedTestBase { @Mock protected Player localPlayer; + @Mock + protected WorldView worldView; + @Bind protected DrawManager drawManager = Mockito.mock(DrawManager.class); @@ -114,6 +121,7 @@ protected void setUp() { when(client.getWorldType()).thenReturn(EnumSet.noneOf(WorldType.class)); when(client.getVarbitValue(Varbits.ACCOUNT_TYPE)).thenReturn(AccountType.GROUP_IRONMAN.ordinal()); when(client.isPrayerActive(any())).thenReturn(false); + when(client.getTopLevelWorldView()).thenReturn(worldView); when(client.getLocalPlayer()).thenReturn(localPlayer); when(client.getGameState()).thenReturn(GameState.LOGGED_IN); when(localPlayer.getName()).thenReturn(PLAYER_NAME); @@ -156,6 +164,16 @@ protected void mockItem(int id, int price, String name) { when(itemManager.canonicalize(id)).thenReturn(id); } + protected void mockNpcs(NPC[] npcs) { + Mockito.>when(worldView.npcs()) + .thenReturn(new IndexedObjectSet<>(npcs, IntStream.range(0, npcs.length).toArray(), npcs.length)); + } + + protected void mockPlayers(Player[] players) { + Mockito.>when(worldView.players()) + .thenReturn(new IndexedObjectSet<>(players, IntStream.range(0, players.length).toArray(), players.length)); + } + @SneakyThrows protected void verifyCreateMessage(String url, boolean image, NotificationBody body) { Mockito.verify(messageHandler).createMessage(url, image, body); From 3bcd4c2f62079e320d46cbe0b810c799ef1590eb Mon Sep 17 00:00:00 2001 From: iProdigy <8106344+iProdigy@users.noreply.github.com> Date: Sun, 22 Dec 2024 22:49:10 -0600 Subject: [PATCH 2/2] chore: update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bffba29..50e00092 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Unreleased +- Dev: Remove deprecated `getCachedNPCs`/`getCachedPlayers` calls. (#632) + ## 1.10.19 - Bugfix: Use latest dynamic config when queried, rather than the cached value from okhttp. (#625)