Tab completion

This commit is contained in:
boy0001 2015-05-03 00:20:04 +10:00
parent b45eaf1c90
commit e24d0c5356
2 changed files with 19 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.StringComparison;
import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; import com.intellectualcrafters.plot.util.bukkit.BukkitUtil;
/** /**
@ -46,13 +47,22 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
return null; return null;
} }
final List<String> tabOptions = new ArrayList<>(); final List<String> 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(); final String arg = strings[0].toLowerCase();
for (final SubCommand cmd : MainCommand.subCommands) { for (final SubCommand cmd : MainCommand.subCommands) {
if (cmd.permission.hasPermission(player)) { if (!cmd.cmd.equalsIgnoreCase(best)) {
if (cmd.cmd.startsWith(arg)) { if (cmd.permission.hasPermission(player)) {
tabOptions.add(cmd.cmd); if (cmd.cmd.startsWith(arg)) {
} else if (cmd.alias.size() > 0 && cmd.alias.get(0).startsWith(arg)) { tabOptions.add(cmd.cmd);
tabOptions.add(cmd.alias.get(0)); } else if (cmd.alias.size() > 0 && cmd.alias.get(0).startsWith(arg)) {
tabOptions.add(cmd.alias.get(0));
}
} }
} }
} }

View File

@ -78,10 +78,12 @@ public class StringComparison {
*/ */
public static int compare(final String s1, final String s2) { public static int compare(final String s1, final String s2) {
int distance = StringUtils.getLevenshteinDistance(s1, 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 -= 4;
} }
// distance += Math.abs(s1.length() - s2.length());
return distance; return distance;
} }