-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvement + Backend: ServerTimeMark (#2823)
Co-authored-by: Empa <itsempa@users.noreply.github.com> Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
- Loading branch information
1 parent
0e70168
commit 9fb3897
Showing
4 changed files
with
128 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/at/hannibal2/skyhanni/events/minecraft/ServerTickEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package at.hannibal2.skyhanni.events.minecraft | ||
|
||
import at.hannibal2.skyhanni.api.event.SkyHanniEvent | ||
|
||
object ServerTickEvent : SkyHanniEvent() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/at/hannibal2/skyhanni/utils/ServerTimeMark.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package at.hannibal2.skyhanni.utils | ||
|
||
import at.hannibal2.skyhanni.data.MinecraftData | ||
import at.hannibal2.skyhanni.utils.TimeUtils.inWholeTicks | ||
import at.hannibal2.skyhanni.utils.TimeUtils.ticks | ||
import kotlin.time.Duration | ||
|
||
/** | ||
* This is a Helper Class similar to [SimpleTimeMark], but for a rough estimate of Server Ticks instead of real time. | ||
* | ||
* This can provide a more accurate estimate of certain timers for ingame events, because some are based off of | ||
* the server's tps instead of real time, and therefore are affected by server lag. | ||
*/ | ||
@JvmInline | ||
value class ServerTimeMark private constructor(val ticks: Long) : Comparable<ServerTimeMark> { | ||
|
||
operator fun minus(other: ServerTimeMark): Duration = | ||
(ticks - other.ticks).ticks | ||
|
||
operator fun plus(other: Duration) = | ||
ServerTimeMark(ticks + other.inWholeTicks) | ||
|
||
operator fun minus(other: Duration): ServerTimeMark = plus(-other) | ||
|
||
fun passedSince(): Duration = now() - this | ||
|
||
fun timeUntil(): Duration = -passedSince() | ||
|
||
fun isInPast(): Boolean = timeUntil().isNegative() | ||
|
||
fun isInFuture(): Boolean = timeUntil().isPositive() | ||
|
||
fun isFarPast(): Boolean = this == FAR_PAST | ||
|
||
fun isFarFuture(): Boolean = ticks == FAR_FUTURE_TICKS | ||
|
||
override fun compareTo(other: ServerTimeMark): Int = ticks.compareTo(other.ticks) | ||
|
||
override fun toString(): String = when (ticks) { | ||
FAR_PAST_TICKS -> "The Far Past" | ||
FAR_FUTURE_TICKS -> "The Far Future" | ||
else -> "ServerTimeMark(ticks=$ticks, now=${MinecraftData.totalServerTicks})" | ||
} | ||
|
||
companion object { | ||
|
||
fun now() = ServerTimeMark(MinecraftData.totalServerTicks) | ||
|
||
private const val FAR_PAST_TICKS = Long.MIN_VALUE | ||
private const val FAR_FUTURE_TICKS = Long.MAX_VALUE | ||
|
||
val FAR_PAST = ServerTimeMark(FAR_PAST_TICKS) | ||
val FAR_FUTURE = ServerTimeMark(FAR_FUTURE_TICKS) | ||
|
||
} | ||
|
||
} |