diff --git a/GauntletPlugin.java b/GauntletPlugin.java index aad97ec..fc73397 100644 --- a/GauntletPlugin.java +++ b/GauntletPlugin.java @@ -33,6 +33,7 @@ import net.runelite.api.events.GameTick; import net.runelite.api.events.NpcDespawned; import net.runelite.api.events.NpcSpawned; import net.runelite.api.events.VarbitChanged; +import net.runelite.client.callback.ClientThread; import net.runelite.client.config.ConfigManager; import net.runelite.client.eventbus.Subscribe; import net.runelite.client.game.SkillIconManager; @@ -87,6 +88,9 @@ public class GauntletPlugin extends Plugin { @Inject private Client client; + @Inject + private ClientThread clientThread; + @Getter(AccessLevel.PUBLIC) @Inject private OverlayManager overlayManager; @@ -113,6 +117,8 @@ public class GauntletPlugin extends Plugin { public boolean tornadoesActive = false; public int tornadoTicks = GauntletUtils.TORNADO_TICKS; + public boolean completeStartup = false; + @Override protected void startUp() { loadImages(config.iconSize()); @@ -121,13 +127,25 @@ public class GauntletPlugin extends Plugin { timerVisible = config.displayTimerWidget(); timer.resetStates(); - timer.initStates(); if (timerVisible) { overlayManager.add(timer); } overlayManager.add(overlay); + + // This section checks if the user is trying to start the timer while mid-raid. + // Varbits can only be checked on the client thread. Perform init check if nessessary. + if (client.getGameState() != GameState.STARTING && client.getGameState() != GameState.UNKNOWN) { + completeStartup = false; + clientThread.invoke(new Runnable() { + public void run() { + timer.initStates(); + completeStartup = true; + } + }); + } else + completeStartup = true; } @Override @@ -179,7 +197,8 @@ public class GauntletPlugin extends Plugin { @Subscribe public void onVarbitChanged(VarbitChanged event) { // This handles the timer based on varp states. - timer.checkStates(true); + if (this.completeStartup) + timer.checkStates(true); } @Subscribe @@ -336,7 +355,8 @@ public class GauntletPlugin extends Plugin { @Subscribe public void onGameTick(GameTick event) { // This handles the timer based on player health. - timer.checkStates(false); + if (this.completeStartup) + timer.checkStates(false); // This section handles the boss attack counter if they perform a projectile attack. Set newProjectiles = new HashSet<>(); diff --git a/GauntletTimer.java b/GauntletTimer.java index ffe2334..e09d4e3 100644 --- a/GauntletTimer.java +++ b/GauntletTimer.java @@ -26,7 +26,6 @@ package net.runelite.client.plugins.gauntlet; import net.runelite.api.ChatMessageType; import net.runelite.api.Client; -import net.runelite.api.GameState; import net.runelite.api.Player; import net.runelite.client.ui.overlay.Overlay; import net.runelite.client.ui.overlay.OverlayMenuEntry; @@ -79,16 +78,16 @@ class GauntletTimer extends Overlay { * Good luck to you. If you restart plugin mid raid, oh well. Your timer's going to be inaccurate. */ public void initStates() { -// timeRaidStart = -1L; -// timeBossEnter = -1L; -// -// if (GauntletUtils.inRaid(client)) { -// currentState = RaidState.IN_RAID; -// if (GauntletUtils.inBoss(client)) { -// currentState = RaidState.IN_BOSS; -// } -// } else -// currentState = RaidState.UNKNOWN; + timeRaidStart = -1L; + timeBossEnter = -1L; + + if (GauntletUtils.inRaid(client)) { + currentState = RaidState.IN_RAID; + if (GauntletUtils.inBoss(client)) { + currentState = RaidState.IN_BOSS; + } + } else + currentState = RaidState.UNKNOWN; } /** @@ -125,7 +124,7 @@ class GauntletTimer extends Overlay { public void checkStates(boolean checkVarps) { final Player p = client.getLocalPlayer(); - if (p == null) + if (p == null || !plugin.completeStartup) return; if (checkVarps) { @@ -201,7 +200,7 @@ class GauntletTimer extends Overlay { @Override public Dimension render(Graphics2D graphics) { - if (currentState == RaidState.UNKNOWN || timeRaidStart == -1L) { + if (currentState == RaidState.UNKNOWN) { return null; } @@ -209,20 +208,24 @@ class GauntletTimer extends Overlay { panelComponent.getChildren().add(TitleComponent.builder().text("Gauntlet Timer").color(Color.WHITE).build()); - String elapsedPrepTime, elapsedBossTime, elapsedTotalTime; - elapsedTotalTime = calculateElapsedTime(System.currentTimeMillis(), timeRaidStart); - - if (currentState == RaidState.IN_RAID) { - elapsedPrepTime = calculateElapsedTime(timeRaidStart, System.currentTimeMillis()); - elapsedBossTime = "0:00"; + if(timeRaidStart == -1L) { // User restarted the plugin mid raid. Timer is inaccurate. + panelComponent.getChildren().add(LineComponent.builder().left("Inactive").right("0:00").build()); } else { - elapsedPrepTime = calculateElapsedTime(timeRaidStart, timeBossEnter); - elapsedBossTime = calculateElapsedTime(System.currentTimeMillis(), timeBossEnter); - } + String elapsedPrepTime, elapsedBossTime, elapsedTotalTime; + elapsedTotalTime = calculateElapsedTime(System.currentTimeMillis(), timeRaidStart); - panelComponent.getChildren().add(LineComponent.builder().left("Preparation").right(elapsedPrepTime).build()); - panelComponent.getChildren().add(LineComponent.builder().left("Boss Fight").right(elapsedBossTime).build()); - panelComponent.getChildren().add(LineComponent.builder().left("Total Time").right(elapsedTotalTime).build()); + if (currentState == RaidState.IN_RAID) { + elapsedPrepTime = calculateElapsedTime(timeRaidStart, System.currentTimeMillis()); + elapsedBossTime = "0:00"; + } else { + elapsedPrepTime = calculateElapsedTime(timeRaidStart, timeBossEnter); + elapsedBossTime = calculateElapsedTime(System.currentTimeMillis(), timeBossEnter); + } + + panelComponent.getChildren().add(LineComponent.builder().left("Preparation").right(elapsedPrepTime).build()); + panelComponent.getChildren().add(LineComponent.builder().left("Boss Fight").right(elapsedBossTime).build()); + panelComponent.getChildren().add(LineComponent.builder().left("Total Time").right(elapsedTotalTime).build()); + } return panelComponent.render(graphics); } diff --git a/GauntletUtils.java b/GauntletUtils.java index b59f8d8..3b1594d 100644 --- a/GauntletUtils.java +++ b/GauntletUtils.java @@ -141,7 +141,7 @@ public class GauntletUtils { public static boolean inRaid(Client client) { try { return client.getVarbitValue(client.getVarps(), VARP_RAID_ROOM) == 1; - } catch (IndexOutOfBoundsException ignore) { + } catch (IndexOutOfBoundsException | NullPointerException ignored) { return false; } } @@ -155,7 +155,7 @@ public class GauntletUtils { public static boolean inBoss(Client client) { try { return client.getVarbitValue(client.getVarps(), VARP_BOSS_ROOM) == 1; - } catch (IndexOutOfBoundsException ignore) { + } catch (IndexOutOfBoundsException | NullPointerException ignored) { return false; } }