From 4f9c22c4c10aaabc46ee7982050c40ea327d4ab1 Mon Sep 17 00:00:00 2001 From: Jordan Atwood Date: Mon, 10 Feb 2020 21:29:41 -0800 Subject: [PATCH 1/2] util: Create RSTimeUnit enum This commit adds a list of time units used in some duration calculations, specifically client ticks and game ticks. --- .../net/runelite/client/util/RSTimeUnit.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 runelite-client/src/main/java/net/runelite/client/util/RSTimeUnit.java diff --git a/runelite-client/src/main/java/net/runelite/client/util/RSTimeUnit.java b/runelite-client/src/main/java/net/runelite/client/util/RSTimeUnit.java new file mode 100644 index 0000000000..bbf5f832a3 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/util/RSTimeUnit.java @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2020, Jordan Atwood + * 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.util; + +import java.time.Duration; +import java.time.temporal.Temporal; +import java.time.temporal.TemporalUnit; +import lombok.Getter; +import net.runelite.api.Constants; + +@Getter +public enum RSTimeUnit implements TemporalUnit +{ + CLIENT_TICKS("Client tick", Duration.ofMillis(Constants.CLIENT_TICK_LENGTH)), + GAME_TICKS("Game tick", Duration.ofMillis(Constants.GAME_TICK_LENGTH)), + ; + + private final String name; + private final Duration duration; + + RSTimeUnit(String name, Duration estimatedDuration) + { + this.name = name; + duration = estimatedDuration; + } + + @Override + public boolean isDurationEstimated() + { + return false; + } + + @Override + public boolean isDateBased() + { + return false; + } + + @Override + public boolean isTimeBased() + { + return true; + } + + @Override + public boolean isSupportedBy(Temporal temporal) + { + return temporal.isSupported(this); + } + + @Override + public R addTo(R temporal, long amount) + { + return (R) temporal.plus(amount, this); + } + + @Override + public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) + { + return temporal1Inclusive.until(temporal2Exclusive, this); + } + + @Override + public String toString() + { + return name + " (" + duration.toMillis() + "ms)"; + } +} From e3db56804e451b6e269f9a0f3f05291ec842fec5 Mon Sep 17 00:00:00 2001 From: Jordan Atwood Date: Mon, 10 Feb 2020 21:31:15 -0800 Subject: [PATCH 2/2] plugins: Use RSTimeUnit util where applicable This replaces uses of `Duration#minusMillis()` and `TemporalUnit` referencing units of time defined in RSTimeUnits with their definitions. The Timers plugin constructors are changed to accept implementations of the `TemporalUnit` interface as they do not require any `ChronoUnit`-specific APIs. Furthermore, the timer lengths updated use the game tick lengths described in the OSRS wiki, which generally closely correspond to the previous length definitions we used. --- .../plugins/barbarianassault/GameTimer.java | 4 ++-- .../plugins/barbarianassault/Round.java | 4 ++-- .../client/plugins/timers/GameTimer.java | 24 ++++++++++--------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/barbarianassault/GameTimer.java b/runelite-client/src/main/java/net/runelite/client/plugins/barbarianassault/GameTimer.java index 1aae11adbd..fbf33bae14 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/barbarianassault/GameTimer.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/barbarianassault/GameTimer.java @@ -28,7 +28,7 @@ import java.time.Duration; import java.time.Instant; import java.time.LocalTime; import java.time.format.DateTimeFormatter; -import net.runelite.api.Constants; +import static net.runelite.client.util.RSTimeUnit.GAME_TICKS; class GameTimer { @@ -46,7 +46,7 @@ class GameTimer } else { - elapsed = Duration.between(startTime, now).minusMillis(Constants.GAME_TICK_LENGTH); + elapsed = Duration.between(startTime, now).minus(Duration.of(1, GAME_TICKS)); } return formatTime(LocalTime.ofSecondOfDay(elapsed.getSeconds())); diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/barbarianassault/Round.java b/runelite-client/src/main/java/net/runelite/client/plugins/barbarianassault/Round.java index 4428d504b4..9d13b74123 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/barbarianassault/Round.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/barbarianassault/Round.java @@ -29,7 +29,7 @@ import java.time.Instant; import javax.inject.Inject; import lombok.Getter; import lombok.Setter; -import net.runelite.api.Constants; +import static net.runelite.client.util.RSTimeUnit.GAME_TICKS; class Round { @@ -53,7 +53,7 @@ class Round public Round(Role role) { this.roundRole = role; - this.roundStartTime = Instant.now().plusMillis(2 * Constants.GAME_TICK_LENGTH); + this.roundStartTime = Instant.now().plus(Duration.of(2, GAME_TICKS)); } public long getRoundTime() diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/timers/GameTimer.java b/runelite-client/src/main/java/net/runelite/client/plugins/timers/GameTimer.java index 957f8867d3..a1aa1f5f21 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/timers/GameTimer.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/timers/GameTimer.java @@ -28,12 +28,14 @@ package net.runelite.client.plugins.timers; import java.time.Duration; import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; import javax.annotation.Nullable; import lombok.AccessLevel; import lombok.Getter; import net.runelite.api.GraphicID; import net.runelite.api.ItemID; import net.runelite.api.SpriteID; +import static net.runelite.client.util.RSTimeUnit.GAME_TICKS; @Getter(AccessLevel.PACKAGE) enum GameTimer @@ -43,19 +45,19 @@ enum GameTimer EXANTIFIRE(ItemID.EXTENDED_ANTIFIRE4, GameTimerImageType.ITEM, "Extended antifire", 12, ChronoUnit.MINUTES), OVERLOAD(ItemID.OVERLOAD_4, GameTimerImageType.ITEM, "Overload", 5, ChronoUnit.MINUTES, true), CANNON(ItemID.CANNON_BARRELS, GameTimerImageType.ITEM, "Cannon", 25, ChronoUnit.MINUTES), - MAGICIMBUE(SpriteID.SPELL_MAGIC_IMBUE, GameTimerImageType.SPRITE, "Magic imbue", 12, ChronoUnit.SECONDS), + MAGICIMBUE(SpriteID.SPELL_MAGIC_IMBUE, GameTimerImageType.SPRITE, "Magic imbue", 21, GAME_TICKS), FULLTB(SpriteID.SPELL_TELE_BLOCK, GameTimerImageType.SPRITE, "Full Teleblock", 5, ChronoUnit.MINUTES, true), HALFTB(SpriteID.SPELL_TELE_BLOCK, GameTimerImageType.SPRITE, "Half Teleblock", 150, ChronoUnit.SECONDS, true), DMM_FULLTB(SpriteID.SPELL_TELE_BLOCK, GameTimerImageType.SPRITE, "Deadman Mode Full Teleblock", 150, ChronoUnit.SECONDS, true), DMM_HALFTB(SpriteID.SPELL_TELE_BLOCK, GameTimerImageType.SPRITE, "Deadman Mode Half Teleblock", 75, ChronoUnit.SECONDS, true), SUPERANTIFIRE(ItemID.SUPER_ANTIFIRE_POTION4, GameTimerImageType.ITEM, "Super antifire", 3, ChronoUnit.MINUTES), - BIND(SpriteID.SPELL_BIND, GameTimerImageType.SPRITE, "Bind", GraphicID.BIND, 5, ChronoUnit.SECONDS, true), - SNARE(SpriteID.SPELL_SNARE, GameTimerImageType.SPRITE, "Snare", GraphicID.SNARE, 10, ChronoUnit.SECONDS, true), - ENTANGLE(SpriteID.SPELL_ENTANGLE, GameTimerImageType.SPRITE, "Entangle", GraphicID.ENTANGLE, 15, ChronoUnit.SECONDS, true), - ICERUSH(SpriteID.SPELL_ICE_RUSH, GameTimerImageType.SPRITE, "Ice rush", GraphicID.ICE_RUSH, 5, ChronoUnit.SECONDS, true), - ICEBURST(SpriteID.SPELL_ICE_BURST, GameTimerImageType.SPRITE, "Ice burst", GraphicID.ICE_BURST, 10, ChronoUnit.SECONDS, true), - ICEBLITZ(SpriteID.SPELL_ICE_BLITZ, GameTimerImageType.SPRITE, "Ice blitz", GraphicID.ICE_BLITZ, 15, ChronoUnit.SECONDS, true), - ICEBARRAGE(SpriteID.SPELL_ICE_BARRAGE, GameTimerImageType.SPRITE, "Ice barrage", GraphicID.ICE_BARRAGE, 20, ChronoUnit.SECONDS, true), + BIND(SpriteID.SPELL_BIND, GameTimerImageType.SPRITE, "Bind", GraphicID.BIND, 8, GAME_TICKS, true), + SNARE(SpriteID.SPELL_SNARE, GameTimerImageType.SPRITE, "Snare", GraphicID.SNARE, 16, GAME_TICKS, true), + ENTANGLE(SpriteID.SPELL_ENTANGLE, GameTimerImageType.SPRITE, "Entangle", GraphicID.ENTANGLE, 24, GAME_TICKS, true), + ICERUSH(SpriteID.SPELL_ICE_RUSH, GameTimerImageType.SPRITE, "Ice rush", GraphicID.ICE_RUSH, 8, GAME_TICKS, true), + ICEBURST(SpriteID.SPELL_ICE_BURST, GameTimerImageType.SPRITE, "Ice burst", GraphicID.ICE_BURST, 16, GAME_TICKS, true), + ICEBLITZ(SpriteID.SPELL_ICE_BLITZ, GameTimerImageType.SPRITE, "Ice blitz", GraphicID.ICE_BLITZ, 24, GAME_TICKS, true), + ICEBARRAGE(SpriteID.SPELL_ICE_BARRAGE, GameTimerImageType.SPRITE, "Ice barrage", GraphicID.ICE_BARRAGE, 32, GAME_TICKS, true), IMBUEDHEART(ItemID.IMBUED_HEART, GameTimerImageType.ITEM, "Imbued heart", GraphicID.IMBUED_HEART, 420, ChronoUnit.SECONDS, true), VENGEANCE(SpriteID.SPELL_VENGEANCE, GameTimerImageType.SPRITE, "Vengeance", 30, ChronoUnit.SECONDS), EXSUPERANTIFIRE(ItemID.EXTENDED_SUPER_ANTIFIRE4, GameTimerImageType.ITEM, "Extended Super AntiFire", 6, ChronoUnit.MINUTES), @@ -86,7 +88,7 @@ enum GameTimer private final int imageId; private final GameTimerImageType imageType; - GameTimer(int imageId, GameTimerImageType idType, String description, Integer graphicId, long time, ChronoUnit unit, boolean removedOnDeath) + GameTimer(int imageId, GameTimerImageType idType, String description, Integer graphicId, long time, TemporalUnit unit, boolean removedOnDeath) { this.description = description; this.graphicId = graphicId; @@ -96,12 +98,12 @@ enum GameTimer this.removedOnDeath = removedOnDeath; } - GameTimer(int imageId, GameTimerImageType idType, String description, long time, ChronoUnit unit, boolean removeOnDeath) + GameTimer(int imageId, GameTimerImageType idType, String description, long time, TemporalUnit unit, boolean removeOnDeath) { this(imageId, idType, description, null, time, unit, removeOnDeath); } - GameTimer(int imageId, GameTimerImageType idType, String description, long time, ChronoUnit unit) + GameTimer(int imageId, GameTimerImageType idType, String description, long time, TemporalUnit unit) { this(imageId, idType, description, null, time, unit, false); }