1.0 #7
@@ -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")
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
		Reference in New Issue
	
	Block a user