2022-07-20 13:18:57 +02:00
|
|
|
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;
|
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
2022-07-10 00:55:56 +02:00
|
|
|
* @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
|
|
|
}
|