try to generate less exceptions

This commit is contained in:
creeper123123321 2021-03-13 17:49:07 -03:00
parent 4de94027d1
commit a3377bd69a
2 changed files with 3 additions and 1 deletions

View File

@ -7,10 +7,11 @@ import io.netty.buffer.ByteBuf
import io.netty.buffer.ByteBufAllocator import io.netty.buffer.ByteBufAllocator
import io.netty.channel.ChannelHandlerContext import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.MessageToMessageCodec import io.netty.handler.codec.MessageToMessageCodec
import us.myles.ViaVersion.exception.CancelEncoderException
class MinecraftCodec : MessageToMessageCodec<ByteBuf, Packet>() { class MinecraftCodec : MessageToMessageCodec<ByteBuf, Packet>() {
override fun encode(ctx: ChannelHandlerContext, msg: Packet, out: MutableList<Any>) { override fun encode(ctx: ChannelHandlerContext, msg: Packet, out: MutableList<Any>) {
if (!ctx.channel().isActive) return if (!ctx.channel().isActive) throw CancelEncoderException.CACHED
val buf = ByteBufAllocator.DEFAULT.buffer() val buf = ByteBufAllocator.DEFAULT.buffer()
try { try {
val handler = ctx.pipeline().get(MinecraftHandler::class.java) val handler = ctx.pipeline().get(MinecraftHandler::class.java)

View File

@ -25,6 +25,7 @@ class ViaCodec(val info: UserConnection) : MessageToMessageCodec<ByteBuf, ByteBu
} }
override fun encode(ctx: ChannelHandlerContext, bytebuf: ByteBuf, out: MutableList<Any>) { override fun encode(ctx: ChannelHandlerContext, bytebuf: ByteBuf, out: MutableList<Any>) {
if (!ctx.channel().isActive) throw CancelEncoderException.CACHED
if (!info.checkOutgoingPacket()) throw CancelEncoderException.generate(null) if (!info.checkOutgoingPacket()) throw CancelEncoderException.generate(null)
if (!info.shouldTransformPacket()) { if (!info.shouldTransformPacket()) {
out.add(bytebuf.retain()) out.add(bytebuf.retain())