From e01b57ceac564bf8e87ba9b5e1e25c28a52adfb1 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Fri, 5 Aug 2016 14:24:00 -0400 Subject: [PATCH] Implement auto restart system for clans servers in off-peak times --- .../game/clans/clans/ClansManager.java | 61 ++++----- .../game/clans/restart/RestartCommand.java | 23 ++++ .../game/clans/restart/RestartManager.java | 121 ++++++++++++++++++ 3 files changed, 176 insertions(+), 29 deletions(-) create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/restart/RestartCommand.java create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/restart/RestartManager.java diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java index 7736952a9..9a4cd01fa 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java @@ -9,6 +9,35 @@ import java.util.Set; import java.util.TimeZone; import java.util.UUID; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Horse; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityShootBowEvent; +import org.bukkit.event.entity.FoodLevelChangeEvent; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.vehicle.VehicleEnterEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.java.JavaPlugin; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; + import mineplex.core.MiniClientPlugin; import mineplex.core.account.CoreClientManager; import mineplex.core.achievement.AchievementManager; @@ -97,6 +126,7 @@ import mineplex.game.clans.gameplay.HiddenChestManager; import mineplex.game.clans.gameplay.safelog.SafeLog; import mineplex.game.clans.gameplay.safelog.npc.NPCManager; import mineplex.game.clans.items.GearManager; +import mineplex.game.clans.restart.RestartManager; import mineplex.game.clans.spawn.Spawn; import mineplex.game.clans.tutorial.TutorialManager; import mineplex.minecraft.game.classcombat.Class.ClassManager; @@ -119,35 +149,6 @@ import mineplex.minecraft.game.core.fire.Fire; import mineplex.minecraft.game.core.mechanics.Weapon; import mineplex.serverdata.commands.ServerCommandManager; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.entity.Horse; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.SignChangeEvent; -import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; -import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.entity.EntityShootBowEvent; -import org.bukkit.event.entity.FoodLevelChangeEvent; -import org.bukkit.event.entity.PlayerDeathEvent; -import org.bukkit.event.player.AsyncPlayerChatEvent; -import org.bukkit.event.player.PlayerCommandPreprocessEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerLoginEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.event.vehicle.VehicleEnterEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.plugin.java.JavaPlugin; - -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; - public class ClansManager extends MiniClientPluginimplements IRelation { public static final int CLAIMABLE_RADIUS = 800; @@ -455,6 +456,8 @@ public class ClansManager extends MiniClientPluginimplements IRelati _siegeManager = new SiegeManager(this); _netherManager = new NetherManager(this); _amplifierManager = new AmplifierManager(plugin); + + new RestartManager(plugin); } @Override diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/restart/RestartCommand.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/restart/RestartCommand.java new file mode 100644 index 000000000..4a33d2f5f --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/restart/RestartCommand.java @@ -0,0 +1,23 @@ +package mineplex.game.clans.restart; + +import org.bukkit.entity.Player; + +import mineplex.core.command.CommandBase; +import mineplex.core.common.Rank; +import mineplex.core.common.util.F; +import mineplex.core.common.util.UtilPlayer; + +public class RestartCommand extends CommandBase +{ + public RestartCommand(RestartManager plugin) + { + super(plugin, Rank.ADMIN, "forceRestart"); + } + + @Override + public void Execute(Player caller, String[] args) + { + UtilPlayer.message(caller, F.main("Clans", "Initiating server restart!")); + Plugin.restart(); + } +} \ No newline at end of file diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/restart/RestartManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/restart/RestartManager.java new file mode 100644 index 000000000..2800434cb --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/restart/RestartManager.java @@ -0,0 +1,121 @@ +package mineplex.game.clans.restart; + +import java.util.Calendar; +import java.util.LinkedList; + +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.plugin.java.JavaPlugin; + +import mineplex.core.MiniPlugin; +import mineplex.core.common.util.C; +import mineplex.core.common.util.F; +import mineplex.core.common.util.UtilTextMiddle; +import mineplex.core.common.util.UtilTime; +import mineplex.core.common.util.UtilTime.TimeUnit; +import mineplex.core.portal.Portal; +import mineplex.core.updater.UpdateType; +import mineplex.core.updater.event.UpdateEvent; +import mineplex.game.clans.clans.ClansManager; + +public class RestartManager extends MiniPlugin +{ + private static final int MAX_RESTART_TIME = 2; //Server won't auto restart after 2am + private final LinkedList _warnings = new LinkedList<>(); + private Long _restartUnlock; + private Long _restartTime = -1L; + private boolean _restarting; + + public RestartManager(JavaPlugin plugin) + { + super("Restart Manager", plugin); + + if (inRestartZone(Calendar.HOUR_OF_DAY)) + { + _restartUnlock = System.currentTimeMillis() + 1000 + UtilTime.convert(MAX_RESTART_TIME - Calendar.HOUR_OF_DAY, TimeUnit.HOURS, TimeUnit.MILLISECONDS); + } + else + { + _restartUnlock = System.currentTimeMillis(); + } + + _warnings.add(60000L); + _warnings.add(30000L); + _warnings.add(10000L); + _warnings.add(5000L); + addCommand(new RestartCommand(this)); + } + + private boolean inRestartZone(int hour) + { + return hour >= 0 && hour < MAX_RESTART_TIME; //12 am = 0 + } + + private boolean tryRestart() + { + if (!inRestartZone(Calendar.HOUR_OF_DAY) || System.currentTimeMillis() < _restartUnlock) + { + return false; + } + if (ClansManager.getInstance().getAmplifierManager().hasActiveAmplifier()) + { + return false; + } + if (ClansManager.getInstance().getNetherManager().InNether.size() > 0) + { + return false; + } + if (ClansManager.getInstance().getWorldEvent().getEvents().size() > 0) + { + return false; + } + + return true; + } + + public void restart() + { + Bukkit.broadcastMessage(F.main("Clans", "This Clans server will be restarting in " + F.elem(UtilTime.MakeStr(120000)) + "!")); + UtilTextMiddle.display(C.cRed + "Server Restart", C.cGray + "This server will restart in " + F.elem(UtilTime.MakeStr(120000)) + "!"); + _restartTime = System.currentTimeMillis() + 120000; + } + + @EventHandler + public void checkRestart(UpdateEvent event) + { + if (event.getType() != UpdateType.FAST) + { + return; + } + + if (_restarting) + { + return; + } + + if (_restartTime != -1) + { + if (!_warnings.isEmpty()) + { + if (_restartTime - System.currentTimeMillis() <= _warnings.getFirst()) + { + Long time = _warnings.removeFirst(); + Bukkit.broadcastMessage(F.main("Clans", "This Clans server will be restarting in " + F.elem(UtilTime.MakeStr(time)) + "!")); + } + } + if (System.currentTimeMillis() >= _restartTime) + { + _restarting = true; + Portal.getInstance().sendAllPlayers("ClansHub"); + runSyncLater(() -> {Bukkit.shutdown();}, 120L); + } + } + else + { + if (tryRestart()) + { + restart(); + } + } + } +} \ No newline at end of file