From 77c6ca150552eb8183e0bd694b2b183083636b97 Mon Sep 17 00:00:00 2001 From: andyksaw Date: Wed, 16 Oct 2024 20:27:01 +0900 Subject: [PATCH] Add register command --- .../pcbridge/http/clients/PCBClientFactory.kt | 1 + .../pcbridge/http/parsing/ResponseParser.kt | 8 +--- .../pcbridge/http/requests/PCBRequest.kt | 2 +- .../http/services/pcb/RegisterHttpService.kt | 23 +++------ .../projectcitybuild/pcbridge/Container.kt | 21 +++++++-- .../com/projectcitybuild/pcbridge/Plugin.kt | 6 +-- .../register/commands/RegisterCommand.kt | 47 +++++++++++++++++++ .../repositories/RegisterRepository.kt | 20 ++++++++ pcbridge-spigot/src/main/resources/plugin.yml | 2 + 9 files changed, 98 insertions(+), 32 deletions(-) create mode 100644 pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/features/register/commands/RegisterCommand.kt create mode 100644 pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/features/register/repositories/RegisterRepository.kt diff --git a/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/clients/PCBClientFactory.kt b/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/clients/PCBClientFactory.kt index 530de8cc..4f7d60fc 100644 --- a/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/clients/PCBClientFactory.kt +++ b/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/clients/PCBClientFactory.kt @@ -23,6 +23,7 @@ internal class PCBClientFactory( val request = chain.request() .newBuilder() .header("Authorization", "Bearer $authToken") + .header("Accept", "application/json") .build() chain.proceed(request) diff --git a/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/parsing/ResponseParser.kt b/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/parsing/ResponseParser.kt index e7a4874f..fe8036c1 100644 --- a/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/parsing/ResponseParser.kt +++ b/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/parsing/ResponseParser.kt @@ -17,16 +17,12 @@ class ResponseParser { }, ) - class NetworkError : Exception( - "Failed to contact PCB auth server", - ) - suspend fun parse(apiCall: suspend () -> T): T = withContext(Dispatchers.IO) { try { apiCall.invoke() - } catch (_: IOException) { - throw NetworkError() + } catch (e: IOException) { + throw e } catch (e: HttpException) { val code = e.code() throw HTTPError(errorBody = convertErrorBody(e, code)) diff --git a/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/requests/PCBRequest.kt b/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/requests/PCBRequest.kt index cf13b6a0..8f5a66a1 100644 --- a/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/requests/PCBRequest.kt +++ b/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/requests/PCBRequest.kt @@ -29,7 +29,7 @@ internal interface PCBRequest { @FormUrlEncoded suspend fun sendRegisterCode( @Path(value = "uuid") uuid: String, - @Field(value = "alias") playerName: String, + @Field(value = "minecraft_alias") playerAlias: String, @Field(value = "email") email: String, ): ApiResponse diff --git a/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/services/pcb/RegisterHttpService.kt b/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/services/pcb/RegisterHttpService.kt index d3fed98b..768b5f30 100644 --- a/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/services/pcb/RegisterHttpService.kt +++ b/pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/services/pcb/RegisterHttpService.kt @@ -6,30 +6,19 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import retrofit2.Retrofit import java.util.UUID -import kotlin.jvm.Throws class RegisterHttpService( private val retrofit: Retrofit, private val responseParser: ResponseParser, ) { - class AlreadyLinkedException : Exception() - - @Throws(AlreadyLinkedException::class) suspend fun sendCode(playerUUID: UUID, playerAlias: String, email: String) = withContext(Dispatchers.IO) { - try { - responseParser.parse { - retrofit.pcb().sendRegisterCode( - uuid = playerUUID.toString(), - playerName = playerAlias, - email = email, - ) - } - } catch (e: ResponseParser.HTTPError) { - if (e.errorBody?.id == "already_authenticated") { - throw AlreadyLinkedException() - } - throw e + responseParser.parse { + retrofit.pcb().sendRegisterCode( + uuid = playerUUID.toString(), + playerAlias = playerAlias, + email = email, + ) } } } diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/Container.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/Container.kt index 614da68f..a40860d4 100644 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/Container.kt +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/Container.kt @@ -35,9 +35,10 @@ import com.projectcitybuild.pcbridge.features.joinmessages.listeners.ServerOverv import com.projectcitybuild.pcbridge.features.joinmessages.repositories.PlayerConfigRepository import com.projectcitybuild.pcbridge.features.nightvision.commands.NightVisionCommand import com.projectcitybuild.pcbridge.features.playerstate.listeners.PlayerStateListener +import com.projectcitybuild.pcbridge.features.register.commands.RegisterCommand +import com.projectcitybuild.pcbridge.features.register.repositories.RegisterRepository import com.projectcitybuild.pcbridge.features.staffchat.commands.StaffChatCommand import com.projectcitybuild.pcbridge.features.sync.actions.SyncPlayerGroups -import com.projectcitybuild.pcbridge.features.sync.commands.SyncCommand import com.projectcitybuild.pcbridge.features.sync.listener.SyncRankOnJoinListener import com.projectcitybuild.pcbridge.features.sync.repositories.SyncRepository import com.projectcitybuild.pcbridge.features.telemetry.listeners.TelemetryPlayerConnectListener @@ -87,6 +88,7 @@ fun pluginModule(_plugin: JavaPlugin) = invisFrames() nightVision() playerState() + register() staffChat() sync() telemetry() @@ -459,6 +461,19 @@ private fun Module.chat() { } } +private fun Module.register() { + factory { + RegisterCommand( + registerRepository = get(), + ) + } + factory { + RegisterRepository( + registerHttpService = get().register, + ) + } +} + private fun Module.telemetry() { factory { TelemetryRepository( @@ -495,10 +510,6 @@ private fun Module.sync() { ) } - factory { - SyncCommand() - } - factory { SyncRankOnJoinListener( syncPlayerGroups = get(), diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/Plugin.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/Plugin.kt index da9b6bac..cba70465 100644 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/Plugin.kt +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/Plugin.kt @@ -17,8 +17,8 @@ import com.projectcitybuild.pcbridge.features.joinmessages.listeners.FirstTimeJo import com.projectcitybuild.pcbridge.features.joinmessages.listeners.ServerOverviewJoinListener import com.projectcitybuild.pcbridge.features.nightvision.commands.NightVisionCommand import com.projectcitybuild.pcbridge.features.playerstate.listeners.PlayerStateListener +import com.projectcitybuild.pcbridge.features.register.commands.RegisterCommand import com.projectcitybuild.pcbridge.features.staffchat.commands.StaffChatCommand -import com.projectcitybuild.pcbridge.features.sync.commands.SyncCommand import com.projectcitybuild.pcbridge.features.sync.listener.SyncRankOnJoinListener import com.projectcitybuild.pcbridge.features.telemetry.listeners.TelemetryPlayerConnectListener import com.projectcitybuild.pcbridge.features.utilities.commands.PCBridgeCommand @@ -107,8 +107,8 @@ private class Lifecycle : KoinComponent { argsParser = InvisFrameCommand.Args.Parser(), ) register( - handler = get(), - argsParser = SyncCommand.Args.Parser(), + handler = get(), + argsParser = RegisterCommand.Args.Parser(), ) } listenerRegistry.register( diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/features/register/commands/RegisterCommand.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/features/register/commands/RegisterCommand.kt new file mode 100644 index 00000000..b12d3ec7 --- /dev/null +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/features/register/commands/RegisterCommand.kt @@ -0,0 +1,47 @@ +package com.projectcitybuild.pcbridge.features.register.commands + +import com.projectcitybuild.pcbridge.features.register.repositories.RegisterRepository +import com.projectcitybuild.pcbridge.support.messages.CommandHelpBuilder +import com.projectcitybuild.pcbridge.support.spigot.BadCommandUsageException +import com.projectcitybuild.pcbridge.support.spigot.CommandArgsParser +import com.projectcitybuild.pcbridge.support.spigot.SpigotCommand +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player + +class RegisterCommand( + private val registerRepository: RegisterRepository, +) : SpigotCommand { + override val label = "register" + + override val usage = CommandHelpBuilder() + + override suspend fun run( + sender: CommandSender, + args: Args, + ) { + check(sender is Player) { + "Only players can use this command" + } + registerRepository.sendCode( + email = args.email, + playerAlias = sender.name, + playerUUID = sender.uniqueId, + ) + } + + data class Args( + val email: String, + ) { + class Parser : CommandArgsParser { + override fun parse(args: List): Args { + if (args.isEmpty()) { + throw BadCommandUsageException() + } + if (args.size > 1) { + throw BadCommandUsageException() + } + return Args(email = args[0]) + } + } + } +} diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/features/register/repositories/RegisterRepository.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/features/register/repositories/RegisterRepository.kt new file mode 100644 index 00000000..d0092742 --- /dev/null +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/features/register/repositories/RegisterRepository.kt @@ -0,0 +1,20 @@ +package com.projectcitybuild.pcbridge.features.register.repositories + +import com.projectcitybuild.pcbridge.http.services.pcb.RegisterHttpService +import java.util.UUID + +class RegisterRepository( + private val registerHttpService: RegisterHttpService, +) { + suspend fun sendCode( + email: String, + playerAlias: String, + playerUUID: UUID, + ) { + registerHttpService.sendCode( + email = email, + playerAlias = playerAlias, + playerUUID = playerUUID, + ) + } +} diff --git a/pcbridge-spigot/src/main/resources/plugin.yml b/pcbridge-spigot/src/main/resources/plugin.yml index 75568d5d..9aa39f5f 100644 --- a/pcbridge-spigot/src/main/resources/plugin.yml +++ b/pcbridge-spigot/src/main/resources/plugin.yml @@ -23,6 +23,8 @@ commands: permission: pcbridge.build.invisframe nv: permission: pcbridge.build.nightvision + register: + permission: pcbridge.register sync: permission: pcbridge.sync.login warp: