Merge pull request #8800 from deathbeam/add-loop-timer

Add loop timer infobox
This commit is contained in:
Tomas Slusny
2019-05-14 20:11:35 +02:00
committed by GitHub
2 changed files with 63 additions and 26 deletions

View File

@@ -27,6 +27,7 @@ package net.runelite.client.plugins.barrows;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.inject.Provides; import com.google.inject.Provides;
import java.time.temporal.ChronoUnit;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
@@ -41,6 +42,7 @@ import net.runelite.api.Item;
import net.runelite.api.ItemContainer; import net.runelite.api.ItemContainer;
import net.runelite.api.NullObjectID; import net.runelite.api.NullObjectID;
import net.runelite.api.ObjectID; import net.runelite.api.ObjectID;
import net.runelite.api.SpriteID;
import net.runelite.api.WallObject; import net.runelite.api.WallObject;
import net.runelite.api.events.ConfigChanged; import net.runelite.api.events.ConfigChanged;
import net.runelite.api.events.GameObjectChanged; import net.runelite.api.events.GameObjectChanged;
@@ -66,6 +68,8 @@ import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor; import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.ui.overlay.OverlayManager; import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.client.ui.overlay.infobox.InfoBoxManager; import net.runelite.client.ui.overlay.infobox.InfoBoxManager;
import net.runelite.client.ui.overlay.infobox.InfoBoxPriority;
import net.runelite.client.ui.overlay.infobox.LoopTimer;
import net.runelite.client.util.StackFormatter; import net.runelite.client.util.StackFormatter;
@PluginDescriptor( @PluginDescriptor(
@@ -92,6 +96,7 @@ public class BarrowsPlugin extends Plugin
WidgetInfo.BARROWS_PUZZLE_ANSWER3 WidgetInfo.BARROWS_PUZZLE_ANSWER3
); );
private static final long PRAYER_DRAIN_INTERVAL_MS = 18200;
private static final int CRYPT_REGION_ID = 14231; private static final int CRYPT_REGION_ID = 14231;
@Getter(AccessLevel.PACKAGE) @Getter(AccessLevel.PACKAGE)
@@ -100,6 +105,7 @@ public class BarrowsPlugin extends Plugin
@Getter(AccessLevel.PACKAGE) @Getter(AccessLevel.PACKAGE)
private final Set<GameObject> ladders = new HashSet<>(); private final Set<GameObject> ladders = new HashSet<>();
private LoopTimer barrowsPrayerDrainTimer;
private boolean wasInCrypt = false; private boolean wasInCrypt = false;
@Getter @Getter
@@ -313,13 +319,25 @@ public class BarrowsPlugin extends Plugin
{ {
if (config.showPrayerDrainTimer()) if (config.showPrayerDrainTimer())
{ {
infoBoxManager.addInfoBox(new BarrowsPrayerDrainTimer(this, spriteManager)); final LoopTimer loopTimer = new LoopTimer(
PRAYER_DRAIN_INTERVAL_MS,
ChronoUnit.MILLIS,
spriteManager.getSprite(SpriteID.TAB_PRAYER, 0),
this,
true);
loopTimer.setPriority(InfoBoxPriority.MED);
loopTimer.setTooltip("Prayer Drain");
infoBoxManager.addInfoBox(loopTimer);
barrowsPrayerDrainTimer = loopTimer;
} }
} }
private void stopPrayerDrainTimer() private void stopPrayerDrainTimer()
{ {
infoBoxManager.removeIf(BarrowsPrayerDrainTimer.class::isInstance); infoBoxManager.removeInfoBox(barrowsPrayerDrainTimer);
barrowsPrayerDrainTimer = null;
} }
private boolean isInCrypt() private boolean isInCrypt()

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, Ryan <progrs.site@gmail.com> * Copyright (c) 2019, Tomas Slusny <slusnucky@gmail.com>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -22,45 +22,59 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package net.runelite.client.plugins.barrows; package net.runelite.client.ui.overlay.infobox;
import com.google.common.base.Preconditions;
import java.awt.Color; import java.awt.Color;
import java.awt.image.BufferedImage;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import net.runelite.api.SpriteID; import java.time.temporal.ChronoUnit;
import net.runelite.client.game.SpriteManager; import lombok.Getter;
import net.runelite.client.ui.overlay.infobox.InfoBox; import lombok.ToString;
import net.runelite.client.ui.overlay.infobox.InfoBoxPriority; import net.runelite.client.plugins.Plugin;
class BarrowsPrayerDrainTimer extends InfoBox @Getter
@ToString
public class LoopTimer extends InfoBox
{ {
private static final long PRAYER_DRAIN_INTERVAL_MS = 18200;
private final Instant startTime; private final Instant startTime;
private final Duration duration;
private final boolean reverse;
BarrowsPrayerDrainTimer(BarrowsPlugin plugin, SpriteManager spriteManager) public LoopTimer(long period, ChronoUnit unit, BufferedImage image, Plugin plugin, boolean reverse)
{ {
super(spriteManager.getSprite(SpriteID.TAB_PRAYER, 0), plugin); super(image, plugin);
setPriority(InfoBoxPriority.MED);
setTooltip("Prayer Drain"); Preconditions.checkArgument(period > 0, "negative period!");
startTime = Instant.now(); startTime = Instant.now();
duration = Duration.of(period, unit);
this.reverse = reverse;
}
public LoopTimer(long period, ChronoUnit unit, BufferedImage image, Plugin plugin)
{
this(period, unit, image, plugin, false);
} }
@Override @Override
public String getText() public String getText()
{ {
Duration timeLeft = Duration.between(Instant.now(), getNextPrayerDrainTime()); final Duration progress = getProgress();
int seconds = (int) (timeLeft.toMillis() / 1000L); final int seconds = (int) (progress.toMillis() / 1000L);
return String.format("0:%02d", seconds); final int minutes = (seconds % 3600) / 60;
final int secs = seconds % 60;
return String.format("%d:%02d", minutes, secs);
} }
@Override @Override
public Color getTextColor() public Color getTextColor()
{ {
Duration timeLeft = Duration.between(Instant.now(), getNextPrayerDrainTime()); final Duration progress = getProgress();
//check if timer has 10% of time left // check if timer has 10% of time left
if (timeLeft.getSeconds() < (PRAYER_DRAIN_INTERVAL_MS / 1000 * .10)) if (progress.getSeconds() < (duration.getSeconds() * .10))
{ {
return Color.RED.brighter(); return Color.RED.brighter();
} }
@@ -68,10 +82,15 @@ class BarrowsPrayerDrainTimer extends InfoBox
return Color.WHITE; return Color.WHITE;
} }
private Instant getNextPrayerDrainTime() private Duration getProgress()
{ {
Duration timePassed = Duration.between(startTime, Instant.now()); final Duration passed = Duration.between(startTime, Instant.now());
// Get how many intervals have passed so far and add one more to find the next prayer drain time final long passedMillis = passed.toMillis();
return startTime.plusMillis((timePassed.toMillis() / PRAYER_DRAIN_INTERVAL_MS + 1) * PRAYER_DRAIN_INTERVAL_MS); final long durationMillis = duration.toMillis();
final long progress = passedMillis % durationMillis;
return Duration.ofMillis(reverse
? durationMillis - progress
: progress);
} }
} }