Added handling of no top level command

This commit is contained in:
Norbi Peti 2016-11-18 20:10:48 +01:00
parent a87c71f37b
commit 98b58dd4b9

View file

@ -17,6 +17,8 @@ import buttondevteam.lib.chat.TBMCCommandBase;
public class CommandCaller implements CommandExecutor { public class CommandCaller implements CommandExecutor {
private static final String REGISTER_ERROR_MSG = "An error occured while registering commands";
private CommandCaller() { private CommandCaller() {
} }
@ -28,27 +30,29 @@ public class CommandCaller implements CommandExecutor {
for (Entry<String, TBMCCommandBase> entry : TBMCChatAPI.GetCommands().entrySet()) { for (Entry<String, TBMCCommandBase> entry : TBMCChatAPI.GetCommands().entrySet()) {
TBMCCommandBase c = entry.getValue(); TBMCCommandBase c = entry.getValue();
if (c == null) { if (c == null) {
TBMCCoreAPI.SendException("An error occured while registering commands", TBMCCoreAPI.SendException(REGISTER_ERROR_MSG,
new Exception("Null command found at " + entry.getKey() + "!")); new Exception("Null command found at " + entry.getKey() + "!"));
continue; continue;
} }
if (c.GetCommandPath() == null) { if (c.GetCommandPath() == null) {
TBMCCoreAPI.SendException("An error occured while registering commands", TBMCCoreAPI.SendException(REGISTER_ERROR_MSG,
new Exception("Command " + entry.getKey() + " has no command path!")); new Exception("Command " + entry.getKey() + " has no command path!"));
continue; continue;
} }
if (c.getPlugin() == null) { if (c.getPlugin() == null) {
TBMCCoreAPI.SendException("An error occured while registering commands", TBMCCoreAPI.SendException(REGISTER_ERROR_MSG,
new Exception("Command " + entry.getKey() + " has no plugin!")); new Exception("Command " + entry.getKey() + " has no plugin!"));
continue; continue;
} }
if (!c.GetCommandPath().contains(" ")) // Top-level command int i;
String topcmd;
if ((i = (topcmd = c.GetCommandPath()).indexOf(' ')) != -1) // Get top-level command
topcmd = c.GetCommandPath().substring(0, i);
{ {
PluginCommand pc = ((JavaPlugin) c.getPlugin()).getCommand(c.GetCommandPath()); PluginCommand pc = ((JavaPlugin) c.getPlugin()).getCommand(topcmd);
if (pc == null) if (pc == null)
TBMCCoreAPI.SendException("An error occured while registering commands", TBMCCoreAPI.SendException(REGISTER_ERROR_MSG, new Exception("Top level command " + topcmd
new Exception("Can't find top-level command: " + c.GetCommandPath() + " for plugin: " + " not registered in plugin.yml for plugin: " + c.getPlugin().getName()));
+ c.getPlugin().getName()));
else else
pc.setExecutor(instance); pc.setExecutor(instance);
} }
@ -62,12 +66,18 @@ public class CommandCaller implements CommandExecutor {
path += " " + arg; path += " " + arg;
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path); TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path);
int argc = 0; int argc = 0;
boolean hadspace = false;
while (cmd == null && path.contains(" ")) { while (cmd == null && path.contains(" ")) {
hadspace = true;
path = path.substring(0, path.lastIndexOf(' ')); path = path.substring(0, path.lastIndexOf(' '));
argc++; argc++;
cmd = TBMCChatAPI.GetCommands().get(path); cmd = TBMCChatAPI.GetCommands().get(path);
} }
if (cmd == null) { if (cmd == null) {
if (hadspace) {
sender.sendMessage(TBMCChatAPI.GetSubCommands(path));
return true;
}
sender.sendMessage("§cInternal error: Command not registered to CommandCaller"); sender.sendMessage("§cInternal error: Command not registered to CommandCaller");
if (sender != Bukkit.getConsoleSender()) if (sender != Bukkit.getConsoleSender())
Bukkit.getConsoleSender().sendMessage("§cInternal error: Command not registered to CommandCaller"); Bukkit.getConsoleSender().sendMessage("§cInternal error: Command not registered to CommandCaller");