From 8e5d371a9f632e324e32f280d5893a3f963acfac Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 2 Nov 2016 17:54:49 +0100 Subject: [PATCH] Error handling implemented --- pom.xml | 2 +- .../java/buttondevteam/core/MainPlugin.java | 4 +- .../lib/EventExceptionCoreHandler.java | 13 ++ .../java/buttondevteam/lib/TBMCCoreAPI.java | 188 +++++++++--------- .../buttondevteam/lib/TBMCExceptionEvent.java | 108 +++++----- 5 files changed, 161 insertions(+), 154 deletions(-) create mode 100644 src/main/java/buttondevteam/lib/EventExceptionCoreHandler.java diff --git a/pom.xml b/pom.xml index 9e03e96..448f34d 100644 --- a/pom.xml +++ b/pom.xml @@ -98,7 +98,7 @@ com.github.TBMCPlugins.ButtonCore Towny - master-SNAPSHOT + master-v1.0-g0f7e08b-32 diff --git a/src/main/java/buttondevteam/core/MainPlugin.java b/src/main/java/buttondevteam/core/MainPlugin.java index f4b7202..707f223 100644 --- a/src/main/java/buttondevteam/core/MainPlugin.java +++ b/src/main/java/buttondevteam/core/MainPlugin.java @@ -7,7 +7,7 @@ import java.util.logging.Logger; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; -import buttondevteam.discordplugin.EventExceptionDiscordSender; +import buttondevteam.lib.EventExceptionCoreHandler; import buttondevteam.lib.EventExceptionHandler; import buttondevteam.lib.TBMCPlayer; @@ -25,7 +25,7 @@ public class MainPlugin extends JavaPlugin { logger = getLogger(); logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ")."); - EventExceptionHandler.registerEvents(new PlayerListener(), this, new EventExceptionDiscordSender()); + EventExceptionHandler.registerEvents(new PlayerListener(), this, new EventExceptionCoreHandler()); } @Override diff --git a/src/main/java/buttondevteam/lib/EventExceptionCoreHandler.java b/src/main/java/buttondevteam/lib/EventExceptionCoreHandler.java new file mode 100644 index 0000000..9ed7476 --- /dev/null +++ b/src/main/java/buttondevteam/lib/EventExceptionCoreHandler.java @@ -0,0 +1,13 @@ +package buttondevteam.lib; + +import org.bukkit.event.Event; + +public class EventExceptionCoreHandler extends EventExceptionHandler { + + @Override + public boolean handle(Throwable ex, Event event) { + TBMCCoreAPI.SendException("An error occured while executing " + event.getEventName() + "!", ex); + return true; + } + +} diff --git a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java index 8293b0a..1214f81 100644 --- a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -1,100 +1,100 @@ -package buttondevteam.lib; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.bukkit.Bukkit; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - -public final class TBMCCoreAPI { - /** - * Updates or installs the specified plugin. The plugin must use Maven. - * - * @param name - * The plugin's repository name. - * @return Error message or empty string - */ - public static String UpdatePlugin(String name) { - List plugins = GetPluginNames(); - String correctname = null; - for (String plugin : plugins) { - if (plugin.equalsIgnoreCase(name)) { - correctname = plugin; // Fixes capitalization - break; - } - } - if (correctname == null) { - Bukkit.getLogger().warning("There was an error while updating TBMC plugin: " + name); - return "Can't find plugin: " + name; - } - Bukkit.getLogger().info("Updating TBMC plugin: " + correctname); - String ret = ""; - URL url; - try { - url = new URL("https://jitpack.io/com/github/TBMCPlugins/" - + (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/master-SNAPSHOT/" - + correctname + "-master-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well - FileUtils.copyURLToFile(url, new File("plugins/" + correctname + ".jar")); - } catch (FileNotFoundException e) { - ret = "Can't find JAR, the build probably failed. Build log (scroll to bottom):\nhttps://jitpack.io/com/github/TBMCPlugins/" - + correctname + "/master-SNAPSHOT/build.log"; - } catch (IOException e) { - ret = "IO error!\n" + e.getMessage(); - } catch (Exception e) { - Bukkit.getLogger().warning("Error!\n" + e); - ret = e.toString(); - } - return ret; +package buttondevteam.lib; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.bukkit.Bukkit; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +public final class TBMCCoreAPI { + /** + * Updates or installs the specified plugin. The plugin must use Maven. + * + * @param name + * The plugin's repository name. + * @return Error message or empty string + */ + public static String UpdatePlugin(String name) { + List plugins = GetPluginNames(); + String correctname = null; + for (String plugin : plugins) { + if (plugin.equalsIgnoreCase(name)) { + correctname = plugin; // Fixes capitalization + break; + } + } + if (correctname == null) { + Bukkit.getLogger().warning("There was an error while updating TBMC plugin: " + name); + return "Can't find plugin: " + name; + } + Bukkit.getLogger().info("Updating TBMC plugin: " + correctname); + String ret = ""; + URL url; + try { + url = new URL("https://jitpack.io/com/github/TBMCPlugins/" + + (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/master-SNAPSHOT/" + + correctname + "-master-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well + FileUtils.copyURLToFile(url, new File("plugins/" + correctname + ".jar")); + } catch (FileNotFoundException e) { + ret = "Can't find JAR, the build probably failed. Build log (scroll to bottom):\nhttps://jitpack.io/com/github/TBMCPlugins/" + + correctname + "/master-SNAPSHOT/build.log"; + } catch (IOException e) { + ret = "IO error!\n" + e.getMessage(); + } catch (Exception e) { + Bukkit.getLogger().warning("Error!\n" + e); + ret = e.toString(); + } + return ret; + } + + /** + * Retrieves all the repository names from the GitHub organization. + * + * @return A list of names + */ + public static List GetPluginNames() { + List ret = new ArrayList<>(); + try { + String resp = DownloadString("https://api.github.com/orgs/TBMCPlugins/repos"); + JsonArray arr = new JsonParser().parse(resp).getAsJsonArray(); + for (JsonElement obj : arr) { + JsonObject jobj = obj.getAsJsonObject(); + ret.add(jobj.get("name").getAsString()); + } + } catch (Exception e) { + e.printStackTrace(); + } + return ret; + } + + public static String DownloadString(String urlstr) throws MalformedURLException, IOException { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TBMCPlugins"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; } - /** - * Retrieves all the repository names from the GitHub organization. - * - * @return A list of names - */ - public static List GetPluginNames() { - List ret = new ArrayList<>(); - try { - String resp = DownloadString("https://api.github.com/orgs/TBMCPlugins/repos"); - JsonArray arr = new JsonParser().parse(resp).getAsJsonArray(); - for (JsonElement obj : arr) { - JsonObject jobj = obj.getAsJsonObject(); - ret.add(jobj.get("name").getAsString()); - } - } catch (Exception e) { - e.printStackTrace(); - } - return ret; - } - - public static String DownloadString(String urlstr) throws MalformedURLException, IOException { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TBMCPlugins"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - public static void SendException(String sourcemsg, Exception e) { + public static void SendException(String sourcemsg, Throwable e) { Bukkit.getPluginManager().callEvent(new TBMCExceptionEvent(sourcemsg, e)); Bukkit.getLogger().warning(sourcemsg); e.printStackTrace(); - } + } } \ 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 34c8b87..277a3f7 100644 --- a/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java +++ b/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java @@ -1,57 +1,51 @@ -package buttondevteam.lib; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -import buttondevteam.lib.TBMCPlayer.InfoTarget; - -/** - *

- * This event gets called (ideally) each time an exception occurs in a TBMC plugin. To call it, use {@link TBMCCoreAPI#SendException(String, Exception)}. - *

- * - * @author Norbi - * - */ -public class TBMCExceptionEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private String sourcemsg; - private Exception exception; - - TBMCExceptionEvent(String sourcemsg, Exception exception) { - this.sourcemsg = sourcemsg; - this.exception = exception; - } - - /** - * Gets the source message (where did this exception occur, etc.) - * - * @return The message - */ - public String getSourceMessage() { - return sourcemsg; - } - - /** - * Gets the exception - * - * @return The exception - */ - public Exception getException() { - return exception; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} +package buttondevteam.lib; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +/** + *

+ * This event gets called (ideally) each time an exception occurs in a TBMC plugin. To call it, use {@link TBMCCoreAPI#SendException(String, Exception)}. + *

+ * + * @author Norbi + * + */ +public class TBMCExceptionEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private String sourcemsg; + private Throwable exception; + + TBMCExceptionEvent(String sourcemsg, Throwable exception) { + this.sourcemsg = sourcemsg; + this.exception = exception; + } + + /** + * Gets the source message (where did this exception occur, etc.) + * + * @return The message + */ + public String getSourceMessage() { + return sourcemsg; + } + + /** + * Gets the exception + * + * @return The exception + */ + public Throwable getException() { + return exception; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +}