commit b5c49268339d168a12a3c3e7f3be2004f4d2b98c
parent ea370bd9603a125c7decbd1b69e44fa32f850773
Author: typable <typable.dev@gmail.com>
Date: Sat, 14 Nov 2020 19:37:56 +0100
Added StandbyListener
Diffstat:
2 files changed, 85 insertions(+), 0 deletions(-)
diff --git a/src/de/typable/minecrafthub/constant/DefaultConstants.java b/src/de/typable/minecrafthub/constant/DefaultConstants.java
@@ -0,0 +1,6 @@
+package de.typable.minecrafthub.constant;
+
+public class DefaultConstants
+{
+ public static final long TICK = 20;
+}
diff --git a/src/de/typable/minecrafthub/event/StandbyListener.java b/src/de/typable/minecrafthub/event/StandbyListener.java
@@ -0,0 +1,79 @@
+package de.typable.minecrafthub.event;
+
+import java.util.Collection;
+
+import org.bukkit.Bukkit;
+import org.bukkit.entity.Player;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.event.player.PlayerJoinEvent;
+import org.bukkit.event.player.PlayerQuitEvent;
+import org.bukkit.plugin.Plugin;
+import org.bukkit.scheduler.BukkitTask;
+
+import de.typable.minecrafthub.constant.DefaultConstants;
+
+
+public class StandbyListener implements Listener, Runnable
+{
+ private Plugin plugin;
+ private BukkitTask task;
+ private boolean enabled;
+ private long delay;
+
+ public StandbyListener(Plugin plugin)
+ {
+ this.plugin = plugin;
+ this.delay = 10 * 60;
+ }
+
+ @EventHandler
+ private void onPlayerJoin(PlayerJoinEvent event)
+ {
+ if(enabled && task != null)
+ {
+ task.cancel();
+ task = null;
+ }
+ }
+
+ @EventHandler
+ private void onPlayerQuit(PlayerQuitEvent event)
+ {
+ if(enabled)
+ {
+ Collection<? extends Player> online = Bukkit.getOnlinePlayers();
+
+ if(online.size() == 1 && online.contains(event.getPlayer()))
+ {
+ task = Bukkit.getScheduler().runTaskLater(plugin, this, delay * DefaultConstants.TICK);
+ }
+ }
+ }
+
+ @Override
+ public void run()
+ {
+ Bukkit.getServer().shutdown();
+ }
+
+ public boolean isEnabled()
+ {
+ return enabled;
+ }
+
+ public void setEnabled(boolean enabled)
+ {
+ this.enabled = enabled;
+ }
+
+ public long getDelay()
+ {
+ return delay;
+ }
+
+ public void setDelay(long delay)
+ {
+ this.delay = delay;
+ }
+}