Skip to content

Commit

Permalink
feat: arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidJoker committed Jan 5, 2025
1 parent 4565666 commit e2389d9
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Player, String> {

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<String> {
return StringArgumentType.word()
}

override fun <S : Any> listSuggestions(
context: CommandContext<S>,
builder: SuggestionsBuilder
): CompletableFuture<Suggestions> {
val currentArg = context.input.lastOrNull() ?: return builder.buildFuture()

Bukkit.getOnlinePlayers().filter { player -> player.name.startsWith(currentArg) }.forEach {
builder.suggest(it.name)
}

return builder.buildFuture()
}

}

0 comments on commit e2389d9

Please sign in to comment.