Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove deprecated cached actor accesses #632

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dinkplugin/notifiers/DeathNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,15 @@ private Actor identifyKiller() {

// find another player interacting with us (that is preferably not a friend or clan member)
if (pvpEnabled) {
Optional<Player> pker = Arrays.stream(client.getCachedPlayers())
Optional<? extends Player> pker = client.getTopLevelWorldView().players().stream()
.filter(interacting)
.min(PK_COMPARATOR.apply(localPlayer)); // O(n)
if (pker.isPresent())
return pker.get();
}

// 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)
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/dinkplugin/notifiers/DeathNotifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/dinkplugin/notifiers/MockedNotifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.<IndexedObjectSet<? extends NPC>>when(worldView.npcs())
.thenReturn(new IndexedObjectSet<>(npcs, IntStream.range(0, npcs.length).toArray(), npcs.length));
}

protected void mockPlayers(Player[] players) {
Mockito.<IndexedObjectSet<? extends Player>>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);
Expand Down
Loading