Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions surf-api-velocity/surf-api-velocity-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ dependencies {
api(libs.commandapi.velocity.kotlin)
}

kotlin {
compilerOptions {
optIn.add("dev.slne.surf.surfapi.core.api.util.InternalSurfApi")
}
}

description = "surf-api-velocity-api"
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.slne.surf.surfapi.velocity.api.command.args

import com.velocitypowered.api.proxy.Player
import dev.slne.surf.surfapi.core.api.util.InternalSurfApi
import dev.slne.surf.surfapi.core.api.util.requiredService
import kotlinx.coroutines.Deferred
import java.util.*

@InternalSurfApi
interface InternalCommandBridge {
fun getPlayer(name: String): Player?
fun getPlayer(uuid: UUID): Player?
fun getPlayers(): Collection<Player>

suspend fun requestPlayerUuid(name: String): UUID?

fun <T> async(block: suspend () -> T): Deferred<T>

companion object : InternalCommandBridge by requiredService<InternalCommandBridge>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dev.slne.surf.surfapi.velocity.api.command.args

import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import dev.jorel.commandapi.CommandAPICommand
import dev.jorel.commandapi.arguments.Argument
import dev.jorel.commandapi.arguments.ArgumentSuggestions
import dev.jorel.commandapi.arguments.CommandAPIArgumentType
import dev.jorel.commandapi.executors.CommandArguments
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Deferred
import java.util.*

class OfflinePlaynameToUuid(nodeName: String) :
Argument<Deferred<UUID?>>(nodeName, StringArgumentType.word()) {

init {
replaceSuggestions(ArgumentSuggestions.stringCollection {
InternalCommandBridge.getPlayers().map { it.username }
})
}

override fun getPrimitiveType(): Class<Deferred<UUID?>> {
@Suppress("UNCHECKED_CAST")
return Deferred::class.java as Class<Deferred<UUID?>>
}

override fun getArgumentType(): CommandAPIArgumentType? {
return CommandAPIArgumentType.PRIMITIVE_STRING
}

override fun <Source : Any> parseArgument(
cmdCtx: CommandContext<Source>,
key: String,
previousArgs: CommandArguments,
): Deferred<UUID?> {
val playerName = StringArgumentType.getString(cmdCtx, key)
val onlinePlayer = InternalCommandBridge.getPlayer(playerName)

if (onlinePlayer != null) {
return CompletableDeferred(onlinePlayer.uniqueId)
}

return InternalCommandBridge.async { InternalCommandBridge.requestPlayerUuid(playerName) }
}
}

inline fun CommandAPICommand.offlinePlaynameToUuid(
nodeName: String,
optional: Boolean = false,
block: Argument<*>.() -> Unit = {},
): CommandAPICommand =
withArguments(OfflinePlaynameToUuid(nodeName).setOptional(optional).apply(block))
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dev.slne.surf.surfapi.velocity.api.command.args

import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import com.velocitypowered.api.proxy.Player
import dev.jorel.commandapi.CommandAPICommand
import dev.jorel.commandapi.arguments.Argument
import dev.jorel.commandapi.arguments.ArgumentSuggestions
import dev.jorel.commandapi.arguments.CommandAPIArgumentType
import dev.jorel.commandapi.executors.CommandArguments

class PlayerArgument(nodeName: String) : Argument<Player>(nodeName, StringArgumentType.word()) {
companion object {
private val NO_PLAYERS_FOUND = SimpleCommandExceptionType("No player was found")
}

init {
replaceSuggestions(ArgumentSuggestions.stringCollection {
InternalCommandBridge.getPlayers().map { it.username }
})
}

override fun getPrimitiveType(): Class<Player> {
return Player::class.java
}

override fun getArgumentType(): CommandAPIArgumentType? {
return CommandAPIArgumentType.PRIMITIVE_STRING
}

override fun <Source : Any> parseArgument(
cmdCtx: CommandContext<Source>,
key: String,
previousArgs: CommandArguments,
): Player {
val playerName = StringArgumentType.getString(cmdCtx, key)
return InternalCommandBridge.getPlayer(playerName) ?: throw NO_PLAYERS_FOUND.create()
}
}

inline fun CommandAPICommand.playerArgument(
nodeName: String,
optional: Boolean = false,
block: Argument<*>.() -> Unit = {},
): CommandAPICommand =
withArguments(PlayerArgument(nodeName).setOptional(optional).apply(block))
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.slne.surf.surfapi.velocity.api.command.args

import com.mojang.brigadier.exceptions.SimpleCommandExceptionType
import com.velocitypowered.api.command.VelocityBrigadierMessage
import dev.slne.surf.surfapi.core.api.messages.adventure.text

fun SimpleCommandExceptionType(message: String) =
SimpleCommandExceptionType(VelocityBrigadierMessage.tooltip(text(message)))