diff --git a/src/main/kotlin/cc/modlabs/kpaper/command/arguments/PlayerArgument.kt b/src/main/kotlin/cc/modlabs/kpaper/command/arguments/PlayerArgument.kt new file mode 100644 index 0000000..ac47ac6 --- /dev/null +++ b/src/main/kotlin/cc/modlabs/kpaper/command/arguments/PlayerArgument.kt @@ -0,0 +1,43 @@ +package cc.modlabs.kpaper.command.arguments + +import com.mojang.brigadier.StringReader +import com.mojang.brigadier.arguments.ArgumentType +import com.mojang.brigadier.arguments.StringArgumentType +import com.mojang.brigadier.context.CommandContext +import com.mojang.brigadier.suggestion.Suggestions +import com.mojang.brigadier.suggestion.SuggestionsBuilder +import io.papermc.paper.command.brigadier.argument.CustomArgumentType +import org.bukkit.Bukkit +import org.bukkit.entity.Player +import java.util.concurrent.CompletableFuture + +class PlayerArgument : CustomArgumentType { + + override fun parse(reader: StringReader): Player { + val stringNotParsed = reader.readString() + val player = Bukkit.getPlayer(stringNotParsed) + if (player != null) { + return player + } + + throw IllegalArgumentException("Player $stringNotParsed not found") + } + + override fun getNativeType(): ArgumentType { + return StringArgumentType.word() + } + + override fun listSuggestions( + context: CommandContext, + builder: SuggestionsBuilder + ): CompletableFuture { + val currentArg = context.input.lastOrNull() ?: return builder.buildFuture() + + Bukkit.getOnlinePlayers().filter { player -> player.name.startsWith(currentArg) }.forEach { + builder.suggest(it.name) + } + + return builder.buildFuture() + } + +} \ No newline at end of file