Minestom/src/main/java/net/minestom/server/utils/TickUtils.java

47 lines
1.4 KiB
Java
Raw Normal View History

package net.minestom.server.utils;
import net.minestom.server.MinecraftServer;
2021-05-15 08:31:24 +02:00
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
import java.time.Duration;
/**
* Tick related utilities.
*/
public class TickUtils {
2021-03-04 14:41:25 +01:00
/**
* Number of ticks per second for the default Java-edition client.
*/
public static final int CLIENT_TPS = 20;
/**
* Length of time per tick for the default Java-edition client.
*/
public static final int CLIENT_TICK_MS = 50;
/**
* Creates a number of ticks from a given duration, based on {@link MinecraftServer#TICK_MS}.
2021-05-15 08:31:24 +02:00
*
* @param duration the duration
* @return the number of ticks
* @throws IllegalArgumentException if duration is negative
*/
public static int fromDuration(@NotNull Duration duration) {
2021-03-04 14:41:25 +01:00
return TickUtils.fromDuration(duration, MinecraftServer.TICK_MS);
}
/**
* Creates a number of ticks from a given duration.
2021-05-15 08:31:24 +02:00
*
* @param duration the duration
2021-03-04 14:41:25 +01:00
* @param msPerTick the number of milliseconds per tick
* @return the number of ticks
* @throws IllegalArgumentException if duration is negative
*/
public static int fromDuration(@NotNull Duration duration, int msPerTick) {
2021-05-15 08:31:24 +02:00
Check.argCondition(duration.isNegative(), "Duration cannot be negative");
2021-03-04 14:41:25 +01:00
return (int) (duration.toMillis() / msPerTick);
}
}