Skip to content

Commit

Permalink
Early return in ViaDecoder, cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Jan 24, 2025
1 parent 402ec26 commit d13db8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/main/java/com/viaversion/vialoader/netty/ViaDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ public ViaDecoder(final UserConnection user) {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (!this.user.checkIncomingPacket()) throw CancelDecoderException.generate(null);
if (!this.user.shouldTransformPacket()) {
out.add(in.retain());
return;
}

final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(in);
try {
if (this.user.shouldTransformPacket()) {
this.user.transformIncoming(transformedBuf, CancelDecoderException::generate);
}
this.user.transformIncoming(transformedBuf, CancelDecoderException::generate);
out.add(transformedBuf.retain());
} finally {
transformedBuf.release();
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/viaversion/vialoader/netty/ViaEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ public ViaEncoder(final UserConnection user) {
}

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
if (!user.checkOutgoingPacket()) throw CancelEncoderException.generate(null);
if (!user.shouldTransformPacket()) {
out.add(byteBuf.retain());
protected void encode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (!this.user.checkOutgoingPacket()) throw CancelEncoderException.generate(null);
if (!this.user.shouldTransformPacket()) {
out.add(in.retain());
return;
}

ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(byteBuf);
final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(in);
try {
user.transformOutgoing(transformedBuf, CancelEncoderException::generate);

this.user.transformOutgoing(transformedBuf, CancelEncoderException::generate);
out.add(transformedBuf.retain());
} finally {
transformedBuf.release();
Expand Down

0 comments on commit d13db8d

Please sign in to comment.