Verified Commit c5c7f473 authored by Gigadoc 2's avatar Gigadoc 2
Browse files

initial working version

Currenttly only features systemd ready-notification on startup.
parent cf81fa9e
Loading
Loading
Loading
Loading

build.gradle

0 → 100644
+15 −0
Original line number Diff line number Diff line
plugins {
	id 'com.github.johnrengelman.shadow' version '2.0.4'
	id 'java'
}

repositories {
	mavenCentral()
	maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
	maven { url = 'https://oss.sonatype.org/content/repositories/snapshots' }
}

dependencies {
	compileOnly 'org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT'
	compile 'info.faljse:SDNotify:1.1'
}

settings.gradle

0 → 100644
+1 −0
Original line number Diff line number Diff line
rootProject.name = 'SpigotSystemd'
+22 −0
Original line number Diff line number Diff line
package jetzt.nicht.minecraft.spigotSystemd;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginEnableEvent;

import jetzt.nicht.minecraft.spigotSystemd.SpigotSystemdPlugin;

public class PluginListener implements Listener {
	SpigotSystemdPlugin mainPlugin;

	public PluginListener(SpigotSystemdPlugin mainPlugin) {
		this.mainPlugin = mainPlugin;
	}

	@EventHandler(priority = EventPriority.MONITOR)
	public void onPluginEnabled(PluginEnableEvent event) {
		System.out.println("Some plugin has been enabled!");
		this.mainPlugin.onPluginEnabled();
	}
}
+81 −0
Original line number Diff line number Diff line
package jetzt.nicht.minecraft.spigotSystemd;

import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import info.faljse.SDNotify.SDNotify;

import jetzt.nicht.minecraft.spigotSystemd.PluginListener;
import jetzt.nicht.minecraft.spigotSystemd.StartupListener;

public class SpigotSystemdPlugin extends JavaPlugin {
	PluginListener pluginListener;
	StartupListener startupListener;

	@Override
	public void onEnable() {
		// we have been loaded, so the server has just been started
		// XXX: Check some other state, to become reload-safe!
		// Check systemd if our service is started!

		// Register our listener that waits for the server to be ready.
		// We pass in a reference to ourselves so the listener can call back.
		this.startupListener = new StartupListener(this);
		System.out.println("Registering StartupListener...");
		getServer().getPluginManager()
			.registerEvents(this.startupListener, this);
	}

	@Override
	public void onDisable() {
	}

	public void onServerMostlyReady() {
		System.out.println("The server is now \"mostly ready\"!");

		// Unregister the StartupListener, as the server is now
		// deemed "mostly ready"
		System.out.println("Unregistering StartupListener...");
		HandlerList.unregisterAll(this.startupListener);

		// Register our listener that waits for plugins to be enabled. See above.
		this.pluginListener = new PluginListener(this);
		System.out.println("Registering PluginListener...");
		getServer().getPluginManager()
			.registerEvents(this.pluginListener, this);

		// Trigger one execution of onPluginEnabled, as all plugins might have
		// been enabled already. This is a bit ugly but technically correct,
		// because at least we ourselves have been enabled.
		this.onPluginEnabled();
	}

	public void onPluginEnabled() {
		if (this.checkPluginsEnabled()) {
			System.out.println("All plugins have been enabled!");
			System.out.println("Unregistering PluginListener...");
			HandlerList.unregisterAll(this.pluginListener);

			System.out.println("Signalling readyness to systemd...");
			SDNotify.sendNotify();
		} else {
			System.out.println("There are still plugins to be enabled, continuing...");
		}
	}

	public boolean checkPluginsEnabled() {
		System.out.println("Checking whether all plugins have been enabled...");
		Plugin[] loadedPlugins = getServer().getPluginManager().getPlugins();
		boolean allPluginsEnabled = true;
		for (Plugin plugin : loadedPlugins) {
			System.out.println(
					String.format("%s: %b", plugin, plugin.isEnabled())
					);
			if (plugin.isEnabled() == false) {
				allPluginsEnabled = false;
				break;
			}
		}
		return allPluginsEnabled;
	}
}
+22 −0
Original line number Diff line number Diff line
package jetzt.nicht.minecraft.spigotSystemd;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldInitEvent;

import jetzt.nicht.minecraft.spigotSystemd.SpigotSystemdPlugin;

public class StartupListener implements Listener {
	SpigotSystemdPlugin mainPlugin;

	public StartupListener(SpigotSystemdPlugin mainPlugin) {
		this.mainPlugin = mainPlugin;
	}

	@EventHandler(priority = EventPriority.MONITOR)
	public void onWorldInitialized(WorldInitEvent event) {
		System.out.println("Some World has been initialized!");
		this.mainPlugin.onServerMostlyReady();
	}
}
Loading