Skip to content

Commit

Permalink
Add register command
Browse files Browse the repository at this point in the history
  • Loading branch information
andyksaw committed Oct 16, 2024
1 parent 6c2f590 commit 77c6ca1
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class PCBClientFactory(
val request = chain.request()
.newBuilder()
.header("Authorization", "Bearer $authToken")
.header("Accept", "application/json")
.build()

chain.proceed(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ class ResponseParser {
},
)

class NetworkError : Exception(
"Failed to contact PCB auth server",
)

suspend fun <T> 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Unit>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -87,6 +88,7 @@ fun pluginModule(_plugin: JavaPlugin) =
invisFrames()
nightVision()
playerState()
register()
staffChat()
sync()
telemetry()
Expand Down Expand Up @@ -459,6 +461,19 @@ private fun Module.chat() {
}
}

private fun Module.register() {
factory {
RegisterCommand(
registerRepository = get(),
)
}
factory {
RegisterRepository(
registerHttpService = get<HttpService>().register,
)
}
}

private fun Module.telemetry() {
factory {
TelemetryRepository(
Expand Down Expand Up @@ -495,10 +510,6 @@ private fun Module.sync() {
)
}

factory {
SyncCommand()
}

factory {
SyncRankOnJoinListener(
syncPlayerGroups = get(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -107,8 +107,8 @@ private class Lifecycle : KoinComponent {
argsParser = InvisFrameCommand.Args.Parser(),
)
register(
handler = get<SyncCommand>(),
argsParser = SyncCommand.Args.Parser(),
handler = get<RegisterCommand>(),
argsParser = RegisterCommand.Args.Parser(),
)
}
listenerRegistry.register(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RegisterCommand.Args> {
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<Args> {
override fun parse(args: List<String>): Args {
if (args.isEmpty()) {
throw BadCommandUsageException()
}
if (args.size > 1) {
throw BadCommandUsageException()
}
return Args(email = args[0])
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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,
)
}
}
2 changes: 2 additions & 0 deletions pcbridge-spigot/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ commands:
permission: pcbridge.build.invisframe
nv:
permission: pcbridge.build.nightvision
register:
permission: pcbridge.register
sync:
permission: pcbridge.sync.login
warp:
Expand Down

0 comments on commit 77c6ca1

Please sign in to comment.