From 176bebac8f21346ab905ee14fc48a13a0734985d Mon Sep 17 00:00:00 2001 From: vince Date: Wed, 25 Nov 2020 09:28:34 +0800 Subject: [PATCH] add noPlayerToggle option this adds an option to automatically pause/unpause the worldBorder fill based on players connecting or disconnecting from the server --- .../java/com/wimbli/WorldBorder/Config.java | 5 ++++ .../com/wimbli/WorldBorder/WBListener.java | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/com/wimbli/WorldBorder/Config.java b/src/main/java/com/wimbli/WorldBorder/Config.java index cdb410e..9aa1b11 100644 --- a/src/main/java/com/wimbli/WorldBorder/Config.java +++ b/src/main/java/com/wimbli/WorldBorder/Config.java @@ -49,6 +49,7 @@ public class Config { private static int fillMemoryTolerance = 500; private static boolean preventBlockPlace = false; private static boolean preventMobSpawn = false; + private static boolean noPlayersToggle = false; // for monitoring plugin efficiency // public static long timeUsed = 0; @@ -290,6 +291,8 @@ public class Config { return knockBack; } + public static boolean NoPlayersToggle() { return noPlayersToggle; } + public static void setTimerTicks(int ticks) { timerTicks = ticks; log("Timer delay set to " + timerTicks + " tick(s). That is roughly " + (timerTicks * 50) + "ms / " + (((double) timerTicks * 50.0) / 1000.0) + " seconds."); @@ -531,6 +534,7 @@ public class Config { fillMemoryTolerance = cfg.getInt("fill-memory-tolerance", 500); preventBlockPlace = cfg.getBoolean("prevent-block-place"); preventMobSpawn = cfg.getBoolean("prevent-mob-spawn"); + noPlayersToggle = cfg.getBoolean("no-players-toggle"); StartBorderTimer(); @@ -634,6 +638,7 @@ public class Config { cfg.set("fill-memory-tolerance", fillMemoryTolerance); cfg.set("prevent-block-place", preventBlockPlace); cfg.set("prevent-mob-spawn", preventMobSpawn); + cfg.set("no-players-toggle", noPlayersToggle); cfg.set("worlds", null); for (Entry stringBorderDataEntry : borders.entrySet()) { diff --git a/src/main/java/com/wimbli/WorldBorder/WBListener.java b/src/main/java/com/wimbli/WorldBorder/WBListener.java index fd3f459..f85e2cc 100644 --- a/src/main/java/com/wimbli/WorldBorder/WBListener.java +++ b/src/main/java/com/wimbli/WorldBorder/WBListener.java @@ -1,11 +1,15 @@ package com.wimbli.WorldBorder; +import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Location; +import org.bukkit.Server; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerPortalEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.world.ChunkLoadEvent; import org.bukkit.event.world.ChunkUnloadEvent; @@ -83,4 +87,23 @@ public class WBListener implements Listener { chunk.setForceLoaded(false); } + // If player joins and noPlayersToggle is on automatically pause any existing fill task + @EventHandler + public void onPlayerJoin(PlayerJoinEvent e) { + if (!Config.NoPlayersToggle() || Config.fillTask == null || Config.fillTask.isPaused()) + return; + + Config.fillTask.pause(true); + Config.log("Detected player online. World map generation task automatically paused."); + } + + // If no players online and noPlayersToggle is on automatically unpause any existing fill task + @EventHandler + public void onPlayerQuit(PlayerQuitEvent e) { + if (!Config.NoPlayersToggle() || Config.fillTask == null || Bukkit.getOnlinePlayers().size() > 1 || !Config.fillTask.isPaused()) + return; + + Config.fillTask.pause(false); + Config.log("No players online. World map generation task automatically unpaused."); + } }