Move most API of CommandParts to util classes
Gradual transition to removing CommandParts altogether. - Move logic from CommandParts to utils classes
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package fr.xephi.authme.command;
|
||||
|
||||
import fr.xephi.authme.permission.PermissionNode;
|
||||
import fr.xephi.authme.util.CollectionUtils;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@@ -185,10 +186,6 @@ public class CommandDescription {
|
||||
* @return True if the command reference is suitable to this command label, false otherwise.
|
||||
*/
|
||||
public boolean isSuitableLabel(CommandParts commandReference) {
|
||||
// Make sure the command reference is valid
|
||||
if (commandReference.getCount() <= 0)
|
||||
return false;
|
||||
|
||||
// Get the parent count
|
||||
//getParent() = getParent().getParentCount() + 1
|
||||
String element = commandReference.get(getParentCount());
|
||||
@@ -252,7 +249,8 @@ public class CommandDescription {
|
||||
CommandParts reference = getCommandReference(other);
|
||||
|
||||
// Compare the two references, return the result
|
||||
return reference.getDifference(new CommandParts(other.getRange(0, reference.getCount())), fullCompare);
|
||||
return CommandUtils.getDifference(reference.getList(),
|
||||
CollectionUtils.getRange(other.getList(), 0, reference.getList().size()), fullCompare);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -469,13 +467,13 @@ public class CommandDescription {
|
||||
return new FoundCommandResult(
|
||||
this,
|
||||
getCommandReference(queryReference),
|
||||
new CommandParts(),
|
||||
new CommandParts(new ArrayList<String>()),
|
||||
queryReference);
|
||||
}
|
||||
|
||||
// Get the new command reference and arguments
|
||||
CommandParts newReference = new CommandParts(queryReference.getRange(0, getParentCount() + 1));
|
||||
CommandParts newArguments = new CommandParts(queryReference.getRange(getParentCount() + 1));
|
||||
CommandParts newReference = new CommandParts(CollectionUtils.getRange(queryReference.getList(), 0, getParentCount() + 1));
|
||||
CommandParts newArguments = new CommandParts(CollectionUtils.getRange(queryReference.getList(), getParentCount() + 1));
|
||||
|
||||
// Handle the child's, if this command has any
|
||||
if (getChildren().size() > 0) {
|
||||
|
||||
@@ -14,12 +14,6 @@ public class CommandParts {
|
||||
*/
|
||||
private final List<String> parts = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public CommandParts() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
@@ -47,17 +41,6 @@ public class CommandParts {
|
||||
this.parts.addAll(parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param base The base part.
|
||||
* @param parts The list of additional parts.
|
||||
*/
|
||||
public CommandParts(String base, List<String> parts) {
|
||||
this.parts.add(base);
|
||||
this.parts.addAll(parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command parts.
|
||||
*
|
||||
@@ -67,41 +50,6 @@ public class CommandParts {
|
||||
return this.parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a part.
|
||||
*
|
||||
* @param part The part to add.
|
||||
*
|
||||
* @return The result.
|
||||
*/
|
||||
public boolean add(String part) {
|
||||
return this.parts.add(part);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add some parts.
|
||||
*
|
||||
* @param parts The parts to add.
|
||||
*
|
||||
* @return The result.
|
||||
*/
|
||||
public boolean add(List<String> parts) {
|
||||
return this.parts.addAll(parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add some parts.
|
||||
*
|
||||
* @param parts The parts to add.
|
||||
*
|
||||
* @return The result.
|
||||
*/
|
||||
public boolean add(String[] parts) {
|
||||
for (String entry : parts)
|
||||
add(entry);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of parts.
|
||||
*
|
||||
@@ -146,6 +94,7 @@ public class CommandParts {
|
||||
*
|
||||
* @return The parts range. Parts that were out of bound are not included.
|
||||
*/
|
||||
@Deprecated
|
||||
public List<String> getRange(int start, int count) {
|
||||
// Create a new list to put the range into
|
||||
List<String> elements = new ArrayList<>();
|
||||
@@ -162,27 +111,7 @@ public class CommandParts {
|
||||
return elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the difference value between two references.
|
||||
*
|
||||
* @param other The other reference.
|
||||
* @param fullCompare True to compare the full references as far as the range reaches.
|
||||
*
|
||||
* @return The result from zero to above. A negative number will be returned on error.
|
||||
*/
|
||||
public double getDifference(CommandParts other, boolean fullCompare) {
|
||||
// Make sure the other reference is correct
|
||||
if (other == null)
|
||||
return -1;
|
||||
|
||||
// Get the range to use
|
||||
int range = Math.min(this.getCount(), other.getCount());
|
||||
|
||||
// Get and the difference
|
||||
if (fullCompare)
|
||||
return StringUtils.getDifference(this.toString(), other.toString());
|
||||
return StringUtils.getDifference(this.getRange(range - 1, 1).toString(), other.getRange(range - 1, 1).toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the parts to a string.
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
package fr.xephi.authme.command;
|
||||
|
||||
import fr.xephi.authme.util.CollectionUtils;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class CommandUtils {
|
||||
|
||||
public static int getMinNumberOfArguments(CommandDescription command) {
|
||||
@@ -16,4 +22,37 @@ public final class CommandUtils {
|
||||
return command.getArguments().size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a textual representation of a list of labels to show it as a command. For example, a list containing
|
||||
* the items ["authme", "register", "player"] it will return "authme register player".
|
||||
*
|
||||
* @param labels The labels to format
|
||||
* @return The space-separated labels
|
||||
*/
|
||||
public static String labelsToString(Iterable<String> labels) {
|
||||
return StringUtils.join(" ", labels);
|
||||
}
|
||||
|
||||
public static double getDifference(List<String> labels1, List<String> labels2, boolean fullCompare) {
|
||||
// Make sure the other reference is correct
|
||||
if (labels1 == null || labels2 == null) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get the range to use
|
||||
int range = Math.min(labels1.size(), labels2.size());
|
||||
|
||||
// Get and the difference
|
||||
if (fullCompare) {
|
||||
return StringUtils.getDifference(CommandUtils.labelsToString(labels1), CommandUtils.labelsToString(labels2));
|
||||
}
|
||||
return StringUtils.getDifference(
|
||||
labelsToString(CollectionUtils.getRange(labels1, range - 1, 1)),
|
||||
labelsToString(CollectionUtils.getRange(labels2, range - 1, 1)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -139,10 +139,11 @@ public class FoundCommandResult {
|
||||
*/
|
||||
public double getDifference() {
|
||||
// Get the difference through the command found
|
||||
if (this.commandDescription != null)
|
||||
if (this.commandDescription != null) {
|
||||
return this.commandDescription.getCommandDifference(this.queryReference);
|
||||
}
|
||||
|
||||
// Get the difference from the query reference
|
||||
return this.queryReference.getDifference(commandReference, true);
|
||||
return CommandUtils.getDifference(queryReference.getList(), commandReference.getList(), true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ import fr.xephi.authme.settings.Settings;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class HelpProvider {
|
||||
@@ -40,7 +43,12 @@ public class HelpProvider {
|
||||
public static void showHelp(CommandSender sender, CommandParts reference, CommandParts helpQuery, boolean showCommand, boolean showDescription, boolean showArguments, boolean showPermissions, boolean showAlternatives, boolean showCommands) {
|
||||
// Find the command for this help query, one with and one without a prefixed base command
|
||||
FoundCommandResult result = AuthMe.getInstance().getCommandHandler().findCommand(new CommandParts(helpQuery.getList()));
|
||||
CommandParts commandReferenceOther = new CommandParts(reference.get(0), helpQuery.getList());
|
||||
|
||||
// TODO ljacqu 20151204 Fix me to nicer code
|
||||
List<String> parts = new ArrayList<>(helpQuery.getList());
|
||||
parts.add(0, reference.get(0));
|
||||
CommandParts commandReferenceOther = new CommandParts(parts);
|
||||
|
||||
FoundCommandResult resultOther = AuthMe.getInstance().getCommandHandler().findCommand(commandReferenceOther);
|
||||
if (resultOther != null) {
|
||||
if (result == null)
|
||||
|
||||
Reference in New Issue
Block a user