From f48c1ca9cc6413a98b3656a530c3a530473d68d0 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 2 Nov 2016 18:31:43 +0100 Subject: [PATCH] Added method for exception events --- .../java/buttondevteam/core/MainPlugin.java | 5 +- .../java/buttondevteam/lib/TBMCCoreAPI.java | 18 +- .../buttondevteam/lib/chat/TBMCChatAPI.java | 198 +++++++++--------- 3 files changed, 113 insertions(+), 108 deletions(-) diff --git a/src/main/java/buttondevteam/core/MainPlugin.java b/src/main/java/buttondevteam/core/MainPlugin.java index 707f223..c61bb6c 100644 --- a/src/main/java/buttondevteam/core/MainPlugin.java +++ b/src/main/java/buttondevteam/core/MainPlugin.java @@ -7,8 +7,7 @@ import java.util.logging.Logger; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; -import buttondevteam.lib.EventExceptionCoreHandler; -import buttondevteam.lib.EventExceptionHandler; +import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCPlayer; public class MainPlugin extends JavaPlugin { @@ -25,7 +24,7 @@ public class MainPlugin extends JavaPlugin { logger = getLogger(); logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ")."); - EventExceptionHandler.registerEvents(new PlayerListener(), this, new EventExceptionCoreHandler()); + TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); } @Override diff --git a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java index 1214f81..318788c 100644 --- a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -13,6 +13,8 @@ import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.bukkit.Bukkit; +import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -90,11 +92,15 @@ public final class TBMCCoreAPI { String body = IOUtils.toString(in, encoding); in.close(); return body; - } - - public static void SendException(String sourcemsg, Throwable e) { - Bukkit.getPluginManager().callEvent(new TBMCExceptionEvent(sourcemsg, e)); - Bukkit.getLogger().warning(sourcemsg); - e.printStackTrace(); + } + + public static void SendException(String sourcemsg, Throwable e) { + Bukkit.getPluginManager().callEvent(new TBMCExceptionEvent(sourcemsg, e)); + Bukkit.getLogger().warning(sourcemsg); + e.printStackTrace(); + } + + public static void RegisterEventsForExceptions(Listener listener, Plugin plugin) { + EventExceptionHandler.registerEvents(listener, plugin, new EventExceptionCoreHandler()); } } \ No newline at end of file diff --git a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java index d07e875..c1804d9 100644 --- a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java +++ b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java @@ -1,99 +1,99 @@ -package buttondevteam.lib.chat; - -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Set; - -import org.bukkit.plugin.java.JavaPlugin; -import org.reflections.Reflections; -import org.reflections.scanners.SubTypesScanner; -import org.reflections.util.ClasspathHelper; -import org.reflections.util.ConfigurationBuilder; - -import buttondevteam.lib.TBMCPlayer; - -public class TBMCChatAPI { - - private static HashMap commands = new HashMap(); - - public static HashMap GetCommands() { - return commands; - } - - public static String[] GetSubCommands(TBMCCommandBase command) { - ArrayList cmds = new ArrayList(); - cmds.add("§6---- Subcommands ----"); - for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values()) { - if (cmd.GetCommandPath().startsWith(command.GetCommandPath() + "/")) { - int ind = cmd.GetCommandPath().indexOf('/', command.GetCommandPath().length() + 2); - if (ind >= 0) - continue; - cmds.add(cmd.GetCommandPath().replace('/', ' ')); - } - } - return cmds.toArray(new String[cmds.size()]); - } - - /** - *

- * This method adds a plugin's commands to help and sets their executor. - *

- *

- * The command must be registered in the caller plugin's plugin.yml. Otherwise the plugin will output a messsage to console. - *

- *

- * Using this method after the server is done loading will have no effect. - *

- * - * @param plugin - * The caller plugin - * @param acmdclass - * A command's class to get the package name for commands. The provided class's package and subpackages are scanned for commands. - */ - public static void AddCommands(JavaPlugin plugin, Class acmdclass) { - plugin.getLogger().info("Registering commands for " + plugin.getName()); - Reflections rf = new Reflections( - new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(plugin.getClass().getClassLoader())) - .addClassLoader(plugin.getClass().getClassLoader()).addScanners(new SubTypesScanner()) - .filterInputsBy((String pkg) -> pkg.contains(acmdclass.getPackage().getName()))); - Set> cmds = rf.getSubTypesOf(TBMCCommandBase.class); - for (Class cmd : cmds) { - try { - if (Modifier.isAbstract(cmd.getModifiers())) - continue; - TBMCCommandBase c = cmd.newInstance(); - c.plugin = plugin; - commands.put(c.GetCommandPath(), c); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - } - - /** - *

- * Add player information for {@link PlayerInfoCommand}. Only mods can see the given information. - *

- * - * @param player - * @param infoline - */ - public void AddPlayerInfoForMods(TBMCPlayer player, String infoline) { - // TODO - } - - /** - *

- * Add player information for hover text at {@link ChatProcessing}. Every online player can see the given information. - *

- * - * @param player - * @param infoline - */ - public void AddPlayerInfoForHover(TBMCPlayer player, String infoline) { - // TODO - } -} +package buttondevteam.lib.chat; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Set; + +import org.bukkit.plugin.java.JavaPlugin; +import org.reflections.Reflections; +import org.reflections.scanners.SubTypesScanner; +import org.reflections.util.ClasspathHelper; +import org.reflections.util.ConfigurationBuilder; + +import buttondevteam.lib.TBMCPlayer; + +public class TBMCChatAPI { + + private static HashMap commands = new HashMap(); + + public static HashMap GetCommands() { + return commands; + } + + public static String[] GetSubCommands(TBMCCommandBase command) { + ArrayList cmds = new ArrayList(); + cmds.add("§6---- Subcommands ----"); + for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values()) { + if (cmd.GetCommandPath().startsWith(command.GetCommandPath() + "/")) { + int ind = cmd.GetCommandPath().indexOf('/', command.GetCommandPath().length() + 2); + if (ind >= 0) + continue; + cmds.add(cmd.GetCommandPath().replace('/', ' ')); + } + } + return cmds.toArray(new String[cmds.size()]); + } + + /** + *

+ * This method adds a plugin's commands to help and sets their executor. + *

+ *

+ * The command must be registered in the caller plugin's plugin.yml. Otherwise the plugin will output a messsage to console. + *

+ *

+ * Using this method after the server is done loading will have no effect. + *

+ * + * @param plugin + * The caller plugin + * @param acmdclass + * A command's class to get the package name for commands. The provided class's package and subpackages are scanned for commands. + */ + public static void AddCommands(JavaPlugin plugin, Class acmdclass) { + plugin.getLogger().info("Registering commands for " + plugin.getName()); + Reflections rf = new Reflections( + new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(plugin.getClass().getClassLoader())) + .addClassLoader(plugin.getClass().getClassLoader()).addScanners(new SubTypesScanner()) + .filterInputsBy((String pkg) -> pkg.contains(acmdclass.getPackage().getName()))); + Set> cmds = rf.getSubTypesOf(TBMCCommandBase.class); + for (Class cmd : cmds) { + try { + if (Modifier.isAbstract(cmd.getModifiers())) + continue; + TBMCCommandBase c = cmd.newInstance(); + c.plugin = plugin; + commands.put(c.GetCommandPath(), c); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + + /** + *

+ * Add player information for {@link PlayerInfoCommand}. Only mods can see the given information. + *

+ * + * @param player + * @param infoline + */ + public void AddPlayerInfoForMods(TBMCPlayer player, String infoline) { + // TODO + } + + /** + *

+ * Add player information for hover text at {@link ChatProcessing}. Every online player can see the given information. + *

+ * + * @param player + * @param infoline + */ + public void AddPlayerInfoForHover(TBMCPlayer player, String infoline) { + // TODO + } +}