-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4565666
commit e2389d9
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/cc/modlabs/kpaper/command/arguments/PlayerArgument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
} |