Verified Commit 6f5cf834 authored by Gigadoc 2's avatar Gigadoc 2

initial working version

Hardcoded idle time of 30 secs.
parents
.gradle/
build/
This diff is collapsed.
plugins {
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'
}
task sourceJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
rootProject.name = 'IdleShutdown'
package jetzt.nicht.minecraft.idleShutdown;
import java.util.Timer;
import java.util.TimerTask;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import jetzt.nicht.minecraft.idleShutdown.PlayerQuitListener;
import jetzt.nicht.minecraft.idleShutdown.PlayerJoinListener;
public class IdleShutdown extends JavaPlugin {
PlayerQuitListener playerQuitListener;
PlayerJoinListener playerJoinListener;
Timer idleTimer;
@Override
public void onEnable() {
// Create our listeners once, assign them later.
// We pass in a reference to ourselves so the listener can call back.
this.playerQuitListener = new PlayerQuitListener(this);
this.playerJoinListener = new PlayerJoinListener(this);
// Register our listener that listens for players quitting.
System.out.println("Registering PlayerQuitListener...");
getServer().getPluginManager()
.registerEvents(this.playerQuitListener, this);
}
@Override
public void onDisable() {
}
public void onTimerExpired() {
System.out.println("Timer expired, shutting down server!");
// Unregister all our Listeners, just to be on the safe side.
HandlerList.unregisterAll(this);
getServer().shutdown();
}
void onPlayerQuit() {
System.out.println("A player has quit!");
if (noPlayersOnline()) {
System.out.println("No players are online anymore!");
// We now register PlayerJoinListener, so we can abort the timer
// if a player joins in the meantime
// XXX: This might be racy: if the Listener is activated before we
// set the timer, we might crash when trying to abort the timer not
// yet running!
System.out.println("Registering PlayerJoinListener...");
getServer().getPluginManager()
.registerEvents(this.playerJoinListener, this);
System.out.println("Creating and scheduling timer...");
this.idleTimer = new Timer();
TimerTask idleTimerTask = new TimerTask() {
@Override
public void run() {
onTimerExpired();
}
};
this.idleTimer.schedule(idleTimerTask, 30*1000);
}
}
void onPlayerJoin() {
System.out.println("A player has joined!");
// XXX: This might also be racy, if a player quits and joins after we
// unregister the Listener but before we abort the timer.
System.out.println("Unregistering PlayerJoinListener...");
HandlerList.unregisterAll(this.playerJoinListener);
System.out.println("Aborting timer...");
this.idleTimer.cancel();
}
private boolean noPlayersOnline() {
// Note: The event gets fired before the player is acutally reported as
// "offline", so we just say there is no one online if the last player
// is in the process of quitting.
if (getServer().getOnlinePlayers().size() <= 1) {
return true;
} else {
return false;
}
}
}
package jetzt.nicht.minecraft.idleShutdown;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import jetzt.nicht.minecraft.idleShutdown.IdleShutdown;
public class PlayerJoinListener implements Listener {
IdleShutdown mainPlugin;
public PlayerJoinListener(IdleShutdown mainPlugin) {
this.mainPlugin = mainPlugin;
}
// Set lowest event priority to execute as late as possible. The idea being
// that other plugins might modify player events to hide "non-player" joins,
// kick players immediately or whatever, and we want to see the end result
// of all that, not the intermediate steps.
// This is just a guess how to behave, and might be totally wrong. Thus, it
// might also be changed in the future.
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(PlayerJoinEvent event) {
this.mainPlugin.onPlayerJoin();
}
}
package jetzt.nicht.minecraft.idleShutdown;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import jetzt.nicht.minecraft.idleShutdown.IdleShutdown;
public class PlayerQuitListener implements Listener {
IdleShutdown mainPlugin;
public PlayerQuitListener(IdleShutdown mainPlugin) {
this.mainPlugin = mainPlugin;
}
// Set lowest event priority to execute as late as possible. The idea being
// that other plugins might modify player events to hide "non-player" joins,
// kick players immediately or whatever, and we want to see the end result
// of all that, not the intermediate steps.
// This is just a guess how to behave, and might be totally wrong. Thus, it
// might also be changed in the future.
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerQuit(PlayerQuitEvent event) {
this.mainPlugin.onPlayerQuit();
}
}
---
name: IdleShutdown
description: Shut down spigot when no one is online.
author: Gigadoc2
version: 0.1.0
main: jetzt.nicht.minecraft.idleShutdown.IdleShutdown
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment