Skip to content

Commit

Permalink
fix: use skip serverbound chat packets
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Dec 10, 2023
1 parent 5aa00a2 commit 98f5b8a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import net.minecraft.network.SkipPacketException
import net.minecraft.network.protocol.Packet
import net.minecraft.network.protocol.PacketFlow
import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket
import net.minecraft.network.protocol.game.ServerboundChatPacket
import net.minecraft.server.MinecraftServer
import net.minecraft.server.network.ServerConnectionListener
import org.bukkit.Bukkit
Expand Down Expand Up @@ -191,7 +192,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {

when {
dataSerializer.readableBytes() > 0 -> throw IOException("Packet $packetID ($packet) was larger than I expected, found ${dataSerializer.readableBytes()} bytes extra whilst reading packet $packetID")
packet is ClientboundPlayerChatPacket -> {
packet is ServerboundChatPacket -> {
val serializer = FriendlyByteBuf(buffferCopy)
serializer.readVarInt()
packet = protocol.createPacket(PacketFlow.SERVERBOUND, packetID, serializer) ?: throw IOException("Bad packet id $packetID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import net.minecraft.network.SkipPacketException
import net.minecraft.network.protocol.Packet
import net.minecraft.network.protocol.PacketFlow
import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket
import net.minecraft.network.protocol.game.ServerboundChatPacket
import net.minecraft.server.MinecraftServer
import net.minecraft.server.network.ServerConnectionListener
import org.bukkit.Bukkit
Expand Down Expand Up @@ -244,7 +245,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {

when {
dataSerializer.readableBytes() > 0 -> throw IOException("Packet $packetID ($packet) was larger than I expected, found ${dataSerializer.readableBytes()} bytes extra whilst reading packet $packetID")
packet is ClientboundPlayerChatPacket -> {
packet is ServerboundChatPacket -> {
val serializer = FriendlyByteBuf(buffferCopy)
serializer.readVarInt()
packet = protocol.createPacket(PacketFlow.SERVERBOUND, packetID, serializer) ?: throw IOException("Bad packet id $packetID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import net.minecraft.nbt.Tag
import net.minecraft.network.*
import net.minecraft.network.protocol.Packet
import net.minecraft.network.protocol.PacketFlow
import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket
import net.minecraft.network.protocol.game.ServerboundChatPacket
import net.minecraft.server.MinecraftServer
import net.minecraft.server.network.ServerConnectionListener
import org.bukkit.Bukkit
Expand Down Expand Up @@ -240,14 +240,13 @@ class EmojyNMSHandler : IEmojyNMSHandler {
val bufferCopy = buffer.copy()
if (buffer.readableBytes() == 0) return

val dataSerializer = CustomDataSerializer(player, buffer)
val packetID = dataSerializer.readVarInt()
val customDataSerializer = CustomDataSerializer(player, buffer)
val packetID = customDataSerializer.readVarInt()
val attribute = ctx.channel().attr(Connection.ATTRIBUTE_SERVERBOUND_PROTOCOL)
var packet = attribute.get().createPacket(packetID, dataSerializer) ?: throw IOException("Bad packet id $packetID")
var packet = attribute.get().createPacket(packetID, customDataSerializer) ?: throw IOException("Bad packet id $packetID")

broadcast(packet is ClientboundPlayerChatPacket)
if (dataSerializer.readableBytes() > 0) throw IOException("Packet $packetID ($packet) was larger than I expected, found ${dataSerializer.readableBytes()} bytes extra whilst reading packet $packetID")
else if (packet is ClientboundPlayerChatPacket) {
if (customDataSerializer.readableBytes() > 0) throw IOException("Packet $packetID ($packet) was larger than I expected, found ${customDataSerializer.readableBytes()} bytes extra whilst reading packet $packetID")
else if (packet is ServerboundChatPacket) {
val serializer = FriendlyByteBuf(bufferCopy)
serializer.readVarInt()
packet = attribute.get().createPacket(packetID, serializer) ?: throw IOException("Bad packet id $packetID")
Expand Down

0 comments on commit 98f5b8a

Please sign in to comment.