Merge ListUtil into StringUtil; refactor HelpSyntaxHelper + create test
The HelpSyntaxHelper had suppressed warnings for string concatenation within StringBuilder - the point of the StringBuilder is that it is faster when you use it to concatenate many elements. If you still use string concatenation with + within these calls it beats the purpose. (cherry picked from commit bb00be2)
This commit is contained in:
@@ -3,7 +3,6 @@ package fr.xephi.authme.command;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import fr.xephi.authme.util.ListUtils;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -165,8 +164,8 @@ public class CommandParts {
|
||||
*
|
||||
* @param other The other reference.
|
||||
*
|
||||
|
||||
* @return The result from zero to above. A negative number will be returned on error. */
|
||||
* @return The result from zero to above. A negative number will be returned on error.
|
||||
*/
|
||||
public double getDifference(CommandParts other) {
|
||||
return getDifference(other, false);
|
||||
}
|
||||
@@ -177,8 +176,8 @@ public class CommandParts {
|
||||
* @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. */
|
||||
* @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)
|
||||
@@ -196,10 +195,10 @@ public class CommandParts {
|
||||
/**
|
||||
* Convert the parts to a string.
|
||||
*
|
||||
|
||||
* @return The part as a string. */
|
||||
* @return The part as a string.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return ListUtils.implode(this.parts, " ");
|
||||
return StringUtils.join(" ", this.parts);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package fr.xephi.authme.command.help;
|
||||
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import fr.xephi.authme.command.CommandArgumentDescription;
|
||||
import fr.xephi.authme.command.CommandDescription;
|
||||
import fr.xephi.authme.command.CommandParts;
|
||||
import fr.xephi.authme.util.ListUtils;
|
||||
|
||||
/**
|
||||
* Helper class for formatting a command's structure (name and arguments)
|
||||
* for a Minecraft user.
|
||||
*/
|
||||
public final class HelpSyntaxHelper {
|
||||
|
||||
@@ -16,52 +18,63 @@ public final class HelpSyntaxHelper {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the proper syntax for a command.
|
||||
* Get the formatted syntax for a command.
|
||||
*
|
||||
* @param commandDescription The command to get the syntax for.
|
||||
* @param commandDescription The command to build the syntax for.
|
||||
* @param commandReference The reference of the command.
|
||||
* @param alternativeLabel The alternative label to use for this command syntax.
|
||||
* @param highlight True to highlight the important parts of this command.
|
||||
*
|
||||
* @return The command with proper syntax.
|
||||
*/
|
||||
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
|
||||
public static String getCommandSyntax(CommandDescription commandDescription, CommandParts commandReference, String alternativeLabel, boolean highlight) {
|
||||
// Create a string builder to build the command
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// Set the color and prefix a slash
|
||||
sb.append(ChatColor.WHITE + "/");
|
||||
public static String getCommandSyntax(CommandDescription commandDescription, CommandParts commandReference,
|
||||
String alternativeLabel, boolean highlight) {
|
||||
// Create a string builder with white color and prefixed slash
|
||||
StringBuilder sb = new StringBuilder()
|
||||
.append(ChatColor.WHITE)
|
||||
.append("/");
|
||||
|
||||
// Get the help command reference, and the command label
|
||||
CommandParts helpCommandReference = commandDescription.getCommandReference(commandReference);
|
||||
final String parentCommand = (new CommandParts(helpCommandReference.getRange(0, helpCommandReference.getCount() - 1))).toString();
|
||||
String commandLabel = helpCommandReference.get(helpCommandReference.getCount() - 1);
|
||||
final String parentCommand = new CommandParts(
|
||||
helpCommandReference.getRange(0, helpCommandReference.getCount() - 1)).toString();
|
||||
|
||||
// Check whether the alternative label should be used
|
||||
if (alternativeLabel != null && alternativeLabel.trim().length() > 0) {
|
||||
String commandLabel;
|
||||
if (StringUtils.isEmpty(alternativeLabel)) {
|
||||
commandLabel = helpCommandReference.get(helpCommandReference.getCount() - 1);
|
||||
} else {
|
||||
commandLabel = alternativeLabel;
|
||||
}
|
||||
|
||||
// Show the important bit of the command, highlight this part if required
|
||||
sb.append(ListUtils.implode(parentCommand, (highlight ? ChatColor.YELLOW + "" + ChatColor.BOLD : "") + commandLabel, " "));
|
||||
if(highlight)
|
||||
sb.append(ChatColor.YELLOW);
|
||||
sb.append(parentCommand)
|
||||
.append(" ")
|
||||
.append(highlight ? ChatColor.YELLOW.toString() + ChatColor.BOLD : "")
|
||||
.append(commandLabel);
|
||||
|
||||
// Add each command arguments
|
||||
for(CommandArgumentDescription arg : commandDescription.getArguments()) {
|
||||
// Add the argument as optional or non-optional argument
|
||||
if(!arg.isOptional())
|
||||
sb.append(ChatColor.ITALIC + " <" + arg.getLabel() + ">");
|
||||
else
|
||||
sb.append(ChatColor.ITALIC + " [" + arg.getLabel() + "]");
|
||||
if (highlight) {
|
||||
sb.append(ChatColor.YELLOW);
|
||||
}
|
||||
|
||||
// Add each command argument
|
||||
for (CommandArgumentDescription arg : commandDescription.getArguments()) {
|
||||
sb.append(ChatColor.ITALIC).append(formatArgument(arg));
|
||||
}
|
||||
|
||||
// Add some dots if the command allows unlimited arguments
|
||||
if(commandDescription.getMaximumArguments() < 0)
|
||||
sb.append(ChatColor.ITALIC + " ...");
|
||||
if (commandDescription.getMaximumArguments() < 0) {
|
||||
sb.append(ChatColor.ITALIC).append(" ...");
|
||||
}
|
||||
|
||||
// Return the build command syntax
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String formatArgument(CommandArgumentDescription argument) {
|
||||
if (argument.isOptional()) {
|
||||
return " [" + argument.getLabel() + "]";
|
||||
}
|
||||
return " <" + argument.getLabel() + ">";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user