From 8516685a511d4933466235e0f1f963387ac346ee Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 20:13:59 +0100 Subject: [PATCH] Added logic to handle stuff Closes #2 Closes #3 Closes #4 --- kd_ontime_handler_spigot.iml | 9 ++ pom.xml | 19 ++- .../de/sh_edraft/OntimeHandlerPlugin.kt | 9 +- .../kotlin/de/sh_edraft/data/DataService.kt | 128 ++++++++++++++++++ .../de/sh_edraft/events/OnJoinListener.kt | 13 +- .../de/sh_edraft/events/OnLeaveListener.kt | 13 +- .../resources/config.edrafts-pc.properties | 4 +- 7 files changed, 185 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/de/sh_edraft/data/DataService.kt diff --git a/kd_ontime_handler_spigot.iml b/kd_ontime_handler_spigot.iml index f8205be..4f789f1 100644 --- a/kd_ontime_handler_spigot.iml +++ b/kd_ontime_handler_spigot.iml @@ -1,5 +1,14 @@ + + + + + SPIGOT + + + + diff --git a/pom.xml b/pom.xml index a3d2ecc..20c0809 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,21 @@ kotlin-stdlib-jdk8 1.7.21 + + org.jetbrains.kotlin + kotlin-compiler + 1.7.21 + + + org.json + json + 20220924 + + + com.google.okhttp + okhttp + 20120626 + @@ -87,8 +102,8 @@ org.apache.maven.plugins maven-compiler-plugin - 6 - 6 + 11 + 11 diff --git a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt index 0c0e836..2ce6ff9 100644 --- a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt +++ b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt @@ -1,6 +1,7 @@ package de.sh_edraft import de.sh_edraft.config.Config +import de.sh_edraft.data.DataService import de.sh_edraft.events.OnJoinListener import de.sh_edraft.events.OnLeaveListener import org.bukkit.plugin.java.JavaPlugin @@ -9,14 +10,18 @@ open class OntimeHandlerPlugin() : JavaPlugin() { private lateinit var config: Config + private lateinit var dataService: DataService + override fun onEnable() { this.config = Config(logger) this.config.read("/config.properties", false) this.config.read("/config.${this.config.environment}.properties", true) this.config.read("/config.${this.config.hostName}.properties", true) - server.pluginManager.registerEvents(OnJoinListener(logger), this) - server.pluginManager.registerEvents(OnLeaveListener(logger), this) + this.dataService = DataService(logger, this.config) + + server.pluginManager.registerEvents(OnJoinListener(logger, this.dataService), this) + server.pluginManager.registerEvents(OnLeaveListener(logger, this.dataService), this) logger.info("OntimeHandlerPlugin enabled :D") } diff --git a/src/main/kotlin/de/sh_edraft/data/DataService.kt b/src/main/kotlin/de/sh_edraft/data/DataService.kt new file mode 100644 index 0000000..60587d0 --- /dev/null +++ b/src/main/kotlin/de/sh_edraft/data/DataService.kt @@ -0,0 +1,128 @@ +package de.sh_edraft.data + +import de.sh_edraft.config.Config +import java.net.URI +import java.net.http.HttpClient +import java.net.http.HttpRequest +import java.net.http.HttpResponse +import java.util.logging.Logger +import org.json.JSONObject + +class DataService( + private val logger: Logger, + private val config: Config +) { + private fun getUserIdByPlayerIdQuery(id: String): String { + return JSONObject( + """ + { + "query": "query { users(filter: { minecraftId: \"$id\" }) { id } }" + } + """.trimIndent() + ).toString() + } + + private fun getUserJoinedMutation(id: Int): String { + return JSONObject( + """ + { + "query": "mutation { userJoinedGameServer { userJoined(input: { userId: $id gameServer: \"Minecraft\" }) { id } } }" + } + """.trimIndent() + ).toString() + } + + private fun getUserLeavedMutation(id: Int): String { + return JSONObject( + """ + { + "query": "mutation { userJoinedGameServer { userLeaved (input: { userId: $id gameServer: \"Minecraft\" }) { id } } }" + } + """.trimIndent() + ).toString() + } + + fun getPlayerGlobalId(name: String): String? { + val client = HttpClient.newBuilder().build(); + val request = HttpRequest.newBuilder() + .uri(URI.create("https://api.mojang.com/users/profiles/minecraft/$name")) + .build(); + + val response = client.send(request, HttpResponse.BodyHandlers.ofString()).body() ?: return null; + val json = JSONObject(response) + return json.getString("id"); + } + + + fun getUserIdByPlayerId(id: String): Int? { + val client = HttpClient.newBuilder().build(); + + val request = HttpRequest.newBuilder() + .uri(URI.create(this.config.ApiURL)) + .POST(HttpRequest.BodyPublishers.ofString(this.getUserIdByPlayerIdQuery(id))) + .header("Authorization", "API-Key ${this.config.ApiKey}") + .header("Content-Type", "application/json") + .build(); + + val response = client.send(request, HttpResponse.BodyHandlers.ofString()); + try { + return JSONObject(response.body()) + .getJSONObject("data") + .getJSONArray("users") + .getJSONObject(0) + .getString("id") + .toIntOrNull(); + } catch (e: Exception) { + logger.severe(e.message); + } + return null; + } + + fun sendJoinedPlayer(id: Int) { + val client = HttpClient.newBuilder().build(); + + val request = HttpRequest.newBuilder() + .uri(URI.create(this.config.ApiURL)) + .POST(HttpRequest.BodyPublishers.ofString(this.getUserJoinedMutation(id))) + .header("Authorization", "API-Key ${this.config.ApiKey}") + .header("Content-Type", "application/json") + .build(); + + val response = client.send(request, HttpResponse.BodyHandlers.ofString()); + try { + // check if response is valid + val joinId = JSONObject(response.body()) + .getJSONObject("data") + .getJSONObject("userJoinedGameServer") + .getJSONObject("userJoined") + .getString("id") + .toIntOrNull(); + } catch (e: Exception) { + logger.severe(e.message); + } + } + + fun sendLeavedPlayer(id: Int) { + val client = HttpClient.newBuilder().build(); + + val request = HttpRequest.newBuilder() + .uri(URI.create(this.config.ApiURL)) + .POST(HttpRequest.BodyPublishers.ofString(this.getUserLeavedMutation(id))) + .header("Authorization", "API-Key ${this.config.ApiKey}") + .header("Content-Type", "application/json") + .build(); + + val response = client.send(request, HttpResponse.BodyHandlers.ofString()); + try { + // check if response is valid + val joinId = JSONObject(response.body()) + .getJSONObject("data") + .getJSONObject("userJoinedGameServer") + .getJSONObject("userJoined") + .getString("id") + .toIntOrNull(); + } catch (e: Exception) { + logger.severe(e.message); + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt b/src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt index 0cf8b2f..b9bd80a 100644 --- a/src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt +++ b/src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt @@ -1,13 +1,22 @@ package de.sh_edraft.events +import de.sh_edraft.data.DataService import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerJoinEvent import java.util.logging.Logger -class OnJoinListener(private val logger: Logger) : Listener { +class OnJoinListener( + private val logger: Logger, + private val dataService: DataService +) : Listener { @EventHandler fun onPlayerJoin(p: PlayerJoinEvent) { - logger.info("Player ${p.player.player?.displayName} joined") + if (p.player.player == null) { + return; + } + val playerId = this.dataService.getPlayerGlobalId(p.player.player!!.displayName) ?: return; + val userId = this.dataService.getUserIdByPlayerId(playerId) ?: return; + this.dataService.sendJoinedPlayer(userId); } } \ No newline at end of file diff --git a/src/main/kotlin/de/sh_edraft/events/OnLeaveListener.kt b/src/main/kotlin/de/sh_edraft/events/OnLeaveListener.kt index 7e0a3db..f6f3702 100644 --- a/src/main/kotlin/de/sh_edraft/events/OnLeaveListener.kt +++ b/src/main/kotlin/de/sh_edraft/events/OnLeaveListener.kt @@ -1,5 +1,6 @@ package de.sh_edraft.events +import de.sh_edraft.data.DataService import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerEvent @@ -7,10 +8,18 @@ import org.bukkit.event.player.PlayerKickEvent import org.bukkit.event.player.PlayerQuitEvent import java.util.logging.Logger -class OnLeaveListener(private val logger: Logger) : Listener { +class OnLeaveListener( + private val logger: Logger, + private val dataService: DataService +) : Listener { private fun handleQuit(p: PlayerEvent) { - this.logger.info("Player ${p.player.player?.displayName} leaved") + if (p.player.player == null) { + return; + } + val playerId = this.dataService.getPlayerGlobalId(p.player.player!!.displayName) ?: return; + val userId = this.dataService.getUserIdByPlayerId(playerId) ?: return; + this.dataService.sendLeavedPlayer(userId); } @EventHandler diff --git a/src/main/resources/config.edrafts-pc.properties b/src/main/resources/config.edrafts-pc.properties index 6359af7..de8cbc9 100644 --- a/src/main/resources/config.edrafts-pc.properties +++ b/src/main/resources/config.edrafts-pc.properties @@ -1,2 +1,2 @@ -apiURL: http://localhost/api/ -apiKey: abcd \ No newline at end of file +apiURL: http://localhost:8044/api/graphql +apiKey: 7b93e3b46bbfbdafc7b1e7a9d3fc05ebf68c0ead3093bda8802fa241081c7173 \ No newline at end of file