From d5ff74b6a4ea5e9b73325c5cd22134a38762a7f3 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 12 Jan 2023 21:28:24 +0100 Subject: [PATCH 1/9] Made plugin executable #1 --- .gitignore | 7 ++++--- kd_ontime_handler_spigot.iml | 1 + src/main/kotlin/Main.kt | 7 ------- .../kotlin/de/sh_edraft/OntimeHandlerPlugin.kt | 13 +++++++++++++ src/main/resources/plugin.yml | 3 +++ .../kd_ontime_handler_spigot.kotlin_module | Bin 36 -> 0 bytes target/maven-archiver/pom.properties | 5 ----- .../compile/default-compile/inputFiles.lst | 0 .../testCompile/default-testCompile/inputFiles.lst | 0 9 files changed, 21 insertions(+), 15 deletions(-) delete mode 100644 src/main/kotlin/Main.kt create mode 100644 src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt create mode 100644 src/main/resources/plugin.yml delete mode 100644 target/classes/META-INF/kd_ontime_handler_spigot.kotlin_module delete mode 100644 target/maven-archiver/pom.properties delete mode 100644 target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst delete mode 100644 target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst diff --git a/.gitignore b/.gitignore index a6e3daf..53c9a3c 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,8 @@ hs_err_pid* replay_pid* # idea -.idea +.idea/* # build sources -target - +target/* +# spigot test server +spigot/* diff --git a/kd_ontime_handler_spigot.iml b/kd_ontime_handler_spigot.iml index 43cdacd..f8205be 100644 --- a/kd_ontime_handler_spigot.iml +++ b/kd_ontime_handler_spigot.iml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt deleted file mode 100644 index f2a59b6..0000000 --- a/src/main/kotlin/Main.kt +++ /dev/null @@ -1,7 +0,0 @@ -fun main(args: Array) { - println("Hello World!") - - // Try adding program arguments via Run/Debug configuration. - // Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html. - println("Program arguments: ${args.joinToString()}") -} \ No newline at end of file diff --git a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt new file mode 100644 index 0000000..6cbc380 --- /dev/null +++ b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt @@ -0,0 +1,13 @@ +package de.sh_edraft + +import org.bukkit.plugin.java.JavaPlugin + +open class OntimeHandlerPlugin() : JavaPlugin() { + override fun onEnable() { + logger.info("onEnable is called!") + } + + override fun onDisable() { + logger.info("onDisable is called!") + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..873804b --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,3 @@ +name: sh_edraft.OntimeHandlerPlugin +version: 1.0 +main: de.sh_edraft.OntimeHandlerPlugin \ No newline at end of file diff --git a/target/classes/META-INF/kd_ontime_handler_spigot.kotlin_module b/target/classes/META-INF/kd_ontime_handler_spigot.kotlin_module deleted file mode 100644 index 4e7592e0878597de3516825ef4c94b1cd2cc3e46..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 36 kcmZQzU|?ooU| Date: Thu, 12 Jan 2023 23:19:29 +0100 Subject: [PATCH 2/9] Added read config logic #1 --- pom.xml | 29 +++++++- .../de/sh_edraft/OntimeHandlerPlugin.kt | 12 +++- src/main/kotlin/de/sh_edraft/config/Config.kt | 69 +++++++++++++++++++ .../resources/config.development.properties | 2 + .../resources/config.edrafts-pc.properties | 2 + src/main/resources/config.properties | 2 + src/main/resources/config.staging.properties | 2 + 7 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/de/sh_edraft/config/Config.kt create mode 100644 src/main/resources/config.development.properties create mode 100644 src/main/resources/config.edrafts-pc.properties create mode 100644 src/main/resources/config.properties create mode 100644 src/main/resources/config.staging.properties diff --git a/pom.xml b/pom.xml index f2fc94a..a3d2ecc 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,6 @@ 1.19.2-R0.1-SNAPSHOT provided - org.jetbrains.kotlin kotlin-test-junit5 @@ -81,7 +80,33 @@ exec-maven-plugin 1.6.0 - MainKt + OntimeHandlerPlugin + + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + maven-assembly-plugin + + + package + + single + + + + + + jar-with-dependencies + + ${project.artifactId}-${project.version} + false diff --git a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt index 6cbc380..9952edc 100644 --- a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt +++ b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt @@ -1,13 +1,21 @@ package de.sh_edraft +import de.sh_edraft.config.Config import org.bukkit.plugin.java.JavaPlugin open class OntimeHandlerPlugin() : JavaPlugin() { + + private lateinit var config: Config + override fun onEnable() { - logger.info("onEnable is called!") + 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) + logger.info("OntimeHandlerPlugin enabled :D") } override fun onDisable() { - logger.info("onDisable is called!") + logger.info("OntimeHandlerPlugin disabled :(") } } \ No newline at end of file diff --git a/src/main/kotlin/de/sh_edraft/config/Config.kt b/src/main/kotlin/de/sh_edraft/config/Config.kt new file mode 100644 index 0000000..9ea089b --- /dev/null +++ b/src/main/kotlin/de/sh_edraft/config/Config.kt @@ -0,0 +1,69 @@ +package de.sh_edraft.config + +import java.net.InetAddress +import java.net.UnknownHostException +import java.util.* +import java.util.logging.Logger + + +class Config(private var logger: Logger) { + private val properties = Properties() + + var hostName: String = "" + get() { + return this.getHostname() ?: "localhost" + } + private set + + var environment: String = "" + get() { + return System.getenv("PLUGIN_ENVIRONMENT") ?: "production" + } + private set + + lateinit var ApiURL: String + private set + + lateinit var ApiKey: String + private set + + private fun getHostname(): String? { + try { + return InetAddress.getLocalHost().hostName + } catch (ex: UnknownHostException) { + println("Hostname can not be resolved") + } + return null + } + + fun read(filename: String, optional: Boolean = false) { + logger.config("Try to read config ${filename}") + var foundFile = false + try { + val file = this::class.java.getResourceAsStream(filename) + if (file == null && optional) { + logger.config("${filename} not found") + return + } + this.properties.load(file) + logger.info("Found config ${filename}") + foundFile = true + } catch (e: Exception) { + this.logger.warning("Error loading ${filename}") + } + + if (!foundFile) { + return + } + try { + this.ApiURL = this.getProperty("apiURL").toString() + this.ApiKey = this.getProperty("apiKey").toString() + } catch (e: Exception) { + this.logger.severe("Error loading config") + } + } + + private fun getProperty(key: String): String? { + return this.properties.getProperty(key) + } +} \ No newline at end of file diff --git a/src/main/resources/config.development.properties b/src/main/resources/config.development.properties new file mode 100644 index 0000000..6359af7 --- /dev/null +++ b/src/main/resources/config.development.properties @@ -0,0 +1,2 @@ +apiURL: http://localhost/api/ +apiKey: abcd \ No newline at end of file diff --git a/src/main/resources/config.edrafts-pc.properties b/src/main/resources/config.edrafts-pc.properties new file mode 100644 index 0000000..6359af7 --- /dev/null +++ b/src/main/resources/config.edrafts-pc.properties @@ -0,0 +1,2 @@ +apiURL: http://localhost/api/ +apiKey: abcd \ No newline at end of file diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties new file mode 100644 index 0000000..adc8eac --- /dev/null +++ b/src/main/resources/config.properties @@ -0,0 +1,2 @@ +apiURL=https://kdb.keksdose-gaming.de/api/ +apiKey=abcd \ No newline at end of file diff --git a/src/main/resources/config.staging.properties b/src/main/resources/config.staging.properties new file mode 100644 index 0000000..b8f03e4 --- /dev/null +++ b/src/main/resources/config.staging.properties @@ -0,0 +1,2 @@ +apiURL: https://kdb-test.keksdose-gaming.de/api/ +apiKey: abcd \ No newline at end of file From 79fe4f1934814c83217a200bffbff5c778c2faa4 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 12 Jan 2023 23:24:15 +0100 Subject: [PATCH 3/9] Added readme text #1 --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index fa96990..35f61d6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # spigot_ontime_handler +Spigot plugin to send Minecraft derivates to OnMemberJoin, OnMemberRemove & OnMessage to the KDB-API to be processed +accordingly \ No newline at end of file From 888eca98455d10e3cc5159c0a118d1fc3ce42594 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 13 Jan 2023 10:40:19 +0100 Subject: [PATCH 4/9] Added build tools #1 --- README.md | 24 +++++++++++++++++++++++- build.sh | 1 + run.sh | 5 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 build.sh create mode 100644 run.sh diff --git a/README.md b/README.md index 35f61d6..0d60cf7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,26 @@ # spigot_ontime_handler Spigot plugin to send Minecraft derivates to OnMemberJoin, OnMemberRemove & OnMessage to the KDB-API to be processed -accordingly \ No newline at end of file +accordingly + +## Execute + +### Install dependencies: + +Install ```spigot server``` in folder ```./spigot``` first!! + +```bash +mvn install +``` + +### Build and Run the plugin: + +```bash +./run.sh +``` + +### Build only: + +```bash +./build.sh +``` \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..0c35637 --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +mvn package -f pom.xml \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..fd9919b --- /dev/null +++ b/run.sh @@ -0,0 +1,5 @@ +bash build.sh +cp target/kd_ontime_handler_spigot-*-SNAPSHOT.jar spigot/plugins +cd spigot +export PLUGIN_ENVIRONMENT=development +java -Xms1G -Xmx8G -jar spigot.jar --nogui \ No newline at end of file From 9f4d25e90f2978d0d11b3f43ee1f56ead82511ae Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 13 Jan 2023 11:52:37 +0100 Subject: [PATCH 5/9] Added event handling --- .../de/sh_edraft/OntimeHandlerPlugin.kt | 6 +++++ .../de/sh_edraft/events/OnJoinListener.kt | 13 ++++++++++ .../de/sh_edraft/events/OnLeaveListener.kt | 25 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt create mode 100644 src/main/kotlin/de/sh_edraft/events/OnLeaveListener.kt diff --git a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt index 9952edc..0c0e836 100644 --- a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt +++ b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt @@ -1,6 +1,8 @@ package de.sh_edraft import de.sh_edraft.config.Config +import de.sh_edraft.events.OnJoinListener +import de.sh_edraft.events.OnLeaveListener import org.bukkit.plugin.java.JavaPlugin open class OntimeHandlerPlugin() : JavaPlugin() { @@ -12,6 +14,10 @@ open class OntimeHandlerPlugin() : JavaPlugin() { 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) + logger.info("OntimeHandlerPlugin enabled :D") } diff --git a/src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt b/src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt new file mode 100644 index 0000000..0cf8b2f --- /dev/null +++ b/src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt @@ -0,0 +1,13 @@ +package de.sh_edraft.events + +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 { + @EventHandler + fun onPlayerJoin(p: PlayerJoinEvent) { + logger.info("Player ${p.player.player?.displayName} joined") + } +} \ 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 new file mode 100644 index 0000000..7e0a3db --- /dev/null +++ b/src/main/kotlin/de/sh_edraft/events/OnLeaveListener.kt @@ -0,0 +1,25 @@ +package de.sh_edraft.events + +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.player.PlayerEvent +import org.bukkit.event.player.PlayerKickEvent +import org.bukkit.event.player.PlayerQuitEvent +import java.util.logging.Logger + +class OnLeaveListener(private val logger: Logger) : Listener { + + private fun handleQuit(p: PlayerEvent) { + this.logger.info("Player ${p.player.player?.displayName} leaved") + } + + @EventHandler + fun onPlayerQuit(p: PlayerQuitEvent) { + this.handleQuit(p) + } + + @EventHandler + fun onPlayerKick(p: PlayerKickEvent) { + this.handleQuit(p) + } +} \ No newline at end of file From 8516685a511d4933466235e0f1f963387ac346ee Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 20:13:59 +0100 Subject: [PATCH 6/9] 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 From d695f2820b5077ef3b9d13ebb45730fa6182cf81 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 14 Feb 2023 08:06:04 +0100 Subject: [PATCH 7/9] Moved config --- .../kotlin/de/sh_edraft/OntimeHandlerPlugin.kt | 6 +++--- src/main/kotlin/de/sh_edraft/config/Config.kt | 18 ++++++++++++++---- .../resources/config.development.properties | 2 -- .../resources/config.edrafts-pc.properties | 2 -- src/main/resources/config.properties | 2 -- src/main/resources/config.staging.properties | 2 -- 6 files changed, 17 insertions(+), 15 deletions(-) delete mode 100644 src/main/resources/config.development.properties delete mode 100644 src/main/resources/config.edrafts-pc.properties delete mode 100644 src/main/resources/config.properties delete mode 100644 src/main/resources/config.staging.properties diff --git a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt index 2ce6ff9..f72537d 100644 --- a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt +++ b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt @@ -14,9 +14,9 @@ open class OntimeHandlerPlugin() : JavaPlugin() { 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) + this.config.read("plugins/kd_ontime/config.properties", false) + this.config.read("plugins/kd_ontime/config.${this.config.environment}.properties", true) + this.config.read("plugins/kd_ontime/config.${this.config.hostName}.properties", true) this.dataService = DataService(logger, this.config) diff --git a/src/main/kotlin/de/sh_edraft/config/Config.kt b/src/main/kotlin/de/sh_edraft/config/Config.kt index 9ea089b..51e37e4 100644 --- a/src/main/kotlin/de/sh_edraft/config/Config.kt +++ b/src/main/kotlin/de/sh_edraft/config/Config.kt @@ -1,5 +1,6 @@ package de.sh_edraft.config +import java.io.File import java.net.InetAddress import java.net.UnknownHostException import java.util.* @@ -40,12 +41,21 @@ class Config(private var logger: Logger) { logger.config("Try to read config ${filename}") var foundFile = false try { - val file = this::class.java.getResourceAsStream(filename) - if (file == null && optional) { - logger.config("${filename} not found") + val file = File(filename); + if (!file.exists()) { + if (!optional) { + file.parentFile.mkdirs(); + logger.config("${filename} not found") + file.writeText( + """ + apiURL: http://localhost/api/graphql + apiKey: abcd + """.trimIndent() + ) + } return } - this.properties.load(file) + this.properties.load(file.reader()) logger.info("Found config ${filename}") foundFile = true } catch (e: Exception) { diff --git a/src/main/resources/config.development.properties b/src/main/resources/config.development.properties deleted file mode 100644 index 6359af7..0000000 --- a/src/main/resources/config.development.properties +++ /dev/null @@ -1,2 +0,0 @@ -apiURL: http://localhost/api/ -apiKey: abcd \ No newline at end of file diff --git a/src/main/resources/config.edrafts-pc.properties b/src/main/resources/config.edrafts-pc.properties deleted file mode 100644 index de8cbc9..0000000 --- a/src/main/resources/config.edrafts-pc.properties +++ /dev/null @@ -1,2 +0,0 @@ -apiURL: http://localhost:8044/api/graphql -apiKey: 7b93e3b46bbfbdafc7b1e7a9d3fc05ebf68c0ead3093bda8802fa241081c7173 \ No newline at end of file diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties deleted file mode 100644 index adc8eac..0000000 --- a/src/main/resources/config.properties +++ /dev/null @@ -1,2 +0,0 @@ -apiURL=https://kdb.keksdose-gaming.de/api/ -apiKey=abcd \ No newline at end of file diff --git a/src/main/resources/config.staging.properties b/src/main/resources/config.staging.properties deleted file mode 100644 index b8f03e4..0000000 --- a/src/main/resources/config.staging.properties +++ /dev/null @@ -1,2 +0,0 @@ -apiURL: https://kdb-test.keksdose-gaming.de/api/ -apiKey: abcd \ No newline at end of file From 00ab06cb62b50734c3a293fdc416da743ad9a974 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 5 Mar 2023 14:16:10 +0100 Subject: [PATCH 8/9] Updated new server handling logic --- .../de/sh_edraft/OntimeHandlerPlugin.kt | 4 +- .../kotlin/de/sh_edraft/data/DataService.kt | 50 +++---------------- .../de/sh_edraft/events/OnJoinListener.kt | 3 +- .../{OnLeaveListener.kt => OnLeftListener.kt} | 5 +- 4 files changed, 13 insertions(+), 49 deletions(-) rename src/main/kotlin/de/sh_edraft/events/{OnLeaveListener.kt => OnLeftListener.kt} (84%) 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 From bc5f7f4d9bec5da80fa3ee5c3b11b41fdf0697c3 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 9 Mar 2023 12:33:54 +0100 Subject: [PATCH 9/9] SendLeftPlayer when plugin is disabled --- src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt index 9379788..0e8fb3a 100644 --- a/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt +++ b/src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt @@ -4,6 +4,7 @@ import de.sh_edraft.config.Config import de.sh_edraft.data.DataService import de.sh_edraft.events.OnJoinListener import de.sh_edraft.events.OnLeftListener +import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin open class OntimeHandlerPlugin() : JavaPlugin() { @@ -27,6 +28,16 @@ open class OntimeHandlerPlugin() : JavaPlugin() { } override fun onDisable() { + try { + for (player in Bukkit.getOnlinePlayers()) { + logger.info("Logout player" + player.displayName) + val playerId = this.dataService.getPlayerGlobalId(player.player!!.displayName) ?: return + this.dataService.sendLeftPlayer(playerId) + } + } catch (ex: Exception) { + logger.severe(ex.message) + } + logger.info("OntimeHandlerPlugin disabled :(") } } \ No newline at end of file