Skip to content
Permalink
Browse files

Filter suggestions more intuitively

  • Loading branch information
DarkArc committed Jan 5, 2020
1 parent 19e5d10 commit beec3074eb1aff916dbae61ca6d5d3654b454f35
@@ -49,13 +49,19 @@ public Component describeAcceptableArguments() {
public List<String> getSuggestions(String input) {
List<String> suggestions = new ArrayList<>();

SuggestionHelper.trialAddPlayerSuggestion(suggestions, "*");
SuggestionHelper.trialAddPlayerSuggestion(suggestions, "#world");
SuggestionHelper.trialAddPlayerSuggestion(suggestions, "#near");
if (input.isEmpty() || input.equals("*")) {
SuggestionHelper.trialAddPlayerSuggestion(suggestions, "*");
}

SuggestionHelper.trialAddPlayerSuggestion(suggestions, input + "*");
SuggestionHelper.trialAddPlayerSuggestion(suggestions, "*" + input);

SuggestionHelper.addPlayerNameSuggestions(suggestions);
if (input.isEmpty() || input.startsWith("#")) {
SuggestionHelper.trialAddPlayerSuggestion(suggestions, "#world");
SuggestionHelper.trialAddPlayerSuggestion(suggestions, "#near");
}

SuggestionHelper.addPlayerNameSuggestions(suggestions, input);

return suggestions;
}
@@ -49,7 +49,7 @@ public Component describeAcceptableArguments() {
public List<String> getSuggestions(String input) {
List<String> suggestions = new ArrayList<>();

SuggestionHelper.addPlayerNameSuggestions(suggestions);
SuggestionHelper.addPlayerNameSuggestions(suggestions, input);

return suggestions;
}
@@ -109,7 +109,7 @@ public Component describeAcceptableArguments() {
List<String> suggestions = new ArrayList<>();

if (normalizedSplit(input).length == 1) {
SuggestionHelper.addPlayerNameSuggestions(suggestions);
SuggestionHelper.addPlayerNameSuggestions(suggestions, input);
}

return suggestions;
@@ -253,7 +253,7 @@ public static int matchMCWorldTime(String timeStr) throws CommandException {
}

// Handle special hash tag groups
if (filter.charAt(0) == '#') {
if (!filter.isEmpty() && filter.charAt(0) == '#') {
// Handle #world, which matches player of the same world as the
// calling source
if (filter.equalsIgnoreCase("#world")) {
@@ -563,7 +563,7 @@ public static Location matchLocation(CommandSender source, String filter)
}

// Handle special hash tag groups
} else if (filter.charAt(0) == '#') {
} else if (!filter.isEmpty() && filter.charAt(0) == '#') {
String[] args = filter.split(":");

// Handle #world, which matches player of the same world as the
@@ -4,6 +4,7 @@
import com.sk89q.commandbook.util.InputUtil;
import com.sk89q.minecraft.util.commands.CommandException;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Collection;
@@ -12,24 +13,40 @@
public class SuggestionHelper {
private SuggestionHelper() { }

public static void trialAddPlayerSuggestion(List<String> suggestions, String trailText) {
public static void trialAddPlayerSuggestion(List<String> suggestions, String trailText, int minMatch) {
try {
InputUtil.PlayerParser.matchPlayers(CommandBook.server().getConsoleSender(), trailText);
suggestions.add(trailText);
CommandSender sender = CommandBook.server().getConsoleSender();
int numMatched = InputUtil.PlayerParser.matchPlayers(sender, trailText).size();
if (numMatched >= minMatch) {
suggestions.add(trailText);
}
} catch (CommandException ignored) { }
}

public static void addPlayerNameSuggestions(List<String> suggestions) {
public static void trialAddPlayerSuggestion(List<String> suggestions, String trialText) {
trialAddPlayerSuggestion(suggestions, trialText, 2);
}

public static void addPlayerNameSuggestions(List<String> suggestions, String input) {
input = input.toLowerCase();

Collection<? extends Player> players = CommandBook.server().getOnlinePlayers();
boolean useDisplayNames = CommandBook.inst().lookupWithDisplayNames;

for (Player player : players) {
String playerName = player.getName();
String displayName = ChatColor.stripColor(player.getDisplayName());

suggestions.add(playerName);
if (useDisplayNames && !playerName.equals(displayName)) {
suggestions.add(displayName);
String lowerPlayerName = playerName.toLowerCase();
if (input.isEmpty() || lowerPlayerName.contains(input)) {
suggestions.add(playerName);
}

String displayName = ChatColor.stripColor(player.getDisplayName());
String lowerDisplayName = displayName.toLowerCase();
if (useDisplayNames && !lowerPlayerName.equals(lowerDisplayName)) {
if (input.isEmpty() || lowerDisplayName.contains(input)) {
suggestions.add(displayName);
}
}
}
}

0 comments on commit beec307

Please sign in to comment.
You can’t perform that action at this time.