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

config support and logging

parent b92b549e
package jetzt.nicht.minecraft.idleShutdown; package jetzt.nicht.minecraft.idleShutdown;
import java.util.logging.Logger;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
...@@ -10,19 +11,38 @@ import jetzt.nicht.minecraft.idleShutdown.PlayerQuitListener; ...@@ -10,19 +11,38 @@ import jetzt.nicht.minecraft.idleShutdown.PlayerQuitListener;
import jetzt.nicht.minecraft.idleShutdown.PlayerJoinListener; import jetzt.nicht.minecraft.idleShutdown.PlayerJoinListener;
public class IdleShutdown extends JavaPlugin { public class IdleShutdown extends JavaPlugin {
PlayerQuitListener playerQuitListener; private Logger log;
PlayerJoinListener playerJoinListener; private Integer idleWaitTime;
Timer idleTimer; private PlayerQuitListener playerQuitListener;
private PlayerJoinListener playerJoinListener;
private Timer idleTimer;
@Override @Override
public void onEnable() { public void onEnable() {
// First of all, get the logger
this.log = getLogger();
// Write the default config, if it does not exist.
saveDefaultConfig();
// Get the current config, default or otherwise.
this.idleWaitTime = getConfig().getInt("idle_wait_time");
// Negative times are not supported (duh).
if (this.idleWaitTime < 0) {
log.warning("You cannot use a negative idle_wait_time! Time set to 0 seconds.");
this.idleWaitTime = 0;
}
// Let the admin know the configured idle time.
log.info(String.format(("This server is running IdleShutdown: " +
"It will stop after %d seconds with no player online."),
this.idleWaitTime));
// Create our listeners once, assign them later. // Create our listeners once, assign them later.
// We pass in a reference to ourselves so the listener can call back. // We pass in a reference to ourselves so the listener can call back.
this.playerQuitListener = new PlayerQuitListener(this); this.playerQuitListener = new PlayerQuitListener(this);
this.playerJoinListener = new PlayerJoinListener(this); this.playerJoinListener = new PlayerJoinListener(this);
// Register our listener that listens for players quitting. // Register our listener that listens for players quitting.
System.out.println("Registering PlayerQuitListener..."); log.finer("Registering PlayerQuitListener...");
getServer().getPluginManager() getServer().getPluginManager()
.registerEvents(this.playerQuitListener, this); .registerEvents(this.playerQuitListener, this);
...@@ -30,7 +50,7 @@ public class IdleShutdown extends JavaPlugin { ...@@ -30,7 +50,7 @@ public class IdleShutdown extends JavaPlugin {
// runtime, or the server was started automatically without anyone // runtime, or the server was started automatically without anyone
// actually joining. TODO: Make this configurable. // actually joining. TODO: Make this configurable.
if (noPlayerOnline()) { if (noPlayerOnline()) {
System.out.println("There are no players online!"); log.fine("There are no players online!");
startIdleTimer(); startIdleTimer();
} }
} }
...@@ -39,55 +59,46 @@ public class IdleShutdown extends JavaPlugin { ...@@ -39,55 +59,46 @@ public class IdleShutdown extends JavaPlugin {
public void onDisable() { public void onDisable() {
// Cancel the timer and unregister all Listeners // Cancel the timer and unregister all Listeners
if (this.idleTimer != null) { if (this.idleTimer != null) {
log.finer("Cancelling timer...");
this.idleTimer.cancel(); this.idleTimer.cancel();
} }
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
} }
public void onTimerExpired() { public void onTimerExpired() {
System.out.println("Timer expired!"); log.fine("Timer expired!");
if(noPlayerOnline()) { if(noPlayerOnline()) {
System.out.println("No players online, shutting down"); log.info("No players online, shutting down");
// Unregister all our Listeners, just to be on the safe side. // Unregister all our Listeners, just to be on the safe side.
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
getServer().shutdown(); getServer().shutdown();
} else { } else {
System.out.println("WARNING: A player has come online and we have " log.warning("A player has come online and we have "
+ "not been notified! Something is very wrong here!"); + "not been notified! Something is very wrong here!");
} }
} }
void onPlayerQuit() { void onPlayerQuit() {
System.out.println("A player has quit!"); log.fine("A player has quit!");
if (lastPlayerOnline()) { if (lastPlayerOnline()) {
System.out.println("The last player is leaving!"); log.info("The last player is leaving!");
// 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);
startIdleTimer(); startIdleTimer();
} }
} }
void onPlayerJoin() { void onPlayerJoin() {
System.out.println("A player has joined!"); log.fine("A player has joined!");
// XXX: This might also be racy, if a player quits and joins after we // XXX: This might also be racy, if a player quits and joins after we
// unregister the Listener but before we abort the timer. // unregister the Listener but before we abort the timer.
System.out.println("Unregistering PlayerJoinListener..."); log.finer("Unregistering PlayerJoinListener...");
HandlerList.unregisterAll(this.playerJoinListener); HandlerList.unregisterAll(this.playerJoinListener);
System.out.println("Aborting timer..."); log.finer("Aborting timer...");
this.idleTimer.cancel(); this.idleTimer.cancel();
} }
...@@ -110,14 +121,28 @@ public class IdleShutdown extends JavaPlugin { ...@@ -110,14 +121,28 @@ public class IdleShutdown extends JavaPlugin {
} }
private void startIdleTimer() { private void startIdleTimer() {
System.out.println("Creating and scheduling timer..."); if (this.idleWaitTime == 0) {
this.idleTimer = new Timer(); log.fine("idle_wait_time is 0, not scheduling timer!");
TimerTask idleTimerTask = new TimerTask() { onTimerExpired();
@Override } else {
public void run() { // We now register PlayerJoinListener, so we can abort the timer
onTimerExpired(); // 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
this.idleTimer.schedule(idleTimerTask, 30*1000); // yet running!
log.finer("Registering PlayerJoinListener...");
getServer().getPluginManager()
.registerEvents(this.playerJoinListener, this);
log.finer("Creating and scheduling timer...");
this.idleTimer = new Timer();
TimerTask idleTimerTask = new TimerTask() {
@Override
public void run() {
onTimerExpired();
}
};
this.idleTimer.schedule(idleTimerTask, this.idleWaitTime*1000);
}
} }
} }
---
# After the last player left, how long should the server wait before
# shutting down? Time in seconds.
# This is not influenced by the servers tick rate.
idle_wait_time: 30
--- ---
name: IdleShutdown name: IdleShutdown
website: https://git.revreso.de/gigadoc2/spigot-idle-shutdown
description: Shut down spigot when no one is online. description: Shut down spigot when no one is online.
author: Gigadoc2 author: Gigadoc2
version: 0.1.0 version: 1.0.0
main: jetzt.nicht.minecraft.idleShutdown.IdleShutdown 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