OttoBot/src/main/kotlin/nl/voidcorp/dbot/Util.kt

50 lines
1.3 KiB
Kotlin
Raw Normal View History

2018-10-10 23:54:01 +02:00
package nl.voidcorp.dbot
2018-10-15 22:25:51 +02:00
import com.google.gson.Gson
2018-10-10 23:54:01 +02:00
import com.google.gson.GsonBuilder
import nl.voidcorp.dbot.commands.UnityCommand
import java.io.File
2018-10-10 23:54:01 +02:00
import java.util.*
import kotlin.random.Random
2018-10-10 23:54:01 +02:00
2018-10-15 22:25:51 +02:00
val gson: Gson = GsonBuilder().setPrettyPrinting().create()
2018-10-10 23:54:01 +02:00
2018-10-15 22:25:51 +02:00
fun <E> MutableList<E>.addAll(vararg e: E) = this.addAll(listOf(*e))
fun <E> List<E>.random(): E = this[Random.nextInt(this.size)]
2018-10-16 13:33:15 +02:00
fun List<UnityCommand>.catMap(): Map<String, List<UnityCommand>> {
val m = mutableMapOf<String, MutableList<UnityCommand>>()
2018-10-16 13:33:15 +02:00
for (c in this) {
val cname = c.category.name
2018-10-16 13:33:15 +02:00
if (!m.containsKey(cname)) {
m[cname] = mutableListOf()
}
2018-10-25 13:47:54 +02:00
m[cname]!! += c
2018-10-16 13:33:15 +02:00
}
return m
2018-10-25 13:47:54 +02:00
}
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)
}