diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/GenericTitlePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/GenericTitlePacket.java index 8641173e74..719635e506 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/GenericTitlePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/GenericTitlePacket.java @@ -22,10 +22,12 @@ import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; import io.netty.buffer.ByteBuf; +import org.jetbrains.annotations.NotNull; public abstract class GenericTitlePacket implements MinecraftPacket { public enum ActionType { + SET_TITLE(0), SET_SUBTITLE(1), SET_ACTION_BAR(2), @@ -45,16 +47,7 @@ public int getAction(ProtocolVersion version) { } } - - private ActionType action; - - protected void setAction(ActionType action) { - this.action = action; - } - - public final ActionType getAction() { - return action; - } + public abstract @NotNull ActionType getAction(); public ComponentHolder getComponent() { throw new UnsupportedOperationException("Invalid function for this TitlePacket ActionType"); @@ -88,7 +81,6 @@ public void setFadeOut(int fadeOut) { throw new UnsupportedOperationException("Invalid function for this TitlePacket ActionType"); } - @Override public final void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { @@ -103,21 +95,17 @@ public final void decode(ByteBuf buf, ProtocolUtils.Direction direction, * @return GenericTitlePacket instance that follows the invoker type/version */ public static GenericTitlePacket constructTitlePacket(ActionType type, ProtocolVersion version) { - GenericTitlePacket packet = null; if (version.noLessThan(ProtocolVersion.MINECRAFT_1_17)) { - packet = switch (type) { + return switch (type) { case SET_ACTION_BAR -> new TitleActionbarPacket(); case SET_SUBTITLE -> new TitleSubtitlePacket(); case SET_TIMES -> new TitleTimesPacket(); case SET_TITLE -> new TitleTextPacket(); - case HIDE, RESET -> new TitleClearPacket(); + case HIDE, RESET -> new TitleClearPacket(type); default -> throw new IllegalArgumentException("Invalid ActionType"); }; } else { - packet = new LegacyTitlePacket(); + return new LegacyTitlePacket(type); } - packet.setAction(type); - return packet; } - } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/LegacyTitlePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/LegacyTitlePacket.java index ebae8a9e5b..f4af08dd96 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/LegacyTitlePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/LegacyTitlePacket.java @@ -22,27 +22,40 @@ import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; import io.netty.buffer.ByteBuf; +import java.util.Objects; import org.checkerframework.checker.nullness.qual.Nullable; +import org.jetbrains.annotations.NotNull; public class LegacyTitlePacket extends GenericTitlePacket { + private final ActionType action; + private @Nullable ComponentHolder component; private int fadeIn; private int stay; private int fadeOut; + public LegacyTitlePacket() { + // This constructor only exists to keep StateRegistry happy (all mappings are encode-only, the constructor isn't stored or used). + throw new AssertionError("A bare LegacyTitlePacket should never be instantiated"); + } + + public LegacyTitlePacket(ActionType action) { + this.action = Objects.requireNonNull(action, "action"); + } + @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { if (version.lessThan(ProtocolVersion.MINECRAFT_1_11) - && getAction() == ActionType.SET_ACTION_BAR) { + && this.action == ActionType.SET_ACTION_BAR) { throw new IllegalStateException("Action bars are only supported on 1.11 and newer"); } - ProtocolUtils.writeVarInt(buf, getAction().getAction(version)); + ProtocolUtils.writeVarInt(buf, this.action.getAction(version)); - switch (getAction()) { + switch (this.action) { case SET_TITLE, SET_SUBTITLE, SET_ACTION_BAR -> { if (component == null) { - throw new IllegalStateException("No component found for " + getAction()); + throw new IllegalStateException("No component found for " + this.action); } component.write(buf); } @@ -52,14 +65,13 @@ && getAction() == ActionType.SET_ACTION_BAR) { buf.writeInt(fadeOut); } case HIDE, RESET -> {} - default -> throw new UnsupportedOperationException("Unknown action " + getAction()); + default -> throw new UnsupportedOperationException("Unknown action " + this.action); } - } @Override - public void setAction(ActionType action) { - super.setAction(action); + public @NotNull ActionType getAction() { + return action; } @Override @@ -105,7 +117,7 @@ public void setFadeOut(int fadeOut) { @Override public String toString() { return "GenericTitlePacket{" - + "action=" + getAction() + + "action=" + action + ", component='" + component + '\'' + ", fadeIn=" + fadeIn + ", stay=" + stay diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleActionbarPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleActionbarPacket.java index f34983eaf9..b52c636490 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleActionbarPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleActionbarPacket.java @@ -22,13 +22,15 @@ import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; import io.netty.buffer.ByteBuf; +import org.jetbrains.annotations.NotNull; public class TitleActionbarPacket extends GenericTitlePacket { private ComponentHolder component; - public TitleActionbarPacket() { - setAction(ActionType.SET_TITLE); + @Override + public @NotNull ActionType getAction() { + return ActionType.SET_ACTION_BAR; } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleClearPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleClearPacket.java index 1b3489691f..cfaf8507e1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleClearPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleClearPacket.java @@ -21,30 +21,37 @@ import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; +import org.jetbrains.annotations.NotNull; public class TitleClearPacket extends GenericTitlePacket { + private final ActionType action; + public TitleClearPacket() { - setAction(ActionType.HIDE); + this.action = ActionType.HIDE; } - @Override - public void setAction(ActionType action) { + public TitleClearPacket(ActionType action) { if (action != ActionType.HIDE && action != ActionType.RESET) { - throw new IllegalArgumentException("TitleClearPacket only accepts CLEAR and RESET actions"); + throw new IllegalArgumentException("TitleClearPacket only accepts the CLEAR and RESET actions."); } - super.setAction(action); + this.action = action; + } + + @Override + public @NotNull ActionType getAction() { + return action; } @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - buf.writeBoolean(getAction() == ActionType.RESET); + buf.writeBoolean(this.action == ActionType.RESET); } @Override public String toString() { return "TitleClearPacket{" - + ", resetTimes=" + (getAction() == ActionType.RESET) + + ", resetTimes=" + (this.action == ActionType.RESET) + '}'; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleSubtitlePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleSubtitlePacket.java index 0f375ae20a..a17d539944 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleSubtitlePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleSubtitlePacket.java @@ -22,13 +22,15 @@ import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; import io.netty.buffer.ByteBuf; +import org.jetbrains.annotations.NotNull; public class TitleSubtitlePacket extends GenericTitlePacket { private ComponentHolder component; - public TitleSubtitlePacket() { - setAction(ActionType.SET_SUBTITLE); + @Override + public @NotNull ActionType getAction() { + return ActionType.SET_SUBTITLE; } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleTextPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleTextPacket.java index ae75f5d618..ceab8290e4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleTextPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleTextPacket.java @@ -22,13 +22,15 @@ import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; import io.netty.buffer.ByteBuf; +import org.jetbrains.annotations.NotNull; public class TitleTextPacket extends GenericTitlePacket { private ComponentHolder component; - public TitleTextPacket() { - setAction(ActionType.SET_TITLE); + @Override + public @NotNull ActionType getAction() { + return ActionType.SET_TITLE; } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleTimesPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleTimesPacket.java index 8764a12fab..219729918a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleTimesPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/TitleTimesPacket.java @@ -21,6 +21,7 @@ import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; +import org.jetbrains.annotations.NotNull; public class TitleTimesPacket extends GenericTitlePacket { @@ -28,8 +29,9 @@ public class TitleTimesPacket extends GenericTitlePacket { private int stay; private int fadeOut; - public TitleTimesPacket() { - setAction(ActionType.SET_TIMES); + @Override + public @NotNull ActionType getAction() { + return ActionType.SET_TIMES; } @Override