Added read config logic #1

This commit is contained in:
Sven Heidemann 2023-01-12 23:19:29 +01:00
parent d5ff74b6a4
commit 1f2a0ec68b
7 changed files with 114 additions and 4 deletions

29
pom.xml
View File

@ -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>
@ -81,7 +80,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>6</source>
<target>6</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>

View File

@ -1,13 +1,21 @@
package de.sh_edraft package de.sh_edraft
import de.sh_edraft.config.Config
import org.bukkit.plugin.java.JavaPlugin import org.bukkit.plugin.java.JavaPlugin
open class OntimeHandlerPlugin() : JavaPlugin() { open class OntimeHandlerPlugin() : JavaPlugin() {
private lateinit var config: Config
override fun onEnable() { 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() { override fun onDisable() {
logger.info("onDisable is called!") logger.info("OntimeHandlerPlugin disabled :(")
} }
} }

View File

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

View File

@ -0,0 +1,2 @@
apiURL: http://localhost/api/
apiKey: abcd

View File

@ -0,0 +1,2 @@
apiURL: http://localhost/api/
apiKey: abcd

View File

@ -0,0 +1,2 @@
apiURL=https://kdb.keksdose-gaming.de/api/
apiKey=abcd

View File

@ -0,0 +1,2 @@
apiURL: https://kdb-test.keksdose-gaming.de/api/
apiKey: abcd