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; path += " " + arg;
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path); TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path);
int argc = 0; 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(' ')); path = path.substring(0, path.lastIndexOf(' '));
argc++; argc++;
cmd = TBMCChatAPI.GetCommands().get(path); cmd = TBMCChatAPI.GetCommands().get(path);
} }
if (cmd == null) { if (cmd == null) {
String[] subcmds = TBMCChatAPI.GetSubCommands(path, sender); if (subcmds == null || subcmds.length > 0)
if (subcmds.length > 0)
sender.sendMessage(subcmds); sender.sendMessage(subcmds);
else { 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); sender.sendMessage(errormsg);
if (!(sender instanceof ConsoleCommandSender))
Bukkit.getConsoleSender().sendMessage(errormsg);
} }
return true; return true;
} }

View file

@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; 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 * @param command
* The command which we want the subcommands of * The command which we want the subcommands of
@ -52,20 +54,24 @@ public class TBMCChatAPI {
*/ */
public static String[] GetSubCommands(String command, CommandSender sender) { public static String[] GetSubCommands(String command, CommandSender sender) {
ArrayList<String> cmds = new ArrayList<String>(); 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()) { for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values()) {
if (cmd.GetCommandPath().startsWith(command + " ")) { if (cmd.GetCommandPath().startsWith(command + " ")) {
int ind = cmd.GetCommandPath().indexOf(' ', command.length() + 2); int ind = cmd.GetCommandPath().indexOf(' ', command.length() + 2);
if (ind >= 0) { if (ind >= 0) {
String newcmd = cmd.GetCommandPath().substring(0, ind); String newcmd = cmd.GetCommandPath().substring(0, ind);
if (!cmds.contains("/" + newcmd)) if (!cmds.contains("/" + newcmd))
cmds.add("/" + newcmd); addToCmds.accept("/" + newcmd);
} }
if (cmd.GetPlayerOnly() && !(sender instanceof Player)) if (cmd.GetPlayerOnly() && !(sender instanceof Player))
continue; continue;
if (cmd.GetModOnly() && !MainPlugin.permission.has(sender, "tbmc.admin")) if (cmd.GetModOnly() && !MainPlugin.permission.has(sender, "tbmc.admin"))
continue; continue;
cmds.add("/" + cmd.GetCommandPath()); addToCmds.accept("/" + cmd.GetCommandPath());
} }
} }
return cmds.toArray(new String[cmds.size()]); return cmds.toArray(new String[cmds.size()]);