From d2440f9c6628be5549f05ef8ffae127e6cd6c8a4 Mon Sep 17 00:00:00 2001 From: Levi Schuck Date: Sat, 19 May 2018 18:51:27 -0500 Subject: [PATCH] Fix til next level time for low xp amounts Cause: Time til next level relied on xp per hour being divided by 3600 (seconds in an hour). For XP rates under 3600 per hour, this would result in a divide by zero. Solution: Use floats and rely on an xp per second implementation that works on seconds instead of hours. It appears to work as expected. ![image](https://user-images.githubusercontent.com/245911/40274136-f8f61314-5b94-11e8-8d22-03b40b6108ff.png) --- .../plugins/xptracker/XpStateSingle.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/xptracker/XpStateSingle.java b/runelite-client/src/main/java/net/runelite/client/plugins/xptracker/XpStateSingle.java index 2b263406ab..a36d1f69d1 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/xptracker/XpStateSingle.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/xptracker/XpStateSingle.java @@ -54,11 +54,6 @@ class XpStateSingle return toHourly(xpGained); } - int getXpSec() - { - return getXpHr() / 3600; - } - int getActionsHr() { return toHourly(actions); @@ -71,12 +66,21 @@ class XpStateSingle return 0; } + return (int) ((1.0 / (getTimeElapsedInSeconds() / 3600.0)) * value); + } + + private long getTimeElapsedInSeconds() + { + if (skillTimeStart == null) + { + return 0; + } + // If the skill started just now, we can divide by near zero, this results in odd behavior. // To prevent that, pretend the skill has been active for a minute (60 seconds) // This will create a lower estimate for the first minute, // but it isn't ridiculous like saying 2 billion XP per hour. - long timeElapsedInSeconds = Math.max(60, Duration.between(skillTimeStart, Instant.now()).getSeconds()); - return (int) ((1.0 / (timeElapsedInSeconds / 3600.0)) * value); + return Math.max(60, Duration.between(skillTimeStart, Instant.now()).getSeconds()); } int getXpRemaining() @@ -120,10 +124,14 @@ class XpStateSingle String getTimeTillLevel() { - if (getXpSec() > 0) + float xpPerSecond = (float)xpGained / (float)getTimeElapsedInSeconds(); + + if (xpPerSecond > 0) { - return LocalTime.MIN.plusSeconds( getXpRemaining() / getXpSec() ).toString(); + float remainingSeconds = getXpRemaining() / xpPerSecond; + return LocalTime.MIN.plusSeconds((long)remainingSeconds).toString(); } + return "\u221e"; }