Skip to content

Commit

Permalink
feat: link accounts endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tristancamejo committed Feb 24, 2023
1 parent 8a4dc98 commit d311eae
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ repositories {
name = 'sonatype'
url = 'https://oss.sonatype.org/content/groups/public/'
}
maven {
name = 'jitpack'
url = 'https://jitpack.io'
}
}

dependencies {
Expand All @@ -35,6 +39,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'com.akuleshov7:ktoml-core:0.3.0'
implementation 'com.akuleshov7:ktoml-file:0.3.0'
implementation 'com.github.jkcclemens:khttp:0.1.0'
compileOnly 'net.luckperms:api:5.4'
}

Expand Down
45 changes: 43 additions & 2 deletions src/main/java/com/tristansmp/elytra/events/ChatListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,62 @@ package com.tristansmp.elytra.events

import com.tristansmp.elytra.Elytra
import io.papermc.paper.event.player.AsyncChatEvent
import khttp.post
import net.kyori.adventure.text.TextComponent
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener


class ChatListener : Listener {
@EventHandler
@EventHandler(priority = EventPriority.HIGHEST)
fun onPlayerChat(event: AsyncChatEvent) {
try {
val message = event.message() as TextComponent


if (!message.content().startsWith("~")) {
return
}


if (message.content()
.startsWith("~link") && Elytra.instance.config.config.token != null && Elytra.instance.config.config.linkAccountEndpoint != null
) {
val player = event.player
val uuid = player.uniqueId.toString()
val code = message.content().split(" ")[1]
val token = Elytra.instance.config.config.token ?: return
val endpoint = Elytra.instance.config.config.linkAccountEndpoint ?: return

event.isCancelled = true

player.sendMessage("§a§l[§b§lElytra§a§l] §a§lLinking account...")

try {
val response = post(
url = endpoint,
data = mapOf(
"uuid" to uuid,
"code" to code
),
headers = mapOf(
"Authorization" to token
)
)

if (response.statusCode == 200) {
player.sendMessage("§a§l[§b§lElytra§a§l] §a§lSuccessfully linked account!")
} else {
player.sendMessage("§a§l[§b§lElytra§a§l] §c§lFailed to link account!")
}
} catch (e: Exception) {
player.sendMessage("§a§l[§b§lElytra§a§l] §c§lFailed to link account!")
}

return;

}

val content = message.content().substring(1)

val needsCollection = Elytra.instance.mstore.get<Boolean>("cc:${event.player.uniqueId}:needs_collection")
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/tristansmp/elytra/lib/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import java.io.IOException

@Serializable
data class Config(
val token: String?,
val token: String? = null,
val linkAccountEndpoint: String? = null,
)

class ConfigManager {
Expand All @@ -32,7 +33,7 @@ class ConfigManager {

if (!file.exists()) {
try {
Toml(outputConfig = outputConfig).encodeToString(Config(null)).toByteArray()
Toml(outputConfig = outputConfig).encodeToString(Config()).toByteArray()
.also { file.writeBytes(it) }
} catch (ex: IOException) {
ex.printStackTrace()
Expand Down

0 comments on commit d311eae

Please sign in to comment.