From 2d7447a46f0b949ca57a62aad093a619d5f1aa7c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 24 Jun 2016 19:02:45 +0200 Subject: [PATCH] New command system is getting ready --- pom.xml | 127 +++++++++--------- .../commands/CommandCaller.java | 76 +++++++++++ .../commands/TBMCCommandBase.java | 42 +----- .../commands/ucmds/UCommand.java | 6 +- .../commands/ucmds/UCommandBase.java | 19 +-- 5 files changed, 150 insertions(+), 120 deletions(-) create mode 100644 src/io/github/norbipeti/thebuttonmcchat/commands/CommandCaller.java diff --git a/pom.xml b/pom.xml index 25bc451..d6b51a7 100644 --- a/pom.xml +++ b/pom.xml @@ -1,61 +1,68 @@ - - 4.0.0 - TheButtonMCPlugin - 0.0.1-SNAPSHOT - The Button Minecraft Plugin - The Button Minecraft Plugin - - src - - - src - - **/*.java - - - - - - maven-compiler-plugin - 3.3 - - 1.7 - 1.7 - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.2 - - - package - - shade - - - - - classworlds:classworlds - junit:junit - jmock:* - *:xml-apis - org.apache.maven:lib:tests - log4j:log4j:jar: - - - - - - - - - tk.sznp - - - spigot-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - + + 4.0.0 + TheButtonMCPlugin + 0.0.1-SNAPSHOT + The Button Minecraft Plugin + The Button Minecraft Plugin + + src + + + src + + **/*.java + + + + + + maven-compiler-plugin + 3.3 + + 1.7 + 1.7 + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4.2 + + + package + + shade + + + + + classworlds:classworlds + junit:junit + jmock:* + *:xml-apis + org.apache.maven:lib:tests + log4j:log4j:jar: + + + + + + + + + tk.sznp + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + + org.reflections + reflections + 0.9.10 + + \ No newline at end of file diff --git a/src/io/github/norbipeti/thebuttonmcchat/commands/CommandCaller.java b/src/io/github/norbipeti/thebuttonmcchat/commands/CommandCaller.java new file mode 100644 index 0000000..6dbc5d3 --- /dev/null +++ b/src/io/github/norbipeti/thebuttonmcchat/commands/CommandCaller.java @@ -0,0 +1,76 @@ +package io.github.norbipeti.thebuttonmcchat.commands; + +import io.github.norbipeti.thebuttonmcchat.PluginMain; + +import java.util.HashMap; +import java.util.Set; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.reflections.Reflections; + +public class CommandCaller implements CommandExecutor { + private CommandCaller() { + } + + private static HashMap commands = new HashMap(); + private static HashMap subcommands = new HashMap(); + + public static void RegisterCommands(PluginMain plugin) { + System.out.println("Registering commands..."); + Reflections rf = new Reflections( + "io.github.norbipeti.thebuttonmcchat.commands"); + Set> cmds = rf + .getSubTypesOf(TBMCCommandBase.class); + for (Class cmd : cmds) { + try { + TBMCCommandBase c = cmd.newInstance(); + commands.put(c.GetCommandName(), c); + plugin.getCommand(c.GetCommandName()).setExecutor(c); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + System.out.println("Registering subcommands..."); + Set> subcmds = rf + .getSubTypesOf(TBMCSubCommandBase.class); + for (Class subcmd : subcmds) { + try { + TBMCSubCommandBase sc = subcmd.newInstance(); + subcommands.put(sc.GetCommandPath(), sc); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + System.out.println("Done registering"); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, + String alias, String[] args) { + // TODO: Test if path exists, if not, + // go up one level, and + // finally fallback to args.length==0 + String path = command.getName(); + for (String arg : args) + path += "/" + arg; + TBMCSubCommandBase cmd = subcommands.get(path); + while (cmd == null && path.contains("/")) + path = path.substring(0, path.indexOf('/')); + if (cmd == null) { + sender.sendMessage("§cInternal error: Subcommand not registered to CommandCaller"); + if (sender != Bukkit.getConsoleSender()) + Bukkit.getConsoleSender() + .sendMessage( + "§cInternal error: Subcommand not registered to CommandCaller"); + return true; + } + return true; + } +} diff --git a/src/io/github/norbipeti/thebuttonmcchat/commands/TBMCCommandBase.java b/src/io/github/norbipeti/thebuttonmcchat/commands/TBMCCommandBase.java index 1615294..ea94901 100644 --- a/src/io/github/norbipeti/thebuttonmcchat/commands/TBMCCommandBase.java +++ b/src/io/github/norbipeti/thebuttonmcchat/commands/TBMCCommandBase.java @@ -13,47 +13,9 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -public abstract class TBMCCommandBase implements CommandExecutor { - - public static void RegisterCommands(PluginMain plugin) { - TBMCCommandBase cmd = new UCommand(); - plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd); - cmd = new OOCCommand(); - plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd); - cmd = new UnlolCommand(); - plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd); - cmd = new MWikiCommand(); - plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd); - cmd = new TableflipCommand(); - plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd); - cmd = new UnflipCommand(); - plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd); - cmd = new ChatonlyCommand(); - plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd); - cmd = new ShrugCommand(); - plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd); - } - - private static HashMap commands = new HashMap(); - - public static HashMap GetCommands() { - return commands; - } +public abstract class TBMCCommandBase { public TBMCCommandBase() { - commands.put(GetCommandName(), this); - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String alias, - String[] args) { - if (GetPlayerOnly() && !(sender instanceof Player)) { - sender.sendMessage("§cError: You must be a player to use this command."); - return true; - } - if (!OnCommand(sender, alias, args)) - sender.sendMessage(GetHelpText(alias)); - return true; } public abstract String[] GetHelpText(String alias); @@ -61,7 +23,7 @@ public abstract class TBMCCommandBase implements CommandExecutor { public abstract boolean OnCommand(CommandSender sender, String alias, String[] args); - public abstract String GetCommandName(); + public abstract String GetCommandPath(); public abstract boolean GetPlayerOnly(); } diff --git a/src/io/github/norbipeti/thebuttonmcchat/commands/ucmds/UCommand.java b/src/io/github/norbipeti/thebuttonmcchat/commands/ucmds/UCommand.java index 2377cbe..8901839 100644 --- a/src/io/github/norbipeti/thebuttonmcchat/commands/ucmds/UCommand.java +++ b/src/io/github/norbipeti/thebuttonmcchat/commands/ucmds/UCommand.java @@ -14,11 +14,7 @@ public final class UCommand extends UCommandBase { @Override public boolean OnUCommand(CommandSender sender, String alias, String[] args) { - if (args.length == 0) - return false; // TODO: Forward call to the correct handler - if (!TBMCCommandBase.GetCommands().containsKey(args[0])) - return false; - TBMCCommandBase cmd = TBMCCommandBase.GetCommands().get(args[0]); //Subcommand + return false; } @Override diff --git a/src/io/github/norbipeti/thebuttonmcchat/commands/ucmds/UCommandBase.java b/src/io/github/norbipeti/thebuttonmcchat/commands/ucmds/UCommandBase.java index 73cc907..5f0e631 100644 --- a/src/io/github/norbipeti/thebuttonmcchat/commands/ucmds/UCommandBase.java +++ b/src/io/github/norbipeti/thebuttonmcchat/commands/ucmds/UCommandBase.java @@ -5,32 +5,21 @@ import java.util.Arrays; import org.bukkit.command.CommandSender; import io.github.norbipeti.thebuttonmcchat.commands.TBMCCommandBase; +import io.github.norbipeti.thebuttonmcchat.commands.TBMCSubCommandBase; public abstract class UCommandBase extends TBMCCommandBase { public abstract String[] GetHelpText(String alias); @Override - public boolean OnCommand(CommandSender sender, String alias, String[] args) { - if (args.length == 0) - return false; - return OnUCommand(sender, alias, - Arrays.copyOfRange(args, 1, args.length)); + public String GetCommandPath() { + return "u/" + GetUCommandPath(); } - public abstract boolean OnUCommand(CommandSender sender, String alias, - String[] args); - - @Override - public String GetCommandName() { - return "u"; - } - - public abstract String GetUCommandName(); // TODO: Help for /u commands + public abstract String GetUCommandPath(); // TODO: Help for /u commands @Override public boolean GetPlayerOnly() { return true; } - }