Skip to content

Commit

Permalink
slayer: fix forgetting initial amount on disconnect
Browse files Browse the repository at this point in the history
The server zeros the varps on login after a disconnect, causing the
plugin to clear the task. Setting amount=0 avoids this. Also set
loginFlag etc like the other gamestate changes  to avoid the infobox
being readded on login.

Co-authored-by: sam <[email protected]>
  • Loading branch information
Adam- and geheur committed May 21, 2023
1 parent 44b13c2 commit 4952c46
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,15 @@ public void onGameStateChanged(GameStateChanged event)
{
switch (event.getGameState())
{
// client (or with CONNECTION_LOST, the server...) will soon zero the slayer varps.
// zero task/amount so that this doesn't cause the plugin to reset the task, which
// would forget the initial amount. The vars are then resynced shortly after
case HOPPING:
case LOGGING_IN:
case CONNECTION_LOST:
taskName = "";
amount = 0;
loginFlag = true;
loginFlag = true; // to reinitialize initialAmount and avoid re-adding the infobox
targets.clear();
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.client.ui.overlay.infobox.InfoBoxManager;
import org.junit.After;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
Expand Down Expand Up @@ -298,4 +299,60 @@ private boolean matches(final String npcName, final Task task)
slayerPlugin.setTask(task.getName(), 0, 0);
return slayerPlugin.isTarget(npc);
}

@Test
public void testDisconnect()
{
when(client.getVarpValue(VarPlayer.SLAYER_TASK_SIZE)).thenReturn(42);
when(client.getVarpValue(VarPlayer.SLAYER_TASK_CREATURE)).thenReturn(1);
when(client.getEnum(EnumID.SLAYER_TASK_CREATURE))
.thenAnswer(a ->
{
EnumComposition e = mock(EnumComposition.class);
when(e.getStringValue(anyInt())).thenReturn("mocked npc");
return e;
});

// initial amount is fetched from config at sync
when(configManager.getRSProfileConfiguration("slayer", "initialAmount", int.class)).thenReturn(99);

slayerPlugin.setTaskName("mocked npc");
slayerPlugin.setAmount(42);
slayerPlugin.setInitialAmount(99);

// Disconnect looks like:
// gs connection_lost
// gs logged_in
// varp=0
// varp=value

GameStateChanged connectionLost = new GameStateChanged();
connectionLost.setGameState(GameState.CONNECTION_LOST);
slayerPlugin.onGameStateChanged(connectionLost);

GameStateChanged loggedIn = new GameStateChanged();
loggedIn.setGameState(GameState.LOGGED_IN);
slayerPlugin.onGameStateChanged(loggedIn);

when(client.getVarpValue(VarPlayer.SLAYER_TASK_SIZE)).thenReturn(0);
when(client.getVarpValue(VarPlayer.SLAYER_TASK_CREATURE)).thenReturn(0);

VarbitChanged taskSizeChanged = new VarbitChanged();
taskSizeChanged.setVarpId(VarPlayer.SLAYER_TASK_SIZE);
slayerPlugin.onVarbitChanged(taskSizeChanged);

VarbitChanged taskCreatureChanged = new VarbitChanged();
taskCreatureChanged.setVarpId(VarPlayer.SLAYER_TASK_CREATURE);
slayerPlugin.onVarbitChanged(taskCreatureChanged);

when(client.getVarpValue(VarPlayer.SLAYER_TASK_SIZE)).thenReturn(42);
when(client.getVarpValue(VarPlayer.SLAYER_TASK_CREATURE)).thenReturn(1);

slayerPlugin.onVarbitChanged(taskSizeChanged);
slayerPlugin.onVarbitChanged(taskCreatureChanged);

assertEquals("mocked npc", slayerPlugin.getTaskName());
assertEquals(42, slayerPlugin.getAmount());
assertEquals(99, slayerPlugin.getInitialAmount());
}
}

0 comments on commit 4952c46

Please sign in to comment.