Added logic to handle stuff

Closes #2
Closes #3
Closes #4
This commit is contained in:
Sven Heidemann 2023-02-12 20:13:59 +01:00
parent 9f4d25e90f
commit 8516685a51
7 changed files with 185 additions and 10 deletions

View File

@ -1,5 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>SPIGOT</platformType>
</autoDetectTypes>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />

19
pom.xml
View File

@ -48,6 +48,21 @@
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
<dependency>
<groupId>com.google.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>20120626</version>
</dependency>
</dependencies>
<build>
@ -87,8 +102,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>

View File

@ -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")
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}

View File

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

View File

@ -1,2 +1,2 @@
apiURL: http://localhost/api/
apiKey: abcd
apiURL: http://localhost:8044/api/graphql
apiKey: 7b93e3b46bbfbdafc7b1e7a9d3fc05ebf68c0ead3093bda8802fa241081c7173