Updated new server handling logic

This commit is contained in:
Sven Heidemann 2023-03-05 14:16:10 +01:00
parent a037697369
commit 00ab06cb62
4 changed files with 13 additions and 49 deletions

View File

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

View File

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

View File

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

View File

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