From b0c145af962085bdc6cf0e9bbe5578cd9f69c23a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 28 Oct 2016 21:23:39 +0200 Subject: [PATCH] Moved command stuff to Lib to avoid depedency loops --- pom.xml | 6 ++ .../buttondevteam/lib/chat/TBMCChatAPI.java | 84 +++++++++++++++++++ .../lib/chat/TBMCCommandBase.java | 26 ++++++ 3 files changed, 116 insertions(+) create mode 100644 src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java create mode 100644 src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java diff --git a/pom.xml b/pom.xml index e3e14bb..b2051ac 100644 --- a/pom.xml +++ b/pom.xml @@ -92,6 +92,12 @@ Towny master-SNAPSHOT + + + org.reflections + reflections + 0.9.10 + TBMCPlugins diff --git a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java new file mode 100644 index 0000000..e5571df --- /dev/null +++ b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java @@ -0,0 +1,84 @@ +package buttondevteam.lib.chat; + +import java.lang.reflect.Modifier; +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; + } + + /** + *

+ * 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/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java b/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java new file mode 100644 index 0000000..d1d4f3d --- /dev/null +++ b/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java @@ -0,0 +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; + } +}