23w31a part 2 electric boogaloo

Fixed tests, updated the login cycle and some misc touch-ups
This commit is contained in:
basaigh 2023-08-13 15:17:10 +01:00
parent d49ac5dd96
commit 48c189f651
14 changed files with 100 additions and 92 deletions

View file

@ -46,6 +46,7 @@ import java.net.Proxy;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.BitSet;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
public class MinecraftProtocolTest { public class MinecraftProtocolTest {
@ -69,7 +70,7 @@ public class MinecraftProtocolTest {
server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, (ServerInfoBuilder) session -> server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, (ServerInfoBuilder) session ->
new ServerStatusInfo( new ServerStatusInfo(
new VersionInfo(MinecraftCodec.CODEC.getMinecraftVersion(), MinecraftCodec.CODEC.getProtocolVersion()), new VersionInfo(MinecraftCodec.CODEC.getMinecraftVersion(), MinecraftCodec.CODEC.getProtocolVersion()),
new PlayerInfo(100, 0, new GameProfile[0]), new PlayerInfo(100, 0, new ArrayList<>()),
Component.text("Hello world!"), Component.text("Hello world!"),
null, null,
false false
@ -80,22 +81,21 @@ public class MinecraftProtocolTest {
session.send(new ClientboundLoginPacket( session.send(new ClientboundLoginPacket(
0, 0,
false, false,
GameMode.SURVIVAL,
GameMode.SURVIVAL,
1,
new String[]{"minecraft:world"}, new String[]{"minecraft:world"},
loadNetworkCodec(),
"minecraft:overworld",
"minecraft:world",
100,
0, 0,
16, 16,
16, 16,
false, false,
false, false,
"minecraft:overworld",
"minecraft:world",
100,
GameMode.SURVIVAL,
GameMode.SURVIVAL,
false, false,
false, false,
null null,
100
)) ))
); );
@ -158,7 +158,7 @@ public class MinecraftProtocolTest {
+ ", " + info.getVersionInfo().getProtocolVersion()); + ", " + info.getVersionInfo().getProtocolVersion());
System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers()
+ " / " + info.getPlayerInfo().getMaxPlayers()); + " / " + info.getPlayerInfo().getMaxPlayers());
System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers())); System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers().toArray()));
System.out.println("Description: " + info.getDescription()); System.out.println("Description: " + info.getDescription());
System.out.println("Icon: " + info.getIconPng()); System.out.println("Icon: " + info.getIconPng());
}); });
@ -205,7 +205,7 @@ public class MinecraftProtocolTest {
@Override @Override
public void packetReceived(Session session, Packet packet) { public void packetReceived(Session session, Packet packet) {
if (packet instanceof ClientboundLoginPacket) { if (packet instanceof ClientboundLoginPacket) {
session.send(new ServerboundChatPacket("Hello, this is a test of MCProtocolLib.", Instant.now().toEpochMilli(), 0, new byte[0], false, new ArrayList<>(), null)); session.send(new ServerboundChatPacket("Hello, this is a test of MCProtocolLib.", Instant.now().toEpochMilli(), 0L, null, 0, new BitSet()));
} else if (packet instanceof ClientboundSystemChatPacket) { } else if (packet instanceof ClientboundSystemChatPacket) {
Component message = ((ClientboundSystemChatPacket) packet).getContent(); Component message = ((ClientboundSystemChatPacket) packet).getContent();
System.out.println("Received Message: " + message); System.out.println("Received Message: " + message);
@ -224,14 +224,4 @@ public class MinecraftProtocolTest {
client.connect(); client.connect();
} }
private static CompoundTag loadNetworkCodec() {
try (InputStream inputStream = MinecraftProtocolTest.class.getClassLoader().getResourceAsStream("network_codec.nbt");
DataInputStream stream = new DataInputStream(new GZIPInputStream(inputStream))) {
return (CompoundTag) NBTIO.readTag((DataInput) stream);
} catch (IOException e) {
e.printStackTrace();
throw new AssertionError("Unable to load network codec.");
}
}
} }

View file

@ -5,7 +5,7 @@
<groupId>com.github.steveice10</groupId> <groupId>com.github.steveice10</groupId>
<artifactId>mcprotocollib</artifactId> <artifactId>mcprotocollib</artifactId>
<version>1.20.2-SNAPSHOT</version> <version>1.20.2-1-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>MCProtocolLib</name> <name>MCProtocolLib</name>
@ -82,9 +82,9 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.github.GeyserMC</groupId> <groupId>com.github.steveice10</groupId> <!-- Local repository groupId, will need to be reverted -->
<artifactId>opennbt</artifactId> <artifactId>opennbt</artifactId>
<version>1.4</version> <version>1.6-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>

View file

@ -12,6 +12,7 @@ import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoHandler; import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoHandler;
import com.github.steveice10.mc.protocol.data.status.handler.ServerPingTimeHandler; import com.github.steveice10.mc.protocol.data.status.handler.ServerPingTimeHandler;
import com.github.steveice10.mc.protocol.packet.configuration.clientbound.ClientboundFinishConfigurationPacket; import com.github.steveice10.mc.protocol.packet.configuration.clientbound.ClientboundFinishConfigurationPacket;
import com.github.steveice10.mc.protocol.packet.configuration.serverbound.ServerboundFinishConfigurationPacket;
import com.github.steveice10.mc.protocol.packet.handshake.serverbound.ClientIntentionPacket; import com.github.steveice10.mc.protocol.packet.handshake.serverbound.ClientIntentionPacket;
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundDisconnectPacket; import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundDisconnectPacket;
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundKeepAlivePacket; import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundKeepAlivePacket;
@ -23,6 +24,7 @@ import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLog
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLoginDisconnectPacket; import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLoginDisconnectPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundHelloPacket; import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundHelloPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundKeyPacket; import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundKeyPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundLoginAcknowledgedPacket;
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundPongResponsePacket; import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundPongResponsePacket;
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundStatusResponsePacket; import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundStatusResponsePacket;
import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundPingRequestPacket; import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundPingRequestPacket;
@ -33,6 +35,7 @@ import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.packet.Packet;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import lombok.SneakyThrows;
import javax.crypto.KeyGenerator; import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
@ -45,6 +48,7 @@ import java.security.NoSuchAlgorithmException;
public class ClientListener extends SessionAdapter { public class ClientListener extends SessionAdapter {
private final @NonNull ProtocolState targetState; private final @NonNull ProtocolState targetState;
@SneakyThrows
@Override @Override
public void packetReceived(Session session, Packet packet) { public void packetReceived(Session session, Packet packet) {
MinecraftProtocol protocol = (MinecraftProtocol) session.getPacketProtocol(); MinecraftProtocol protocol = (MinecraftProtocol) session.getPacketProtocol();
@ -85,6 +89,7 @@ public class ClientListener extends SessionAdapter {
session.send(new ServerboundKeyPacket(helloPacket.getPublicKey(), key, helloPacket.getChallenge())); session.send(new ServerboundKeyPacket(helloPacket.getPublicKey(), key, helloPacket.getChallenge()));
session.enableEncryption(protocol.enableEncryption(key)); session.enableEncryption(protocol.enableEncryption(key));
} else if (packet instanceof ClientboundGameProfilePacket) { } else if (packet instanceof ClientboundGameProfilePacket) {
session.send(new ServerboundLoginAcknowledgedPacket());
protocol.setState(ProtocolState.CONFIGURATION); protocol.setState(ProtocolState.CONFIGURATION);
} else if (packet instanceof ClientboundLoginDisconnectPacket) { } else if (packet instanceof ClientboundLoginDisconnectPacket) {
session.disconnect(((ClientboundLoginDisconnectPacket) packet).getReason()); session.disconnect(((ClientboundLoginDisconnectPacket) packet).getReason());
@ -119,7 +124,7 @@ public class ClientListener extends SessionAdapter {
} }
} else if (protocol.getState() == ProtocolState.CONFIGURATION) { } else if (protocol.getState() == ProtocolState.CONFIGURATION) {
if (packet instanceof ClientboundFinishConfigurationPacket) { if (packet instanceof ClientboundFinishConfigurationPacket) {
protocol.setState(ProtocolState.GAME); session.send(new ServerboundFinishConfigurationPacket());
} }
} }
} }
@ -137,6 +142,8 @@ public class ClientListener extends SessionAdapter {
} else { } else {
session.send(new ServerboundStatusRequestPacket()); session.send(new ServerboundStatusRequestPacket());
} }
} else if (packet instanceof ServerboundFinishConfigurationPacket) {
((MinecraftProtocol) session.getPacketProtocol()).setState(ProtocolState.GAME);
} }
} }

