From 4c10dfbf3358e62d3b04867ee212c90ee279f004 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 3 Nov 2016 22:45:10 +0100 Subject: [PATCH] Made the exception handler more reliable --- .../java/buttondevteam/lib/TBMCCoreAPI.java | 22 ++++++++++++++++++- .../buttondevteam/lib/TBMCExceptionEvent.java | 17 ++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java index be7e772..09b6f9a 100644 --- a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -11,7 +11,9 @@ import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map.Entry; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; @@ -106,6 +108,8 @@ public final class TBMCCoreAPI { return body; } + private static HashMap exceptionsToSend = new HashMap<>(); + /** * Send exception to the {@link TBMCExceptionEvent}. * @@ -115,7 +119,11 @@ public final class TBMCCoreAPI { * The exception to send */ public static void SendException(String sourcemsg, Throwable e) { - Bukkit.getPluginManager().callEvent(new TBMCExceptionEvent(sourcemsg, e)); + SendUnsentExceptions(); + TBMCExceptionEvent event = new TBMCExceptionEvent(sourcemsg, e); + Bukkit.getPluginManager().callEvent(event); + if (!event.isHandled()) + exceptionsToSend.put(sourcemsg, e); Bukkit.getLogger().warning(sourcemsg); e.printStackTrace(); } @@ -131,4 +139,16 @@ public final class TBMCCoreAPI { public static void RegisterEventsForExceptions(Listener listener, Plugin plugin) { EventExceptionHandler.registerEvents(listener, plugin, new EventExceptionCoreHandler()); } + + /** + * Send exceptions that haven't been sent (their events didn't get handled). This method is used by the DiscordPlugin's ready event + */ + public static void SendUnsentExceptions() { + for (Entry entry : exceptionsToSend.entrySet()) { + TBMCExceptionEvent event = new TBMCExceptionEvent(entry.getKey(), entry.getValue()); + Bukkit.getPluginManager().callEvent(event); + if (event.isHandled()) + exceptionsToSend.remove(entry.getKey()); + } + } } \ No newline at end of file diff --git a/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java b/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java index 277a3f7..8526248 100644 --- a/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java +++ b/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java @@ -16,6 +16,7 @@ public class TBMCExceptionEvent extends Event { private String sourcemsg; private Throwable exception; + private boolean handled; TBMCExceptionEvent(String sourcemsg, Throwable exception) { this.sourcemsg = sourcemsg; @@ -40,6 +41,22 @@ public class TBMCExceptionEvent extends Event { return exception; } + /** + * Gets if this event was handled + * + * @return True if it was handled + */ + public boolean isHandled() { + return handled; + } + + /** + * Flags the event as handled + */ + public void setHandled() { + this.handled = true; + } + @Override public HandlerList getHandlers() { return handlers;