From a5689a55503c032706bb7b7e4e33a4734eace4b8 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Thu, 2 Apr 2015 14:28:29 -0400 Subject: [PATCH] Handle uncaught exceptions to avoid console spam --- .../injector/netty/ChannelInjector.java | 1 + .../injector/netty/ExceptionHandler.java | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ExceptionHandler.java diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java index 5d0f702f..0de69be8 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java @@ -252,6 +252,7 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector { originalChannel.pipeline().addBefore("decoder", "protocol_lib_decoder", this); originalChannel.pipeline().addBefore("protocol_lib_decoder", "protocol_lib_finish", finishHandler); originalChannel.pipeline().addAfter("encoder", "protocol_lib_encoder", protocolEncoder); + originalChannel.pipeline().addLast("protocol_lib_exception_handler", new ExceptionHandler()); // Intercept all write methods channelField.setValue(new ChannelProxy(originalChannel, MinecraftReflection.getPacketClass()) { diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ExceptionHandler.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ExceptionHandler.java new file mode 100644 index 00000000..3cc29c13 --- /dev/null +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ExceptionHandler.java @@ -0,0 +1,49 @@ +/** + * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. + * Copyright (C) 2015 dmulloy2 + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + */ +package com.comphenix.protocol.injector.netty; + +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; + +import java.nio.channels.ClosedChannelException; + +/** + * @author dmulloy2 + */ + +public class ExceptionHandler implements ChannelHandler { + + @Override + public void exceptionCaught(ChannelHandlerContext context, Throwable ex) throws Exception { + if (ex instanceof ClosedChannelException) { + // Ignore + } else { + System.err.println("[ProtocolLib] Encountered an uncaught exception in the channel pipeline:"); + ex.printStackTrace(); + } + } + + @Override + public void handlerAdded(ChannelHandlerContext context) throws Exception { + // Ignore + } + + @Override + public void handlerRemoved(ChannelHandlerContext context) throws Exception { + // Ignore + } +} \ No newline at end of file