From caa63f7912a0d94f7111dd3c415fbd62b3615eec Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 28 Oct 2016 21:42:34 +0200 Subject: [PATCH] Moved some command stuff It took a while --- .../java/buttondevteam/chat/PluginMain.java | 5 +- .../java/buttondevteam/chat/TBMCChatAPI.java | 56 ----------------- .../chat/commands/ChatonlyCommand.java | 1 + .../chat/commands/CommandCaller.java | 61 +++++-------------- .../chat/commands/MWikiCommand.java | 2 + .../chat/commands/OOCCommand.java | 1 + .../chat/commands/TBMCCommandBase.java | 19 ------ .../chat/commands/UnlolCommand.java | 1 + .../chat/commands/YeehawCommand.java | 1 + .../appendtext/AppendTextCommandBase.java | 2 +- .../chat/commands/ucmds/HelpCommand.java | 8 +-- .../chat/commands/ucmds/UCommand.java | 2 +- .../chat/commands/ucmds/UCommandBase.java | 2 +- .../ucmds/announce/AnnounceCommand.java | 2 +- 14 files changed, 32 insertions(+), 131 deletions(-) delete mode 100644 src/main/java/buttondevteam/chat/TBMCChatAPI.java delete mode 100644 src/main/java/buttondevteam/chat/commands/TBMCCommandBase.java diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index 80a59d2..9e3d783 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -16,7 +16,9 @@ import org.htmlcleaner.HtmlCleaner; import org.htmlcleaner.TagNode; import buttondevteam.chat.commands.CommandCaller; +import buttondevteam.chat.commands.YeehawCommand; import buttondevteam.lib.TBMCCoreAPI; +import buttondevteam.lib.chat.TBMCChatAPI; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -66,7 +68,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. Instance = this; getServer().getPluginManager().registerEvents(new PlayerListener(), this); - CommandCaller.RegisterChatCommands(this); + TBMCChatAPI.AddCommands(this, YeehawCommand.class); Console = this.getServer().getConsoleSender(); LoadFiles(false); @@ -101,6 +103,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. }; t = new Thread(r); t.start(); + Bukkit.getScheduler().runTaskLater(this, () -> CommandCaller.RegisterCommands(), 0); } public Boolean stop = false; diff --git a/src/main/java/buttondevteam/chat/TBMCChatAPI.java b/src/main/java/buttondevteam/chat/TBMCChatAPI.java deleted file mode 100644 index bbb1e2b..0000000 --- a/src/main/java/buttondevteam/chat/TBMCChatAPI.java +++ /dev/null @@ -1,56 +0,0 @@ -package buttondevteam.chat; - -import org.bukkit.plugin.java.JavaPlugin; - -import buttondevteam.chat.commands.CommandCaller; -import buttondevteam.chat.commands.TBMCCommandBase; -import buttondevteam.chat.commands.ucmds.admin.PlayerInfoCommand; - -public class TBMCChatAPI { - - /** - *

- * 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. - *

- * - * @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 void AddCommands(JavaPlugin plugin, Class acmdclass) { - CommandCaller.AddCommands(plugin, acmdclass); // TODO: Make it scan for all "buttondevteam" packages - } - - /** - *

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

- * - * @param player - * @param infoline - */ - public void AddPlayerInfoForMods(ChatPlayer 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(ChatPlayer player, String infoline) { - // TODO - } -} diff --git a/src/main/java/buttondevteam/chat/commands/ChatonlyCommand.java b/src/main/java/buttondevteam/chat/commands/ChatonlyCommand.java index 30e3d7f..0536ef6 100644 --- a/src/main/java/buttondevteam/chat/commands/ChatonlyCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ChatonlyCommand.java @@ -5,6 +5,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import buttondevteam.chat.ChatPlayer; +import buttondevteam.lib.chat.TBMCCommandBase; public final class ChatonlyCommand extends TBMCCommandBase { diff --git a/src/main/java/buttondevteam/chat/commands/CommandCaller.java b/src/main/java/buttondevteam/chat/commands/CommandCaller.java index 2bb04af..72ba5fb 100644 --- a/src/main/java/buttondevteam/chat/commands/CommandCaller.java +++ b/src/main/java/buttondevteam/chat/commands/CommandCaller.java @@ -1,11 +1,7 @@ package buttondevteam.chat.commands; -import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; -import java.util.Set; - import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -13,58 +9,29 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; 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.chat.PluginMain; import buttondevteam.discordplugin.TBMCDiscordAPI; +import buttondevteam.lib.chat.TBMCChatAPI; +import buttondevteam.lib.chat.TBMCCommandBase; public class CommandCaller implements CommandExecutor { private CommandCaller() { } - private static HashMap commands = new HashMap(); - - public static HashMap GetCommands() { - return commands; - } - private static CommandCaller instance; - public static void RegisterChatCommands(PluginMain plugin) { - AddCommands(plugin, TBMCCommandBase.class); - } - - public static void AddCommands(JavaPlugin plugin, Class acmdclass) { - plugin.getLogger().info("Registering commands for " + plugin.getName()); + public static void RegisterCommands() { if (instance == null) instance = new CommandCaller(); - 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(); - commands.put(c.GetCommandPath(), c); - if (!c.GetCommandPath().contains("/")) // Top-level command - { - PluginCommand pc = plugin.getCommand(c.GetCommandPath()); - if (pc == null) - new Exception("Can't find top-level command: " + c.GetCommandPath()).printStackTrace(); - else - pc.setExecutor(instance); - } - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); + for (TBMCCommandBase c : TBMCChatAPI.GetCommands().values()) { + if (!c.GetCommandPath().contains("/")) // Top-level command + { + PluginCommand pc = ((JavaPlugin) c.getPlugin()).getCommand(c.GetCommandPath()); + if (pc == null) + new Exception("Can't find top-level command: " + c.GetCommandPath()).printStackTrace(); + else + pc.setExecutor(instance); } } } @@ -74,12 +41,12 @@ public class CommandCaller implements CommandExecutor { String path = command.getName(); for (String arg : args) path += "/" + arg; - TBMCCommandBase cmd = commands.get(path); + TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path); int argc = 0; while (cmd == null && path.contains("/")) { path = path.substring(0, path.lastIndexOf('/')); argc++; - cmd = commands.get(path); + cmd = TBMCChatAPI.GetCommands().get(path); } if (cmd == null) { sender.sendMessage("§cInternal error: Command not registered to CommandCaller"); @@ -109,7 +76,7 @@ public class CommandCaller implements CommandExecutor { public static String[] GetSubCommands(TBMCCommandBase command) { ArrayList cmds = new ArrayList(); cmds.add("§6---- Subcommands ----"); - for (TBMCCommandBase cmd : CommandCaller.GetCommands().values()) { + for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values()) { if (cmd.GetCommandPath().startsWith(command.GetCommandPath() + "/")) { int ind = cmd.GetCommandPath().indexOf('/', command.GetCommandPath().length() + 2); if (ind >= 0) diff --git a/src/main/java/buttondevteam/chat/commands/MWikiCommand.java b/src/main/java/buttondevteam/chat/commands/MWikiCommand.java index 8139889..49406d7 100644 --- a/src/main/java/buttondevteam/chat/commands/MWikiCommand.java +++ b/src/main/java/buttondevteam/chat/commands/MWikiCommand.java @@ -2,6 +2,8 @@ package buttondevteam.chat.commands; import org.bukkit.command.CommandSender; +import buttondevteam.lib.chat.TBMCCommandBase; + public class MWikiCommand extends TBMCCommandBase { @Override diff --git a/src/main/java/buttondevteam/chat/commands/OOCCommand.java b/src/main/java/buttondevteam/chat/commands/OOCCommand.java index d3292ba..0950427 100644 --- a/src/main/java/buttondevteam/chat/commands/OOCCommand.java +++ b/src/main/java/buttondevteam/chat/commands/OOCCommand.java @@ -4,6 +4,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import buttondevteam.chat.ChatPlayer; +import buttondevteam.lib.chat.TBMCCommandBase; public final class OOCCommand extends TBMCCommandBase { diff --git a/src/main/java/buttondevteam/chat/commands/TBMCCommandBase.java b/src/main/java/buttondevteam/chat/commands/TBMCCommandBase.java deleted file mode 100644 index a6040b9..0000000 --- a/src/main/java/buttondevteam/chat/commands/TBMCCommandBase.java +++ /dev/null @@ -1,19 +0,0 @@ -package buttondevteam.chat.commands; - -import org.bukkit.command.CommandSender; - -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(); -} diff --git a/src/main/java/buttondevteam/chat/commands/UnlolCommand.java b/src/main/java/buttondevteam/chat/commands/UnlolCommand.java index 28df0c3..fa79fd2 100644 --- a/src/main/java/buttondevteam/chat/commands/UnlolCommand.java +++ b/src/main/java/buttondevteam/chat/commands/UnlolCommand.java @@ -7,6 +7,7 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import buttondevteam.chat.PluginMain; +import buttondevteam.lib.chat.TBMCCommandBase; public final class UnlolCommand extends TBMCCommandBase { diff --git a/src/main/java/buttondevteam/chat/commands/YeehawCommand.java b/src/main/java/buttondevteam/chat/commands/YeehawCommand.java index 7a32336..2abecee 100644 --- a/src/main/java/buttondevteam/chat/commands/YeehawCommand.java +++ b/src/main/java/buttondevteam/chat/commands/YeehawCommand.java @@ -4,6 +4,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import buttondevteam.chat.PluginMain; +import buttondevteam.lib.chat.TBMCCommandBase; public class YeehawCommand extends TBMCCommandBase { diff --git a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java index 4929278..497058c 100644 --- a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java +++ b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java @@ -3,7 +3,7 @@ package buttondevteam.chat.commands.appendtext; import org.bukkit.command.CommandSender; import buttondevteam.chat.ChatProcessing; -import buttondevteam.chat.commands.TBMCCommandBase; +import buttondevteam.lib.chat.TBMCCommandBase; public abstract class AppendTextCommandBase extends TBMCCommandBase { diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/HelpCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/HelpCommand.java index fb51cd7..d7205af 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/HelpCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/HelpCommand.java @@ -4,8 +4,8 @@ import java.util.ArrayList; import org.bukkit.command.CommandSender; -import buttondevteam.chat.commands.CommandCaller; -import buttondevteam.chat.commands.TBMCCommandBase; +import buttondevteam.lib.chat.TBMCChatAPI; +import buttondevteam.lib.chat.TBMCCommandBase; public final class HelpCommand extends UCommandBase { @@ -32,7 +32,7 @@ public final class HelpCommand extends UCommandBase { else if (args[0].equalsIgnoreCase("commands")) { ArrayList text = new ArrayList(); text.add("§6---- Command list ----"); - for (TBMCCommandBase cmd : CommandCaller.GetCommands().values()) + for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values()) if (!cmd.GetCommandPath().contains("/")) text.add("/" + cmd.GetCommandPath()); sender.sendMessage(text.toArray(new String[text.size()])); @@ -55,7 +55,7 @@ public final class HelpCommand extends UCommandBase { String path = args[0]; for (int i = 1; i < args.length; i++) path += "/" + args[i]; - TBMCCommandBase cmd = CommandCaller.GetCommands().get(path); + TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path); if (cmd == null) sender.sendMessage(new String[] { "§cError: Command not found: " + path.replace('/', ' '), "Usage example: /u accept --> /u help u accept" }); diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/UCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/UCommand.java index 2b720ba..b750efb 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/UCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/UCommand.java @@ -3,7 +3,7 @@ package buttondevteam.chat.commands.ucmds; import org.bukkit.command.CommandSender; import buttondevteam.chat.commands.CommandCaller; -import buttondevteam.chat.commands.TBMCCommandBase; +import buttondevteam.lib.chat.TBMCCommandBase; public final class UCommand extends TBMCCommandBase { diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/UCommandBase.java b/src/main/java/buttondevteam/chat/commands/ucmds/UCommandBase.java index 271c924..3dc0d47 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/UCommandBase.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/UCommandBase.java @@ -1,6 +1,6 @@ package buttondevteam.chat.commands.ucmds; -import buttondevteam.chat.commands.TBMCCommandBase; +import buttondevteam.lib.chat.TBMCCommandBase; public abstract class UCommandBase extends TBMCCommandBase { diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/announce/AnnounceCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/announce/AnnounceCommand.java index 0a791ee..8bfc7ac 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/announce/AnnounceCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/announce/AnnounceCommand.java @@ -2,8 +2,8 @@ package buttondevteam.chat.commands.ucmds.announce; import org.bukkit.command.CommandSender; -import buttondevteam.chat.commands.CommandCaller; import buttondevteam.chat.commands.ucmds.UCommandBase; +import buttondevteam.chat.commands.CommandCaller; public class AnnounceCommand extends UCommandBase {