From ea027616a4b1843c4d7262dcd7ff7c2cadfb9510 Mon Sep 17 00:00:00 2001 From: Brianna Date: Mon, 19 Aug 2019 11:07:10 -0400 Subject: [PATCH] Added TabComplete --- .../command/AbstractCommand.java | 45 ++++++++++-- .../command/CommandManager.java | 41 ++++++----- .../ultimatestacker/command/TabManager.java | 64 ++++++++++++++++++ .../command/commands/CommandConvert.java | 9 ++- .../command/commands/CommandReload.java | 10 ++- .../command/commands/CommandRemoveAll.java | 9 ++- .../command/commands/CommandSettings.java | 9 ++- .../commands/CommandUltimateStacker.java | 9 ++- .../command/commands/CommandSettings.class | Bin 1878 -> 2282 bytes 9 files changed, 168 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/songoda/ultimatestacker/command/TabManager.java diff --git a/src/main/java/com/songoda/ultimatestacker/command/AbstractCommand.java b/src/main/java/com/songoda/ultimatestacker/command/AbstractCommand.java index ebdcd47..331db15 100644 --- a/src/main/java/com/songoda/ultimatestacker/command/AbstractCommand.java +++ b/src/main/java/com/songoda/ultimatestacker/command/AbstractCommand.java @@ -3,18 +3,36 @@ package com.songoda.ultimatestacker.command; import com.songoda.ultimatestacker.UltimateStacker; import org.bukkit.command.CommandSender; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public abstract class AbstractCommand { - private final AbstractCommand parent; - private final String command; private final boolean noConsole; + private AbstractCommand parent = null; + private boolean hasArgs = false; + private String command; - protected AbstractCommand(String command, AbstractCommand parent, boolean noConsole) { - this.command = command; + private List subCommand = new ArrayList<>(); + + protected AbstractCommand(AbstractCommand parent, boolean noConsole, String... command) { + if (parent != null) { + this.subCommand = Arrays.asList(command); + } else { + this.command = Arrays.asList(command).get(0); + } this.parent = parent; this.noConsole = noConsole; } + protected AbstractCommand(boolean noConsole, boolean hasArgs, String... command) { + this.command = Arrays.asList(command).get(0); + + this.hasArgs = hasArgs; + this.noConsole = noConsole; + } + public AbstractCommand getParent() { return parent; } @@ -23,17 +41,32 @@ public abstract class AbstractCommand { return command; } - public boolean isNoConsole() { - return noConsole; + public List getSubCommand() { + return subCommand; + } + + public void addSubCommand(String command) { + subCommand.add(command); } protected abstract ReturnType runCommand(UltimateStacker instance, CommandSender sender, String... args); + protected abstract List onTab(UltimateStacker instance, CommandSender sender, String... args); + public abstract String getPermissionNode(); public abstract String getSyntax(); public abstract String getDescription(); + public boolean hasArgs() { + return hasArgs; + } + + public boolean isNoConsole() { + return noConsole; + } + public enum ReturnType {SUCCESS, FAILURE, SYNTAX_ERROR} } + diff --git a/src/main/java/com/songoda/ultimatestacker/command/CommandManager.java b/src/main/java/com/songoda/ultimatestacker/command/CommandManager.java index 5b0dae1..6aeac2d 100644 --- a/src/main/java/com/songoda/ultimatestacker/command/CommandManager.java +++ b/src/main/java/com/songoda/ultimatestacker/command/CommandManager.java @@ -2,7 +2,6 @@ package com.songoda.ultimatestacker.command; import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.command.commands.*; -import com.songoda.ultimatestacker.utils.Methods; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -15,20 +14,27 @@ import java.util.List; public class CommandManager implements CommandExecutor { private static final List commands = new ArrayList<>(); - private UltimateStacker instance; + private UltimateStacker plugin; + private TabManager tabManager; - public CommandManager(UltimateStacker instance) { - this.instance = instance; + public CommandManager(UltimateStacker plugin) { + this.plugin = plugin; + this.tabManager = new TabManager(this); - instance.getCommand("UltimateStacker").setExecutor(this); + plugin.getCommand("UltimateStacker").setExecutor(this); AbstractCommand commandUltimateStacker = addCommand(new CommandUltimateStacker()); addCommand(new CommandSettings(commandUltimateStacker)); addCommand(new CommandRemoveAll(commandUltimateStacker)); addCommand(new CommandReload(commandUltimateStacker)); - addCommand(new CommandGive(commandUltimateStacker)); + addCommand(new CommandGiveSpawner(commandUltimateStacker)); addCommand(new CommandConvert(commandUltimateStacker)); + + for (AbstractCommand abstractCommand : commands) { + if (abstractCommand.getParent() != null) continue; + plugin.getCommand(abstractCommand.getCommand()).setTabCompleter(tabManager); + } } private AbstractCommand addCommand(AbstractCommand abstractCommand) { @@ -39,20 +45,23 @@ public class CommandManager implements CommandExecutor { @Override public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { for (AbstractCommand abstractCommand : commands) { - if (abstractCommand.getCommand().equalsIgnoreCase(command.getName())) { - if (strings.length == 0) { + if (abstractCommand.getCommand() != null && abstractCommand.getCommand().equalsIgnoreCase(command.getName().toLowerCase())) { + if (strings.length == 0 || abstractCommand.hasArgs()) { processRequirements(abstractCommand, commandSender, strings); return true; } } else if (strings.length != 0 && abstractCommand.getParent() != null && abstractCommand.getParent().getCommand().equalsIgnoreCase(command.getName())) { String cmd = strings[0]; - if (cmd.equalsIgnoreCase(abstractCommand.getCommand())) { - processRequirements(abstractCommand, commandSender, strings); - return true; + String cmd2 = strings.length >= 2 ? String.join(" ", strings[0], strings[1]) : null; + for (String cmds : abstractCommand.getSubCommand()) { + if (cmd.equalsIgnoreCase(cmds) || (cmd2 != null && cmd2.equalsIgnoreCase(cmds))) { + processRequirements(abstractCommand, commandSender, strings); + return true; + } } } } - instance.getLocale().newMessage("&7The command you entered does not exist or is spelt incorrectly.").sendPrefixedMessage(commandSender); + plugin.getLocale().newMessage("&7The command you entered does not exist or is spelt incorrectly.").sendPrefixedMessage(commandSender); return true; } @@ -62,14 +71,14 @@ public class CommandManager implements CommandExecutor { return; } if (command.getPermissionNode() == null || sender.hasPermission(command.getPermissionNode())) { - AbstractCommand.ReturnType returnType = command.runCommand(instance, sender, strings); + AbstractCommand.ReturnType returnType = command.runCommand(plugin, sender, strings); if (returnType == AbstractCommand.ReturnType.SYNTAX_ERROR) { - instance.getLocale().newMessage("&cInvalid Syntax!").sendPrefixedMessage(sender); - instance.getLocale().newMessage("&7The valid syntax is: &6" + command.getSyntax() + "&7.").sendPrefixedMessage(sender); + plugin.getLocale().newMessage("&cInvalid Syntax!").sendPrefixedMessage(sender); + plugin.getLocale().newMessage("&7The valid syntax is: &6" + command.getSyntax() + "&7.").sendPrefixedMessage(sender); } return; } - instance.getLocale().getMessage("event.general.nopermission").sendPrefixedMessage(sender); + plugin.getLocale().getMessage("event.general.nopermission").sendPrefixedMessage(sender); } public List getCommands() { diff --git a/src/main/java/com/songoda/ultimatestacker/command/TabManager.java b/src/main/java/com/songoda/ultimatestacker/command/TabManager.java new file mode 100644 index 0000000..e28c93d --- /dev/null +++ b/src/main/java/com/songoda/ultimatestacker/command/TabManager.java @@ -0,0 +1,64 @@ +package com.songoda.ultimatestacker.command; + +import com.songoda.ultimatestacker.UltimateStacker; +import org.apache.commons.lang.ArrayUtils; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; + +import java.util.ArrayList; +import java.util.List; + +public class TabManager implements TabCompleter { + + private final CommandManager commandManager; + + TabManager(CommandManager commandManager) { + this.commandManager = commandManager; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] strings) { + for (AbstractCommand abstractCommand : commandManager.getCommands()) { + if (abstractCommand.getCommand() != null && abstractCommand.getCommand().equalsIgnoreCase(command.getName()) && !abstractCommand.hasArgs()) { + if (strings.length == 1) { + List subs = new ArrayList<>(); + for (AbstractCommand ac : commandManager.getCommands()) { + if (ac.getSubCommand() == null) continue; + subs.addAll(ac.getSubCommand()); + } + subs.removeIf(s -> !s.toLowerCase().startsWith(strings[0].toLowerCase())); + return subs; + } + } else if (strings.length != 0 && abstractCommand.getParent() != null && abstractCommand.getParent().getCommand().equalsIgnoreCase(command.getName()) + || abstractCommand.hasArgs() && abstractCommand.getCommand().equalsIgnoreCase(command.getName())) { + String[] args = abstractCommand.hasArgs() ? (String[]) ArrayUtils.add(strings, 0, command.getName()) : strings; + String cmd = abstractCommand.hasArgs() ? command.getName() : args[0]; + String cmd2 = args.length >= 2 ? String.join(" ", args[0], args[1]) : null; + if (!abstractCommand.hasArgs()) { + for (String cmds : abstractCommand.getSubCommand()) { + if (cmd.equalsIgnoreCase(cmds) || (cmd2 != null && cmd2.equalsIgnoreCase(cmds))) { + return fetchList(abstractCommand, args, sender); + } + } + } else { + return fetchList(abstractCommand, args, sender); + } + } + } + return new ArrayList<>(); + } + + private List fetchList(AbstractCommand abstractCommand, String[] args, CommandSender sender) { + List list = abstractCommand.onTab(UltimateStacker.getInstance(), sender, args); + String str = args[args.length - 1]; + if (list != null && str != null && str.length() >= 1) { + try { + list.removeIf(s -> !s.toLowerCase().startsWith(str.toLowerCase())); + } catch (UnsupportedOperationException ignored) { + } + } + return list; + } + +} diff --git a/src/main/java/com/songoda/ultimatestacker/command/commands/CommandConvert.java b/src/main/java/com/songoda/ultimatestacker/command/commands/CommandConvert.java index dce7382..c2cdd1c 100644 --- a/src/main/java/com/songoda/ultimatestacker/command/commands/CommandConvert.java +++ b/src/main/java/com/songoda/ultimatestacker/command/commands/CommandConvert.java @@ -8,10 +8,12 @@ import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.List; + public class CommandConvert extends AbstractCommand { public CommandConvert(AbstractCommand parent) { - super("convert", parent, true); + super(parent, true, "convert"); } @Override @@ -26,6 +28,11 @@ public class CommandConvert extends AbstractCommand { return ReturnType.SUCCESS; } + @Override + protected List onTab(UltimateStacker instance, CommandSender sender, String... args) { + return null; + } + @Override public String getPermissionNode() { return "ultimatestacker.admin"; diff --git a/src/main/java/com/songoda/ultimatestacker/command/commands/CommandReload.java b/src/main/java/com/songoda/ultimatestacker/command/commands/CommandReload.java index 3cd42da..eb5bad3 100644 --- a/src/main/java/com/songoda/ultimatestacker/command/commands/CommandReload.java +++ b/src/main/java/com/songoda/ultimatestacker/command/commands/CommandReload.java @@ -2,13 +2,14 @@ package com.songoda.ultimatestacker.command.commands; import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.command.AbstractCommand; -import com.songoda.ultimatestacker.utils.Methods; import org.bukkit.command.CommandSender; +import java.util.List; + public class CommandReload extends AbstractCommand { public CommandReload(AbstractCommand parent) { - super("reload", parent, false); + super(parent, false, "reload"); } @Override @@ -18,6 +19,11 @@ public class CommandReload extends AbstractCommand { return ReturnType.SUCCESS; } + @Override + protected List onTab(UltimateStacker instance, CommandSender sender, String... args) { + return null; + } + @Override public String getPermissionNode() { return "ultimatestacker.admin"; diff --git a/src/main/java/com/songoda/ultimatestacker/command/commands/CommandRemoveAll.java b/src/main/java/com/songoda/ultimatestacker/command/commands/CommandRemoveAll.java index 178315c..bed9534 100644 --- a/src/main/java/com/songoda/ultimatestacker/command/commands/CommandRemoveAll.java +++ b/src/main/java/com/songoda/ultimatestacker/command/commands/CommandRemoveAll.java @@ -13,10 +13,12 @@ import org.bukkit.entity.Item; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import java.util.List; + public class CommandRemoveAll extends AbstractCommand { public CommandRemoveAll(AbstractCommand parent) { - super("removeall", parent, false); + super(parent, false, "removeall"); } @Override @@ -62,6 +64,11 @@ public class CommandRemoveAll extends AbstractCommand { return ReturnType.SUCCESS; } + @Override + protected List onTab(UltimateStacker instance, CommandSender sender, String... args) { + return null; + } + @Override public String getPermissionNode() { return "ultimatestacker.admin"; diff --git a/src/main/java/com/songoda/ultimatestacker/command/commands/CommandSettings.java b/src/main/java/com/songoda/ultimatestacker/command/commands/CommandSettings.java index b387067..205c8cf 100644 --- a/src/main/java/com/songoda/ultimatestacker/command/commands/CommandSettings.java +++ b/src/main/java/com/songoda/ultimatestacker/command/commands/CommandSettings.java @@ -5,10 +5,12 @@ import com.songoda.ultimatestacker.command.AbstractCommand; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.List; + public class CommandSettings extends AbstractCommand { public CommandSettings(AbstractCommand parent) { - super("Settings", parent, true); + super(parent, true, "Settings"); } @Override @@ -17,6 +19,11 @@ public class CommandSettings extends AbstractCommand { return ReturnType.SUCCESS; } + @Override + protected List onTab(UltimateStacker instance, CommandSender sender, String... args) { + return null; + } + @Override public String getPermissionNode() { return "ultimatestacker.admin"; diff --git a/src/main/java/com/songoda/ultimatestacker/command/commands/CommandUltimateStacker.java b/src/main/java/com/songoda/ultimatestacker/command/commands/CommandUltimateStacker.java index 2f07d3d..394510a 100644 --- a/src/main/java/com/songoda/ultimatestacker/command/commands/CommandUltimateStacker.java +++ b/src/main/java/com/songoda/ultimatestacker/command/commands/CommandUltimateStacker.java @@ -5,10 +5,12 @@ import com.songoda.ultimatestacker.command.AbstractCommand; import com.songoda.ultimatestacker.utils.Methods; import org.bukkit.command.CommandSender; +import java.util.List; + public class CommandUltimateStacker extends AbstractCommand { public CommandUltimateStacker() { - super("UltimateStacker", null, false); + super(null, false, "UltimateStacker"); } @Override @@ -27,6 +29,11 @@ public class CommandUltimateStacker extends AbstractCommand { return ReturnType.SUCCESS; } + @Override + protected List onTab(UltimateStacker instance, CommandSender sender, String... args) { + return null; + } + @Override public String getPermissionNode() { return null; diff --git a/target/classes/com/songoda/ultimatestacker/command/commands/CommandSettings.class b/target/classes/com/songoda/ultimatestacker/command/commands/CommandSettings.class index 352bc52db8e3fd7c115cf134d776e4fdafe8e0e8..5ceb7ca63dafd04b704f9cc98cb4c0c2a5085c54 100644 GIT binary patch delta 782 zcmZvYO-~bH5Xb-X?6$jIw%xLV6t$vNUZjPhQ~_Jd+r^jO>P0w^B_YikvLx`}wJ|Zq z#1q*sqBMcTM59MNdGbT}A&B#An@S6Nm?tyypWlCW{-pj6uKzmw@(n-<>jv^B3Kne4 zSeQl8z?_Bqc#yyd=1r7LJT&phz=DBALcHP!zN``!r@|jpI+8zWsa2&HWX1M^m)}h;;ao7RFwCZ@l86^mmIj|3H=riMrniJbAdkL-^36&Gl+4wJ6{9f=00}_x+$z zu7>Z$ULoCICz$meDOqt~BZKDd`E$Bm&eC3!O;nS z$A_Bg2yJ9GLfR2t>#RzcY-?FH!yRR~x`B7@{F8l(=Q%(v9H_d;lYo zVB*5K_5pkdAHq14#zfMym^pLi|G$~@9sLRyK7Zcc0Tgk_v7#V|RRw9R$;c?kA{T&x zJjXhR#jzn{Q^r=Wq%cSwd#|K8x5XCT66-W>$ z2QoU#O0N)lR0z;0Do!H^PVxxV#z=@LbgIaL3B(9S@^l#M}p@0emm; zKLeeA)CesTb*Pz<{*S6*8VRBi12afEGd0X&4kEp#Fz-&vQy!{BT_(B<_d8F916D_E p7tA_X@bL0+Z^FxqMGtFW+?w*R3RrTMTZU%RMWLMx`ufyS{1?D2G0Ok|