Skip to content

Commit

Permalink
Fixed modern decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalsin committed Nov 29, 2020
1 parent 84c97f0 commit 24ea54e
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/main/java/cc/ghast/packet/codec/ArtemisDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.handler.codec.DecoderException;
import org.bukkit.Bukkit;

import java.util.Arrays;
import java.util.zip.DataFormatException;
Expand Down Expand Up @@ -90,7 +91,14 @@ protected boolean decode(MutableByteBuf in) throws Exception {

// If there's no readable bytes, the packet is empty. Don't worry, such can happen
if (direction.equals(ProtocolDirection.IN)){
in = decompress(in);
try {
in = decompress(in);
} catch (DecoderException e){
Bukkit.getScheduler().runTask(PacketManager.INSTANCE.getPlugin(), () -> {
Bukkit.getPlayer(profile.getUuid()).kickPlayer(e.getMessage());
});
}

} else {
profile.setProtocol(EnumProtocolCurrent.PLAY);
}
Expand All @@ -108,24 +116,26 @@ protected boolean decode(MutableByteBuf in) throws Exception {
// Collect the packet from the enum map. This needs to be rewritten for better accuracy tho
Packet<?> packet = profile.getProtocol().getPacket(direction, id, profile.getUuid(), profile.getVersion());

if (packet instanceof ReadableBuffer) {
ReadableBuffer buffer = (ReadableBuffer) packet;
buffer.read(new ProtocolByteBuf(in));
}
if (packet != null) {
if (packet instanceof ReadableBuffer) {
ReadableBuffer buffer = (ReadableBuffer) packet;
buffer.read(new ProtocolByteBuf(in));
}

//System.out.println("pakcet=" + packet.getRealName());
//System.out.println("pakcet=" + packet.getRealName());

// Handle and collect the handshake
if (packet instanceof PacketHandshakeClientSetProtocol){
handleHandshake((PacketHandshakeClientSetProtocol) packet);
}
// Handle and collect the handshake
if (packet instanceof PacketHandshakeClientSetProtocol){
handleHandshake((PacketHandshakeClientSetProtocol) packet);
}

PacketManager.INSTANCE.getManager().callPacket(profile, packet);
PacketManager.INSTANCE.getManager().callPacket(profile, packet);

if (packet.isCancelled()) {
return true;
if (packet.isCancelled()) {
return true;
}
}

// Reset the reader index to prevent following pipelines to have a sort of issue. Normally it doesn't, I'm
// Still new to Netty. I'll need to investigate. More can be seen @ https://netty.io/4.0/api/io/netty/buffer/ByteBuf.html
in.resetReaderIndex();
Expand All @@ -141,7 +151,7 @@ protected boolean decode(MutableByteBuf in) throws Exception {
return false;
}

private MutableByteBuf decompress(MutableByteBuf byteBuf) throws DataFormatException {
private MutableByteBuf decompress(MutableByteBuf byteBuf) throws DataFormatException, DecoderException {
if (byteBuf.readableBytes() != 0) {
ProtocolByteBuf packetdataserializer = new ProtocolByteBuf(byteBuf);
int i = packetdataserializer.readVarInt();
Expand Down

0 comments on commit 24ea54e

Please sign in to comment.