Added tab complete.

This commit is contained in:
Brianna O'Keefe 2019-02-16 07:41:21 -05:00
parent 38f84dcb79
commit 212558bb98
9 changed files with 161 additions and 17 deletions

View File

@ -3,14 +3,24 @@ package com.songoda.epichoppers.command;
import com.songoda.epichoppers.EpicHoppersPlugin;
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 String command;
protected AbstractCommand(String command, AbstractCommand parent, boolean noConsole) {
this.command = command;
private List<String> 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;
}
@ -23,17 +33,27 @@ public abstract class AbstractCommand {
return command;
}
public boolean isNoConsole() {
return noConsole;
public List<String> getSubCommand() {
return subCommand;
}
public void addSubCommand(String command) {
subCommand.add(command);
}
protected abstract ReturnType runCommand(EpicHoppersPlugin instance, CommandSender sender, String... args);
protected abstract List<String> onTab(EpicHoppersPlugin instance, CommandSender sender, String... args);
public abstract String getPermissionNode();
public abstract String getSyntax();
public abstract String getDescription();
public boolean isNoConsole() {
return noConsole;
}
public enum ReturnType {SUCCESS, FAILURE, SYNTAX_ERROR}
}

View File

@ -15,11 +15,13 @@ import java.util.List;
public class CommandManager implements CommandExecutor {
private EpicHoppersPlugin instance;
private TabManager tabManager;
private List<AbstractCommand> commands = new ArrayList<>();
public CommandManager(EpicHoppersPlugin instance) {
this.instance = instance;
this.tabManager = new TabManager(this);
instance.getCommand("EpicHoppers").setExecutor(this);
@ -30,6 +32,11 @@ public class CommandManager implements CommandExecutor {
addCommand(new CommandGive(commandEpicHoppers));
addCommand(new CommandBoost(commandEpicHoppers));
addCommand(new CommandBook(commandEpicHoppers));
for (AbstractCommand abstractCommand : commands) {
if (abstractCommand.getParent() != null) continue;
instance.getCommand(abstractCommand.getCommand()).setTabCompleter(tabManager);
}
}
private AbstractCommand addCommand(AbstractCommand abstractCommand) {
@ -40,16 +47,19 @@ 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 (abstractCommand.getCommand() != null && abstractCommand.getCommand().equalsIgnoreCase(command.getName().toLowerCase())) {
if (strings.length == 0) {
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;
}
}
}
}

View File

@ -0,0 +1,52 @@
package com.songoda.epichoppers.command;
import com.songoda.epichoppers.EpicHoppersPlugin;
import org.bukkit.Bukkit;
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<String> onTabComplete(CommandSender sender, Command command, String alias, String[] strings) {
for (AbstractCommand abstractCommand : commandManager.getCommands()) {
if (abstractCommand.getCommand() != null && abstractCommand.getCommand().equalsIgnoreCase(command.getName().toLowerCase())) {
if (strings.length == 1) {
List<String> 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().toLowerCase())) {
String cmd = strings[0];
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))) {
List<String> list = abstractCommand.onTab(EpicHoppersPlugin.getInstance(), sender, strings);
String str = strings[strings.length - 1];
if (list != null && str != null && str.length() >= 1) {
try {
list.removeIf(s -> !s.toLowerCase().startsWith(str.toLowerCase()));
} catch (UnsupportedOperationException ignored) {}
}
return list;
}
}
}
}
return null;
}
}

View File

@ -7,10 +7,12 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
public class CommandBook extends AbstractCommand {
public CommandBook(AbstractCommand parent) {
super("book", parent, false);
super(parent, false, "book");
}
@Override
@ -30,6 +32,11 @@ public class CommandBook extends AbstractCommand {
return ReturnType.FAILURE;
}
@Override
protected List<String> onTab(EpicHoppersPlugin instance, CommandSender sender, String... args) {
return null;
}
@Override
public String getPermissionNode() {
return "epichoppers.admin";

View File

@ -6,14 +6,14 @@ import com.songoda.epichoppers.command.AbstractCommand;
import com.songoda.epichoppers.utils.Methods;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.Calendar;
import java.util.Date;
import java.util.*;
public class CommandBoost extends AbstractCommand {
public CommandBoost(AbstractCommand parent) {
super("boost", parent, false);
super(parent, false, "boost");
}
@Override
@ -66,6 +66,22 @@ public class CommandBoost extends AbstractCommand {
return ReturnType.FAILURE;
}
@Override
protected List<String> onTab(EpicHoppersPlugin instance, CommandSender sender, String... args) {
if (args.length == 2) {
List<String> players = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
players.add(player.getName());
}
return players;
} else if (args.length == 3) {
return Arrays.asList("1", "2", "3", "4", "5");
} else if (args.length == 4) {
return Arrays.asList("m:", "h:", "d:", "y:");
}
return null;
}
@Override
public String getPermissionNode() {
return "epichoppers.admin";

View File

@ -5,10 +5,12 @@ import com.songoda.epichoppers.command.AbstractCommand;
import com.songoda.epichoppers.utils.Methods;
import org.bukkit.command.CommandSender;
import java.util.List;
public class CommandEpicHoppers extends AbstractCommand {
public CommandEpicHoppers() {
super("EpicHoppers", null, false);
super(null, false, "EpicHoppers");
}
@Override
@ -26,6 +28,11 @@ public class CommandEpicHoppers extends AbstractCommand {
return ReturnType.SUCCESS;
}
@Override
protected List<String> onTab(EpicHoppersPlugin instance, CommandSender sender, String... args) {
return null;
}
@Override
public String getPermissionNode() {
return null;

View File

@ -8,10 +8,14 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CommandGive extends AbstractCommand {
public CommandGive(AbstractCommand parent) {
super("give", parent, false);
super(parent, false,"give");
}
@Override
@ -52,6 +56,20 @@ public class CommandGive extends AbstractCommand {
return ReturnType.SUCCESS;
}
@Override
protected List<String> onTab(EpicHoppersPlugin instance, CommandSender sender, String... args) {
if (args.length == 2) {
List<String> players = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
players.add(player.getName());
}
return players;
} else if (args.length == 3) {
return Arrays.asList("1", "2", "3", "4", "5");
}
return null;
}
@Override
public String getPermissionNode() {
return "epichoppers.admin";

View File

@ -5,10 +5,12 @@ import com.songoda.epichoppers.command.AbstractCommand;
import com.songoda.epichoppers.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 +20,11 @@ public class CommandReload extends AbstractCommand {
return ReturnType.SUCCESS;
}
@Override
protected List<String> onTab(EpicHoppersPlugin instance, CommandSender sender, String... args) {
return null;
}
@Override
public String getPermissionNode() {
return "epichoppers.admin";

View File

@ -5,10 +5,12 @@ import com.songoda.epichoppers.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
@ -18,6 +20,11 @@ public class CommandSettings extends AbstractCommand {
return ReturnType.SUCCESS;
}
@Override
protected List<String> onTab(EpicHoppersPlugin instance, CommandSender sender, String... args) {
return null;
}
@Override
public String getPermissionNode() {
return "epichoppers.admin";