View file

@ -85,7 +85,7 @@ public class MinecraftProtocol extends PacketProtocol {
* @param username Username to use. * @param username Username to use.
*/ */
public MinecraftProtocol(@NonNull String username) { public MinecraftProtocol(@NonNull String username) {
this(new GameProfile((UUID) null, username), null); this(new GameProfile(UUID.randomUUID(), username), null);
} }
/** /**
@ -95,7 +95,7 @@ public class MinecraftProtocol extends PacketProtocol {
* @param username Username to use. * @param username Username to use.
*/ */
public MinecraftProtocol(@NonNull PacketCodec codec, @NonNull String username) { public MinecraftProtocol(@NonNull PacketCodec codec, @NonNull String username) {
this(codec, new GameProfile((UUID) null, username), null); this(codec, new GameProfile(UUID.randomUUID(), username), null);
} }
/** /**

View file

@ -9,6 +9,7 @@ import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
import com.github.steveice10.mc.protocol.data.status.VersionInfo; import com.github.steveice10.mc.protocol.data.status.VersionInfo;
import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoBuilder; import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoBuilder;
import com.github.steveice10.mc.protocol.packet.configuration.clientbound.ClientboundFinishConfigurationPacket; import com.github.steveice10.mc.protocol.packet.configuration.clientbound.ClientboundFinishConfigurationPacket;
import com.github.steveice10.mc.protocol.packet.configuration.clientbound.ClientboundRegistryDataPacket;
import com.github.steveice10.mc.protocol.packet.configuration.serverbound.ServerboundFinishConfigurationPacket; import com.github.steveice10.mc.protocol.packet.configuration.serverbound.ServerboundFinishConfigurationPacket;
import com.github.steveice10.mc.protocol.packet.handshake.serverbound.ClientIntentionPacket; import com.github.steveice10.mc.protocol.packet.handshake.serverbound.ClientIntentionPacket;
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundDisconnectPacket; import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundDisconnectPacket;
@ -21,10 +22,13 @@ import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLog
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLoginDisconnectPacket; import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLoginDisconnectPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundHelloPacket; import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundHelloPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundKeyPacket; import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundKeyPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundLoginAcknowledgedPacket;
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundPongResponsePacket; import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundPongResponsePacket;
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundStatusResponsePacket; import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundStatusResponsePacket;
import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundPingRequestPacket; import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundPingRequestPacket;
import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundStatusRequestPacket; import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundStatusRequestPacket;
import com.github.steveice10.opennbt.NBTIO;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.ConnectedEvent; import com.github.steveice10.packetlib.event.session.ConnectedEvent;
import com.github.steveice10.packetlib.event.session.DisconnectingEvent; import com.github.steveice10.packetlib.event.session.DisconnectingEvent;
@ -33,6 +37,10 @@ import com.github.steveice10.packetlib.packet.Packet;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyPair; import java.security.KeyPair;
import java.security.KeyPairGenerator; import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -41,6 +49,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.util.Random;
import java.util.UUID; import java.util.UUID;
import java.util.zip.GZIPInputStream;
/** /**
* Handles initial login and status requests for servers. * Handles initial login and status requests for servers.
@ -121,6 +130,10 @@ public class ServerListener extends SessionAdapter {
SecretKey key = keyPacket.getSecretKey(privateKey); SecretKey key = keyPacket.getSecretKey(privateKey);
session.enableEncryption(protocol.enableEncryption(key)); session.enableEncryption(protocol.enableEncryption(key));
new Thread(new UserAuthTask(session, key)).start(); new Thread(new UserAuthTask(session, key)).start();
} else if (packet instanceof ServerboundLoginAcknowledgedPacket) {
((MinecraftProtocol) session.getPacketProtocol()).setState(ProtocolState.CONFIGURATION);
session.send(new ClientboundRegistryDataPacket(loadNetworkCodec()));
session.send(new ClientboundFinishConfigurationPacket());
} }
} else if (protocol.getState() == ProtocolState.STATUS) { } else if (protocol.getState() == ProtocolState.STATUS) {
if (packet instanceof ServerboundStatusRequestPacket) { if (packet instanceof ServerboundStatusRequestPacket) {
@ -152,6 +165,14 @@ public class ServerListener extends SessionAdapter {
} else if (protocol.getState() == ProtocolState.CONFIGURATION) { } else if (protocol.getState() == ProtocolState.CONFIGURATION) {
if (packet instanceof ServerboundFinishConfigurationPacket) { if (packet instanceof ServerboundFinishConfigurationPacket) {
protocol.setState(ProtocolState.GAME); protocol.setState(ProtocolState.GAME);
ServerLoginHandler handler = session.getFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY);
if (handler != null) {
handler.loggedIn(session);
}
if (session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) {
new Thread(new KeepAliveTask(session)).start();
}
} }
} }
} }
@ -161,16 +182,6 @@ public class ServerListener extends SessionAdapter {
if (packet instanceof ClientboundLoginCompressionPacket) { if (packet instanceof ClientboundLoginCompressionPacket) {
session.setCompressionThreshold(((ClientboundLoginCompressionPacket) packet).getThreshold(), true); session.setCompressionThreshold(((ClientboundLoginCompressionPacket) packet).getThreshold(), true);
session.send(new ClientboundGameProfilePacket((GameProfile) session.getFlag(MinecraftConstants.PROFILE_KEY))); session.send(new ClientboundGameProfilePacket((GameProfile) session.getFlag(MinecraftConstants.PROFILE_KEY)));
} else if (packet instanceof ClientboundGameProfilePacket) {
((MinecraftProtocol) session.getPacketProtocol()).setState(ProtocolState.GAME);
ServerLoginHandler handler = session.getFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY);
if (handler != null) {
handler.loggedIn(session);
}
if (session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) {
new Thread(new KeepAliveTask(session)).start();
}
} }
} }
@ -241,4 +252,14 @@ public class ServerListener extends SessionAdapter {
} }
} }
} }
public static CompoundTag loadNetworkCodec() {
try (InputStream inputStream = ServerListener.class.getClassLoader().getResourceAsStream("networkCodec.nbt");
DataInputStream stream = new DataInputStream(new GZIPInputStream(inputStream))) {
return (CompoundTag) NBTIO.readTag((DataInput) stream);
} catch (IOException e) {
e.printStackTrace();
throw new AssertionError("Unable to load network codec.");
}
}
} }

View file

@ -203,6 +203,30 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper {
return expected.cast(tag); return expected.cast(tag);
} }
public CompoundTag readAnyTag(ByteBuf buf) throws IOException {
return readAnyTag(buf, CompoundTag.class);
}
@Nullable
public <T extends Tag> T readAnyTag(ByteBuf buf, Class<T> expected) throws IOException {
Tag tag = NBTIO.readAnyTag(new InputStream() {
@Override
public int read() {
return buf.readUnsignedByte();
}
});
if (tag == null) {
return null;
}
if (tag.getClass() != expected) {
throw new IllegalArgumentException("Expected tag of type " + expected.getName() + " but got " + tag.getClass().getName());
}
return expected.cast(tag);
}
public CompoundTag readTagLE(ByteBuf buf) throws IOException { public CompoundTag readTagLE(ByteBuf buf) throws IOException {
return readTagLE(buf, CompoundTag.class); return readTagLE(buf, CompoundTag.class);
} }
@ -236,6 +260,15 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper {
}, tag); }, tag);
} }
public <T extends Tag> void writeAnyTag(ByteBuf buf, T tag) throws IOException {
NBTIO.writeAnyTag(new OutputStream() {
@Override
public void write(int b) throws IOException {
buf.writeByte(b);
}
}, tag);
}
public <T extends Tag> void writeTagLE(ByteBuf buf, T tag) throws IOException { public <T extends Tag> void writeTagLE(ByteBuf buf, T tag) throws IOException {
NBTIO.writeTag(new OutputStream() { NBTIO.writeTag(new OutputStream() {
@Override @Override

View file

@ -17,11 +17,11 @@ public class ClientboundRegistryDataPacket implements MinecraftPacket {
private final CompoundTag registry; private final CompoundTag registry;
public ClientboundRegistryDataPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { public ClientboundRegistryDataPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException {
this.registry = helper.readTag(in); this.registry = helper.readAnyTag(in);
} }
@Override @Override
public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException {
helper.writeTag(out, this.registry); helper.writeAnyTag(out, this.registry);
} }
} }

View file

@ -21,7 +21,6 @@ public class ClientboundLoginPacket implements MinecraftPacket {
private final int entityId; private final int entityId;
private final boolean hardcore; private final boolean hardcore;
private final @NonNull String[] worldNames; private final @NonNull String[] worldNames;
private final @NonNull CompoundTag registry;
private final int maxPlayers; private final int maxPlayers;
private final int viewDistance; private final int viewDistance;
private final int simulationDistance; private final int simulationDistance;
@ -45,7 +44,6 @@ public class ClientboundLoginPacket implements MinecraftPacket {
for (int i = 0; i < worldCount; i++) { for (int i = 0; i < worldCount; i++) {
this.worldNames[i] = helper.readString(in); this.worldNames[i] = helper.readString(in);
} }
this.registry = helper.readTag(in);
this.maxPlayers = helper.readVarInt(in); this.maxPlayers = helper.readVarInt(in);
this.viewDistance = helper.readVarInt(in); this.viewDistance = helper.readVarInt(in);
this.simulationDistance = helper.readVarInt(in); this.simulationDistance = helper.readVarInt(in);
@ -74,7 +72,6 @@ public class ClientboundLoginPacket implements MinecraftPacket {
for (String worldName : this.worldNames) { for (String worldName : this.worldNames) {
helper.writeString(out, worldName); helper.writeString(out, worldName);
} }
helper.writeTag(out, this.registry);
helper.writeVarInt(out, this.maxPlayers); helper.writeVarInt(out, this.maxPlayers);
helper.writeVarInt(out, this.viewDistance); helper.writeVarInt(out, this.viewDistance);
helper.writeVarInt(out, this.simulationDistance); helper.writeVarInt(out, this.simulationDistance);

View file

@ -15,7 +15,7 @@ public class ServerboundCustomQueryAnswerPacket implements MinecraftPacket {
private final byte[] data; private final byte[] data;
public ServerboundCustomQueryAnswerPacket(int transactionId) { public ServerboundCustomQueryAnswerPacket(int transactionId) {
this(transactionId, null); this(transactionId, new byte[0]);
} }
public ServerboundCustomQueryAnswerPacket(ByteBuf in, MinecraftCodecHelper helper) { public ServerboundCustomQueryAnswerPacket(ByteBuf in, MinecraftCodecHelper helper) {

View file

@ -2,6 +2,7 @@ package com.github.steveice10.packetlib.tcp;
import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.Session;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec; import io.netty.handler.codec.ByteToMessageCodec;
import io.netty.handler.codec.DecoderException; import io.netty.handler.codec.DecoderException;
@ -73,45 +74,14 @@ public class TcpPacketCompression extends ByteToMessageCodec<ByteBuf> {
} }
} }
this.setupInflaterInput(buf); byte[] bytes = new byte[buf.readableBytes()];
ByteBuf inflated = this.inflate(ctx, size); buf.readBytes(bytes);
this.inflater.setInput(bytes);
byte[] inflated = new byte[size];
this.inflater.inflate(inflated);
out.add(Unpooled.wrappedBuffer(inflated));
this.inflater.reset(); this.inflater.reset();
out.add(inflated);
} }
} }
} }
private void setupInflaterInput(ByteBuf buf) {
ByteBuffer inputBuffer;
if (buf.nioBufferCount() > 0) {
inputBuffer = buf.nioBuffer();
buf.skipBytes(buf.readableBytes());
} else {
inputBuffer = ByteBuffer.allocateDirect(buf.readableBytes());
buf.readBytes(inputBuffer);
inputBuffer.flip();
}
this.inflater.setInput(inputBuffer.array());
}
private ByteBuf inflate(ChannelHandlerContext ctx, int size) throws DataFormatException {
ByteBuf buf = ctx.alloc().directBuffer(size);
try {
ByteBuffer nioBuf = buf.internalNioBuffer(0, size);
int originalPos = nioBuf.position();
this.inflater.inflate(nioBuf.array());
int actualSize = nioBuf.position() - originalPos;
if (actualSize != size) {
throw new DecoderException("Badly compressed packet: actual length of uncompressed payload " + actualSize + " does not match declared size " + size);
} else {
buf.writerIndex(buf.writerIndex() + actualSize);
return buf;
}
} catch (Exception e) {
buf.release();
throw e;
}
}
} }

View file

@ -51,7 +51,7 @@ public class MinecraftProtocolTest {
null, null,
false false
); );
private static final ClientboundLoginPacket JOIN_GAME_PACKET = new ClientboundLoginPacket(0, false, new String[]{"minecraft:world"}, loadLoginRegistry(), 0, 16, 16, false, false, "overworld", "minecraft:world", 100, GameMode.SURVIVAL, GameMode.SURVIVAL, false, false, null, 100); private static final ClientboundLoginPacket JOIN_GAME_PACKET = new ClientboundLoginPacket(0, false, new String[]{"minecraft:world"}, 0, 16, 16, false, false, "overworld", "minecraft:world", 100, GameMode.SURVIVAL, GameMode.SURVIVAL, false, false, null, 100);
private static Server server; private static Server server;
@ -141,14 +141,4 @@ public class MinecraftProtocolTest {
} }
} }
} }
public static CompoundTag loadLoginRegistry() {
try (InputStream inputStream = MinecraftProtocolTest.class.getClassLoader().getResourceAsStream("networkCodec.nbt");
DataInputStream stream = new DataInputStream(new GZIPInputStream(inputStream))) {
return (CompoundTag) NBTIO.readTag((DataInput) stream);
} catch (IOException e) {
e.printStackTrace();
throw new AssertionError("Unable to load network codec.");
}
}
} }

View file

@ -8,6 +8,6 @@ import java.util.UUID;
public class ServerboundHelloPacketTest extends PacketTest { public class ServerboundHelloPacketTest extends PacketTest {
@Before @Before
public void setup() { public void setup() {
this.setPackets(new ServerboundHelloPacket("Username", null)); this.setPackets(new ServerboundHelloPacket("Username", UUID.randomUUID()));
} }
} }