From f2897eedcaad763d05f5c69c51389b596daa7c9b Mon Sep 17 00:00:00 2001 From: Kieran Wallbanks Date: Thu, 4 Mar 2021 13:41:25 +0000 Subject: [PATCH] fix title ticks --- .../packet/server/play/TitlePacket.java | 5 +++-- .../net/minestom/server/utils/TickUtils.java | 22 ++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java index 98b2f1961..262423b8b 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java @@ -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; diff --git a/src/main/java/net/minestom/server/utils/TickUtils.java b/src/main/java/net/minestom/server/utils/TickUtils.java index 6b6bfcc89..aa5c98500 100644 --- a/src/main/java/net/minestom/server/utils/TickUtils.java +++ b/src/main/java/net/minestom/server/utils/TickUtils.java @@ -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); } }