From 83f5a7e5b7f3dd4a8ca30dc7d67315778545f59c Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Sun, 9 Dec 2012 04:51:11 +0100 Subject: [PATCH] Add packet information to the default packet error reporter. --- .../protocol/error/DetailedErrorReporter.java | 15 +++++++++++ .../protocol/error/ErrorReporter.java | 9 +++++++ .../protocol/injector/EntityUtilities.java | 25 +++++++++++-------- .../injector/SortedPacketListenerList.java | 6 +++-- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java b/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java index 2257cbca..9b6ef329 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java @@ -95,6 +95,21 @@ public class DetailedErrorReporter implements ErrorReporter { this.logger = logger; } + @Override + public void reportMinimal(Plugin sender, String methodName, Throwable error, Object... parameters) { + reportMinimal(sender, methodName, error); + + // Print parameters, if they are given + if (parameters != null && parameters.length > 0) { + logger.log(Level.SEVERE, " Parameters:"); + + // Print each parameter + for (Object parameter : parameters) { + logger.log(Level.SEVERE, " " + getStringDescription(parameter)); + } + } + } + @Override public void reportMinimal(Plugin sender, String methodName, Throwable error) { logger.log(Level.SEVERE, "[" + PLUGIN_NAME + "] Unhandled exception occured in " + methodName + " for " + diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/error/ErrorReporter.java b/ProtocolLib/src/main/java/com/comphenix/protocol/error/ErrorReporter.java index de6e1e36..54b45f48 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/error/ErrorReporter.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/error/ErrorReporter.java @@ -12,6 +12,15 @@ public interface ErrorReporter { */ public abstract void reportMinimal(Plugin sender, String methodName, Throwable error); + /** + * Prints a small minimal error report about an exception from another plugin. + * @param sender - the other plugin. + * @param methodName - name of the caller method. + * @param error - the exception itself. + * @param parameters - any relevant parameters to print. + */ + public abstract void reportMinimal(Plugin sender, String methodName, Throwable error, Object... parameters); + /** * Prints a warning message from the current plugin. * @param sender - the object containing the caller method. diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java index 9927230a..2362ed55 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java @@ -52,7 +52,7 @@ class EntityUtilities { private static Method hashGetMethod; private static Method scanPlayersMethod; - + /* * While this function may look pretty bad, it's essentially just a reflection-warped * version of the following: @@ -246,20 +246,23 @@ class EntityUtilities { public static Entity getEntityFromID(World world, int entityID) throws FieldAccessException { try { Object trackerEntry = getEntityTrackerEntry(world, entityID); - - if (trackerField == null) - trackerField = trackerEntry.getClass().getField("tracker"); - Object tracker = FieldUtils.readField(trackerField, trackerEntry, true); - + Object tracker = null; + // Handle NULL cases - if (trackerEntry != null && tracker != null) { - return (Entity) MinecraftReflection.getBukkitEntity(tracker); - } else { - return null; + if (trackerEntry != null) { + if (trackerField == null) + trackerField = trackerEntry.getClass().getField("tracker"); + tracker = FieldUtils.readField(trackerField, trackerEntry, true); } + // If the tracker is NULL, we'll just assume this entity doesn't exist + if (tracker != null) + return (Entity) MinecraftReflection.getBukkitEntity(tracker); + else + return null; + } catch (Exception e) { - throw new FieldAccessException("Cannot find entity from ID.", e); + throw new FieldAccessException("Cannot find entity from ID " + entityID + ".", e); } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/SortedPacketListenerList.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/SortedPacketListenerList.java index d3184b10..9c895e28 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/SortedPacketListenerList.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/SortedPacketListenerList.java @@ -48,7 +48,8 @@ public final class SortedPacketListenerList extends AbstractConcurrentListenerMu element.getListener().onPacketReceiving(event); } catch (Throwable e) { // Minecraft doesn't want your Exception. - reporter.reportMinimal(element.getListener().getPlugin(), "onPacketReceiving()", e); + reporter.reportMinimal(element.getListener().getPlugin(), "onPacketReceiving(PacketEvent)", e, + event.getPacket().getHandle()); } } } @@ -69,7 +70,8 @@ public final class SortedPacketListenerList extends AbstractConcurrentListenerMu element.getListener().onPacketSending(event); } catch (Throwable e) { // Minecraft doesn't want your Exception. - reporter.reportMinimal(element.getListener().getPlugin(), "onPacketSending()", e); + reporter.reportMinimal(element.getListener().getPlugin(), "onPacketSending(PacketEvent)", e, + event.getPacket().getHandle()); } } }