Probably fixed subcommands list

This commit is contained in:
Norbi Peti 2016-12-30 14:38:17 +01:00
parent e82f8c9eb3
commit 6a5dda7028
2 changed files with 14 additions and 10 deletions

View file

@ -56,20 +56,18 @@ public class CommandCaller implements CommandExecutor {
path += " " + arg;
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path);
int argc = 0;
while (cmd == null && path.contains(" ")) {
String[] subcmds = null;
while (cmd == null && (subcmds = TBMCChatAPI.GetSubCommands(path, sender)).length == 0 && path.contains(" ")) {
path = path.substring(0, path.lastIndexOf(' '));
argc++;
cmd = TBMCChatAPI.GetCommands().get(path);
}
if (cmd == null) {
String[] subcmds = TBMCChatAPI.GetSubCommands(path, sender);
if (subcmds.length > 0)
if (subcmds == null || subcmds.length > 0)
sender.sendMessage(subcmds);
else {
final String errormsg = "§cYou don't have access to any of this command's subcommands.";
final String errormsg = "§cYou don't have access to any of this command's subcommands or it doesn't have any.";
sender.sendMessage(errormsg);
if (!(sender instanceof ConsoleCommandSender))
Bukkit.getConsoleSender().sendMessage(errormsg);
}
return true;
}

View file

@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;
import java.util.function.Consumer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -42,7 +43,8 @@ public class TBMCChatAPI {
}
/**
* Returns messages formatted for Minecraft chat listing the subcommands of the command.
* Returns messages formatted for Minecraft chat listing the subcommands of the command.<br>
* Returns a header if subcommands were found, otherwise returns an empty array.
*
* @param command
* The command which we want the subcommands of
@ -52,20 +54,24 @@ public class TBMCChatAPI {
*/
public static String[] GetSubCommands(String command, CommandSender sender) {
ArrayList<String> cmds = new ArrayList<String>();
cmds.add("§6---- Subcommands ----");
Consumer<String> addToCmds = cmd -> {
if (cmds.size() == 0)
cmds.add("§6---- Subcommands ----");
cmds.add(cmd);
};
for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values()) {
if (cmd.GetCommandPath().startsWith(command + " ")) {
int ind = cmd.GetCommandPath().indexOf(' ', command.length() + 2);
if (ind >= 0) {
String newcmd = cmd.GetCommandPath().substring(0, ind);
if (!cmds.contains("/" + newcmd))
cmds.add("/" + newcmd);
addToCmds.accept("/" + newcmd);
}
if (cmd.GetPlayerOnly() && !(sender instanceof Player))
continue;
if (cmd.GetModOnly() && !MainPlugin.permission.has(sender, "tbmc.admin"))
continue;
cmds.add("/" + cmd.GetCommandPath());
addToCmds.accept("/" + cmd.GetCommandPath());
}
}
return cmds.toArray(new String[cmds.size()]);