From bee3299e1d9d54277f76f3043fee638e621b1273 Mon Sep 17 00:00:00 2001 From: William Maga Date: Sat, 19 Jan 2019 13:42:25 -0700 Subject: [PATCH] Refactor NpcIndicators functions --- .../plugins/npchighlight/MemorizedNpc.java | 9 ----- .../npchighlight/NpcIndicatorsPlugin.java | 36 +++++++++++++------ .../plugins/npchighlight/NpcSceneOverlay.java | 5 +-- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/MemorizedNpc.java b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/MemorizedNpc.java index 41ce059c42..d9e1cad9a4 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/MemorizedNpc.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/MemorizedNpc.java @@ -24,7 +24,6 @@ */ package net.runelite.client.plugins.npchighlight; -import java.time.Instant; import java.util.ArrayList; import java.util.List; import lombok.Getter; @@ -77,12 +76,4 @@ class MemorizedNpc this.npcSize = composition.getSize(); } } - - public double getSecondsFromRespawn(int tickCount, Instant lastTickUpdate) - { - final Instant now = Instant.now(); - final double baseTick = NpcIndicatorsPlugin.ESTIMATED_TICK_LENGTH * (diedOnTick + respawnTime - tickCount); - final double sinceLast = (now.toEpochMilli() - lastTickUpdate.toEpochMilli()) / 1000.0; - return Math.max(0.0, baseTick - sinceLast); - } } \ No newline at end of file diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcIndicatorsPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcIndicatorsPlugin.java index d2eb9a5ddf..9f793204ad 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcIndicatorsPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcIndicatorsPlugin.java @@ -74,10 +74,15 @@ public class NpcIndicatorsPlugin extends Plugin { private static final int MAX_ACTOR_VIEW_RANGE = 15; - public static final NumberFormat TIME_LEFT_FORMATTER = DecimalFormat.getInstance(Locale.US); - // Estimated time of a game tick in seconds - public static final double ESTIMATED_TICK_LENGTH = 0.6; + private static final double ESTIMATED_TICK_LENGTH = 0.6; + + private static final NumberFormat TIME_LEFT_FORMATTER = DecimalFormat.getInstance(Locale.getDefault()); + + static + { + ((DecimalFormat)TIME_LEFT_FORMATTER).applyPattern("#0.0"); + } // Option added to NPC menu private static final String TAG = "Tag"; @@ -88,11 +93,6 @@ public class NpcIndicatorsPlugin extends Plugin // Regex for splitting the hidden items in the config. private static final Splitter COMMA_SPLITTER = Splitter.on(",").omitEmptyStrings().trimResults(); - static - { - ((DecimalFormat)TIME_LEFT_FORMATTER).applyPattern("#0.0"); - } - @Inject private Client client; @@ -417,6 +417,16 @@ public class NpcIndicatorsPlugin extends Plugin return new WorldPoint(currWP.getX() - dx, currWP.getY() - dy, currWP.getPlane()); } + public double getTimeLeftForNpc(MemorizedNpc npc) + { + final Instant now = Instant.now(); + final double baseTick = NpcIndicatorsPlugin.ESTIMATED_TICK_LENGTH * ( + npc.getDiedOnTick() + npc.getRespawnTime() - client.getTickCount() + ); + final double sinceLast = (now.toEpochMilli() - lastTickUpdate.toEpochMilli()) / 1000.0; + return Math.max(0.0, baseTick - sinceLast); + } + private void memorizeNpc(NPC npc) { final int npcIndex = npc.getIndex(); @@ -503,6 +513,11 @@ public class NpcIndicatorsPlugin extends Plugin } } + public String formatTime(double time) + { + return TIME_LEFT_FORMATTER.format(time); + } + private void checkNotifyNpcs() { if (!config.getNotifyOnRespawn()) @@ -511,14 +526,13 @@ public class NpcIndicatorsPlugin extends Plugin } final double notifyDelay = ((double)config.getNotifyOnRespawnDelay()) / 1000; - final int tickCount = client.getTickCount(); final String notifyDelayStr = notifyDelay > 0 - ? " is less than " + TIME_LEFT_FORMATTER.format(notifyDelay) + " seconds from respawn" + ? " is less than " + formatTime(notifyDelay) + " seconds from respawn" : " respawned."; for (MemorizedNpc npc : pendingNotificationNpcs) { - if (npc.getSecondsFromRespawn(tickCount, lastTickUpdate) <= notifyDelay) + if (getTimeLeftForNpc(npc) <= notifyDelay) { pendingNotificationNpcs.remove(npc); notifier.notify(npc.getNpcName() + notifyDelayStr); diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcSceneOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcSceneOverlay.java index ed3dd3170a..81d4e7f06b 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcSceneOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcSceneOverlay.java @@ -107,10 +107,7 @@ public class NpcSceneOverlay extends Overlay OverlayUtil.renderPolygon(graphics, poly, color); } - final String timeLeftStr = NpcIndicatorsPlugin.TIME_LEFT_FORMATTER.format(npc.getSecondsFromRespawn( - client.getTickCount(), - plugin.getLastTickUpdate() - )); + final String timeLeftStr = plugin.formatTime(plugin.getTimeLeftForNpc(npc)); final int textWidth = graphics.getFontMetrics().stringWidth(timeLeftStr); final int textHeight = graphics.getFontMetrics().getAscent();