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 Mar 1, 2023
1 parent d311eae commit 7bdfc48
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 40 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/tristansmp/elytra/Elytra.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tristansmp.elytra

import com.tristansmp.elytra.commands.CommandLink
import com.tristansmp.elytra.events.ChatListener
import com.tristansmp.elytra.lib.ConfigManager
import com.tristansmp.elytra.lib.MemoryStore
Expand Down Expand Up @@ -44,6 +45,7 @@ class Elytra : JavaPlugin() {
}

server.pluginManager.registerEvents(ChatListener(), this)
this.getCommand("elink")?.setExecutor(CommandLink())
}

override fun onDisable() {
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/tristansmp/elytra/commands/CommandLink.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.tristansmp.elytra.commands

import com.tristansmp.elytra.Elytra
import khttp.post
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player

class CommandLink : CommandExecutor {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>?): Boolean {
if (sender !is Player || Elytra.instance.config.config.token == null || Elytra.instance.config.config.linkAccountEndpoint == null) {
return false
}

// make sure we have a code
if (args == null || args.isEmpty()) {
return false
}

val player = sender
val uuid = player.uniqueId.toString()
val code = args[0]
val token = Elytra.instance.config.config.token ?: return false
val endpoint = Elytra.instance.config.config.linkAccountEndpoint ?: return false

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
)
)

// check the response code
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 true
}
}
40 changes: 0 additions & 40 deletions src/main/java/com/tristansmp/elytra/events/ChatListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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
Expand All @@ -19,45 +18,6 @@ class ChatListener : Listener {
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

0 comments on commit 7bdfc48

Please sign in to comment.