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 29a25df60e
commit 0adf5876db
45 changed files with 782 additions and 718 deletions

View File

@@ -12,40 +12,44 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@@ -0,0 +0,0 @@
+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..00000000000000000000000000000000
+ * 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..00000000000000000000000000000000
+
+ @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;
+ }
+}