Merge origin/enum into local branch
This commit is contained in:
@@ -1,45 +1,62 @@
|
||||
package fr.xephi.authme.command;
|
||||
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class CommandDescription {
|
||||
|
||||
/** Defines the acceptable labels. */
|
||||
/**
|
||||
* Defines the acceptable labels.
|
||||
*/
|
||||
private List<String> labels = new ArrayList<>();
|
||||
/** Command description. */
|
||||
/**
|
||||
* Command description.
|
||||
*/
|
||||
private String description = "";
|
||||
/** Detailed description. */
|
||||
/**
|
||||
* Detailed description.
|
||||
*/
|
||||
private String detailedDescription = "";
|
||||
/** The executable command instance. */
|
||||
/**
|
||||
* The executable command instance.
|
||||
*/
|
||||
private ExecutableCommand executableCommand;
|
||||
/** The parent command. */
|
||||
/**
|
||||
* The parent command.
|
||||
*/
|
||||
private CommandDescription parent = null;
|
||||
/** The child labels. */
|
||||
/**
|
||||
* The child labels.
|
||||
*/
|
||||
private List<CommandDescription> children = new ArrayList<>();
|
||||
/** The command arguments. */
|
||||
/**
|
||||
* The command arguments.
|
||||
*/
|
||||
private List<CommandArgumentDescription> arguments = new ArrayList<>();
|
||||
/** Defines whether there is an argument maximum or not. */
|
||||
/**
|
||||
* Defines whether there is an argument maximum or not.
|
||||
*/
|
||||
private boolean noArgumentMaximum = false;
|
||||
/** Defines the command permissions. */
|
||||
/**
|
||||
* Defines the command permissions.
|
||||
*/
|
||||
private CommandPermissions permissions = new CommandPermissions();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param label Command label.
|
||||
* @param description Command description.
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param label Command label.
|
||||
* @param description Command description.
|
||||
* @param detailedDescription Detailed comment description.
|
||||
* @param parent Parent command.
|
||||
* @param parent Parent command.
|
||||
*/
|
||||
public CommandDescription(ExecutableCommand executableCommand, String label, String description, String detailedDescription, CommandDescription parent) {
|
||||
this(executableCommand, label, description, parent, detailedDescription, null);
|
||||
@@ -48,11 +65,11 @@ public class CommandDescription {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param labels List of command labels.
|
||||
* @param description Command description.
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param labels List of command labels.
|
||||
* @param description Command description.
|
||||
* @param detailedDescription Detailed comment description.
|
||||
* @param parent Parent command.
|
||||
* @param parent Parent command.
|
||||
*/
|
||||
public CommandDescription(ExecutableCommand executableCommand, List<String> labels, String description, String detailedDescription, CommandDescription parent) {
|
||||
this(executableCommand, labels, description, detailedDescription, parent, null);
|
||||
@@ -61,12 +78,12 @@ public class CommandDescription {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param label Command label.
|
||||
* @param description Command description.
|
||||
* @param parent Parent command.
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param label Command label.
|
||||
* @param description Command description.
|
||||
* @param parent Parent command.
|
||||
* @param detailedDescription Detailed comment description.
|
||||
* @param arguments Command arguments.
|
||||
* @param arguments Command arguments.
|
||||
*/
|
||||
public CommandDescription(ExecutableCommand executableCommand, String label, String description, CommandDescription parent, String detailedDescription, List<CommandArgumentDescription> arguments) {
|
||||
setExecutableCommand(executableCommand);
|
||||
@@ -80,12 +97,12 @@ public class CommandDescription {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param labels List of command labels.
|
||||
* @param description Command description.
|
||||
* @param executableCommand The executable command, or null.
|
||||
* @param labels List of command labels.
|
||||
* @param description Command description.
|
||||
* @param detailedDescription Detailed comment description.
|
||||
* @param parent Parent command.
|
||||
* @param arguments Command arguments.
|
||||
* @param parent Parent command.
|
||||
* @param arguments Command arguments.
|
||||
*/
|
||||
public CommandDescription(ExecutableCommand executableCommand, List<String> labels, String description, String detailedDescription, CommandDescription parent, List<CommandArgumentDescription> arguments) {
|
||||
setExecutableCommand(executableCommand);
|
||||
@@ -96,6 +113,69 @@ public class CommandDescription {
|
||||
setArguments(arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a label is valid to use.
|
||||
*
|
||||
* @param label The label to test.
|
||||
*
|
||||
* @return True if the label is valid to use, false otherwise.
|
||||
*/
|
||||
public static boolean isValidLabel(String label) {
|
||||
// Make sure the label isn't null
|
||||
if (label == null)
|
||||
return false;
|
||||
|
||||
// Trim the label
|
||||
label = label.trim();
|
||||
|
||||
// Make sure the label is at least one character long
|
||||
if (label.length() <= 0)
|
||||
return false;
|
||||
|
||||
// Make sure the label doesn't contain any spaces, return the result
|
||||
return !label.contains(" ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether two labels equal to each other.
|
||||
*
|
||||
* @param commandLabel The first command label.
|
||||
* @param otherCommandLabel The other command label.
|
||||
*
|
||||
* @return True if the labels are equal to each other.
|
||||
*/
|
||||
private static boolean commandLabelEquals(String commandLabel, String otherCommandLabel) {
|
||||
// Trim the command labels from unwanted whitespaces
|
||||
commandLabel = commandLabel.trim();
|
||||
otherCommandLabel = otherCommandLabel.trim();
|
||||
|
||||
// Check whether the the two command labels are equal (case insensitive)
|
||||
return (commandLabel.equalsIgnoreCase(otherCommandLabel));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first relative command label.
|
||||
*
|
||||
* @return First relative command label.
|
||||
*/
|
||||
public String getLabel() {
|
||||
// Ensure there's any item in the command list
|
||||
if (this.labels.size() == 0)
|
||||
return "";
|
||||
|
||||
// Return the first command on the list
|
||||
return this.labels.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command label, this will append the command label to already existing ones.
|
||||
*
|
||||
* @param commandLabel Command label to set or add.
|
||||
*/
|
||||
public void setLabel(String commandLabel) {
|
||||
setLabel(commandLabel, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the label most similar to the reference. The first label will be returned if no reference was supplied.
|
||||
*
|
||||
@@ -105,11 +185,11 @@ public class CommandDescription {
|
||||
*/
|
||||
public String getLabel(CommandParts reference) {
|
||||
// Ensure there's any item in the command list
|
||||
if(this.labels.size() == 0)
|
||||
if (this.labels.size() == 0)
|
||||
return "";
|
||||
|
||||
// Return the first label if we can't use the reference
|
||||
if(reference == null)
|
||||
if (reference == null)
|
||||
return this.labels.get(0);
|
||||
|
||||
// Get the correct label from the reference
|
||||
@@ -118,9 +198,9 @@ public class CommandDescription {
|
||||
// Check whether the preferred label is in the label list
|
||||
double currentDifference = -1;
|
||||
String currentLabel = this.labels.get(0);
|
||||
for(String entry : this.labels) {
|
||||
for (String entry : this.labels) {
|
||||
double entryDifference = StringUtils.getDifference(entry, preferred);
|
||||
if(entryDifference < currentDifference || currentDifference < 0) {
|
||||
if (entryDifference < currentDifference || currentDifference < 0) {
|
||||
currentDifference = entryDifference;
|
||||
currentLabel = entry;
|
||||
}
|
||||
@@ -153,27 +233,18 @@ public class CommandDescription {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command label, this will append the command label to already existing ones.
|
||||
*
|
||||
* @param commandLabel Command label to set or add.
|
||||
*/
|
||||
public void setLabel(String commandLabel) {
|
||||
setLabel(commandLabel, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command label.
|
||||
*
|
||||
* @param commandLabel Command label to set.
|
||||
* @param overwrite True to replace all old command labels, false to append this command label to the currently
|
||||
* existing labels.
|
||||
* @param overwrite True to replace all old command labels, false to append this command label to the currently
|
||||
* existing labels.
|
||||
*
|
||||
|
||||
* @return Trie if the command label is added, or if it was added already. False on failure. */
|
||||
* @return Trie if the command label is added, or if it was added already. False on failure.
|
||||
*/
|
||||
public boolean setLabel(String commandLabel, boolean overwrite) {
|
||||
// Check whether this new command should overwrite the previous ones
|
||||
if(!overwrite)
|
||||
if (!overwrite)
|
||||
return addLabel(commandLabel);
|
||||
|
||||
// Replace all labels with this new one
|
||||
@@ -186,15 +257,15 @@ public class CommandDescription {
|
||||
*
|
||||
* @param commandLabel Command label to add.
|
||||
*
|
||||
|
||||
* @return True if the label was added, or if it was added already. False on error. */
|
||||
* @return True if the label was added, or if it was added already. False on error.
|
||||
*/
|
||||
public boolean addLabel(String commandLabel) {
|
||||
// Verify the label
|
||||
if(!isValidLabel(commandLabel))
|
||||
if (!isValidLabel(commandLabel))
|
||||
return false;
|
||||
|
||||
// Ensure this command isn't a duplicate
|
||||
if(hasLabel(commandLabel))
|
||||
if (hasLabel(commandLabel))
|
||||
return true;
|
||||
|
||||
// Add the command to the list
|
||||
@@ -206,12 +277,12 @@ public class CommandDescription {
|
||||
*
|
||||
* @param commandLabels List of command labels to add.
|
||||
*
|
||||
|
||||
* @return True if succeed, false on failure. */
|
||||
* @return True if succeed, false on failure.
|
||||
*/
|
||||
public boolean addLabels(List<String> commandLabels) {
|
||||
// Add each command label separately
|
||||
for(String cmd : commandLabels)
|
||||
if(!addLabel(cmd))
|
||||
for (String cmd : commandLabels)
|
||||
if (!addLabel(cmd))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@@ -221,12 +292,12 @@ public class CommandDescription {
|
||||
*
|
||||
* @param commandLabel Command to check for.
|
||||
*
|
||||
|
||||
* @return True if this command label equals to the param command. */
|
||||
* @return True if this command label equals to the param command.
|
||||
*/
|
||||
public boolean hasLabel(String commandLabel) {
|
||||
// Check whether any command matches with the argument
|
||||
for(String entry : this.labels)
|
||||
if(commandLabelEquals(entry, commandLabel))
|
||||
for (String entry : this.labels)
|
||||
if (commandLabelEquals(entry, commandLabel))
|
||||
return true;
|
||||
|
||||
// No match found, return false
|
||||
@@ -235,13 +306,15 @@ public class CommandDescription {
|
||||
|
||||
/**
|
||||
* Check whether this command description has a list of labels
|
||||
*
|
||||
* @param commandLabels List of labels
|
||||
|
||||
* @return True if all labels match, false otherwise */
|
||||
*
|
||||
* @return True if all labels match, false otherwise
|
||||
*/
|
||||
public boolean hasLabels(List<String> commandLabels) {
|
||||
// Check if there's a match for every command
|
||||
for(String cmd : commandLabels)
|
||||
if(!hasLabel(cmd))
|
||||
for (String cmd : commandLabels)
|
||||
if (!hasLabel(cmd))
|
||||
return false;
|
||||
|
||||
// There seems to be a match for every command, return true
|
||||
@@ -254,11 +327,11 @@ public class CommandDescription {
|
||||
*
|
||||
* @param commandReference The command reference.
|
||||
*
|
||||
|
||||
* @return True if the command reference is suitable to this command label, false otherwise. */
|
||||
* @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)
|
||||
if (commandReference.getCount() <= 0)
|
||||
return false;
|
||||
|
||||
// Get the parent count
|
||||
@@ -268,29 +341,6 @@ public class CommandDescription {
|
||||
return hasLabel(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a label is valid to use.
|
||||
*
|
||||
* @param label The label to test.
|
||||
*
|
||||
|
||||
* @return True if the label is valid to use, false otherwise. */
|
||||
public static boolean isValidLabel(String label) {
|
||||
// Make sure the label isn't null
|
||||
if(label == null)
|
||||
return false;
|
||||
|
||||
// Trim the label
|
||||
label = label.trim();
|
||||
|
||||
// Make sure the label is at least one character long
|
||||
if(label.length() <= 0)
|
||||
return false;
|
||||
|
||||
// Make sure the label doesn't contain any spaces, return the result
|
||||
return !label.contains(" ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the absolute command label, without a slash.
|
||||
*
|
||||
@@ -304,6 +354,7 @@ public class CommandDescription {
|
||||
* Get the absolute command label.
|
||||
*
|
||||
* @param includeSlash boolean
|
||||
*
|
||||
* @return Absolute command label.
|
||||
*/
|
||||
public String getAbsoluteLabel(boolean includeSlash) {
|
||||
@@ -313,14 +364,15 @@ public class CommandDescription {
|
||||
/**
|
||||
* Get the absolute command label.
|
||||
*
|
||||
* @param includeSlash boolean
|
||||
* @param reference CommandParts
|
||||
* @param includeSlash
|
||||
* @param reference
|
||||
*
|
||||
* @return Absolute command label.
|
||||
*/
|
||||
public String getAbsoluteLabel(boolean includeSlash, CommandParts reference) {
|
||||
// Get the command reference, and make sure it is valid
|
||||
CommandParts out = getCommandReference(reference);
|
||||
if(out == null)
|
||||
if (out == null)
|
||||
return "";
|
||||
|
||||
// Return the result
|
||||
@@ -339,7 +391,7 @@ public class CommandDescription {
|
||||
List<String> referenceList = new ArrayList<>();
|
||||
|
||||
// Check whether this command has a parent, if so, add the absolute parent command
|
||||
if(getParent() != null)
|
||||
if (getParent() != null)
|
||||
referenceList.addAll(getParent().getCommandReference(reference).getList());
|
||||
|
||||
// Get the current label
|
||||
@@ -363,14 +415,14 @@ public class CommandDescription {
|
||||
/**
|
||||
* Get the difference between this command and another command reference.
|
||||
*
|
||||
* @param other The other command reference.
|
||||
* @param other The other command reference.
|
||||
* @param fullCompare True to fully compare both command references.
|
||||
*
|
||||
* @return The command difference. Zero if there's no difference. A negative number on error.
|
||||
*/
|
||||
public double getCommandDifference(CommandParts other, boolean fullCompare) {
|
||||
// Make sure the reference is valid
|
||||
if(other == null)
|
||||
if (other == null)
|
||||
return -1;
|
||||
|
||||
// Get the command reference
|
||||
@@ -410,7 +462,7 @@ public class CommandDescription {
|
||||
/**
|
||||
* Execute the command, if possible.
|
||||
*
|
||||
* @param sender The command sender that triggered the execution of this command.
|
||||
* @param sender The command sender that triggered the execution of this command.
|
||||
* @param commandReference The command reference.
|
||||
* @param commandArguments The command arguments.
|
||||
*
|
||||
@@ -418,7 +470,7 @@ public class CommandDescription {
|
||||
*/
|
||||
public boolean execute(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Make sure the command is executable
|
||||
if(!isExecutable())
|
||||
if (!isExecutable())
|
||||
return false;
|
||||
|
||||
// Execute the command, return the result
|
||||
@@ -428,8 +480,8 @@ public class CommandDescription {
|
||||
/**
|
||||
* Get the parent command if this command description has a parent.
|
||||
*
|
||||
|
||||
* @return Parent command, or null */
|
||||
* @return Parent command, or null
|
||||
*/
|
||||
public CommandDescription getParent() {
|
||||
return this.parent;
|
||||
}
|
||||
@@ -437,11 +489,11 @@ public class CommandDescription {
|
||||
/**
|
||||
* Get the number of parent this description has.
|
||||
*
|
||||
|
||||
* @return The number of parents. */
|
||||
* @return The number of parents.
|
||||
*/
|
||||
public int getParentCount() {
|
||||
// Check whether the this description has a parent
|
||||
if(!hasParent())
|
||||
if (!hasParent())
|
||||
return 0;
|
||||
|
||||
// Get the parent count of the parent, return the result
|
||||
@@ -453,18 +505,18 @@ public class CommandDescription {
|
||||
*
|
||||
* @param parent Parent command.
|
||||
*
|
||||
|
||||
* @return True on success, false on failure. */
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
public boolean setParent(CommandDescription parent) {
|
||||
// Make sure the parent is different
|
||||
if(this.parent == parent)
|
||||
if (this.parent == parent)
|
||||
return true;
|
||||
|
||||
// Set the parent
|
||||
this.parent = parent;
|
||||
|
||||
// Make sure the parent isn't null
|
||||
if(parent == null)
|
||||
if (parent == null)
|
||||
return true;
|
||||
|
||||
// Add this description as a child to the parent
|
||||
@@ -474,8 +526,8 @@ public class CommandDescription {
|
||||
/**
|
||||
* Check whether the plugin description has a parent command.
|
||||
*
|
||||
|
||||
* @return True if the description has a parent command, false otherwise. */
|
||||
* @return True if the description has a parent command, false otherwise.
|
||||
*/
|
||||
public boolean hasParent() {
|
||||
return this.parent != null;
|
||||
}
|
||||
@@ -483,38 +535,12 @@ public class CommandDescription {
|
||||
/**
|
||||
* Get all command children.
|
||||
*
|
||||
|
||||
* @return Command children. */
|
||||
* @return Command children.
|
||||
*/
|
||||
public List<CommandDescription> getChildren() {
|
||||
return this.children;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a child to the command description.
|
||||
*
|
||||
* @param commandDescription The child to add.
|
||||
*
|
||||
|
||||
* @return True on success, false on failure. */
|
||||
public boolean addChild(CommandDescription commandDescription) {
|
||||
// Make sure the description is valid
|
||||
if(commandDescription == null)
|
||||
return false;
|
||||
if(!commandDescription.isValid())
|
||||
return false;
|
||||
|
||||
// Make sure the child doesn't exist already
|
||||
if(isChild(commandDescription))
|
||||
return true;
|
||||
|
||||
// The command description to add as a child
|
||||
if(!this.children.add(commandDescription))
|
||||
return false;
|
||||
|
||||
// Set this description as parent on the child
|
||||
return commandDescription.setParent(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the children of this command.
|
||||
*
|
||||
@@ -522,19 +548,45 @@ public class CommandDescription {
|
||||
*/
|
||||
public void setChildren(List<CommandDescription> children) {
|
||||
// Check whether the children list should be cleared
|
||||
if(children == null)
|
||||
if (children == null)
|
||||
this.children.clear();
|
||||
|
||||
else
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a child to the command description.
|
||||
*
|
||||
* @param commandDescription The child to add.
|
||||
*
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
public boolean addChild(CommandDescription commandDescription) {
|
||||
// Make sure the description is valid
|
||||
if (commandDescription == null)
|
||||
return false;
|
||||
if (!commandDescription.isValid())
|
||||
return false;
|
||||
|
||||
// Make sure the child doesn't exist already
|
||||
if (isChild(commandDescription))
|
||||
return true;
|
||||
|
||||
// The command description to add as a child
|
||||
if (!this.children.add(commandDescription))
|
||||
return false;
|
||||
|
||||
// Set this description as parent on the child
|
||||
return commandDescription.setParent(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this command has any child labels.
|
||||
*
|
||||
|
||||
* @return True if this command has any child labels. */
|
||||
public boolean hasChilds() {
|
||||
* @return True if this command has any child labels.
|
||||
*/
|
||||
public boolean hasChildren() {
|
||||
return (this.children.size() != 0);
|
||||
}
|
||||
|
||||
@@ -543,13 +595,13 @@ public class CommandDescription {
|
||||
*
|
||||
* @param commandDescription The command description to check for.
|
||||
*
|
||||
|
||||
* @return True if this command description has the specific child, false otherwise. */
|
||||
* @return True if this command description has the specific child, false otherwise.
|
||||
*/
|
||||
public boolean isChild(CommandDescription commandDescription) {
|
||||
// Make sure the description is valid
|
||||
if(commandDescription == null)
|
||||
if (commandDescription == null)
|
||||
return false;
|
||||
if(!commandDescription.isValid())
|
||||
if (!commandDescription.isValid())
|
||||
return false;
|
||||
|
||||
// Check whether this child exists, return the result
|
||||
@@ -561,15 +613,15 @@ public class CommandDescription {
|
||||
*
|
||||
* @param argument The argument to add.
|
||||
*
|
||||
|
||||
* @return True if succeed, false if failed. */
|
||||
* @return True if succeed, false if failed.
|
||||
*/
|
||||
public boolean addArgument(CommandArgumentDescription argument) {
|
||||
// Make sure the argument is valid
|
||||
if(argument == null)
|
||||
if (argument == null)
|
||||
return false;
|
||||
|
||||
// Make sure the argument isn't added already
|
||||
if(hasArgument(argument))
|
||||
if (hasArgument(argument))
|
||||
return true;
|
||||
|
||||
// Add the argument, return the result
|
||||
@@ -630,9 +682,9 @@ public class CommandDescription {
|
||||
int optionalArgument = 0;
|
||||
|
||||
// Loop through each argument
|
||||
for(CommandArgumentDescription argument : this.arguments) {
|
||||
for (CommandArgumentDescription argument : this.arguments) {
|
||||
// Check whether the command is optional
|
||||
if(!argument.isOptional()) {
|
||||
if (!argument.isOptional()) {
|
||||
requiredArguments += optionalArgument + 1;
|
||||
optionalArgument = 0;
|
||||
|
||||
@@ -647,11 +699,11 @@ public class CommandDescription {
|
||||
/**
|
||||
* Get the maximum number of arguments.
|
||||
*
|
||||
|
||||
* @return The maximum number of arguments. A negative number will be returned if there's no maximum. */
|
||||
* @return The maximum number of arguments. A negative number will be returned if there's no maximum.
|
||||
*/
|
||||
public int getMaximumArguments() {
|
||||
// Check whether there is a maximum set
|
||||
if(this.noArgumentMaximum)
|
||||
if (this.noArgumentMaximum)
|
||||
return -1;
|
||||
|
||||
// Return the maximum based on the registered arguments
|
||||
@@ -671,8 +723,8 @@ public class CommandDescription {
|
||||
/**
|
||||
* Get the command description.
|
||||
*
|
||||
|
||||
* @return Command description. */
|
||||
* @return Command description.
|
||||
*/
|
||||
public String getDescription() {
|
||||
return hasDescription() ? this.description : this.detailedDescription;
|
||||
}
|
||||
@@ -683,7 +735,7 @@ public class CommandDescription {
|
||||
* @param description New command description. Null to reset the description.
|
||||
*/
|
||||
public void setDescription(String description) {
|
||||
if(description == null)
|
||||
if (description == null)
|
||||
this.description = "";
|
||||
|
||||
else
|
||||
@@ -693,8 +745,8 @@ public class CommandDescription {
|
||||
/**
|
||||
* Check whether this command has any description.
|
||||
*
|
||||
|
||||
* @return True if this command has any description. */
|
||||
* @return True if this command has any description.
|
||||
*/
|
||||
public boolean hasDescription() {
|
||||
return (this.description.trim().length() != 0);
|
||||
}
|
||||
@@ -702,8 +754,8 @@ public class CommandDescription {
|
||||
/**
|
||||
* Get the command detailed description.
|
||||
*
|
||||
|
||||
* @return Command detailed description. */
|
||||
* @return Command detailed description.
|
||||
*/
|
||||
public String getDetailedDescription() {
|
||||
return hasDetailedDescription() ? this.detailedDescription : this.description;
|
||||
}
|
||||
@@ -714,7 +766,7 @@ public class CommandDescription {
|
||||
* @param detailedDescription New command description. Null to reset the description.
|
||||
*/
|
||||
public void setDetailedDescription(String detailedDescription) {
|
||||
if(detailedDescription == null)
|
||||
if (detailedDescription == null)
|
||||
this.detailedDescription = "";
|
||||
|
||||
else
|
||||
@@ -724,8 +776,8 @@ public class CommandDescription {
|
||||
/**
|
||||
* Check whether this command has any detailed description.
|
||||
*
|
||||
|
||||
* @return True if this command has any detailed description. */
|
||||
* @return True if this command has any detailed description.
|
||||
*/
|
||||
public boolean hasDetailedDescription() {
|
||||
return (this.detailedDescription.trim().length() != 0);
|
||||
}
|
||||
@@ -735,35 +787,35 @@ public class CommandDescription {
|
||||
*
|
||||
* @param queryReference The query reference to find a command for.
|
||||
*
|
||||
|
||||
* @return The command found, or null. */
|
||||
* @return The command found, or null.
|
||||
*/
|
||||
public FoundCommandResult findCommand(final CommandParts queryReference) {
|
||||
// Make sure the command reference is valid
|
||||
if(queryReference.getCount() <= 0)
|
||||
if (queryReference.getCount() <= 0)
|
||||
return null;
|
||||
|
||||
// Check whether this description is for the last element in the command reference, if so return the current command
|
||||
if(queryReference.getCount() <= getParentCount() + 1)
|
||||
if (queryReference.getCount() <= getParentCount() + 1)
|
||||
return new FoundCommandResult(
|
||||
this,
|
||||
getCommandReference(queryReference),
|
||||
new CommandParts(),
|
||||
queryReference);
|
||||
this,
|
||||
getCommandReference(queryReference),
|
||||
new CommandParts(),
|
||||
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));
|
||||
|
||||
// Handle the child's, if this command has any
|
||||
if(getChildren().size() > 0) {
|
||||
if (getChildren().size() > 0) {
|
||||
// Get a new instance of the child's list, and sort them by their difference in comparison to the query reference
|
||||
List<CommandDescription> commandChildren = new ArrayList<>(getChildren());
|
||||
Collections.sort(commandChildren, new Comparator<CommandDescription>() {
|
||||
@Override
|
||||
public int compare(CommandDescription o1, CommandDescription o2) {
|
||||
return Double.compare(
|
||||
o1.getCommandDifference(queryReference),
|
||||
o2.getCommandDifference(queryReference));
|
||||
o1.getCommandDifference(queryReference),
|
||||
o2.getCommandDifference(queryReference));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -771,21 +823,21 @@ public class CommandDescription {
|
||||
double firstChildDifference = commandChildren.get(0).getCommandDifference(queryReference, true);
|
||||
|
||||
// Check if the reference perfectly suits the arguments of the current command if it doesn't perfectly suits a child command
|
||||
if(firstChildDifference > 0.0)
|
||||
if(getSuitableArgumentsDifference(queryReference) == 0)
|
||||
if (firstChildDifference > 0.0)
|
||||
if (getSuitableArgumentsDifference(queryReference) == 0)
|
||||
return new FoundCommandResult(this, newReference, newArguments, queryReference);
|
||||
|
||||
// Loop through each child
|
||||
for(CommandDescription child : commandChildren) {
|
||||
for (CommandDescription child : commandChildren) {
|
||||
// Get the best suitable command
|
||||
FoundCommandResult result = child.findCommand(queryReference);
|
||||
if(result != null)
|
||||
if (result != null)
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the remaining command reference elements fit the arguments for this command
|
||||
if(getSuitableArgumentsDifference(queryReference) >= 0)
|
||||
if (getSuitableArgumentsDifference(queryReference) >= 0)
|
||||
return new FoundCommandResult(this, newReference, newArguments, queryReference);
|
||||
|
||||
// No command found, return null
|
||||
@@ -797,8 +849,8 @@ public class CommandDescription {
|
||||
*
|
||||
* @param commandReference The command reference.
|
||||
*
|
||||
|
||||
* @return True if so, false otherwise. */
|
||||
* @return True if so, false otherwise.
|
||||
*/
|
||||
public boolean hasSuitableCommand(CommandParts commandReference) {
|
||||
return findCommand(commandReference) != null;
|
||||
}
|
||||
@@ -808,8 +860,8 @@ public class CommandDescription {
|
||||
*
|
||||
* @param commandReference The command reference.
|
||||
*
|
||||
|
||||
* @return True if the arguments are suitable, false otherwise. */
|
||||
* @return True if the arguments are suitable, false otherwise.
|
||||
*/
|
||||
public boolean hasSuitableArguments(CommandParts commandReference) {
|
||||
return getSuitableArgumentsDifference(commandReference) == 0;
|
||||
}
|
||||
@@ -866,27 +918,13 @@ public class CommandDescription {
|
||||
/**
|
||||
* Set the command permissions.
|
||||
*
|
||||
* @param permissionNode The permission node required.
|
||||
* @param permissionNode The permission node required.
|
||||
* @param defaultPermission The default permission.
|
||||
*/
|
||||
public void setCommandPermissions(String permissionNode, CommandPermissions.DefaultPermission defaultPermission) {
|
||||
this.permissions = new CommandPermissions(permissionNode, defaultPermission);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether two labels equal to each other.
|
||||
*
|
||||
* @param commandLabel The first command label.
|
||||
* @param otherCommandLabel The other command label.
|
||||
*
|
||||
* @return True if the labels are equal to each other.
|
||||
*/
|
||||
private static boolean commandLabelEquals(String commandLabel, String otherCommandLabel) {
|
||||
commandLabel = commandLabel.trim();
|
||||
otherCommandLabel = otherCommandLabel.trim();
|
||||
return commandLabel.equalsIgnoreCase(otherCommandLabel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the command description has been set up properly.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user