From 471a0ba73f926c33b3e04b25b7e27c4d59f64671 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Fri, 10 May 2019 17:42:16 +0200 Subject: [PATCH 1/2] Add loop timer infobox Add loop timer infobox that will keep looping through specified interval. Signed-off-by: Tomas Slusny --- .../client/ui/overlay/infobox/LoopTimer.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 runelite-client/src/main/java/net/runelite/client/ui/overlay/infobox/LoopTimer.java diff --git a/runelite-client/src/main/java/net/runelite/client/ui/overlay/infobox/LoopTimer.java b/runelite-client/src/main/java/net/runelite/client/ui/overlay/infobox/LoopTimer.java new file mode 100644 index 0000000000..1e11f1900e --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/ui/overlay/infobox/LoopTimer.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2019, Tomas Slusny + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.ui.overlay.infobox; + +import com.google.common.base.Preconditions; +import java.awt.Color; +import java.awt.image.BufferedImage; +import java.time.Duration; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import lombok.Getter; +import lombok.ToString; +import net.runelite.client.plugins.Plugin; + +@Getter +@ToString +public class LoopTimer extends InfoBox +{ + private final Instant startTime; + private final Duration duration; + private final boolean reverse; + + public LoopTimer(long period, ChronoUnit unit, BufferedImage image, Plugin plugin, boolean reverse) + { + super(image, plugin); + + Preconditions.checkArgument(period > 0, "negative period!"); + + 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 + public String getText() + { + final Duration progress = getProgress(); + final int seconds = (int) (progress.toMillis() / 1000L); + final int minutes = (seconds % 3600) / 60; + final int secs = seconds % 60; + return String.format("%d:%02d", minutes, secs); + } + + @Override + public Color getTextColor() + { + final Duration progress = getProgress(); + + // check if timer has 10% of time left + if (progress.getSeconds() < (duration.getSeconds() * .10)) + { + return Color.RED.brighter(); + } + + return Color.WHITE; + } + + private Duration getProgress() + { + final Duration passed = Duration.between(startTime, Instant.now()); + final long passedMillis = passed.toMillis(); + final long durationMillis = duration.toMillis(); + final long progress = passedMillis % durationMillis; + + return Duration.ofMillis(reverse + ? durationMillis - progress + : progress); + } +} \ No newline at end of file From f82aa6be93e7f5387ac1697e9216bf8b79ddc506 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Tue, 14 May 2019 00:00:36 +0200 Subject: [PATCH 2/2] Use LoopTimer in Barrows plugin Signed-off-by: Tomas Slusny --- .../client/plugins/barrows/BarrowsPlugin.java | 22 +++++- .../barrows/BarrowsPrayerDrainTimer.java | 77 ------------------- 2 files changed, 20 insertions(+), 79 deletions(-) delete mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/barrows/BarrowsPrayerDrainTimer.java diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/barrows/BarrowsPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/barrows/BarrowsPlugin.java index 72bf08fe7e..30ecefb885 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/barrows/BarrowsPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/barrows/BarrowsPlugin.java @@ -27,6 +27,7 @@ package net.runelite.client.plugins.barrows; import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; import com.google.inject.Provides; +import java.time.temporal.ChronoUnit; import java.util.HashSet; import java.util.Set; import javax.inject.Inject; @@ -41,6 +42,7 @@ import net.runelite.api.Item; import net.runelite.api.ItemContainer; import net.runelite.api.NullObjectID; import net.runelite.api.ObjectID; +import net.runelite.api.SpriteID; import net.runelite.api.WallObject; import net.runelite.api.events.ConfigChanged; 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.ui.overlay.OverlayManager; 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; @PluginDescriptor( @@ -92,6 +96,7 @@ public class BarrowsPlugin extends Plugin WidgetInfo.BARROWS_PUZZLE_ANSWER3 ); + private static final long PRAYER_DRAIN_INTERVAL_MS = 18200; private static final int CRYPT_REGION_ID = 14231; @Getter(AccessLevel.PACKAGE) @@ -100,6 +105,7 @@ public class BarrowsPlugin extends Plugin @Getter(AccessLevel.PACKAGE) private final Set ladders = new HashSet<>(); + private LoopTimer barrowsPrayerDrainTimer; private boolean wasInCrypt = false; @Getter @@ -313,13 +319,25 @@ public class BarrowsPlugin extends Plugin { 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() { - infoBoxManager.removeIf(BarrowsPrayerDrainTimer.class::isInstance); + infoBoxManager.removeInfoBox(barrowsPrayerDrainTimer); + barrowsPrayerDrainTimer = null; } private boolean isInCrypt() diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/barrows/BarrowsPrayerDrainTimer.java b/runelite-client/src/main/java/net/runelite/client/plugins/barrows/BarrowsPrayerDrainTimer.java deleted file mode 100644 index 4428a54f07..0000000000 --- a/runelite-client/src/main/java/net/runelite/client/plugins/barrows/BarrowsPrayerDrainTimer.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (c) 2019, Ryan - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package net.runelite.client.plugins.barrows; - -import java.awt.Color; -import java.time.Duration; -import java.time.Instant; -import net.runelite.api.SpriteID; -import net.runelite.client.game.SpriteManager; -import net.runelite.client.ui.overlay.infobox.InfoBox; -import net.runelite.client.ui.overlay.infobox.InfoBoxPriority; - -class BarrowsPrayerDrainTimer extends InfoBox -{ - private static final long PRAYER_DRAIN_INTERVAL_MS = 18200; - - private final Instant startTime; - - BarrowsPrayerDrainTimer(BarrowsPlugin plugin, SpriteManager spriteManager) - { - super(spriteManager.getSprite(SpriteID.TAB_PRAYER, 0), plugin); - setPriority(InfoBoxPriority.MED); - setTooltip("Prayer Drain"); - startTime = Instant.now(); - } - - @Override - public String getText() - { - Duration timeLeft = Duration.between(Instant.now(), getNextPrayerDrainTime()); - int seconds = (int) (timeLeft.toMillis() / 1000L); - return String.format("0:%02d", seconds); - } - - @Override - public Color getTextColor() - { - Duration timeLeft = Duration.between(Instant.now(), getNextPrayerDrainTime()); - - //check if timer has 10% of time left - if (timeLeft.getSeconds() < (PRAYER_DRAIN_INTERVAL_MS / 1000 * .10)) - { - return Color.RED.brighter(); - } - - return Color.WHITE; - } - - private Instant getNextPrayerDrainTime() - { - Duration timePassed = Duration.between(startTime, Instant.now()); - // Get how many intervals have passed so far and add one more to find the next prayer drain time - return startTime.plusMillis((timePassed.toMillis() / PRAYER_DRAIN_INTERVAL_MS + 1) * PRAYER_DRAIN_INTERVAL_MS); - } -}