From 3f647e4eb2097230a6d4f0bb1daabd3bea9659cd Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 15 Oct 2021 08:28:05 -0400 Subject: [PATCH] Implement sendTitlePart for Players --- .../connection/client/ConnectedPlayer.java | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index e975afd73..de09c1fde 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -95,6 +95,8 @@ import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer; +import net.kyori.adventure.title.Title.Times; +import net.kyori.adventure.title.TitlePart; import net.kyori.adventure.translation.GlobalTranslator; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -369,7 +371,6 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { GsonComponentSerializer serializer = ProtocolUtils.getJsonChatSerializer(this .getProtocolVersion()); - GenericTitlePacket timesPkt = GenericTitlePacket.constructTitlePacket( GenericTitlePacket.ActionType.SET_TIMES, this.getProtocolVersion()); net.kyori.adventure.title.Title.Times times = title.times(); @@ -394,6 +395,41 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { } } + @Override + public void sendTitlePart(@NotNull TitlePart part, @NotNull T value) { + if (part == null) { + throw new NullPointerException("part"); + } + if (value == null) { + throw new NullPointerException("value"); + } + + GsonComponentSerializer serializer = ProtocolUtils.getJsonChatSerializer(this + .getProtocolVersion()); + + if (part == TitlePart.TITLE) { + GenericTitlePacket titlePkt = GenericTitlePacket.constructTitlePacket( + GenericTitlePacket.ActionType.SET_TITLE, this.getProtocolVersion()); + titlePkt.setComponent(serializer.serialize(translateMessage((Component) value))); + connection.write(titlePkt); + } else if (part == TitlePart.SUBTITLE) { + GenericTitlePacket titlePkt = GenericTitlePacket.constructTitlePacket( + GenericTitlePacket.ActionType.SET_TITLE, this.getProtocolVersion()); + titlePkt.setComponent(serializer.serialize(translateMessage((Component) value))); + connection.write(titlePkt); + } else if (part == TitlePart.TIMES) { + Times times = (Times) value; + GenericTitlePacket timesPkt = GenericTitlePacket.constructTitlePacket( + GenericTitlePacket.ActionType.SET_TIMES, this.getProtocolVersion()); + timesPkt.setFadeIn((int) DurationUtils.toTicks(times.fadeIn())); + timesPkt.setStay((int) DurationUtils.toTicks(times.stay())); + timesPkt.setFadeOut((int) DurationUtils.toTicks(times.fadeOut())); + connection.write(timesPkt); + } else { + throw new IllegalArgumentException("Title part " + part + " is not valid"); + } + } + @Override public void clearTitle() { if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) {