PandaLib/pandalib-util/src/main/java/fr/pandacube/lib/util/Tick.java

60 lines
1.4 KiB
Java
Raw Normal View History

package fr.pandacube.lib.util;
2021-03-06 16:51:53 +01:00
2023-12-28 19:15:47 +01:00
import java.time.Duration;
/**
* Provides methods related to Minecraft Java server ticks.
*/
2021-03-06 16:51:53 +01:00
public class Tick {
2023-10-28 23:16:54 +02:00
2023-10-28 23:46:47 +02:00
/**
* The number of Minecraft server ticks in a second.
*/
2023-10-28 23:16:54 +02:00
public static final int TPS = 20;
2023-10-28 23:46:47 +02:00
/**
* The duration of a Minecraft server tick in millisecond.
*/
2023-10-28 23:16:54 +02:00
public static final int MS_PER_TICK = 1000 / TPS;
2021-03-06 16:51:53 +01:00
/**
2023-10-28 23:16:54 +02:00
* Returns the number of ticks for the provided number of seconds.
* @param seconds the duration in seconds
2021-03-06 16:51:53 +01:00
* @return the same duration as provided, but in Minecraft server ticks
*/
2023-10-28 23:16:54 +02:00
public static long ofSec(long seconds) {
return seconds * TPS;
2021-03-06 16:51:53 +01:00
}
/**
2023-10-28 23:16:54 +02:00
* Returns the number of ticks for the provided number of minutes.
* @param minutes the duration in minutes
2021-03-06 16:51:53 +01:00
* @return the same duration as provided, but in Minecraft server ticks
*/
2023-10-28 23:16:54 +02:00
public static long ofMin(long minutes) {
return minutes * TPS * 60;
}
/**
* Returns the number of milliseconds for the provided number of ticks.
* @param tick the duration in ticks
* @return the same duration as provided, but in milliseconds
*/
public static long toMs(long tick) {
return tick * MS_PER_TICK;
2021-03-06 16:51:53 +01:00
}
2023-12-28 19:15:47 +01:00
/**
* Returns the {@link Duration} for the provided number of ticks.
* @param tick the duration in ticks
* @return the {@link Duration} for the provided number of ticks.
*/
public static Duration toDuration(long tick) {
return Duration.ofMillis(toMs(tick));
}
2021-03-06 16:51:53 +01:00
}