When sorting commands for //help, ignore / characters.

This commit is contained in:
sk89q 2014-06-30 23:17:44 -07:00
parent 75b1fe6090
commit 2effc57b78
3 changed files with 26 additions and 7 deletions

View File

@ -35,6 +35,7 @@ import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.CommandManager;
import com.sk89q.worldedit.patterns.Pattern; import com.sk89q.worldedit.patterns.Pattern;
import com.sk89q.worldedit.patterns.SingleBlockPattern; import com.sk89q.worldedit.patterns.SingleBlockPattern;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
@ -44,12 +45,12 @@ import com.sk89q.worldedit.util.command.CommandMapping;
import com.sk89q.worldedit.util.command.Dispatcher; import com.sk89q.worldedit.util.command.Dispatcher;
import com.sk89q.worldedit.util.command.PrimaryAliasComparator; import com.sk89q.worldedit.util.command.PrimaryAliasComparator;
import com.sk89q.worldedit.util.command.parametric.Optional; import com.sk89q.worldedit.util.command.parametric.Optional;
import com.sk89q.worldedit.util.formatting.components.Code;
import com.sk89q.worldedit.util.formatting.ColorCodeBuilder; import com.sk89q.worldedit.util.formatting.ColorCodeBuilder;
import com.sk89q.worldedit.util.formatting.components.CommandListBox;
import com.sk89q.worldedit.util.formatting.components.CommandUsageBox;
import com.sk89q.worldedit.util.formatting.Style; import com.sk89q.worldedit.util.formatting.Style;
import com.sk89q.worldedit.util.formatting.StyledFragment; import com.sk89q.worldedit.util.formatting.StyledFragment;
import com.sk89q.worldedit.util.formatting.components.Code;
import com.sk89q.worldedit.util.formatting.components.CommandListBox;
import com.sk89q.worldedit.util.formatting.components.CommandUsageBox;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
import java.util.ArrayList; import java.util.ArrayList;
@ -606,7 +607,7 @@ public class UtilityCommands {
// Get a list of aliases // Get a list of aliases
List<CommandMapping> aliases = new ArrayList<CommandMapping>(dispatcher.getCommands()); List<CommandMapping> aliases = new ArrayList<CommandMapping>(dispatcher.getCommands());
Collections.sort(aliases, PrimaryAliasComparator.INSTANCE); Collections.sort(aliases, new PrimaryAliasComparator(CommandManager.COMMAND_CLEAN_PATTERN));
// Calculate pagination // Calculate pagination
int offset = perPage * page; int offset = perPage * page;

View File

@ -52,6 +52,7 @@ import java.io.IOException;
import java.util.logging.FileHandler; import java.util.logging.FileHandler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Pattern;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -62,6 +63,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/ */
public final class CommandManager { public final class CommandManager {
public static final Pattern COMMAND_CLEAN_PATTERN = Pattern.compile("^[/]+");
private static final Logger logger = Logger.getLogger(CommandManager.class.getCanonicalName()); private static final Logger logger = Logger.getLogger(CommandManager.class.getCanonicalName());
private static final java.util.regex.Pattern numberFormatExceptionPattern = java.util.regex.Pattern.compile("^For input string: \"(.*)\"$"); private static final java.util.regex.Pattern numberFormatExceptionPattern = java.util.regex.Pattern.compile("^For input string: \"(.*)\"$");

View File

@ -19,7 +19,9 @@
package com.sk89q.worldedit.util.command; package com.sk89q.worldedit.util.command;
import javax.annotation.Nullable;
import java.util.Comparator; import java.util.Comparator;
import java.util.regex.Pattern;
/** /**
* Compares the primary aliases of two {@link CommandMapping} using * Compares the primary aliases of two {@link CommandMapping} using
@ -30,14 +32,28 @@ public final class PrimaryAliasComparator implements Comparator<CommandMapping>
/** /**
* An instance of this class. * An instance of this class.
*/ */
public static final PrimaryAliasComparator INSTANCE = new PrimaryAliasComparator(); public static final PrimaryAliasComparator INSTANCE = new PrimaryAliasComparator(null);
private final @Nullable Pattern removalPattern;
private PrimaryAliasComparator() { /**
* Create a new instance.
*
* @param removalPattern a regex to remove unwanted characters from the compared aliases
*/
public PrimaryAliasComparator(@Nullable Pattern removalPattern) {
this.removalPattern = removalPattern;
}
private String clean(String alias) {
if (removalPattern != null) {
return removalPattern.matcher(alias).replaceAll("");
}
return alias;
} }
@Override @Override
public int compare(CommandMapping o1, CommandMapping o2) { public int compare(CommandMapping o1, CommandMapping o2) {
return o1.getPrimaryAlias().compareTo(o2.getPrimaryAlias()); return clean(o1.getPrimaryAlias()).compareTo(clean(o2.getPrimaryAlias()));
} }
} }