50 lines
1.3 KiB
Kotlin
50 lines
1.3 KiB
Kotlin
package nl.voidcorp.dbot
|
|
|
|
import com.google.gson.Gson
|
|
import com.google.gson.GsonBuilder
|
|
import nl.voidcorp.dbot.commands.UnityCommand
|
|
import java.io.File
|
|
import java.util.*
|
|
import kotlin.random.Random
|
|
|
|
|
|
val gson: Gson = GsonBuilder().setPrettyPrinting().create()
|
|
|
|
|
|
fun <E> MutableList<E>.addAll(vararg e: E) = this.addAll(listOf(*e))
|
|
|
|
fun <E> List<E>.random(): E = this[Random.nextInt(this.size)]
|
|
|
|
fun List<UnityCommand>.catMap(): Map<String, List<UnityCommand>> {
|
|
val m = mutableMapOf<String, MutableList<UnityCommand>>()
|
|
for (c in this) {
|
|
val cname = c.category.name
|
|
if (!m.containsKey(cname)) {
|
|
m[cname] = mutableListOf()
|
|
}
|
|
|
|
m[cname]!! += c
|
|
}
|
|
return m
|
|
}
|
|
|
|
fun String.emptyOr(other: String): String = if (this.isEmpty()) other else this
|
|
|
|
fun restartApplication() {
|
|
val javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"
|
|
val currentJar = File(Events::class.java.protectionDomain.codeSource.location.toURI())
|
|
|
|
/* is it a jar file? */
|
|
if (!currentJar.name.endsWith(".jar"))
|
|
return
|
|
|
|
/* Build command: java -jar application.jar */
|
|
val command = ArrayList<String>()
|
|
command.add(javaBin)
|
|
command.add("-jar")
|
|
command.add(currentJar.path)
|
|
|
|
val builder = ProcessBuilder(command)
|
|
builder.start()
|
|
System.exit(0)
|
|
} |