Finish converting most of the undeprecated api to jspecify

This commit is contained in:
Jake Potrebic 2024-09-30 11:44:36 -07:00
parent ba3c29b92e
commit e7e1ab56ca
No known key found for this signature in database
GPG key ID: ECE0B3C133C016C5
45 changed files with 1046 additions and 982 deletions

View file

@ -6,46 +6,50 @@ Subject: [PATCH] Add Tick TemporalUnit
diff --git a/src/main/java/io/papermc/paper/util/Tick.java b/src/main/java/io/papermc/paper/util/Tick.java
new file mode 100644
index 0000000000000000000000000000000000000000..10430f02e1d1e654383154c04473f07469bc7fee
index 0000000000000000000000000000000000000000..1b264819c6faf2a4390d76350deb8e93804c6772
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/Tick.java
@@ -0,0 +1,95 @@
@@ -0,0 +1,101 @@
+package io.papermc.paper.util;
+
+import net.kyori.adventure.util.Ticks;
+import org.jetbrains.annotations.NotNull;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.time.temporal.Temporal;
+import java.time.temporal.TemporalUnit;
+import java.util.Objects;
+import net.kyori.adventure.util.Ticks;
+import org.jspecify.annotations.NullMarked;
+
+/**
+ * A TemporalUnit that represents the target length of one server tick. This is defined
+ * as 50 milliseconds. Note that this class is not for measuring the length that a tick
+ * took, rather it is used for simple conversion between times and ticks.
+ *
+ * @see #tick()
+ */
+@NullMarked
+public final class Tick implements TemporalUnit {
+
+ private static final Tick INSTANCE = new Tick(Ticks.SINGLE_TICK_DURATION_MS);
+
+ private final long milliseconds;
+
+ /**
+ * Gets the instance of the tick temporal unit.
+ *
+ * @return the tick instance
+ */
+ public static @NotNull Tick tick() {
+ public static Tick tick() {
+ return INSTANCE;
+ }
+
+ /**
+ * Creates a new tick.
+ *
+ * @param length the length of the tick in milliseconds
+ * @see #tick()
+ */
+ private Tick(long length) {
+ private Tick(final long length) {
+ this.milliseconds = length;
+ }
+
@ -53,27 +57,29 @@ index 0000000000000000000000000000000000000000..10430f02e1d1e654383154c04473f074
+ * Creates a duration from an amount of ticks. This is shorthand for
+ * {@link Duration#of(long, TemporalUnit)} called with the amount of ticks and
+ * {@link #tick()}.
+ *
+ * @param ticks the amount of ticks
+ * @return the duration
+ */
+ public static @NotNull Duration of(long ticks) {
+ public static Duration of(final long ticks) {
+ return Duration.of(ticks, INSTANCE);
+ }
+
+ /**
+ * Gets the number of whole ticks that occur in the provided duration. Note that this
+ * method returns an {@code int} as this is the unit that Minecraft stores ticks in.
+ *
+ * @param duration the duration
+ * @return the number of whole ticks in this duration
+ * @throws ArithmeticException if the duration is zero or an overflow occurs
+ */
+ public int fromDuration(@NotNull Duration duration) {
+ public int fromDuration(final Duration duration) {
+ Objects.requireNonNull(duration, "duration cannot be null");
+ return Math.toIntExact(Math.floorDiv(duration.toMillis(), this.milliseconds));
+ }
+
+ @Override
+ public @NotNull Duration getDuration() {
+ public Duration getDuration() {
+ return Duration.ofMillis(this.milliseconds);
+ }
+
@ -96,12 +102,12 @@ index 0000000000000000000000000000000000000000..10430f02e1d1e654383154c04473f074
+
+ @SuppressWarnings("unchecked") // following ChronoUnit#addTo
+ @Override
+ public <R extends Temporal> @NotNull R addTo(@NotNull R temporal, long amount) {
+ return (R) temporal.plus(getDuration().multipliedBy(amount));
+ public <R extends Temporal> R addTo(final R temporal, final long amount) {
+ return (R) temporal.plus(this.getDuration().multipliedBy(amount));
+ }
+
+ @Override
+ public long between(@NotNull Temporal start, @NotNull Temporal end) {
+ public long between(final Temporal start, final Temporal end) {
+ return start.until(end, ChronoUnit.MILLIS) / this.milliseconds;
+ }
+}