From 2effc57b7860f7c16af6c0f5ba22e9b58febc909 Mon Sep 17 00:00:00 2001 From: sk89q Date: Mon, 30 Jun 2014 23:17:44 -0700 Subject: [PATCH] When sorting commands for //help, ignore / characters. --- .../worldedit/command/UtilityCommands.java | 9 ++++---- .../extension/platform/CommandManager.java | 2 ++ .../util/command/PrimaryAliasComparator.java | 22 ++++++++++++++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java b/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java index ce43904b9..fc3ba4311 100644 --- a/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java +++ b/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java @@ -35,6 +35,7 @@ import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.entity.Player; 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.SingleBlockPattern; 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.PrimaryAliasComparator; 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.components.CommandListBox; -import com.sk89q.worldedit.util.formatting.components.CommandUsageBox; import com.sk89q.worldedit.util.formatting.Style; 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 java.util.ArrayList; @@ -606,7 +607,7 @@ public class UtilityCommands { // Get a list of aliases List aliases = new ArrayList(dispatcher.getCommands()); - Collections.sort(aliases, PrimaryAliasComparator.INSTANCE); + Collections.sort(aliases, new PrimaryAliasComparator(CommandManager.COMMAND_CLEAN_PATTERN)); // Calculate pagination int offset = perPage * page; diff --git a/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java b/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java index a477043bf..1f1afcc98 100644 --- a/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java +++ b/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java @@ -52,6 +52,7 @@ import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.regex.Pattern; 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 static final Pattern COMMAND_CLEAN_PATTERN = Pattern.compile("^[/]+"); 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: \"(.*)\"$"); diff --git a/src/main/java/com/sk89q/worldedit/util/command/PrimaryAliasComparator.java b/src/main/java/com/sk89q/worldedit/util/command/PrimaryAliasComparator.java index bcc4197ff..b7b980acf 100644 --- a/src/main/java/com/sk89q/worldedit/util/command/PrimaryAliasComparator.java +++ b/src/main/java/com/sk89q/worldedit/util/command/PrimaryAliasComparator.java @@ -19,7 +19,9 @@ package com.sk89q.worldedit.util.command; +import javax.annotation.Nullable; import java.util.Comparator; +import java.util.regex.Pattern; /** * Compares the primary aliases of two {@link CommandMapping} using @@ -30,14 +32,28 @@ public final class PrimaryAliasComparator implements Comparator /** * 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 public int compare(CommandMapping o1, CommandMapping o2) { - return o1.getPrimaryAlias().compareTo(o2.getPrimaryAlias()); + return clean(o1.getPrimaryAlias()).compareTo(clean(o2.getPrimaryAlias())); } }