From 3dc3c40c336113c00d9c9e2010cf848f2176d8e7 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 23 Apr 2021 19:01:40 -0400 Subject: [PATCH] Convert some more packets to use PacketReader/PacketWriter --- .../ClientboundDisconnectPacket.java | 29 ++++--------------- .../ClientboundHeaderAndFooterPacket.java | 14 ++++----- .../ClientboundResourcePackRequestPacket.java | 11 +++---- .../ClientboundServerLoginSuccessPacket.java | 23 +++++++-------- .../ServerboundStatusRequestPacket.java | 2 -- 5 files changed, 26 insertions(+), 53 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundDisconnectPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundDisconnectPacket.java index 12472db77..a4a518dab 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundDisconnectPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundDisconnectPacket.java @@ -31,13 +31,13 @@ import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundDisconnectPacket implements Packet { - public static final PacketReader DECODER = PacketReader.method(ClientboundDisconnectPacket::new); - public static final PacketWriter ENCODER = PacketWriter.deprecatedEncode(); + public static final PacketReader DECODER = (buf, version) -> + new ClientboundDisconnectPacket(ProtocolUtils.readString(buf)); + public static final PacketWriter ENCODER = (out, packet, version) -> { + ProtocolUtils.writeString(out, packet.reason); + }; - private @Nullable String reason; - - public ClientboundDisconnectPacket() { - } + private final String reason; public ClientboundDisconnectPacket(String reason) { this.reason = Preconditions.checkNotNull(reason, "reason"); @@ -50,23 +50,6 @@ public class ClientboundDisconnectPacket implements Packet { return reason; } - public void setReason(@Nullable String reason) { - this.reason = reason; - } - - @Override - public void decode(ByteBuf buf, PacketDirection direction, ProtocolVersion version) { - reason = ProtocolUtils.readString(buf); - } - - @Override - public void encode(ByteBuf buf, ProtocolVersion version) { - if (reason == null) { - throw new IllegalStateException("No reason specified."); - } - ProtocolUtils.writeString(buf, reason); - } - @Override public boolean handle(PacketHandler handler) { return handler.handle(this); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundHeaderAndFooterPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundHeaderAndFooterPacket.java index 1bfa98756..2f53d8d62 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundHeaderAndFooterPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundHeaderAndFooterPacket.java @@ -22,6 +22,7 @@ import static com.velocitypowered.proxy.network.ProtocolUtils.writeString; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.velocitypowered.api.network.ProtocolVersion; +import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketHandler; import com.velocitypowered.proxy.network.packet.PacketReader; @@ -29,8 +30,11 @@ import com.velocitypowered.proxy.network.packet.PacketWriter; import io.netty.buffer.ByteBuf; public class ClientboundHeaderAndFooterPacket implements Packet { - public static final PacketReader DECODER = PacketReader.method(ClientboundHeaderAndFooterPacket::new); - public static final PacketWriter ENCODER = PacketWriter.deprecatedEncode(); + public static final PacketReader DECODER = PacketReader.unsupported(); + public static final PacketWriter ENCODER = (out, packet, version) -> { + writeString(out, packet.header); + writeString(out, packet.footer); + }; private static final String EMPTY_COMPONENT = "{\"translate\":\"\"}"; private static final ClientboundHeaderAndFooterPacket RESET @@ -48,12 +52,6 @@ public class ClientboundHeaderAndFooterPacket implements Packet { this.footer = Preconditions.checkNotNull(footer, "footer"); } - @Override - public void encode(ByteBuf buf, ProtocolVersion version) { - writeString(buf, header); - writeString(buf, footer); - } - @Override public boolean handle(PacketHandler handler) { return handler.handle(this); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundResourcePackRequestPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundResourcePackRequestPacket.java index 599bfa1ae..6d2e055b5 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundResourcePackRequestPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundResourcePackRequestPacket.java @@ -33,7 +33,10 @@ public class ClientboundResourcePackRequestPacket implements Packet { final String hash = ProtocolUtils.readString(buf); return new ClientboundResourcePackRequestPacket(url, hash); }; - public static final PacketWriter ENCODER = PacketWriter.deprecatedEncode(); + public static final PacketWriter ENCODER = (out, packet, version) -> { + ProtocolUtils.writeString(out, packet.url); + ProtocolUtils.writeString(out, packet.hash); + }; private final String url; private final String hash; @@ -43,12 +46,6 @@ public class ClientboundResourcePackRequestPacket implements Packet { this.hash = Objects.requireNonNull(hash, "hash"); } - @Override - public void encode(ByteBuf buf, ProtocolVersion protocolVersion) { - ProtocolUtils.writeString(buf, url); - ProtocolUtils.writeString(buf, hash); - } - @Override public boolean handle(PacketHandler handler) { return handler.handle(this); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundServerLoginSuccessPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundServerLoginSuccessPacket.java index 54ca5d114..40055510a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundServerLoginSuccessPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundServerLoginSuccessPacket.java @@ -42,7 +42,16 @@ public class ClientboundServerLoginSuccessPacket implements Packet { final String username = ProtocolUtils.readString(buf, 16); return new ClientboundServerLoginSuccessPacket(uuid, username); }; - public static final PacketWriter ENCODER = PacketWriter.deprecatedEncode(); + public static final PacketWriter ENCODER = (out, packet, version) -> { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { + ProtocolUtils.writeUuidIntArray(out, packet.uuid); + } else if (version.gte(ProtocolVersion.MINECRAFT_1_7_6)) { + ProtocolUtils.writeString(out, packet.uuid.toString()); + } else { + ProtocolUtils.writeString(out, UuidUtils.toUndashed(packet.uuid)); + } + ProtocolUtils.writeString(out, packet.username); + }; private final UUID uuid; private final String username; @@ -52,18 +61,6 @@ public class ClientboundServerLoginSuccessPacket implements Packet { this.username = Objects.requireNonNull(username, "username"); } - @Override - public void encode(ByteBuf buf, ProtocolVersion version) { - if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { - ProtocolUtils.writeUuidIntArray(buf, uuid); - } else if (version.gte(ProtocolVersion.MINECRAFT_1_7_6)) { - ProtocolUtils.writeString(buf, uuid.toString()); - } else { - ProtocolUtils.writeString(buf, UuidUtils.toUndashed(uuid)); - } - ProtocolUtils.writeString(buf, username); - } - @Override public boolean handle(PacketHandler handler) { return handler.handle(this); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusRequestPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusRequestPacket.java index 34562d9ab..8db8ab693 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusRequestPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusRequestPacket.java @@ -17,12 +17,10 @@ package com.velocitypowered.proxy.network.packet.serverbound; -import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketHandler; import com.velocitypowered.proxy.network.packet.PacketReader; import com.velocitypowered.proxy.network.packet.PacketWriter; -import io.netty.buffer.ByteBuf; public class ServerboundStatusRequestPacket implements Packet { public static final ServerboundStatusRequestPacket INSTANCE = new ServerboundStatusRequestPacket();