diff --git a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt index f72537d..9379788 100644 --- a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt +++ b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt @@ -3,7 +3,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 de.sh_edraft.events.OnLeftListener import org.bukkit.plugin.java.JavaPlugin open class OntimeHandlerPlugin() : JavaPlugin() { @@ -21,7 +21,7 @@ open class OntimeHandlerPlugin() : JavaPlugin() { this.dataService = DataService(logger, this.config) server.pluginManager.registerEvents(OnJoinListener(logger, this.dataService), this) - server.pluginManager.registerEvents(OnLeaveListener(logger, this.dataService), this) + server.pluginManager.registerEvents(OnLeftListener(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 index 60587d0..130f5d0 100644 --- a/src/main/kotlin/de/sh_edraft/data/DataService.kt +++ b/src/main/kotlin/de/sh_edraft/data/DataService.kt @@ -12,31 +12,22 @@ class DataService( private val logger: Logger, private val config: Config ) { - private fun getUserIdByPlayerIdQuery(id: String): String { + + private fun getUserJoinedMutation(id: String): String { return JSONObject( """ { - "query": "query { users(filter: { minecraftId: \"$id\" }) { id } }" + "query": "mutation { userJoinedGameServer { userJoined(input: { ident: \"$id\" }) { id } } }" } """.trimIndent() ).toString() } - private fun getUserJoinedMutation(id: Int): String { + private fun getUserLeftMutation(id: String): 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 } } }" + "query": "mutation { userJoinedGameServer { userLeft(input: { ident: \"$id\" }) { id } } }" } """.trimIndent() ).toString() @@ -53,32 +44,7 @@ class DataService( 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) { + fun sendJoinedPlayer(id: String) { val client = HttpClient.newBuilder().build(); val request = HttpRequest.newBuilder() @@ -102,12 +68,12 @@ class DataService( } } - fun sendLeavedPlayer(id: Int) { + fun sendLeftPlayer(id: String) { val client = HttpClient.newBuilder().build(); val request = HttpRequest.newBuilder() .uri(URI.create(this.config.ApiURL)) - .POST(HttpRequest.BodyPublishers.ofString(this.getUserLeavedMutation(id))) + .POST(HttpRequest.BodyPublishers.ofString(this.getUserLeftMutation(id))) .header("Authorization", "API-Key ${this.config.ApiKey}") .header("Content-Type", "application/json") .build(); diff --git a/src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt b/src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt index b9bd80a..e15a711 100644 --- a/src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt +++ b/src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt @@ -16,7 +16,6 @@ class OnJoinListener( return; } val playerId = this.dataService.getPlayerGlobalId(p.player.player!!.displayName) ?: return; - val userId = this.dataService.getUserIdByPlayerId(playerId) ?: return; - this.dataService.sendJoinedPlayer(userId); + this.dataService.sendJoinedPlayer(playerId); } } \ 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/OnLeftListener.kt similarity index 84% rename from src/main/kotlin/de/sh_edraft/events/OnLeaveListener.kt rename to src/main/kotlin/de/sh_edraft/events/OnLeftListener.kt index f6f3702..8f02336 100644 --- a/src/main/kotlin/de/sh_edraft/events/OnLeaveListener.kt +++ b/src/main/kotlin/de/sh_edraft/events/OnLeftListener.kt @@ -8,7 +8,7 @@ import org.bukkit.event.player.PlayerKickEvent import org.bukkit.event.player.PlayerQuitEvent import java.util.logging.Logger -class OnLeaveListener( +class OnLeftListener( private val logger: Logger, private val dataService: DataService ) : Listener { @@ -18,8 +18,7 @@ class OnLeaveListener( return; } val playerId = this.dataService.getPlayerGlobalId(p.player.player!!.displayName) ?: return; - val userId = this.dataService.getUserIdByPlayerId(playerId) ?: return; - this.dataService.sendLeavedPlayer(userId); + this.dataService.sendLeftPlayer(playerId); } @EventHandler