From d907e275eea1ed5c8fa675e53a746efc14d2bb6a Mon Sep 17 00:00:00 2001 From: BoomEaro <21033866+BoomEaro@users.noreply.github.com> Date: Mon, 28 Oct 2024 17:40:44 +0200 Subject: [PATCH] Fix possible NPE when trying to get encoder/decoder protocol --- .../net/md_5/bungee/ServerConnection.java | 4 +++ .../java/net/md_5/bungee/UserConnection.java | 4 +++ .../net/md_5/bungee/netty/ChannelWrapper.java | 35 ++++++++++++++----- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java index b4cc85b23b..b0fbbbf0cd 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java @@ -46,6 +46,10 @@ public void sendPacket(DefinedPacket packet) public void sendPacketQueued(DefinedPacket packet) { Protocol encodeProtocol = ch.getEncodeProtocol(); + if ( encodeProtocol == null ) + { + return; + } if ( !encodeProtocol.TO_SERVER.hasPacket( packet.getClass(), ch.getEncodeVersion() ) ) { packetQueue.add( packet ); diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index 2359e86472..b6e1241e61 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -187,6 +187,10 @@ public void sendPacket(PacketWrapper packet) public void sendPacketQueued(DefinedPacket packet) { Protocol encodeProtocol = ch.getEncodeProtocol(); + if ( encodeProtocol == null ) + { + return; + } if ( !encodeProtocol.TO_CLIENT.hasPacket( packet.getClass(), getPendingConnection().getVersion() ) ) { packetQueue.add( packet ); diff --git a/proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java b/proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java index 682bb80734..a234df01bf 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java @@ -42,23 +42,32 @@ public ChannelWrapper(ChannelHandlerContext ctx) public Protocol getDecodeProtocol() { - return ch.pipeline().get( MinecraftDecoder.class ).getProtocol(); + MinecraftDecoder minecraftDecoder = getMinecraftDecoder(); + if ( minecraftDecoder == null ) + { + return null; + } + return minecraftDecoder.getProtocol(); } public void setDecodeProtocol(Protocol protocol) { - ch.pipeline().get( MinecraftDecoder.class ).setProtocol( protocol ); + getMinecraftDecoder().setProtocol( protocol ); } public Protocol getEncodeProtocol() { - return ch.pipeline().get( MinecraftEncoder.class ).getProtocol(); - + MinecraftEncoder minecraftEncoder = getMinecraftEncoder(); + if ( minecraftEncoder == null ) + { + return null; + } + return minecraftEncoder.getProtocol(); } public void setEncodeProtocol(Protocol protocol) { - ch.pipeline().get( MinecraftEncoder.class ).setProtocol( protocol ); + getMinecraftEncoder().setProtocol( protocol ); } public void setProtocol(Protocol protocol) @@ -69,13 +78,23 @@ public void setProtocol(Protocol protocol) public void setVersion(int protocol) { - ch.pipeline().get( MinecraftDecoder.class ).setProtocolVersion( protocol ); - ch.pipeline().get( MinecraftEncoder.class ).setProtocolVersion( protocol ); + getMinecraftDecoder().setProtocolVersion( protocol ); + getMinecraftEncoder().setProtocolVersion( protocol ); + } + + public MinecraftDecoder getMinecraftDecoder() + { + return ch.pipeline().get( MinecraftDecoder.class ); + } + + public MinecraftEncoder getMinecraftEncoder() + { + return ch.pipeline().get( MinecraftEncoder.class ); } public int getEncodeVersion() { - return ch.pipeline().get( MinecraftEncoder.class ).getProtocolVersion(); + return getMinecraftEncoder().getProtocolVersion(); } public void write(Object packet)