Refactor NpcIndicators functions

This commit is contained in:
William Maga
2019-01-19 13:42:25 -07:00
parent 98ca1bc056
commit bee3299e1d
3 changed files with 26 additions and 24 deletions

View File

@@ -24,7 +24,6 @@
*/ */
package net.runelite.client.plugins.npchighlight; package net.runelite.client.plugins.npchighlight;
import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import lombok.Getter; import lombok.Getter;
@@ -77,12 +76,4 @@ class MemorizedNpc
this.npcSize = composition.getSize(); 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);
}
} }

View File

@@ -74,10 +74,15 @@ public class NpcIndicatorsPlugin extends Plugin
{ {
private static final int MAX_ACTOR_VIEW_RANGE = 15; 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 // 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 // Option added to NPC menu
private static final String TAG = "Tag"; private static final String TAG = "Tag";
@@ -88,11 +93,6 @@ public class NpcIndicatorsPlugin extends Plugin
// Regex for splitting the hidden items in the config. // Regex for splitting the hidden items in the config.
private static final Splitter COMMA_SPLITTER = Splitter.on(",").omitEmptyStrings().trimResults(); private static final Splitter COMMA_SPLITTER = Splitter.on(",").omitEmptyStrings().trimResults();
static
{
((DecimalFormat)TIME_LEFT_FORMATTER).applyPattern("#0.0");
}
@Inject @Inject
private Client client; private Client client;
@@ -417,6 +417,16 @@ public class NpcIndicatorsPlugin extends Plugin
return new WorldPoint(currWP.getX() - dx, currWP.getY() - dy, currWP.getPlane()); 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) private void memorizeNpc(NPC npc)
{ {
final int npcIndex = npc.getIndex(); 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() private void checkNotifyNpcs()
{ {
if (!config.getNotifyOnRespawn()) if (!config.getNotifyOnRespawn())
@@ -511,14 +526,13 @@ public class NpcIndicatorsPlugin extends Plugin
} }
final double notifyDelay = ((double)config.getNotifyOnRespawnDelay()) / 1000; final double notifyDelay = ((double)config.getNotifyOnRespawnDelay()) / 1000;
final int tickCount = client.getTickCount();
final String notifyDelayStr = notifyDelay > 0 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."; : " respawned.";
for (MemorizedNpc npc : pendingNotificationNpcs) for (MemorizedNpc npc : pendingNotificationNpcs)
{ {
if (npc.getSecondsFromRespawn(tickCount, lastTickUpdate) <= notifyDelay) if (getTimeLeftForNpc(npc) <= notifyDelay)
{ {
pendingNotificationNpcs.remove(npc); pendingNotificationNpcs.remove(npc);
notifier.notify(npc.getNpcName() + notifyDelayStr); notifier.notify(npc.getNpcName() + notifyDelayStr);

View File

@@ -107,10 +107,7 @@ public class NpcSceneOverlay extends Overlay
OverlayUtil.renderPolygon(graphics, poly, color); OverlayUtil.renderPolygon(graphics, poly, color);
} }
final String timeLeftStr = NpcIndicatorsPlugin.TIME_LEFT_FORMATTER.format(npc.getSecondsFromRespawn( final String timeLeftStr = plugin.formatTime(plugin.getTimeLeftForNpc(npc));
client.getTickCount(),
plugin.getLastTickUpdate()
));
final int textWidth = graphics.getFontMetrics().stringWidth(timeLeftStr); final int textWidth = graphics.getFontMetrics().stringWidth(timeLeftStr);
final int textHeight = graphics.getFontMetrics().getAscent(); final int textHeight = graphics.getFontMetrics().getAscent();