From 9433ea5e48c77ccc8adc0eb68e466b87b10ffdfa Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Wed, 7 Oct 2015 18:12:50 -0400 Subject: [PATCH] Add some debug info for exception caught messages Also added support for deprecated block id's and suppressed some compiler warnings with Java 7. --- .../independent/NettyChannelInjector.java | 18 ++++++++- .../protocol/reflect/FuzzyReflection.java | 3 ++ .../com/comphenix/protocol/utility/Util.java | 2 + .../protocol/wrappers/BukkitConverters.java | 37 +++++++++++++++---- .../protocol/wrappers/WrappedBlockData.java | 19 ++++++++++ .../protocol/wrappers/nbt/NbtFactory.java | 2 + .../netty/shaded/ShadedChannelInjector.java | 26 ++++++++++--- 7 files changed, 93 insertions(+), 14 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyChannelInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyChannelInjector.java index 41142b13..59ec400f 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyChannelInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyChannelInjector.java @@ -27,6 +27,7 @@ import io.netty.channel.ChannelPromise; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.MessageToByteEncoder; +import io.netty.util.ReferenceCountUtil; import io.netty.util.concurrent.GenericFutureListener; import io.netty.util.internal.TypeParameterMatcher; @@ -285,9 +286,24 @@ public class NettyChannelInjector extends ByteToMessageDecoder implements Channe ChannelHandlerAdapter exceptionHandler = new ChannelHandlerAdapter() { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + if (channelListener.isDebug()) { + // People were complaining about this on the forums, figure I might as well figure out the cause + System.out.println("------------ ProtocolLib Debug ------------"); + System.out.println("Caught an exception in " + playerName + "\'s channel pipeline."); + System.out.println("Context: " + ctx); + System.out.println("The exception was: " + cause); + System.out.println("Stack trace:"); + cause.printStackTrace(System.out); + System.out.println("Please create an issue on GitHub with the above message."); + System.out.println("https://github.com/dmulloy2/ProtocolLib/issues"); + System.out.println("-------------------------------------------"); + } + if (cause instanceof ClosedChannelException) { - // Ignore + // This is what the DefaultChannelPipeline does + ReferenceCountUtil.release(cause); } else { + // We only care about closed channel exceptions, pass everything else along super.exceptionCaught(ctx, cause); } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java index de756e69..639feb25 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java @@ -602,6 +602,9 @@ public class FuzzyReflection { } // Prevent duplicate fields + + // @SafeVarargs + @SuppressWarnings("unchecked") private static Set setUnion(T[]... array) { Set result = new LinkedHashSet(); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/Util.java b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/Util.java index a0bd58a8..19b09bd1 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/Util.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/Util.java @@ -74,6 +74,8 @@ public class Util { * @param elements Array to convert * @return The list */ + // @SafeVarargs + @SuppressWarnings("unchecked") public static List asList(E... elements) { List list = new ArrayList(elements.length); for (E element : elements) { diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java index a07436e0..7d43945b 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java @@ -781,6 +781,29 @@ public class BukkitConverters { * @return A converter for block instances. */ public static EquivalentConverter getBlockConverter() { + return new IgnoreNullConverter() { + @Override + protected Object getGenericValue(Class genericType, Material specific) { + return getBlockIDConverter().getGeneric(genericType, specific.getId()); + } + + @Override + protected Material getSpecificValue(Object generic) { + return Material.getMaterial(getBlockIDConverter().getSpecific(generic)); + } + + @Override + public Class getSpecificType() { + return Material.class; + } + }; + } + + /** + * @deprecated ID's are deprecated + */ + @Deprecated + public static EquivalentConverter getBlockIDConverter() { // Initialize if we have't already if (GET_BLOCK == null || GET_BLOCK_ID == null) { Class block = MinecraftReflection.getBlockClass(); @@ -798,20 +821,20 @@ public class BukkitConverters { GET_BLOCK_ID = Accessors.getMethodAccessor(FuzzyReflection.fromClass(block).getMethod(getIdContract)); } - return new IgnoreNullConverter() { + return new IgnoreNullConverter() { @Override - protected Object getGenericValue(Class genericType, Material specific) { - return GET_BLOCK.invoke(null, specific.getId()); + protected Object getGenericValue(Class genericType, Integer specific) { + return GET_BLOCK.invoke(null, specific); } @Override - protected Material getSpecificValue(Object generic) { - return Material.getMaterial((Integer) GET_BLOCK_ID.invoke(null, generic)); + protected Integer getSpecificValue(Object generic) { + return (Integer) GET_BLOCK_ID.invoke(null, generic); } @Override - public Class getSpecificType() { - return Material.class; + public Class getSpecificType() { + return Integer.class; } }; } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java index cbf2693f..63387bca 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java @@ -81,6 +81,17 @@ public class WrappedBlockData extends AbstractWrapper { return BukkitConverters.getBlockConverter().getSpecific(block); } + /** + * Retrieves the type id of this BlockData. + * @return The type id of this BlockData. + * @deprecated ID's are deprecated + */ + @Deprecated + public int getTypeId() { + Object block = GET_BLOCK.invoke(handle); + return BukkitConverters.getBlockIDConverter().getSpecific(block); + } + /** * Retrieves the data of this BlockData. * @return The data of this BlockData. @@ -98,6 +109,14 @@ public class WrappedBlockData extends AbstractWrapper { setTypeAndData(type, 0); } + /** + * Sets the data of this BlockData. + * @param data New data + */ + public void setData(int data) { + setTypeAndData(getType(), data); + } + /** * Sets the type and data of this BlockData. * @param type New type diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java index 144b9424..7ad74037 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java @@ -449,6 +449,8 @@ public class NbtFactory { * @param elements - elements to add. * @return The new filled NBT list. */ + // @SafeVarargs + @SuppressWarnings("unchecked") public static NbtList ofList(String name, T... elements) { return WrappedList.fromArray(name, elements); } diff --git a/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedChannelInjector.java b/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedChannelInjector.java index 41206bbd..ee7cb06e 100644 --- a/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedChannelInjector.java +++ b/modules/v1_7_R4/src/main/java/com/comphenix/protocol/compat/netty/shaded/ShadedChannelInjector.java @@ -40,6 +40,7 @@ import net.minecraft.util.io.netty.channel.ChannelPromise; import net.minecraft.util.io.netty.channel.socket.SocketChannel; import net.minecraft.util.io.netty.handler.codec.ByteToMessageDecoder; import net.minecraft.util.io.netty.handler.codec.MessageToByteEncoder; +import net.minecraft.util.io.netty.util.ReferenceCountUtil; import net.minecraft.util.io.netty.util.concurrent.GenericFutureListener; import net.minecraft.util.io.netty.util.internal.TypeParameterMatcher; import net.sf.cglib.proxy.Factory; @@ -283,13 +284,26 @@ public class ShadedChannelInjector extends ByteToMessageDecoder implements Chann ChannelHandlerAdapter exceptionHandler = new ChannelHandlerAdapter() { @Override - public void exceptionCaught(ChannelHandlerContext context, Throwable ex) throws Exception { - if (ex instanceof ClosedChannelException) { - // Ignore + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + if (channelListener.isDebug()) { + // People were complaining about this on the forums, figure I might as well figure out the cause + System.out.println("------------ ProtocolLib Debug ------------"); + System.out.println("Caught an exception in " + playerName + "\'s channel pipeline."); + System.out.println("Context: " + ctx); + System.out.println("The exception was: " + cause); + System.out.println("Stack trace:"); + cause.printStackTrace(System.out); + System.out.println("Please create an issue on GitHub with the above message."); + System.out.println("https://github.com/dmulloy2/ProtocolLib/issues"); + System.out.println("-------------------------------------------"); + } + + if (cause instanceof ClosedChannelException) { + // This is what the DefaultChannelPipeline does + ReferenceCountUtil.release(cause); } else { - // TODO Actually handle exceptions? - System.err.println("[ProtocolLib] Encountered an uncaught exception in the channel pipeline:"); - ex.printStackTrace(); + // We only care about closed channel exceptions, pass everything else along + super.exceptionCaught(ctx, cause); } } };