Skip to content

Commit

Permalink
fix(bukkit): improve invalid user handling in OfflinePlayerParser
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jan 20, 2024
1 parent 14bb0cb commit bd0d349
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.arguments.parser.ParserDescriptor;
import cloud.commandframework.arguments.suggestion.BlockingSuggestionProvider;
import cloud.commandframework.arguments.suggestion.Suggestion;
import cloud.commandframework.bukkit.BukkitCaptionKeys;
import cloud.commandframework.bukkit.BukkitCommandContextKeys;
import cloud.commandframework.captions.CaptionVariable;
Expand All @@ -51,7 +50,7 @@
*
* @param <C> Command sender type
*/
public final class OfflinePlayerParser<C> implements ArgumentParser<C, OfflinePlayer>, BlockingSuggestionProvider<C> {
public final class OfflinePlayerParser<C> implements ArgumentParser<C, OfflinePlayer>, BlockingSuggestionProvider.Strings<C> {

/**
* Creates a new offline player parser.
Expand Down Expand Up @@ -84,26 +83,29 @@ public final class OfflinePlayerParser<C> implements ArgumentParser<C, OfflinePl
final @NonNull CommandInput commandInput
) {
final String input = commandInput.readString();
if (input.length() > 16) {
return ArgumentParseResult.failure(new OfflinePlayerParseException(input, commandContext));
}

final OfflinePlayer player = Bukkit.getOfflinePlayer(input);

if (!player.hasPlayedBefore() && !player.isOnline()) {
final OfflinePlayer player;
try {
player = Bukkit.getOfflinePlayer(input);
} catch (final Exception e) {
return ArgumentParseResult.failure(new OfflinePlayerParseException(input, commandContext));
}

return ArgumentParseResult.success(player);
}

@Override
public @NonNull Iterable<@NonNull Suggestion> suggestions(
public @NonNull Iterable<@NonNull String> stringSuggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull CommandInput input
) {
final CommandSender bukkit = commandContext.get(BukkitCommandContextKeys.BUKKIT_COMMAND_SENDER);
return Bukkit.getOnlinePlayers().stream()
.filter(player -> !(bukkit instanceof Player && !((Player) bukkit).canSee(player)))
.map(Player::getName)
.map(Suggestion::simple)
.collect(Collectors.toList());
}

Expand Down

0 comments on commit bd0d349

Please sign in to comment.