From fef61b18018afddea44aa853406fd76d01dd66f3 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 2 Nov 2016 00:05:25 +0100 Subject: [PATCH] Moved files --- ButtonLib/.gitignore | 223 ----- ButtonLib/README.md | 2 - ButtonLib/pom.xml | 122 --- ButtonLib/src/main/resources/plugin.yml | 4 - ButtonLib/jitpack.yml => jitpack.yml | 0 .../lib/EventExceptionHandler.java | 306 +++--- .../java/buttondevteam/lib/TBMCCoreAPI.java | 198 ++-- .../buttondevteam/lib/TBMCExceptionEvent.java | 114 +-- .../java/buttondevteam/lib/TBMCPlayer.java | 930 +++++++++--------- .../buttondevteam/lib/TBMCPlayerAddEvent.java | 72 +- .../lib/TBMCPlayerGetInfoEvent.java | 146 +-- .../lib/TBMCPlayerJoinEvent.java | 54 +- .../lib/TBMCPlayerLoadEvent.java | 68 +- .../lib/TBMCPlayerQuitEvent.java | 54 +- .../lib/TBMCPlayerSaveEvent.java | 68 +- .../buttondevteam/lib/chat/TBMCChatAPI.java | 198 ++-- .../lib/chat/TBMCCommandBase.java | 52 +- 17 files changed, 1130 insertions(+), 1481 deletions(-) delete mode 100644 ButtonLib/.gitignore delete mode 100644 ButtonLib/README.md delete mode 100644 ButtonLib/pom.xml delete mode 100644 ButtonLib/src/main/resources/plugin.yml rename ButtonLib/jitpack.yml => jitpack.yml (100%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/EventExceptionHandler.java (97%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/TBMCCoreAPI.java (96%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/TBMCExceptionEvent.java (95%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/TBMCPlayer.java (96%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/TBMCPlayerAddEvent.java (94%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/TBMCPlayerGetInfoEvent.java (95%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/TBMCPlayerJoinEvent.java (94%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/TBMCPlayerLoadEvent.java (95%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/TBMCPlayerQuitEvent.java (94%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/TBMCPlayerSaveEvent.java (95%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/chat/TBMCChatAPI.java (97%) rename {ButtonLib/src => src}/main/java/buttondevteam/lib/chat/TBMCCommandBase.java (95%) diff --git a/ButtonLib/.gitignore b/ButtonLib/.gitignore deleted file mode 100644 index 77e539d..0000000 --- a/ButtonLib/.gitignore +++ /dev/null @@ -1,223 +0,0 @@ -################# -## Eclipse -################# - -*.pydevproject -.project -.metadata/ -bin/ -tmp/ -*.tmp -*.bak -*.swp -*~.nib -local.properties -.classpath -.settings/ -.loadpath -target/ -.project - -# External tool builders -.externalToolBuilders/ - -# Locally stored "Eclipse launch configurations" -*.launch - -# CDT-specific -.cproject - -# PDT-specific -.buildpath - - -################# -## Visual Studio -################# - -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. - -# User-specific files -*.suo -*.user -*.sln.docstates - -# Build results - -[Dd]ebug/ -[Rr]elease/ -x64/ -build/ -[Bb]in/ -[Oo]bj/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -*_i.c -*_p.c -*.ilk -*.meta -*.obj -*.pch -*.pdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.log -*.scc - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opensdf -*.sdf -*.cachefile - -# Visual Studio profiler -*.psess -*.vsp -*.vspx - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# NCrunch -*.ncrunch* -.*crunch*.local.xml - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.Publish.xml -*.pubxml -*.publishproj - -# NuGet Packages Directory -## TODO: If you have NuGet Package Restore enabled, uncomment the next line -#packages/ - -# Windows Azure Build Output -csx -*.build.csdef - -# Windows Store app package directory -AppPackages/ - -# Others -sql/ -*.Cache -ClientBin/ -[Ss]tyle[Cc]op.* -~$* -*~ -*.dbmdl -*.[Pp]ublish.xml -*.pfx -*.publishsettings - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file to a newer -# Visual Studio version. Backup files are not needed, because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm - -# SQL Server files -App_Data/*.mdf -App_Data/*.ldf - -############# -## Windows detritus -############# - -# Windows image file caches -Thumbs.db -ehthumbs.db - -# Folder config file -Desktop.ini - -# Recycle Bin used on file shares -$RECYCLE.BIN/ - -# Mac crap -.DS_Store - - -############# -## Python -############# - -*.py[cod] - -# Packages -*.egg -*.egg-info -dist/ -build/ -eggs/ -parts/ -var/ -sdist/ -develop-eggs/ -.installed.cfg - -# Installer logs -pip-log.txt - -# Unit test / coverage reports -.coverage -.tox - -#Translations -*.mo - -#Mr Developer -.mr.developer.cfg -.metadata/* -TheButtonAutoFlair/out/artifacts/Autoflair/Autoflair.jar -*.iml -*.name -.idea/compiler.xml diff --git a/ButtonLib/README.md b/ButtonLib/README.md deleted file mode 100644 index fda2dc0..0000000 --- a/ButtonLib/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# ButtonLib -A library that stores everything common to the TBMC plugins diff --git a/ButtonLib/pom.xml b/ButtonLib/pom.xml deleted file mode 100644 index 0c5e083..0000000 --- a/ButtonLib/pom.xml +++ /dev/null @@ -1,122 +0,0 @@ - - 4.0.0 - com.github.TBMCPlugins.ButtonLib - ButtonLib - master-SNAPSHOT - ButtonLib - ButtonCore - - src/main/java - - - src - - **/*.java - - - - src/main/resources - - *.properties - *.yml - *.csv - *.txt - - true - - - ButtonCore - - - maven-compiler-plugin - 3.3 - - 1.8 - 1.8 - - - - - org.apache.maven.plugins - maven-resources-plugin - 3.0.1 - - - copy - compile - - copy-resources - - - target - - - resources - - - - - - - - - - - spigot-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - - jitpack.io - https://jitpack.io/ - - - - - org.spigotmc - spigot-api - 1.9.2-R0.1-SNAPSHOT - - - - commons-io - commons-io - 1.3.2 - - - com.github.TBMCPlugins.ButtonLib - Towny - master-SNAPSHOT - - - - org.reflections - reflections - 0.9.10 - - - - TBMCPlugins - https://github.com/TBMCPlugins - - - - internal.repo - Temporary Staging Repository - file://${project.build.directory}/mvn-repo/${project.name} - - - - - github - - - https://github.com/TBMCPlugins/mvn-repo - scm:git:https://github.com/TBMCPlugins/mvn-repo.git - scm:git:https://github.com/TBMCPlugins/mvn-repo.git - - \ No newline at end of file diff --git a/ButtonLib/src/main/resources/plugin.yml b/ButtonLib/src/main/resources/plugin.yml deleted file mode 100644 index f64dc39..0000000 --- a/ButtonLib/src/main/resources/plugin.yml +++ /dev/null @@ -1,4 +0,0 @@ -name: ButtonPluginBucket -main: buttondevteam.bucket.MainPlugin -version: 1.0 -author: TBMCPlugins \ No newline at end of file diff --git a/ButtonLib/jitpack.yml b/jitpack.yml similarity index 100% rename from ButtonLib/jitpack.yml rename to jitpack.yml diff --git a/ButtonLib/src/main/java/buttondevteam/lib/EventExceptionHandler.java b/src/main/java/buttondevteam/lib/EventExceptionHandler.java similarity index 97% rename from ButtonLib/src/main/java/buttondevteam/lib/EventExceptionHandler.java rename to src/main/java/buttondevteam/lib/EventExceptionHandler.java index 7c64e4f..e2d1b44 100644 --- a/ButtonLib/src/main/java/buttondevteam/lib/EventExceptionHandler.java +++ b/src/main/java/buttondevteam/lib/EventExceptionHandler.java @@ -1,154 +1,154 @@ -package buttondevteam.lib; - -import java.lang.reflect.Method; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.apache.commons.lang.Validate; -import org.bukkit.event.Event; -import org.bukkit.event.EventException; -import org.bukkit.event.HandlerList; -import org.bukkit.event.Listener; -import org.bukkit.plugin.EventExecutor; -import org.bukkit.plugin.IllegalPluginAccessException; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.RegisteredListener; - -import com.google.common.collect.Lists; - -public abstract class EventExceptionHandler { // https://gist.github.com/aadnk/5430459 - // For wrapping a registered listener - private static class ExceptionRegisteredListener extends RegisteredListener { - /** - * Represents an event executor that does nothing. This is not really necessary in the current - * implementation of CraftBukkit, but we will take no chances. - */ - private static EventExecutor NULL_EXECUTOR = new EventExecutor() { - @Override - public void execute(Listener listener, Event event) throws EventException { - // Do nothing - } - }; - - private final RegisteredListener delegate; - private final EventExceptionHandler handler; - - public ExceptionRegisteredListener(RegisteredListener delegate, EventExceptionHandler handler) { - super(delegate.getListener(), NULL_EXECUTOR, delegate.getPriority(), - delegate.getPlugin(), delegate.isIgnoringCancelled()); - this.delegate = delegate; - this.handler = handler; - } - - @Override - public void callEvent(Event event) throws EventException { - try { - delegate.callEvent(event); - } catch (EventException e) { - if (!handler.handle(e.getCause(), event)) { - throw e; - } - } catch (Throwable e) { - if (!handler.handle(e, event)) { - doThrow(e); - } - } - } - - // WARNING: HORRIBLE, HORRIBLE HACK to get around checked exceptions - private static void doThrow(Throwable e) { - ExceptionRegisteredListener. doThrowInner(e); - } - - @SuppressWarnings("unchecked") - private static void doThrowInner(Throwable e) throws E { - throw (E) e; - } - } - - /** - * Register Bukkit event handlers with a given exception handler. - * @param listener - a class of event handlers. - * @param plugin - the current plugin. - * @param handler - exception handler. - */ - public static void registerEvents(Listener listener, Plugin plugin, EventExceptionHandler handler) { - Validate.notNull(plugin, "Plugin cannot be NULL."); - - registerEvents(plugin.getServer().getPluginManager(), listener, plugin, handler); - } - - /** - * Register Bukkit event handlers with a given exception handler. - * @param manager - the current plugin manager. - * @param listener - a class of event handlers. - * @param plugin - the current plugin. - * @param handler - exception handler. - */ - public static void registerEvents(PluginManager manager, Listener listener, Plugin plugin, EventExceptionHandler handler) { - Validate.notNull(manager, "Manager cannot be NULL."); - Validate.notNull(listener, "Listener cannot be NULL."); - Validate.notNull(plugin, "Plugin cannot be NULL."); - Validate.notNull(handler, "Handler cannot be NULL."); - - if (!plugin.isEnabled()) { - throw new IllegalPluginAccessException("Plugin attempted to register " + listener + " while not enabled"); - } - - // Create normal listeners - for (Map.Entry, Set> entry : - plugin.getPluginLoader().createRegisteredListeners(listener, plugin).entrySet()) { - - // Wrap these listeners in our exception handler - getHandlerList(entry.getKey()).registerAll(wrapAll(entry.getValue(), handler)); - } - } - - /** - * Wrap every listener in the given collection around an exception handler. - * @param listeners - the listeners to wrap. - * @param handler - the exception handler to add. - * @return The wrapped listeners. - */ - private static Collection wrapAll(Collection listeners, EventExceptionHandler handler) { - List output = Lists.newArrayList(); - - for (RegisteredListener listener : listeners) { - output.add(new ExceptionRegisteredListener(listener, handler)); - } - return output; - } - - /** - * Retrieve the handler list associated with the given class. - * @param clazz - given event class. - * @return Associated handler list. - */ - private static HandlerList getHandlerList(Class clazz) { - // Class must have Event as its superclass - while (clazz.getSuperclass() != null && Event.class.isAssignableFrom(clazz.getSuperclass())) { - try { - Method method = clazz.getDeclaredMethod("getHandlerList"); - method.setAccessible(true); - return (HandlerList) method.invoke(null); - } catch (NoSuchMethodException e) { - // Keep on searching - clazz = clazz.getSuperclass().asSubclass(Event.class); - } catch (Exception e) { - throw new IllegalPluginAccessException(e.getMessage()); - } - } - throw new IllegalPluginAccessException("Unable to find handler list for event " + clazz.getName()); - } - - /** - * Handle a given exception. - * @param ex - the exception to handle. - * @param event - the event that was being handled. - * @return TRUE to indicate that the exception has been handled, FALSE to rethrow it. - */ - public abstract boolean handle(Throwable ex, Event event); +package buttondevteam.lib; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.lang.Validate; +import org.bukkit.event.Event; +import org.bukkit.event.EventException; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; +import org.bukkit.plugin.EventExecutor; +import org.bukkit.plugin.IllegalPluginAccessException; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.RegisteredListener; + +import com.google.common.collect.Lists; + +public abstract class EventExceptionHandler { // https://gist.github.com/aadnk/5430459 + // For wrapping a registered listener + private static class ExceptionRegisteredListener extends RegisteredListener { + /** + * Represents an event executor that does nothing. This is not really necessary in the current + * implementation of CraftBukkit, but we will take no chances. + */ + private static EventExecutor NULL_EXECUTOR = new EventExecutor() { + @Override + public void execute(Listener listener, Event event) throws EventException { + // Do nothing + } + }; + + private final RegisteredListener delegate; + private final EventExceptionHandler handler; + + public ExceptionRegisteredListener(RegisteredListener delegate, EventExceptionHandler handler) { + super(delegate.getListener(), NULL_EXECUTOR, delegate.getPriority(), + delegate.getPlugin(), delegate.isIgnoringCancelled()); + this.delegate = delegate; + this.handler = handler; + } + + @Override + public void callEvent(Event event) throws EventException { + try { + delegate.callEvent(event); + } catch (EventException e) { + if (!handler.handle(e.getCause(), event)) { + throw e; + } + } catch (Throwable e) { + if (!handler.handle(e, event)) { + doThrow(e); + } + } + } + + // WARNING: HORRIBLE, HORRIBLE HACK to get around checked exceptions + private static void doThrow(Throwable e) { + ExceptionRegisteredListener. doThrowInner(e); + } + + @SuppressWarnings("unchecked") + private static void doThrowInner(Throwable e) throws E { + throw (E) e; + } + } + + /** + * Register Bukkit event handlers with a given exception handler. + * @param listener - a class of event handlers. + * @param plugin - the current plugin. + * @param handler - exception handler. + */ + public static void registerEvents(Listener listener, Plugin plugin, EventExceptionHandler handler) { + Validate.notNull(plugin, "Plugin cannot be NULL."); + + registerEvents(plugin.getServer().getPluginManager(), listener, plugin, handler); + } + + /** + * Register Bukkit event handlers with a given exception handler. + * @param manager - the current plugin manager. + * @param listener - a class of event handlers. + * @param plugin - the current plugin. + * @param handler - exception handler. + */ + public static void registerEvents(PluginManager manager, Listener listener, Plugin plugin, EventExceptionHandler handler) { + Validate.notNull(manager, "Manager cannot be NULL."); + Validate.notNull(listener, "Listener cannot be NULL."); + Validate.notNull(plugin, "Plugin cannot be NULL."); + Validate.notNull(handler, "Handler cannot be NULL."); + + if (!plugin.isEnabled()) { + throw new IllegalPluginAccessException("Plugin attempted to register " + listener + " while not enabled"); + } + + // Create normal listeners + for (Map.Entry, Set> entry : + plugin.getPluginLoader().createRegisteredListeners(listener, plugin).entrySet()) { + + // Wrap these listeners in our exception handler + getHandlerList(entry.getKey()).registerAll(wrapAll(entry.getValue(), handler)); + } + } + + /** + * Wrap every listener in the given collection around an exception handler. + * @param listeners - the listeners to wrap. + * @param handler - the exception handler to add. + * @return The wrapped listeners. + */ + private static Collection wrapAll(Collection listeners, EventExceptionHandler handler) { + List output = Lists.newArrayList(); + + for (RegisteredListener listener : listeners) { + output.add(new ExceptionRegisteredListener(listener, handler)); + } + return output; + } + + /** + * Retrieve the handler list associated with the given class. + * @param clazz - given event class. + * @return Associated handler list. + */ + private static HandlerList getHandlerList(Class clazz) { + // Class must have Event as its superclass + while (clazz.getSuperclass() != null && Event.class.isAssignableFrom(clazz.getSuperclass())) { + try { + Method method = clazz.getDeclaredMethod("getHandlerList"); + method.setAccessible(true); + return (HandlerList) method.invoke(null); + } catch (NoSuchMethodException e) { + // Keep on searching + clazz = clazz.getSuperclass().asSubclass(Event.class); + } catch (Exception e) { + throw new IllegalPluginAccessException(e.getMessage()); + } + } + throw new IllegalPluginAccessException("Unable to find handler list for event " + clazz.getName()); + } + + /** + * Handle a given exception. + * @param ex - the exception to handle. + * @param event - the event that was being handled. + * @return TRUE to indicate that the exception has been handled, FALSE to rethrow it. + */ + public abstract boolean handle(Throwable ex, Event event); } \ No newline at end of file diff --git a/ButtonLib/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java similarity index 96% rename from ButtonLib/src/main/java/buttondevteam/lib/TBMCCoreAPI.java rename to src/main/java/buttondevteam/lib/TBMCCoreAPI.java index 44da4df..0fef393 100644 --- a/ButtonLib/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -1,99 +1,99 @@ -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 + "/master-SNAPSHOT/" + correctname - + "-master-SNAPSHOT.jar"); // ButtonLib exception not required, not a separate plugin - 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; - } - - public static void SendException(String sourcemsg, Exception e) { - Bukkit.getPluginManager().callEvent(new TBMCExceptionEvent(sourcemsg, e)); - Bukkit.getLogger().warning(sourcemsg); - e.printStackTrace(); - } -} +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 + "/master-SNAPSHOT/" + correctname + + "-master-SNAPSHOT.jar"); // ButtonLib exception not required, not a separate plugin + 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; + } + + public static void SendException(String sourcemsg, Exception e) { + Bukkit.getPluginManager().callEvent(new TBMCExceptionEvent(sourcemsg, e)); + Bukkit.getLogger().warning(sourcemsg); + e.printStackTrace(); + } +} diff --git a/ButtonLib/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java b/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java similarity index 95% rename from ButtonLib/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java rename to src/main/java/buttondevteam/lib/TBMCExceptionEvent.java index ec7f11d..34c8b87 100644 --- a/ButtonLib/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java +++ b/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java @@ -1,57 +1,57 @@ -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 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; + } +} diff --git a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayer.java b/src/main/java/buttondevteam/lib/TBMCPlayer.java similarity index 96% rename from ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayer.java rename to src/main/java/buttondevteam/lib/TBMCPlayer.java index a15f266..c5e5e7c 100644 --- a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayer.java +++ b/src/main/java/buttondevteam/lib/TBMCPlayer.java @@ -1,465 +1,465 @@ -package buttondevteam.lib; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map.Entry; -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.configuration.file.YamlConfiguration; -import com.palmergames.bukkit.towny.Towny; -import com.palmergames.bukkit.towny.object.Resident; -import com.palmergames.bukkit.towny.object.TownyUniverse; - -/** - *

- * The class for holding data common to all TBMC plugins - *

- *

- * Use {@link #asPluginPlayer(Class)} to get plugin-specific data - *

- * - * @author Norbi - * - */ -public class TBMCPlayer implements AutoCloseable { - private static final String TBMC_PLAYERS_DIR = "TBMC/players"; - - private HashMap data = new HashMap<>(); - - /** - *

- * Gets a player data entry for the caller plugin returning the desired type.
- * It will automatically determine the key and the return type.
- * Usage: - *

- * - *
-	 * {@code
-	 * public String getPlayerName() {
-	 * 	return getData();
-	 * }
-	 * 
- * - * @return The value or null if not found - */ - @SuppressWarnings("unchecked") - protected T getData() { - StackTraceElement st = new Exception().getStackTrace()[1]; - String mname = st.getMethodName(); - if (!mname.startsWith("get")) - throw new UnsupportedOperationException("Can only use getData from a getXYZ method"); - Object ret = getLoadedPlayers().get(uuid).data.get(mname.substring("get".length()).toLowerCase()); - if (Integer.class.isAssignableFrom(ret.getClass())) - throw new UnsupportedOperationException("For integers use getIntData()"); - return (T) ret; - } - - /** - * Sets a player data entry based on the caller method
- * Usage: - *

- * - *
-	 * {@code
-	 * public String setPlayerName(String value) {
-	 * 	return setData(value);
-	 * }
-	 * 
- * - * @param value - * The value to set - */ - protected void setData(Object value) { - StackTraceElement st = new Exception().getStackTrace()[1]; - String mname = st.getMethodName(); - if (!mname.startsWith("set")) - throw new UnsupportedOperationException("Can only use setData from a setXYZ method"); - getLoadedPlayers().get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value); - } - - /** - *

- * Gets a player data entry for the caller plugin returning the desired type, which is an enum
- * It will automatically determine the key and the return type.
- * Usage: - *

- * - *
-	 * {@code
-	 * public String getSomeEnum() {
-	 * 	return getEnumData();
-	 * }
-	 * 
- * - * @return The value or null if not found - */ - protected > T getEnumData(Class cl) { - StackTraceElement st = new Exception().getStackTrace()[1]; - String mname = st.getMethodName(); - if (!mname.startsWith("get")) - throw new UnsupportedOperationException("Can only use getEnumData from a getXYZ method"); - final String retstr = (String) getLoadedPlayers().get(uuid).data - .get(mname.substring("get".length()).toLowerCase()); - if (retstr != null) - return Enum.valueOf(cl, retstr); - else - return null; - } - - /** - * Sets a player data entry based on the caller method
- * Usage: - *

- * - *
-	 * {@code
-	 * public String setSomeEnum(SomeEnum value) {
-	 * 	return setEnumData(value);
-	 * }
-	 * 
- * - * @param value - * The value to set - */ - protected void setEnumData(Enum value) { - StackTraceElement st = new Exception().getStackTrace()[1]; - String mname = st.getMethodName(); - if (!mname.startsWith("set")) - throw new UnsupportedOperationException("Can only use setEnumData from a setXYZ method"); - getLoadedPlayers().get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value.toString()); - } - - /** - *

- * Gets a player data entry for the caller plugin returning the desired type, which is a number
- * It will automatically determine the key and the return type.
- * Usage: - *

- * - *
-	 * {@code
-	 * public short getNumber() {
-	 * 	return getIntData();
-	 * }
-	 * 
- * - * @return The value or null if not found - */ - @SuppressWarnings("unchecked") - protected T getIntData(Class cl) { - StackTraceElement st = new Exception().getStackTrace()[1]; - String mname = st.getMethodName(); - if (!mname.startsWith("get")) - throw new UnsupportedOperationException("Can only use getIntData from a getXYZ method"); - Object obj = getLoadedPlayers().get(uuid).data.get(mname.substring("get".length()).toLowerCase()); - if (!(obj instanceof Integer)) - throw new UnsupportedOperationException("The retrieved object isn't a number: " + obj); - Integer int_ = (Integer) obj; - if (Short.class.isAssignableFrom(cl)) - return (T) (Object) int_.shortValue(); - else - return (T) (Object) int_; - } - - /** - * Sets a player data entry based on the caller method
- * Usage: - *

- * - *
-	 * {@code
-	 * public String setNumber(short value) {
-	 * 	return setIntData(value);
-	 * }
-	 * 
- * - * @param value - * The value to set - */ - protected void setIntData(Number value) { - StackTraceElement st = new Exception().getStackTrace()[1]; - String mname = st.getMethodName(); - if (!mname.startsWith("set")) - throw new UnsupportedOperationException("Can only use setIntData from a setXYZ method"); - getLoadedPlayers().get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value); - } - - /** - * Gets the player's Minecraft name - * - * @return The player's Minecraft name - */ - public String getPlayerName() { - return getData(); - } - - /** - * Sets the player's Minecraft name - * - * @param playerName - * the new name - */ - public void setPlayerName(String playerName) { - setData(playerName); - } - - private UUID uuid; // Do not save it in the file - - /** - * Get the player's UUID - * - * @return The Minecraft UUID of the player - */ - public UUID getUuid() { - return uuid; - } - - /** - * Gets the TBMCPlayer object as a specific plugin player, keeping it's data * - * - * @param p - * Player to get - * @param cl - * The TBMCPlayer subclass - */ - public T asPluginPlayer(Class cl) { - T obj = null; - try { - obj = cl.newInstance(); - ((TBMCPlayer) obj).uuid = uuid; - ((TBMCPlayer) obj).data.putAll(data); - } catch (Exception e) { - e.printStackTrace(); - } - return obj; - } - - private static HashMap LoadedPlayers = new HashMap<>(); - - /** - * This method returns a TBMC player from their name. Calling this method may return an offline player which will load it, therefore it's highly recommended to use {@link #close()} to unload the - * player data. Using try-with-resources may be the easiest way to achieve this. Example: - * - *
-	 * {@code
-	 * try(TBMCPlayer player = getFromName(p))
-	 * {
-	 * 	...
-	 * }
-	 * 
- * - * @param name - * The player's name - * @return The {@link TBMCPlayer} object for the player - */ - public static TBMCPlayer getFromName(String name) { - @SuppressWarnings("deprecation") - OfflinePlayer p = Bukkit.getOfflinePlayer(name); - if (p != null) - return getPlayer(p); - else - return null; - } - - /** - * This method returns a TBMC player from a Bukkit player. Calling this method may return an offline player, therefore it's highly recommended to use {@link #close()} to unload the player data. - * Using try-with-resources may be the easiest way to achieve this. Example: - * - *
-	 * {@code
-	 * try(TBMCPlayer player = getPlayer(p))
-	 * {
-	 * 	...
-	 * }
-	 * 
- * - * @param p - * The Player object - * @return The {@link TBMCPlayer} object for the player - */ - public static TBMCPlayer getPlayer(OfflinePlayer p) { - if (TBMCPlayer.getLoadedPlayers().containsKey(p.getUniqueId())) - return TBMCPlayer.getLoadedPlayers().get(p.getUniqueId()); - else - return TBMCPlayer.loadPlayer(p); - } - - /** - * This method returns a TBMC player from a player UUID. Calling this method may return an offline player, therefore it's highly recommended to use {@link #close()} to unload the player data. - * Using try-with-resources may be the easiest way to achieve this. Example: - * - *
-	 * {@code
-	 * try(TBMCPlayer player = getPlayer(p))
-	 * {
-	 * 	...
-	 * }
-	 * 
- * - * @param p - * The Player object - * @return The {@link TBMCPlayer} object for the player - */ - public static TBMCPlayer getPlayer(UUID uuid) { - if (TBMCPlayer.getLoadedPlayers().containsKey(uuid)) - return TBMCPlayer.getLoadedPlayers().get(uuid); - else - return TBMCPlayer.loadPlayer(Bukkit.getOfflinePlayer(uuid)); - } - - /** - * This is a convenience method for {@link #getPlayer(OfflinePlayer)}.{@link #asPluginPlayer(Class)}. - * - * See those methods for more information. - * - * @param p - * Player to get - * @param cl - * The TBMCPlayer subclass - * @return The player as a subtype of TBMCPlayer - */ - public static T getPlayerAs(OfflinePlayer p, Class cl) { - return getPlayer(p).asPluginPlayer(cl); - } - - /** - * This is a convenience method for {@link #getPlayer(UUID)}.{@link #asPluginPlayer(Class)} - * - * See those methods for more information. - * - * @param uuid - * The UUID of the player to get - * @param cl - * The TBMCPlayer subclass - * @return The player as a subtype of TBMCPlayer - */ - public static T getPlayerAs(UUID uuid, Class cl) { - return getPlayer(uuid).asPluginPlayer(cl); - } - - /** - * Only intended to use from ButtonCore - */ - public static TBMCPlayer loadPlayer(OfflinePlayer p) { - if (getLoadedPlayers().containsKey(p.getUniqueId())) - return getLoadedPlayers().get(p.getUniqueId()); - File file = new File(TBMC_PLAYERS_DIR); - file.mkdirs(); - file = new File(TBMC_PLAYERS_DIR, p.getUniqueId().toString() + ".yml"); - if (!file.exists()) - return addPlayer(p); - else { - final YamlConfiguration yc = new YamlConfiguration(); - try { - yc.load(file); - } catch (Exception e) { - new Exception("Failed to load player data for " + p.getUniqueId(), e).printStackTrace(); - return null; - } - TBMCPlayer player = new TBMCPlayer(); - player.uuid = p.getUniqueId(); - player.data.putAll(yc.getValues(true)); - getLoadedPlayers().put(p.getUniqueId(), player); // Accessing any value requires it to be in the map - Bukkit.getLogger().info("Loaded player: " + player.getPlayerName()); - if (player.getPlayerName() == null) { - player.setPlayerName(p.getName()); - Bukkit.getLogger().info("Player name saved: " + player.getPlayerName()); - } else if (!p.getName().equals(player.getPlayerName())) { - Bukkit.getLogger().info("Renaming " + player.getPlayerName() + " to " + p.getName()); - TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse(); - Resident resident = tu.getResidentMap().get(player.getPlayerName()); - if (resident == null) - Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny."); - else if (tu.getResidentMap().contains(p.getName())) - Bukkit.getLogger().warning("Target resident name is already in use."); // TODO: Handle - else - resident.setName(p.getName()); - player.setPlayerName(p.getName()); - Bukkit.getLogger().info("Renaming done."); - } - - // Load in other plugins - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(yc, player)); - return player; - } - } - - /** - * Only intended to use from ButtonCore - */ - public static TBMCPlayer addPlayer(OfflinePlayer p) { - TBMCPlayer player = new TBMCPlayer(); - player.uuid = p.getUniqueId(); - getLoadedPlayers().put(p.getUniqueId(), player); // Accessing any value requires it to be in the map - player.setPlayerName(p.getName()); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerAddEvent(player)); - savePlayer(player); - return player; - } - - /** - * Only intended to use from ButtonCore - */ - public static void savePlayer(TBMCPlayer player) { - YamlConfiguration yc = new YamlConfiguration(); - for (Entry item : player.data.entrySet()) - yc.set(item.getKey(), item.getValue()); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(yc, player)); - try { - yc.save(TBMC_PLAYERS_DIR + "/" + player.uuid + ".yml"); - } catch (IOException e) { - new Exception("Failed to save player data for " + player.getPlayerName(), e).printStackTrace(); - } - } - - /** - * Only intended to use from ButtonCore - */ - public static void joinPlayer(TBMCPlayer player) { - getLoadedPlayers().put(player.uuid, player); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player)); - } - - /** - * Only intended to use from ButtonCore - */ - public static void quitPlayer(TBMCPlayer player) { - getLoadedPlayers().remove(player.uuid); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); - } - - /** - * By default the player data will only get cleaned from memory when the player quits. Therefore this method must be called when accessing an offline player to clean the player data up. Calling - * this method will have no effect on online players.
- * Therefore, the recommended use is to call it when using {@link #GetPlayer} or use try-with-resources. - */ - @Override - public void close() { - if (!Bukkit.getPlayer(uuid).isOnline()) - getLoadedPlayers().remove(uuid); - } - - public static HashMap getLoadedPlayers() { - return LoadedPlayers; - } - - /** - * Get player information. This method calls the {@link TBMCPlayerGetInfoEvent} to get all the player information across the TBMC plugins. - * - * @param target - * The {@link InfoTarget} to return the info for. - * @return The player information. - */ - public String getInfo(InfoTarget target) { - TBMCPlayerGetInfoEvent event = new TBMCPlayerGetInfoEvent(this, target); - Bukkit.getServer().getPluginManager().callEvent(event); - return event.getResult(); - } - - public enum InfoTarget { - MCHover, MCCommand, Discord - } -} +package buttondevteam.lib; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map.Entry; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.configuration.file.YamlConfiguration; +import com.palmergames.bukkit.towny.Towny; +import com.palmergames.bukkit.towny.object.Resident; +import com.palmergames.bukkit.towny.object.TownyUniverse; + +/** + *

+ * The class for holding data common to all TBMC plugins + *

+ *

+ * Use {@link #asPluginPlayer(Class)} to get plugin-specific data + *

+ * + * @author Norbi + * + */ +public class TBMCPlayer implements AutoCloseable { + private static final String TBMC_PLAYERS_DIR = "TBMC/players"; + + private HashMap data = new HashMap<>(); + + /** + *

+ * Gets a player data entry for the caller plugin returning the desired type.
+ * It will automatically determine the key and the return type.
+ * Usage: + *

+ * + *
+	 * {@code
+	 * public String getPlayerName() {
+	 * 	return getData();
+	 * }
+	 * 
+ * + * @return The value or null if not found + */ + @SuppressWarnings("unchecked") + protected T getData() { + StackTraceElement st = new Exception().getStackTrace()[1]; + String mname = st.getMethodName(); + if (!mname.startsWith("get")) + throw new UnsupportedOperationException("Can only use getData from a getXYZ method"); + Object ret = getLoadedPlayers().get(uuid).data.get(mname.substring("get".length()).toLowerCase()); + if (Integer.class.isAssignableFrom(ret.getClass())) + throw new UnsupportedOperationException("For integers use getIntData()"); + return (T) ret; + } + + /** + * Sets a player data entry based on the caller method
+ * Usage: + *

+ * + *
+	 * {@code
+	 * public String setPlayerName(String value) {
+	 * 	return setData(value);
+	 * }
+	 * 
+ * + * @param value + * The value to set + */ + protected void setData(Object value) { + StackTraceElement st = new Exception().getStackTrace()[1]; + String mname = st.getMethodName(); + if (!mname.startsWith("set")) + throw new UnsupportedOperationException("Can only use setData from a setXYZ method"); + getLoadedPlayers().get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value); + } + + /** + *

+ * Gets a player data entry for the caller plugin returning the desired type, which is an enum
+ * It will automatically determine the key and the return type.
+ * Usage: + *

+ * + *
+	 * {@code
+	 * public String getSomeEnum() {
+	 * 	return getEnumData();
+	 * }
+	 * 
+ * + * @return The value or null if not found + */ + protected > T getEnumData(Class cl) { + StackTraceElement st = new Exception().getStackTrace()[1]; + String mname = st.getMethodName(); + if (!mname.startsWith("get")) + throw new UnsupportedOperationException("Can only use getEnumData from a getXYZ method"); + final String retstr = (String) getLoadedPlayers().get(uuid).data + .get(mname.substring("get".length()).toLowerCase()); + if (retstr != null) + return Enum.valueOf(cl, retstr); + else + return null; + } + + /** + * Sets a player data entry based on the caller method
+ * Usage: + *

+ * + *
+	 * {@code
+	 * public String setSomeEnum(SomeEnum value) {
+	 * 	return setEnumData(value);
+	 * }
+	 * 
+ * + * @param value + * The value to set + */ + protected void setEnumData(Enum value) { + StackTraceElement st = new Exception().getStackTrace()[1]; + String mname = st.getMethodName(); + if (!mname.startsWith("set")) + throw new UnsupportedOperationException("Can only use setEnumData from a setXYZ method"); + getLoadedPlayers().get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value.toString()); + } + + /** + *

+ * Gets a player data entry for the caller plugin returning the desired type, which is a number
+ * It will automatically determine the key and the return type.
+ * Usage: + *

+ * + *
+	 * {@code
+	 * public short getNumber() {
+	 * 	return getIntData();
+	 * }
+	 * 
+ * + * @return The value or null if not found + */ + @SuppressWarnings("unchecked") + protected T getIntData(Class cl) { + StackTraceElement st = new Exception().getStackTrace()[1]; + String mname = st.getMethodName(); + if (!mname.startsWith("get")) + throw new UnsupportedOperationException("Can only use getIntData from a getXYZ method"); + Object obj = getLoadedPlayers().get(uuid).data.get(mname.substring("get".length()).toLowerCase()); + if (!(obj instanceof Integer)) + throw new UnsupportedOperationException("The retrieved object isn't a number: " + obj); + Integer int_ = (Integer) obj; + if (Short.class.isAssignableFrom(cl)) + return (T) (Object) int_.shortValue(); + else + return (T) (Object) int_; + } + + /** + * Sets a player data entry based on the caller method
+ * Usage: + *

+ * + *
+	 * {@code
+	 * public String setNumber(short value) {
+	 * 	return setIntData(value);
+	 * }
+	 * 
+ * + * @param value + * The value to set + */ + protected void setIntData(Number value) { + StackTraceElement st = new Exception().getStackTrace()[1]; + String mname = st.getMethodName(); + if (!mname.startsWith("set")) + throw new UnsupportedOperationException("Can only use setIntData from a setXYZ method"); + getLoadedPlayers().get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value); + } + + /** + * Gets the player's Minecraft name + * + * @return The player's Minecraft name + */ + public String getPlayerName() { + return getData(); + } + + /** + * Sets the player's Minecraft name + * + * @param playerName + * the new name + */ + public void setPlayerName(String playerName) { + setData(playerName); + } + + private UUID uuid; // Do not save it in the file + + /** + * Get the player's UUID + * + * @return The Minecraft UUID of the player + */ + public UUID getUuid() { + return uuid; + } + + /** + * Gets the TBMCPlayer object as a specific plugin player, keeping it's data * + * + * @param p + * Player to get + * @param cl + * The TBMCPlayer subclass + */ + public T asPluginPlayer(Class cl) { + T obj = null; + try { + obj = cl.newInstance(); + ((TBMCPlayer) obj).uuid = uuid; + ((TBMCPlayer) obj).data.putAll(data); + } catch (Exception e) { + e.printStackTrace(); + } + return obj; + } + + private static HashMap LoadedPlayers = new HashMap<>(); + + /** + * This method returns a TBMC player from their name. Calling this method may return an offline player which will load it, therefore it's highly recommended to use {@link #close()} to unload the + * player data. Using try-with-resources may be the easiest way to achieve this. Example: + * + *
+	 * {@code
+	 * try(TBMCPlayer player = getFromName(p))
+	 * {
+	 * 	...
+	 * }
+	 * 
+ * + * @param name + * The player's name + * @return The {@link TBMCPlayer} object for the player + */ + public static TBMCPlayer getFromName(String name) { + @SuppressWarnings("deprecation") + OfflinePlayer p = Bukkit.getOfflinePlayer(name); + if (p != null) + return getPlayer(p); + else + return null; + } + + /** + * This method returns a TBMC player from a Bukkit player. Calling this method may return an offline player, therefore it's highly recommended to use {@link #close()} to unload the player data. + * Using try-with-resources may be the easiest way to achieve this. Example: + * + *
+	 * {@code
+	 * try(TBMCPlayer player = getPlayer(p))
+	 * {
+	 * 	...
+	 * }
+	 * 
+ * + * @param p + * The Player object + * @return The {@link TBMCPlayer} object for the player + */ + public static TBMCPlayer getPlayer(OfflinePlayer p) { + if (TBMCPlayer.getLoadedPlayers().containsKey(p.getUniqueId())) + return TBMCPlayer.getLoadedPlayers().get(p.getUniqueId()); + else + return TBMCPlayer.loadPlayer(p); + } + + /** + * This method returns a TBMC player from a player UUID. Calling this method may return an offline player, therefore it's highly recommended to use {@link #close()} to unload the player data. + * Using try-with-resources may be the easiest way to achieve this. Example: + * + *
+	 * {@code
+	 * try(TBMCPlayer player = getPlayer(p))
+	 * {
+	 * 	...
+	 * }
+	 * 
+ * + * @param p + * The Player object + * @return The {@link TBMCPlayer} object for the player + */ + public static TBMCPlayer getPlayer(UUID uuid) { + if (TBMCPlayer.getLoadedPlayers().containsKey(uuid)) + return TBMCPlayer.getLoadedPlayers().get(uuid); + else + return TBMCPlayer.loadPlayer(Bukkit.getOfflinePlayer(uuid)); + } + + /** + * This is a convenience method for {@link #getPlayer(OfflinePlayer)}.{@link #asPluginPlayer(Class)}. + * + * See those methods for more information. + * + * @param p + * Player to get + * @param cl + * The TBMCPlayer subclass + * @return The player as a subtype of TBMCPlayer + */ + public static T getPlayerAs(OfflinePlayer p, Class cl) { + return getPlayer(p).asPluginPlayer(cl); + } + + /** + * This is a convenience method for {@link #getPlayer(UUID)}.{@link #asPluginPlayer(Class)} + * + * See those methods for more information. + * + * @param uuid + * The UUID of the player to get + * @param cl + * The TBMCPlayer subclass + * @return The player as a subtype of TBMCPlayer + */ + public static T getPlayerAs(UUID uuid, Class cl) { + return getPlayer(uuid).asPluginPlayer(cl); + } + + /** + * Only intended to use from ButtonCore + */ + public static TBMCPlayer loadPlayer(OfflinePlayer p) { + if (getLoadedPlayers().containsKey(p.getUniqueId())) + return getLoadedPlayers().get(p.getUniqueId()); + File file = new File(TBMC_PLAYERS_DIR); + file.mkdirs(); + file = new File(TBMC_PLAYERS_DIR, p.getUniqueId().toString() + ".yml"); + if (!file.exists()) + return addPlayer(p); + else { + final YamlConfiguration yc = new YamlConfiguration(); + try { + yc.load(file); + } catch (Exception e) { + new Exception("Failed to load player data for " + p.getUniqueId(), e).printStackTrace(); + return null; + } + TBMCPlayer player = new TBMCPlayer(); + player.uuid = p.getUniqueId(); + player.data.putAll(yc.getValues(true)); + getLoadedPlayers().put(p.getUniqueId(), player); // Accessing any value requires it to be in the map + Bukkit.getLogger().info("Loaded player: " + player.getPlayerName()); + if (player.getPlayerName() == null) { + player.setPlayerName(p.getName()); + Bukkit.getLogger().info("Player name saved: " + player.getPlayerName()); + } else if (!p.getName().equals(player.getPlayerName())) { + Bukkit.getLogger().info("Renaming " + player.getPlayerName() + " to " + p.getName()); + TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse(); + Resident resident = tu.getResidentMap().get(player.getPlayerName()); + if (resident == null) + Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny."); + else if (tu.getResidentMap().contains(p.getName())) + Bukkit.getLogger().warning("Target resident name is already in use."); // TODO: Handle + else + resident.setName(p.getName()); + player.setPlayerName(p.getName()); + Bukkit.getLogger().info("Renaming done."); + } + + // Load in other plugins + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(yc, player)); + return player; + } + } + + /** + * Only intended to use from ButtonCore + */ + public static TBMCPlayer addPlayer(OfflinePlayer p) { + TBMCPlayer player = new TBMCPlayer(); + player.uuid = p.getUniqueId(); + getLoadedPlayers().put(p.getUniqueId(), player); // Accessing any value requires it to be in the map + player.setPlayerName(p.getName()); + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerAddEvent(player)); + savePlayer(player); + return player; + } + + /** + * Only intended to use from ButtonCore + */ + public static void savePlayer(TBMCPlayer player) { + YamlConfiguration yc = new YamlConfiguration(); + for (Entry item : player.data.entrySet()) + yc.set(item.getKey(), item.getValue()); + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(yc, player)); + try { + yc.save(TBMC_PLAYERS_DIR + "/" + player.uuid + ".yml"); + } catch (IOException e) { + new Exception("Failed to save player data for " + player.getPlayerName(), e).printStackTrace(); + } + } + + /** + * Only intended to use from ButtonCore + */ + public static void joinPlayer(TBMCPlayer player) { + getLoadedPlayers().put(player.uuid, player); + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player)); + } + + /** + * Only intended to use from ButtonCore + */ + public static void quitPlayer(TBMCPlayer player) { + getLoadedPlayers().remove(player.uuid); + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); + } + + /** + * By default the player data will only get cleaned from memory when the player quits. Therefore this method must be called when accessing an offline player to clean the player data up. Calling + * this method will have no effect on online players.
+ * Therefore, the recommended use is to call it when using {@link #GetPlayer} or use try-with-resources. + */ + @Override + public void close() { + if (!Bukkit.getPlayer(uuid).isOnline()) + getLoadedPlayers().remove(uuid); + } + + public static HashMap getLoadedPlayers() { + return LoadedPlayers; + } + + /** + * Get player information. This method calls the {@link TBMCPlayerGetInfoEvent} to get all the player information across the TBMC plugins. + * + * @param target + * The {@link InfoTarget} to return the info for. + * @return The player information. + */ + public String getInfo(InfoTarget target) { + TBMCPlayerGetInfoEvent event = new TBMCPlayerGetInfoEvent(this, target); + Bukkit.getServer().getPluginManager().callEvent(event); + return event.getResult(); + } + + public enum InfoTarget { + MCHover, MCCommand, Discord + } +} diff --git a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerAddEvent.java b/src/main/java/buttondevteam/lib/TBMCPlayerAddEvent.java similarity index 94% rename from ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerAddEvent.java rename to src/main/java/buttondevteam/lib/TBMCPlayerAddEvent.java index ae576ec..2a975dc 100644 --- a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerAddEvent.java +++ b/src/main/java/buttondevteam/lib/TBMCPlayerAddEvent.java @@ -1,36 +1,36 @@ -package buttondevteam.lib; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -/** - *

- * This event gets called when a new player joins. After this event, the - * {@link TBMCPlayerSaveEvent} will be called. - *

- * - * @author Norbi - * - */ -public class TBMCPlayerAddEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private TBMCPlayer player; - - public TBMCPlayerAddEvent(TBMCPlayer player) { - this.player = player; - } - - public TBMCPlayer GetPlayer() { - return player; - } - - @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 when a new player joins. After this event, the + * {@link TBMCPlayerSaveEvent} will be called. + *

+ * + * @author Norbi + * + */ +public class TBMCPlayerAddEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private TBMCPlayer player; + + public TBMCPlayerAddEvent(TBMCPlayer player) { + this.player = player; + } + + public TBMCPlayer GetPlayer() { + return player; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerGetInfoEvent.java b/src/main/java/buttondevteam/lib/TBMCPlayerGetInfoEvent.java similarity index 95% rename from ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerGetInfoEvent.java rename to src/main/java/buttondevteam/lib/TBMCPlayerGetInfoEvent.java index 6dac56d..ded8f2e 100644 --- a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerGetInfoEvent.java +++ b/src/main/java/buttondevteam/lib/TBMCPlayerGetInfoEvent.java @@ -1,73 +1,73 @@ -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 when player information is requested. It can be used to give more per-plugin information about a player. - *

- * - * @author Norbi - * - */ -public class TBMCPlayerGetInfoEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private TBMCPlayer player; - private List infolines; - private TBMCPlayer.InfoTarget target; - - TBMCPlayerGetInfoEvent(TBMCPlayer player, TBMCPlayer.InfoTarget target) { - this.player = player; - infolines = new ArrayList<>(); - this.target = target; - } - - /** - * Get the {@link TBMCPlayer} object - * - * @return A player object - */ - public TBMCPlayer getPlayer() { - return player; - } - - /** - * Add a line to the information text. The line should be in the format of key: value . - * - * @param infoline - * The line to add - */ - public void addInfo(String infoline) { - infolines.add(infoline); - } - - /** - * Get the {@link InfoTarget} we want the information for. Use this to format the returned string. - * - * @return The target of the information. - */ - public TBMCPlayer.InfoTarget getTarget() { - return target; - } - - String getResult() { - return infolines.stream().collect(Collectors.joining("\n")); - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} +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 when player information is requested. It can be used to give more per-plugin information about a player. + *

+ * + * @author Norbi + * + */ +public class TBMCPlayerGetInfoEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private TBMCPlayer player; + private List infolines; + private TBMCPlayer.InfoTarget target; + + TBMCPlayerGetInfoEvent(TBMCPlayer player, TBMCPlayer.InfoTarget target) { + this.player = player; + infolines = new ArrayList<>(); + this.target = target; + } + + /** + * Get the {@link TBMCPlayer} object + * + * @return A player object + */ + public TBMCPlayer getPlayer() { + return player; + } + + /** + * Add a line to the information text. The line should be in the format of key: value . + * + * @param infoline + * The line to add + */ + public void addInfo(String infoline) { + infolines.add(infoline); + } + + /** + * Get the {@link InfoTarget} we want the information for. Use this to format the returned string. + * + * @return The target of the information. + */ + public TBMCPlayer.InfoTarget getTarget() { + return target; + } + + String getResult() { + return infolines.stream().collect(Collectors.joining("\n")); + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerJoinEvent.java b/src/main/java/buttondevteam/lib/TBMCPlayerJoinEvent.java similarity index 94% rename from ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerJoinEvent.java rename to src/main/java/buttondevteam/lib/TBMCPlayerJoinEvent.java index fbcc3d0..1cce842 100644 --- a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerJoinEvent.java +++ b/src/main/java/buttondevteam/lib/TBMCPlayerJoinEvent.java @@ -1,27 +1,27 @@ -package buttondevteam.lib; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class TBMCPlayerJoinEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private TBMCPlayer player; - - public TBMCPlayerJoinEvent(TBMCPlayer player) { - this.player = player; - } - - public TBMCPlayer GetPlayer() { - return player; - } - - @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; + +public class TBMCPlayerJoinEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private TBMCPlayer player; + + public TBMCPlayerJoinEvent(TBMCPlayer player) { + this.player = player; + } + + public TBMCPlayer GetPlayer() { + return player; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerLoadEvent.java b/src/main/java/buttondevteam/lib/TBMCPlayerLoadEvent.java similarity index 95% rename from ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerLoadEvent.java rename to src/main/java/buttondevteam/lib/TBMCPlayerLoadEvent.java index 2d017ce..08c6a3d 100644 --- a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerLoadEvent.java +++ b/src/main/java/buttondevteam/lib/TBMCPlayerLoadEvent.java @@ -1,34 +1,34 @@ -package buttondevteam.lib; - -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class TBMCPlayerLoadEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private YamlConfiguration yaml; - private TBMCPlayer player; - - public TBMCPlayerLoadEvent(YamlConfiguration yaml, TBMCPlayer player) { - this.yaml = yaml; - this.player = player; - } - - public YamlConfiguration GetPlayerConfig() { - return yaml; - } - - public TBMCPlayer GetPlayer() { - return player; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} +package buttondevteam.lib; + +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class TBMCPlayerLoadEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private YamlConfiguration yaml; + private TBMCPlayer player; + + public TBMCPlayerLoadEvent(YamlConfiguration yaml, TBMCPlayer player) { + this.yaml = yaml; + this.player = player; + } + + public YamlConfiguration GetPlayerConfig() { + return yaml; + } + + public TBMCPlayer GetPlayer() { + return player; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerQuitEvent.java b/src/main/java/buttondevteam/lib/TBMCPlayerQuitEvent.java similarity index 94% rename from ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerQuitEvent.java rename to src/main/java/buttondevteam/lib/TBMCPlayerQuitEvent.java index 3835605..90bd4f7 100644 --- a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerQuitEvent.java +++ b/src/main/java/buttondevteam/lib/TBMCPlayerQuitEvent.java @@ -1,27 +1,27 @@ -package buttondevteam.lib; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class TBMCPlayerQuitEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private TBMCPlayer player; - - public TBMCPlayerQuitEvent(TBMCPlayer player) { - this.player = player; - } - - public TBMCPlayer GetPlayer() { - return player; - } - - @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; + +public class TBMCPlayerQuitEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private TBMCPlayer player; + + public TBMCPlayerQuitEvent(TBMCPlayer player) { + this.player = player; + } + + public TBMCPlayer GetPlayer() { + return player; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerSaveEvent.java b/src/main/java/buttondevteam/lib/TBMCPlayerSaveEvent.java similarity index 95% rename from ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerSaveEvent.java rename to src/main/java/buttondevteam/lib/TBMCPlayerSaveEvent.java index 114aea0..931c4c1 100644 --- a/ButtonLib/src/main/java/buttondevteam/lib/TBMCPlayerSaveEvent.java +++ b/src/main/java/buttondevteam/lib/TBMCPlayerSaveEvent.java @@ -1,34 +1,34 @@ -package buttondevteam.lib; - -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class TBMCPlayerSaveEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private YamlConfiguration yaml; - private TBMCPlayer player; - - public TBMCPlayerSaveEvent(YamlConfiguration yaml, TBMCPlayer player) { - this.yaml = yaml; - this.player = player; - } - - public YamlConfiguration GetPlayerConfig() { - return yaml; - } - - public TBMCPlayer GetPlayer() { - return player; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} +package buttondevteam.lib; + +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class TBMCPlayerSaveEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private YamlConfiguration yaml; + private TBMCPlayer player; + + public TBMCPlayerSaveEvent(YamlConfiguration yaml, TBMCPlayer player) { + this.yaml = yaml; + this.player = player; + } + + public YamlConfiguration GetPlayerConfig() { + return yaml; + } + + public TBMCPlayer GetPlayer() { + return player; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/ButtonLib/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java similarity index 97% rename from ButtonLib/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java rename to src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java index c1804d9..d07e875 100644 --- a/ButtonLib/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 + } +} diff --git a/ButtonLib/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java b/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java similarity index 95% rename from ButtonLib/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java rename to src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java index d1d4f3d..0151324 100644 --- a/ButtonLib/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java +++ b/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java @@ -1,26 +1,26 @@ -package buttondevteam.lib.chat; - -import org.bukkit.command.CommandSender; -import org.bukkit.plugin.Plugin; - -public abstract class TBMCCommandBase { - - public TBMCCommandBase() { - } - - public abstract String[] GetHelpText(String alias); - - public abstract boolean OnCommand(CommandSender sender, String alias, String[] args); - - public abstract String GetCommandPath(); - - public abstract boolean GetPlayerOnly(); - - public abstract boolean GetModOnly(); - - Plugin plugin; // Used By TBMCChatAPI - - public Plugin getPlugin() { // Used by CommandCaller (ButtonChat) - return plugin; - } -} +package buttondevteam.lib.chat; + +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.Plugin; + +public abstract class TBMCCommandBase { + + public TBMCCommandBase() { + } + + public abstract String[] GetHelpText(String alias); + + public abstract boolean OnCommand(CommandSender sender, String alias, String[] args); + + public abstract String GetCommandPath(); + + public abstract boolean GetPlayerOnly(); + + public abstract boolean GetModOnly(); + + Plugin plugin; // Used By TBMCChatAPI + + public Plugin getPlugin() { // Used by CommandCaller (ButtonChat) + return plugin; + } +}