Plugin.kt 1.05 KB
Newer Older
Linus K.'s avatar
Bugfix    
Linus K. committed
1
package me.linuscde.mcplugins.gracefuldockerstop
Linus K.'s avatar
Linus K. committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
import sun.misc.Signal
import java.lang.IllegalArgumentException

class Plugin: JavaPlugin() {

    override fun onEnable() {
        try {
            Signal.handle(Signal("USR2")) { signal: Signal ->
                val pl = Bukkit.getPluginManager().getPlugin(name);
                if(pl == null) {
                    println("[$name] Cannot get plugin instance!")
                    return@handle
                }
                Bukkit.getScheduler().scheduleSyncDelayedTask(pl) {
                    println("[$name] Got signal ${signal.name} ({$signal.number}). Executing /stop from console...")
                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "stop")
                };
            }
        }catch (ex: IllegalArgumentException) {
            println("[$name] Failed to register signal handler for USR2. This might not be a problem if this plugin was previously loaded.")
            ex.printStackTrace()
        }
    }
}