From e24d0c53560f865fe97d0f72ad9f42be2fa276a2 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sun, 3 May 2015 00:20:04 +1000 Subject: [PATCH] Tab completion --- .../plot/commands/BukkitCommand.java | 20 ++++++++++++++----- .../plot/util/StringComparison.java | 6 ++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/BukkitCommand.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/BukkitCommand.java index f9e375390..4aea2ba5b 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/BukkitCommand.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/BukkitCommand.java @@ -11,6 +11,7 @@ import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.StringComparison; import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; /** @@ -46,13 +47,22 @@ public class BukkitCommand implements CommandExecutor, TabCompleter { return null; } final List tabOptions = new ArrayList<>(); + final String[] commands = new String[MainCommand.subCommands.size()]; + for (int x = 0; x < MainCommand.subCommands.size(); x++) { + commands[x] = MainCommand.subCommands.get(x).cmd; + } + String best = new StringComparison(strings[0], commands).getBestMatch(); + tabOptions.add(best); + System.out.print("TAB OPTIONS"); final String arg = strings[0].toLowerCase(); for (final SubCommand cmd : MainCommand.subCommands) { - if (cmd.permission.hasPermission(player)) { - if (cmd.cmd.startsWith(arg)) { - tabOptions.add(cmd.cmd); - } else if (cmd.alias.size() > 0 && cmd.alias.get(0).startsWith(arg)) { - tabOptions.add(cmd.alias.get(0)); + if (!cmd.cmd.equalsIgnoreCase(best)) { + if (cmd.permission.hasPermission(player)) { + if (cmd.cmd.startsWith(arg)) { + tabOptions.add(cmd.cmd); + } else if (cmd.alias.size() > 0 && cmd.alias.get(0).startsWith(arg)) { + tabOptions.add(cmd.alias.get(0)); + } } } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/StringComparison.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/StringComparison.java index 7c4f71842..07dd3b116 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/StringComparison.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/StringComparison.java @@ -78,10 +78,12 @@ public class StringComparison { */ public static int compare(final String s1, final String s2) { int distance = StringUtils.getLevenshteinDistance(s1, s2); - if (s2.contains(s1) || s2.contains(s1)) { + if (s2.contains(s1)) { + distance -= (Math.min(s1.length(), s2.length())); + } + if (s2.startsWith(s1)) { distance -= 4; } -// distance += Math.abs(s1.length() - s2.length()); return distance; }