Convert some more packets to use PacketReader/PacketWriter

This commit is contained in:
Andrew Steinborn
2021-04-23 19:01:40 -04:00
parent e416b9fc45
commit 3dc3c40c33
5 changed files with 26 additions and 53 deletions

View File

@@ -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<ClientboundDisconnectPacket> DECODER = PacketReader.method(ClientboundDisconnectPacket::new);
public static final PacketWriter<ClientboundDisconnectPacket> ENCODER = PacketWriter.deprecatedEncode();
public static final PacketReader<ClientboundDisconnectPacket> DECODER = (buf, version) ->
new ClientboundDisconnectPacket(ProtocolUtils.readString(buf));
public static final PacketWriter<ClientboundDisconnectPacket> 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);

View File

@@ -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<ClientboundHeaderAndFooterPacket> DECODER = PacketReader.method(ClientboundHeaderAndFooterPacket::new);
public static final PacketWriter<ClientboundHeaderAndFooterPacket> ENCODER = PacketWriter.deprecatedEncode();
public static final PacketReader<ClientboundHeaderAndFooterPacket> DECODER = PacketReader.unsupported();
public static final PacketWriter<ClientboundHeaderAndFooterPacket> 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);

View File

@@ -33,7 +33,10 @@ public class ClientboundResourcePackRequestPacket implements Packet {
final String hash = ProtocolUtils.readString(buf);
return new ClientboundResourcePackRequestPacket(url, hash);
};
public static final PacketWriter<ClientboundResourcePackRequestPacket> ENCODER = PacketWriter.deprecatedEncode();
public static final PacketWriter<ClientboundResourcePackRequestPacket> 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);

View File

@@ -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<ClientboundServerLoginSuccessPacket> ENCODER = PacketWriter.deprecatedEncode();
public static final PacketWriter<ClientboundServerLoginSuccessPacket> 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);

View File

@@ -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();