From 1b4a1c854dad67b8732aa0372cf6c578545dd93f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 7 Oct 2017 17:50:37 +1100 Subject: [PATCH] Fix ZS not working properly when the level it is unloaded. --- MCGalaxy/Commands/World/CmdDeleteLvl.cs | 3 ++- MCGalaxy/Commands/World/CmdGoto.cs | 4 ++-- MCGalaxy/Commands/World/CmdOverseer.SubCommands.cs | 7 +++++-- MCGalaxy/Games/ZombieSurvival/ZSPlugin.cs | 7 +++++++ MCGalaxy/Levels/LevelActions.cs | 12 ++++++------ MCGalaxy/Server/Server.cs | 4 ---- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/MCGalaxy/Commands/World/CmdDeleteLvl.cs b/MCGalaxy/Commands/World/CmdDeleteLvl.cs index 6a7d343ef..1a90267b4 100644 --- a/MCGalaxy/Commands/World/CmdDeleteLvl.cs +++ b/MCGalaxy/Commands/World/CmdDeleteLvl.cs @@ -39,7 +39,8 @@ namespace MCGalaxy.Commands.World { if (!LevelInfo.ValidateAction(p, map, "delete this level")) return; Player.Message(p, "Created backup."); - LevelActions.Delete(map.ToLower()); + if (LevelActions.Delete(map)) return; + Player.Message(p, LevelActions.DeleteFailedMessage); } public override void Help(Player p) { diff --git a/MCGalaxy/Commands/World/CmdGoto.cs b/MCGalaxy/Commands/World/CmdGoto.cs index 6e01f7259..08f0accf2 100644 --- a/MCGalaxy/Commands/World/CmdGoto.cs +++ b/MCGalaxy/Commands/World/CmdGoto.cs @@ -25,8 +25,8 @@ namespace MCGalaxy.Commands.World { public override string type { get { return CommandTypes.World; } } public override LevelPermission defaultRank { get { return LevelPermission.Guest; } } public override CommandAlias[] Aliases { - get { return new[] { new CommandAlias("j"), new CommandAlias("Join"), - new CommandAlias("gr", "-random"), new CommandAlias("GotoRandom", "-random") }; } + get { return new[] { new CommandAlias("j"), new CommandAlias("Join"), new CommandAlias("gr", "-random"), + new CommandAlias("GotoRandom", "-random"), new CommandAlias("JoinRandom", "-random") }; } } public override bool SuperUseable { get { return false; } } diff --git a/MCGalaxy/Commands/World/CmdOverseer.SubCommands.cs b/MCGalaxy/Commands/World/CmdOverseer.SubCommands.cs index c3749ac14..541dbb6a7 100644 --- a/MCGalaxy/Commands/World/CmdOverseer.SubCommands.cs +++ b/MCGalaxy/Commands/World/CmdOverseer.SubCommands.cs @@ -200,8 +200,11 @@ namespace MCGalaxy.Commands.World { if (!OwnsMap(p, p.level)) return; Player.Message(p, "Created backup."); - LevelActions.Delete(map); - Player.Message(p, "Map " + map + " was removed."); + if (LevelActions.Delete(map)) { + Player.Message(p, "Map " + map + " was removed."); + } else { + Player.Message(p, LevelActions.DeleteFailedMessage); + } } diff --git a/MCGalaxy/Games/ZombieSurvival/ZSPlugin.cs b/MCGalaxy/Games/ZombieSurvival/ZSPlugin.cs index bd7cf5c87..616695904 100644 --- a/MCGalaxy/Games/ZombieSurvival/ZSPlugin.cs +++ b/MCGalaxy/Games/ZombieSurvival/ZSPlugin.cs @@ -19,6 +19,7 @@ using System; using MCGalaxy.Events.EconomyEvents; using MCGalaxy.Events.EntityEvents; +using MCGalaxy.Events.LevelEvents; using MCGalaxy.Events.PlayerEvents; using MCGalaxy.Network; @@ -39,6 +40,7 @@ namespace MCGalaxy.Games.ZS { OnPlayerActionEvent.Register(HandlePlayerAction, Priority.High); OnPlayerSpawningEvent.Register(HandlePlayerSpawning, Priority.High); OnBlockChangeEvent.Register(HandleBlockChange, Priority.High); + OnLevelUnloadEvent.Register(HandleLevelUnload, Priority.High); } public override void Unload(bool shutdown) { @@ -51,6 +53,7 @@ namespace MCGalaxy.Games.ZS { OnPlayerActionEvent.Unregister(HandlePlayerAction); OnPlayerSpawningEvent.Unregister(HandlePlayerSpawning); OnBlockChangeEvent.Unregister(HandleBlockChange); + OnLevelUnloadEvent.Unregister(HandleLevelUnload); } void HandleTabListEntryAdded(Entity entity, ref string tabName, ref string tabGroup, Player dst) { @@ -165,5 +168,9 @@ namespace MCGalaxy.Games.ZS { Player.Message(p, "Blocks Left: &4" + p.Game.BlocksLeft); } } + + void HandleLevelUnload(Level lvl) { + if (lvl == Game.Map) lvl.cancelunload = true; + } } } diff --git a/MCGalaxy/Levels/LevelActions.cs b/MCGalaxy/Levels/LevelActions.cs index b21400bfa..731360cac 100644 --- a/MCGalaxy/Levels/LevelActions.cs +++ b/MCGalaxy/Levels/LevelActions.cs @@ -26,8 +26,7 @@ namespace MCGalaxy { public static class LevelActions { - /// Renames the .lvl (and related) files and database tables. - /// Does not perform any unloading. + /// Renames the .lvl (and related) files and database tables. Does not unload. public static void Rename(string src, string dst) { File.Move(LevelInfo.MapPath(src), LevelInfo.MapPath(dst)); @@ -108,11 +107,11 @@ namespace MCGalaxy { }*/ - /// Deletes the .lvl (and related) files and database tables. - /// Unloads a level (if present) which exactly matches name. - public static void Delete(string name) { + public const string DeleteFailedMessage = "Unable to delete the level, because it could not be unloaded. A game may currently be running on it."; + /// Deletes the .lvl (and related) files and database tables. Unloads level if it is loaded. + public static bool Delete(string name) { Level lvl = LevelInfo.FindExact(name); - if (lvl != null) lvl.Unload(); + if (lvl != null && !lvl.Unload()) return false; if (!Directory.Exists("levels/deleted")) Directory.CreateDirectory("levels/deleted"); @@ -135,6 +134,7 @@ namespace MCGalaxy { DeleteDatabaseTables(name); BlockDBFile.DeleteBackingFile(name); + return true; } static void DeleteDatabaseTables(string name) { diff --git a/MCGalaxy/Server/Server.cs b/MCGalaxy/Server/Server.cs index 221fda925..9f5d99869 100644 --- a/MCGalaxy/Server/Server.cs +++ b/MCGalaxy/Server/Server.cs @@ -105,10 +105,6 @@ namespace MCGalaxy { Economy.LoadDatabase(); Server.zombie.CheckTableExists(); - Level[] loaded = LevelInfo.Loaded.Items; - foreach (Level l in loaded) - l.Unload(); - Background.QueueOnce(UpgradeTasks.CombineEnvFiles); Background.QueueOnce(LoadMainLevel); Plugin.Load();