Merge pull request '1.0' (#7) from 1.0 into master
Reviewed-on: sh-edraft.de/kd_ontime_handler_spigot#7 Reviewed-by: Ebola-Chan <nick.jungmann@gmail.com>
This commit is contained in:
commit
999e134b64
7
.gitignore
vendored
7
.gitignore
vendored
@ -25,7 +25,8 @@ hs_err_pid*
|
|||||||
replay_pid*
|
replay_pid*
|
||||||
|
|
||||||
# idea
|
# idea
|
||||||
.idea
|
.idea/*
|
||||||
# build sources
|
# build sources
|
||||||
target
|
target/*
|
||||||
|
# spigot test server
|
||||||
|
spigot/*
|
||||||
|
24
README.md
24
README.md
@ -1,2 +1,26 @@
|
|||||||
# spigot_ontime_handler
|
# spigot_ontime_handler
|
||||||
|
|
||||||
|
Spigot plugin to send Minecraft derivates to OnMemberJoin, OnMemberRemove & OnMessage to the KDB-API to be processed
|
||||||
|
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
|
||||||
|
```
|
@ -1,8 +1,18 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module version="4">
|
<module version="4">
|
||||||
|
<component name="FacetManager">
|
||||||
|
<facet type="minecraft" name="Minecraft">
|
||||||
|
<configuration>
|
||||||
|
<autoDetectTypes>
|
||||||
|
<platformType>SPIGOT</platformType>
|
||||||
|
</autoDetectTypes>
|
||||||
|
</configuration>
|
||||||
|
</facet>
|
||||||
|
</component>
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/spigot" />
|
||||||
</content>
|
</content>
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
44
pom.xml
44
pom.xml
@ -31,7 +31,6 @@
|
|||||||
<version>1.19.2-R0.1-SNAPSHOT</version>
|
<version>1.19.2-R0.1-SNAPSHOT</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jetbrains.kotlin</groupId>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
<artifactId>kotlin-test-junit5</artifactId>
|
<artifactId>kotlin-test-junit5</artifactId>
|
||||||
@ -49,6 +48,21 @@
|
|||||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||||
<version>1.7.21</version>
|
<version>1.7.21</version>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -81,7 +95,33 @@
|
|||||||
<artifactId>exec-maven-plugin</artifactId>
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
<version>1.6.0</version>
|
<version>1.6.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<mainClass>MainKt</mainClass>
|
<mainClass>OntimeHandlerPlugin</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>11</source>
|
||||||
|
<target>11</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>single</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<descriptorRefs>
|
||||||
|
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||||
|
</descriptorRefs>
|
||||||
|
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||||
|
<appendAssemblyId>false</appendAssemblyId>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
5
run.sh
Normal file
5
run.sh
Normal file
@ -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
|
@ -1,7 +0,0 @@
|
|||||||
fun main(args: Array<String>) {
|
|
||||||
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()}")
|
|
||||||
}
|
|
43
src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt
Normal file
43
src/main/kotlin/de/sh_edraft/OntimeHandlerPlugin.kt
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
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.OnLeftListener
|
||||||
|
import org.bukkit.Bukkit
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin
|
||||||
|
|
||||||
|
open class OntimeHandlerPlugin() : JavaPlugin() {
|
||||||
|
|
||||||
|
private lateinit var config: Config
|
||||||
|
|
||||||
|
private lateinit var dataService: DataService
|
||||||
|
|
||||||
|
override fun onEnable() {
|
||||||
|
this.config = Config(logger)
|
||||||
|
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)
|
||||||
|
|
||||||
|
server.pluginManager.registerEvents(OnJoinListener(logger, this.dataService), this)
|
||||||
|
server.pluginManager.registerEvents(OnLeftListener(logger, this.dataService), this)
|
||||||
|
|
||||||
|
logger.info("OntimeHandlerPlugin enabled :D")
|
||||||
|
}
|
||||||
|
|
||||||
|
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 :(")
|
||||||
|
}
|
||||||
|
}
|
79
src/main/kotlin/de/sh_edraft/config/Config.kt
Normal file
79
src/main/kotlin/de/sh_edraft/config/Config.kt
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
package de.sh_edraft.config
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
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 = 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.reader())
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
94
src/main/kotlin/de/sh_edraft/data/DataService.kt
Normal file
94
src/main/kotlin/de/sh_edraft/data/DataService.kt
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
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 getUserJoinedMutation(id: String): String {
|
||||||
|
return JSONObject(
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"query": "mutation { userJoinedGameServer { userJoined(input: { ident: \"$id\" }) { id } } }"
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
).toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getUserLeftMutation(id: String): String {
|
||||||
|
return JSONObject(
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"query": "mutation { userJoinedGameServer { userLeft(input: { ident: \"$id\" }) { 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 sendJoinedPlayer(id: String) {
|
||||||
|
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 sendLeftPlayer(id: String) {
|
||||||
|
val client = HttpClient.newBuilder().build();
|
||||||
|
|
||||||
|
val request = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(this.config.ApiURL))
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString(this.getUserLeftMutation(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
21
src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt
Normal file
21
src/main/kotlin/de/sh_edraft/events/OnJoinListener.kt
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
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,
|
||||||
|
private val dataService: DataService
|
||||||
|
) : Listener {
|
||||||
|
@EventHandler
|
||||||
|
fun onPlayerJoin(p: PlayerJoinEvent) {
|
||||||
|
if (p.player.player == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
val playerId = this.dataService.getPlayerGlobalId(p.player.player!!.displayName) ?: return;
|
||||||
|
this.dataService.sendJoinedPlayer(playerId);
|
||||||
|
}
|
||||||
|
}
|
33
src/main/kotlin/de/sh_edraft/events/OnLeftListener.kt
Normal file
33
src/main/kotlin/de/sh_edraft/events/OnLeftListener.kt
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
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
|
||||||
|
import org.bukkit.event.player.PlayerKickEvent
|
||||||
|
import org.bukkit.event.player.PlayerQuitEvent
|
||||||
|
import java.util.logging.Logger
|
||||||
|
|
||||||
|
class OnLeftListener(
|
||||||
|
private val logger: Logger,
|
||||||
|
private val dataService: DataService
|
||||||
|
) : Listener {
|
||||||
|
|
||||||
|
private fun handleQuit(p: PlayerEvent) {
|
||||||
|
if (p.player.player == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
val playerId = this.dataService.getPlayerGlobalId(p.player.player!!.displayName) ?: return;
|
||||||
|
this.dataService.sendLeftPlayer(playerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
fun onPlayerQuit(p: PlayerQuitEvent) {
|
||||||
|
this.handleQuit(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
fun onPlayerKick(p: PlayerKickEvent) {
|
||||||
|
this.handleQuit(p)
|
||||||
|
}
|
||||||
|
}
|
3
src/main/resources/plugin.yml
Normal file
3
src/main/resources/plugin.yml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
name: sh_edraft.OntimeHandlerPlugin
|
||||||
|
version: 1.0
|
||||||
|
main: de.sh_edraft.OntimeHandlerPlugin
|
Binary file not shown.
@ -1,5 +0,0 @@
|
|||||||
#Generated by Maven
|
|
||||||
#Thu Jan 12 20:27:56 CET 2023
|
|
||||||
groupId=de.sh-edraft
|
|
||||||
artifactId=kd_ontime_handler_spigot
|
|
||||||
version=1.0-SNAPSHOT
|
|
Loading…
Reference in New Issue
Block a user