Removed useless constructor in CompositeCommand + updated unit tests

This commit is contained in:
Florian CUNY 2018-03-01 14:43:33 +01:00
parent 6ba27eef4e
commit e4cedb58d0
2 changed files with 19 additions and 42 deletions

View File

@ -26,8 +26,8 @@ import us.tastybento.bskyblock.util.Util;
/** /**
* BSB composite command * BSB composite command
* @author tastybento, poslovich * @author tastybento
* * @author Poslovitch
*/ */
public abstract class CompositeCommand extends Command implements PluginIdentifiableCommand, BSBCommand { public abstract class CompositeCommand extends Command implements PluginIdentifiableCommand, BSBCommand {
@ -66,23 +66,28 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
private String usage; private String usage;
/** /**
* Used only for testing.... * This is the top-level command constructor for commands that have no parent.
* @param label - string for this command
* @param aliases - aliases for this command
*/ */
public CompositeCommand(BSkyBlock plugin, String label, String... string) { public CompositeCommand(String label, String... aliases) {
super(label); super(label);
setAliases(new ArrayList<>(Arrays.asList(string))); setAliases(new ArrayList<>(Arrays.asList(aliases)));
parent = null; parent = null;
setUsage(""); setUsage("");
subCommandLevel = 0; // Top level subCommandLevel = 0; // Top level
subCommands = new LinkedHashMap<>(); subCommands = new LinkedHashMap<>();
subCommandAliases = new LinkedHashMap<>(); subCommandAliases = new LinkedHashMap<>();
// Register command if it is not already registered
if (getPlugin().getCommand(label) == null) {
getPlugin().getCommandsManager().registerCommand(this);
}
setup(); setup();
if (!getSubCommand("help").isPresent() && !label.equals("help")) { if (!getSubCommand("help").isPresent() && !label.equals("help")) {
new DefaultHelpCommand(this); new DefaultHelpCommand(this);
} }
} }
/** /**
* Sub-command constructor * Sub-command constructor
* @param parent - the parent composite command * @param parent - the parent composite command
@ -110,30 +115,6 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
} }
} }
/**
* This is the top-level command constructor for commands that have no parent.
* @param label - string for this command
* @param aliases - aliases for this command
*/
public CompositeCommand(String label, String... aliases) {
super(label);
setAliases(new ArrayList<>(Arrays.asList(aliases)));
parent = null;
setUsage("");
subCommandLevel = 0; // Top level
subCommands = new LinkedHashMap<>();
subCommandAliases = new LinkedHashMap<>();
// Register command if it is not already registered
if (getPlugin().getCommand(label) == null) {
getPlugin().getCommandsManager().registerCommand(this);
}
setup();
if (!getSubCommand("help").isPresent() && !label.equals("help")) {
new DefaultHelpCommand(this);
}
}
/* /*
* This method deals with the command execution. It traverses the tree of * This method deals with the command execution. It traverses the tree of
* subcommands until it finds the right object and then runs execute on it. * subcommands until it finds the right object and then runs execute on it.
@ -257,7 +238,6 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
return BSkyBlock.getInstance(); return BSkyBlock.getInstance();
} }
/** /**
* @return Settings object * @return Settings object
*/ */
@ -265,7 +245,6 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
return getPlugin().getSettings(); return getPlugin().getSettings();
} }
/** /**
* Returns the CompositeCommand object referring to this command label * Returns the CompositeCommand object referring to this command label
* @param label - command label or alias * @param label - command label or alias

View File

@ -226,10 +226,10 @@ public class TestBSkyBlock {
String[] args3 = {"sub2","subsub", ""}; String[] args3 = {"sub2","subsub", ""};
assertEquals(Arrays.asList("subsubsub", "help"), testCommand.tabComplete(player, "test", args3)); assertEquals(Arrays.asList("subsubsub", "help"), testCommand.tabComplete(player, "test", args3));
// Test for overridden tabcomplete // Test for overridden tabcomplete
assertEquals(Arrays.asList(new String[] {"Florian", "Ben", "Bill", "Ted", "help"}), assertEquals(Arrays.asList("Florian", "Ben", "Bill", "Ted", "help"),
testCommand.tabComplete(player, "test", new String[] {"sub2", "subsub", "subsubsub", ""})); testCommand.tabComplete(player, "test", new String[] {"sub2", "subsub", "subsubsub", ""}));
// Test for partial word // Test for partial word
assertEquals(Arrays.asList(new String[] {"Ben", "Bill"}), assertEquals(Arrays.asList("Ben", "Bill"),
testCommand.tabComplete(player, "test", new String[] {"sub2", "subsub", "subsubsub", "b"})); testCommand.tabComplete(player, "test", new String[] {"sub2", "subsub", "subsubsub", "b"}));
// Test command arguments // Test command arguments
@ -253,7 +253,7 @@ public class TestBSkyBlock {
private class TestCommand extends CompositeCommand { private class TestCommand extends CompositeCommand {
public TestCommand() { public TestCommand() {
super(plugin, "test", "t", "tt"); super("test", "t", "tt");
setParameters("test.params"); setParameters("test.params");
} }
@ -340,17 +340,15 @@ public class TestBSkyBlock {
@Override @Override
public boolean execute(User user, List<String> args) { public boolean execute(User user, List<String> args) {
Bukkit.getLogger().info("args are " + args.toString()); Bukkit.getLogger().info("args are " + args.toString());
if (args.size() == 3) {
return true; return args.size() == 3;
}
return false;
} }
@Override @Override
public Optional<List<String>> tabComplete(final User user, final String alias, final LinkedList<String> args) { public Optional<List<String>> tabComplete(final User user, final String alias, final LinkedList<String> args) {
List<String> options = new ArrayList<>(); List<String> options = new ArrayList<>();
String lastArg = (!args.isEmpty() ? args.getLast() : ""); String lastArg = (!args.isEmpty() ? args.getLast() : "");
options.addAll(Arrays.asList(new String[] {"Florian", "Ben", "Bill", "Ted"})); options.addAll(Arrays.asList("Florian", "Ben", "Bill", "Ted"));
return Optional.of(Util.tabLimit(options, lastArg)); return Optional.of(Util.tabLimit(options, lastArg));
} }
} }
@ -358,7 +356,7 @@ public class TestBSkyBlock {
private class Test3ArgsCommand extends CompositeCommand { private class Test3ArgsCommand extends CompositeCommand {
public Test3ArgsCommand() { public Test3ArgsCommand() {
super(plugin, "args", ""); super("args", "");
} }
@Override @Override
@ -367,7 +365,7 @@ public class TestBSkyBlock {
@Override @Override
public boolean execute(User user, List<String> args) { public boolean execute(User user, List<String> args) {
Bukkit.getLogger().info("args are " + args.toString()); Bukkit.getLogger().info("args are " + args.toString());
return args.size() == 3 ? true : false; return args.size() == 3;
} }
} }