fix title ticks

This commit is contained in:
Kieran Wallbanks 2021-03-04 13:41:25 +00:00
parent 87b30a8361
commit f2897eedca
2 changed files with 24 additions and 3 deletions

View File

@ -115,8 +115,9 @@ public class TitlePacket implements ServerPacket {
// times packet
Title.Times times = title.times();
if (times != null) {
packets.add(new TitlePacket(TickUtils.fromDuration(times.fadeIn()), TickUtils.fromDuration(times.stay()),
TickUtils.fromDuration(times.fadeOut())));
packets.add(new TitlePacket(TickUtils.fromDuration(times.fadeIn(), TickUtils.CLIENT_TICK_MS),
TickUtils.fromDuration(times.stay(), TickUtils.CLIENT_TICK_MS),
TickUtils.fromDuration(times.fadeOut(), TickUtils.CLIENT_TICK_MS)));
}
return packets;

View File

@ -10,6 +10,15 @@ import java.time.Duration;
* Tick related utilities.
*/
public class TickUtils {
/**
* 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}.
@ -18,8 +27,19 @@ public class TickUtils {
* @throws IllegalArgumentException if duration is negative
*/
public static int fromDuration(@NotNull Duration duration) {
return TickUtils.fromDuration(duration, MinecraftServer.TICK_MS);
}
/**
* Creates a number of ticks from a given duration.
* @param duration the duration
* @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) {
Validate.isTrue(!duration.isNegative(), "Duration cannot be negative");
return (int) (duration.toMillis() / MinecraftServer.TICK_MS);
return (int) (duration.toMillis() / msPerTick);
}
}