Skip to content

Commit

Permalink
Filter suggestions more intuitively
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkArc committed Jan 5, 2020
1 parent 19e5d10 commit beec307
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
Expand Up @@ -49,13 +49,19 @@ public ConversionResult<MultiPlayerTarget> convert(String argument, InjectedValu
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;
}
Expand Down
Expand Up @@ -49,7 +49,7 @@ public ConversionResult<SinglePlayerTarget> convert(String argument, InjectedVal
public List<String> getSuggestions(String input) {
List<String> suggestions = new ArrayList<>();

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

return suggestions;
}
Expand Down
Expand Up @@ -109,7 +109,7 @@ public List<String> getSuggestions(String input) {
List<String> suggestions = new ArrayList<>();

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

return suggestions;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/sk89q/commandbook/util/InputUtil.java
Expand Up @@ -253,7 +253,7 @@ public static List<Player> matchPlayers(CommandSender source, String filter) thr
}

// 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")) {
Expand Down Expand Up @@ -563,7 +563,7 @@ private static List<Location> matchLocations(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
Expand Down
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
}
Expand Down

0 comments on commit beec307

Please sign in to comment.