diff --git a/GUI/PropertyWindow/PropertyWindow.cs b/GUI/PropertyWindow/PropertyWindow.cs index b5ad679e1..47d945131 100644 --- a/GUI/PropertyWindow/PropertyWindow.cs +++ b/GUI/PropertyWindow/PropertyWindow.cs @@ -109,7 +109,7 @@ namespace MCGalaxy.Gui SaveBlocks(); SaveGameProps(); - SrvProperties.Load(); // loads when saving? + SrvProperties.ApplyChanges(); } void btnDiscard_Click(object sender, EventArgs e) { Dispose(); } diff --git a/MCGalaxy/Commands/World/CmdMain.cs b/MCGalaxy/Commands/World/CmdMain.cs index 67640d087..aa2860208 100644 --- a/MCGalaxy/Commands/World/CmdMain.cs +++ b/MCGalaxy/Commands/World/CmdMain.cs @@ -49,8 +49,11 @@ namespace MCGalaxy.Commands.World { if (!LevelInfo.Check(p, data.Rank, map, "set main to this map")) return; Server.SetMainLevel(map); + Server.Config.MainLevel = map; SrvProperties.Save(); - p.Message("Set main level to {0}", Server.mainLevel.ColoredName); + + p.Message("Set main level to {0}", + LevelInfo.GetConfig(map).Color + map); } } diff --git a/MCGalaxy/Events/LevelEvents.cs b/MCGalaxy/Events/LevelEvents.cs index ce58ac77a..de6b54f39 100644 --- a/MCGalaxy/Events/LevelEvents.cs +++ b/MCGalaxy/Events/LevelEvents.cs @@ -22,8 +22,8 @@ using BlockID = System.UInt16; namespace MCGalaxy.Events.LevelEvents { public delegate void OnLevelLoaded(Level lvl); - public sealed class OnLevelLoadedEvent : IEvent { - + public sealed class OnLevelLoadedEvent : IEvent + { public static void Call(Level lvl) { if (handlers.Count == 0) return; CallCommon(pl => pl(lvl)); @@ -31,11 +31,12 @@ namespace MCGalaxy.Events.LevelEvents { } public delegate void OnLevelLoad(string name, string path, ref bool cancel); - public sealed class OnLevelLoadEvent : IEvent { - + public sealed class OnLevelLoadEvent : IEvent + { public static void Call(string name, string path, ref bool cancel) { IEvent[] items = handlers.Items; - for (int i = 0; i < items.Length; i++) { + for (int i = 0; i < items.Length; i++) + { try { items[i].method(name, path, ref cancel); } catch (Exception ex) { LogHandlerException(ex, items[i]); } } @@ -43,11 +44,12 @@ namespace MCGalaxy.Events.LevelEvents { } public delegate void OnLevelSave(Level lvl, ref bool cancel); - public sealed class OnLevelSaveEvent : IEvent { - + public sealed class OnLevelSaveEvent : IEvent + { public static void Call(Level lvl, ref bool cancel) { IEvent[] items = handlers.Items; - for (int i = 0; i < items.Length; i++) { + for (int i = 0; i < items.Length; i++) + { try { items[i].method(lvl, ref cancel); } catch (Exception ex) { LogHandlerException(ex, items[i]); } } @@ -55,11 +57,12 @@ namespace MCGalaxy.Events.LevelEvents { } public delegate void OnLevelUnload(Level lvl, ref bool cancel); - public sealed class OnLevelUnloadEvent : IEvent { - + public sealed class OnLevelUnloadEvent : IEvent + { public static void Call(Level lvl, ref bool cancel) { IEvent[] items = handlers.Items; - for (int i = 0; i < items.Length; i++) { + for (int i = 0; i < items.Length; i++) + { try { items[i].method(lvl, ref cancel); } catch (Exception ex) { LogHandlerException(ex, items[i]); } } @@ -67,8 +70,8 @@ namespace MCGalaxy.Events.LevelEvents { } public delegate void OnLevelAdded(Level lvl); - public sealed class OnLevelAddedEvent : IEvent { - + public sealed class OnLevelAddedEvent : IEvent + { public static void Call(Level lvl) { if (handlers.Count == 0) return; CallCommon(pl => pl(lvl)); @@ -76,8 +79,8 @@ namespace MCGalaxy.Events.LevelEvents { } public delegate void OnLevelRemoved(Level lvl); - public sealed class OnLevelRemovedEvent : IEvent { - + public sealed class OnLevelRemovedEvent : IEvent + { public static void Call(Level lvl) { if (handlers.Count == 0) return; CallCommon(pl => pl(lvl)); @@ -85,8 +88,8 @@ namespace MCGalaxy.Events.LevelEvents { } public delegate void OnPhysicsStateChanged(Level lvl, PhysicsState state); - public sealed class OnPhysicsStateChangedEvent : IEvent { - + public sealed class OnPhysicsStateChangedEvent : IEvent + { public static void Call(Level lvl, PhysicsState state) { if (handlers.Count == 0) return; CallCommon(pl => pl(lvl, state)); @@ -94,8 +97,8 @@ namespace MCGalaxy.Events.LevelEvents { } public delegate void OnPhysicsLevelChanged(Level lvl, int level); - public sealed class OnPhysicsLevelChangedEvent : IEvent { - + public sealed class OnPhysicsLevelChangedEvent : IEvent + { public static void Call(Level lvl, int level) { if (handlers.Count == 0) return; CallCommon(pl => pl(lvl, level)); @@ -103,17 +106,17 @@ namespace MCGalaxy.Events.LevelEvents { } public delegate void OnPhysicsUpdate(ushort x, ushort y, ushort z, PhysicsArgs args, Level lvl); - public sealed class OnPhysicsUpdateEvent : IEvent { - public static void Call(ushort x, ushort y, ushort z, PhysicsArgs extraInfo, Level l) { - + public sealed class OnPhysicsUpdateEvent : IEvent + { + public static void Call(ushort x, ushort y, ushort z, PhysicsArgs extraInfo, Level l) { if (handlers.Count == 0) return; CallCommon(pl => pl(x, y, z, extraInfo, l)); } } public delegate void OnLevelRenamed(string srcMap, string dstMap); - public sealed class OnLevelRenamedEvent : IEvent { - + public sealed class OnLevelRenamedEvent : IEvent + { public static void Call(string srcMap, string dstMap) { if (handlers.Count == 0) return; CallCommon(pl => pl(srcMap, dstMap)); @@ -121,8 +124,8 @@ namespace MCGalaxy.Events.LevelEvents { } public delegate void OnLevelCopied(string srcMap, string dstMap); - public sealed class OnLevelCopiedEvent : IEvent { - + public sealed class OnLevelCopiedEvent : IEvent + { public static void Call(string srcMap, string dstMap) { if (handlers.Count == 0) return; CallCommon(pl => pl(srcMap, dstMap)); @@ -130,8 +133,8 @@ namespace MCGalaxy.Events.LevelEvents { } public delegate void OnLevelDeleted(string map); - public sealed class OnLevelDeletedEvent : IEvent { - + public sealed class OnLevelDeletedEvent : IEvent + { public static void Call(string map) { if (handlers.Count == 0) return; CallCommon(pl => pl(map)); @@ -139,11 +142,24 @@ namespace MCGalaxy.Events.LevelEvents { } public delegate void OnBlockHandlersUpdated(Level lvl, BlockID block); - public sealed class OnBlockHandlersUpdatedEvent : IEvent { - + public sealed class OnBlockHandlersUpdatedEvent : IEvent + { public static void Call(Level lvl, BlockID block) { if (handlers.Count == 0) return; CallCommon(pl => pl(lvl, block)); } } + + public delegate void OnMainLevelChanging(ref string map); + public sealed class OnMainLevelChangingEvent : IEvent + { + public static void Call(ref string map) { + IEvent[] items = handlers.Items; + for (int i = 0; i < items.Length; i++) + { + try { items[i].method(ref map); } + catch (Exception ex) { LogHandlerException(ex, items[i]); } + } + } + } } diff --git a/MCGalaxy/Events/PlayerDBEvents.cs b/MCGalaxy/Events/PlayerDBEvents.cs index ee743bab0..d66168669 100644 --- a/MCGalaxy/Events/PlayerDBEvents.cs +++ b/MCGalaxy/Events/PlayerDBEvents.cs @@ -17,15 +17,16 @@ */ using System; -namespace MCGalaxy.Events.PlayerDBEvents { - +namespace MCGalaxy.Events.PlayerDBEvents +{ public delegate void OnInfoSave(Player p, ref bool cancel); /// Called whenever the server saves player's stats to the database. - public sealed class OnInfoSaveEvent : IEvent { - + public sealed class OnInfoSaveEvent : IEvent + { public static void Call(Player p, ref bool cancel) { IEvent[] items = handlers.Items; - for (int i = 0; i < items.Length; i++) { + for (int i = 0; i < items.Length; i++) + { try { items[i].method(p, ref cancel); } catch (Exception ex) { LogHandlerException(ex, items[i]); } } @@ -34,8 +35,8 @@ namespace MCGalaxy.Events.PlayerDBEvents { public delegate void OnInfoSwap(string src, string dst); /// Called when the information of two players is being swapped. - public sealed class OnInfoSwapEvent : IEvent { - + public sealed class OnInfoSwapEvent : IEvent + { public static void Call(string src, string dst) { if (handlers.Count == 0) return; CallCommon(pl => pl(src, dst)); diff --git a/MCGalaxy/Games/RoundsGame/RoundsGame.Plugin.cs b/MCGalaxy/Games/RoundsGame/RoundsGame.Plugin.cs index f48751945..10b24110e 100644 --- a/MCGalaxy/Games/RoundsGame/RoundsGame.Plugin.cs +++ b/MCGalaxy/Games/RoundsGame/RoundsGame.Plugin.cs @@ -27,7 +27,9 @@ namespace MCGalaxy.Games { public abstract partial class RoundsGame : IGame { protected virtual void HookEventHandlers() { - OnLevelUnloadEvent.Register(HandleLevelUnload, Priority.High); + OnLevelUnloadEvent.Register(HandleLevelUnload, Priority.High); + OnMainLevelChangingEvent.Register(HandleMainChanged, Priority.High); + OnSendingHeartbeatEvent.Register(HandleSendingHeartbeat, Priority.High); OnInfoSaveEvent.Register(HandleSaveStats, Priority.High); @@ -38,6 +40,8 @@ namespace MCGalaxy.Games { protected virtual void UnhookEventHandlers() { OnLevelUnloadEvent.Unregister(HandleLevelUnload); + OnMainLevelChangingEvent.Unregister(HandleMainChanged); + OnSendingHeartbeatEvent.Unregister(HandleSendingHeartbeat); OnInfoSaveEvent.Unregister(HandleSaveStats); @@ -83,12 +87,6 @@ namespace MCGalaxy.Games { } } - protected void HandleLevelUnload(Level lvl, ref bool cancel) { - if (lvl != Map) return; - Logger.Log(LogType.GameActivity, "Unload cancelled! A {0} game is currently going on!", GameName); - cancel = true; - } - protected void HandlePlayerAction(Player p, PlayerAction action, string message, bool stealth) { if (!(action == PlayerAction.Referee || action == PlayerAction.UnReferee)) return; if (p.level != Map) return; @@ -106,5 +104,18 @@ namespace MCGalaxy.Games { Entities.GlobalSpawn(p, false, ""); TabList.Update(p, true); } + + + void HandleLevelUnload(Level lvl, ref bool cancel) { + if (lvl != Map) return; + Logger.Log(LogType.GameActivity, "Unload cancelled! A {0} game is currently going on!", GameName); + cancel = true; + } + + void HandleMainChanged(ref string map) { + Level cur = Map; // in case Map is changed by another thread + if (!GetConfig().SetMainLevel || cur == null) return; + map = cur.name; + } } } diff --git a/MCGalaxy/Scripting/ScriptingOperations.cs b/MCGalaxy/Scripting/ScriptingOperations.cs index 8bc315635..7bf32f5a4 100644 --- a/MCGalaxy/Scripting/ScriptingOperations.cs +++ b/MCGalaxy/Scripting/ScriptingOperations.cs @@ -42,7 +42,7 @@ namespace MCGalaxy.Scripting if (!File.Exists(path)) { p.Message("File &9{0} &Snot found.", path); return false; - } + } // TODO: Move error handling elsewhere List plugins = IScripting.LoadPlugin(path, false); if (plugins == null) { diff --git a/MCGalaxy/Server/Server.cs b/MCGalaxy/Server/Server.cs index 0e9e466be..a7991b5a3 100644 --- a/MCGalaxy/Server/Server.cs +++ b/MCGalaxy/Server/Server.cs @@ -29,6 +29,7 @@ using MCGalaxy.Commands; using MCGalaxy.DB; using MCGalaxy.Drawing; using MCGalaxy.Eco; +using MCGalaxy.Events.LevelEvents; using MCGalaxy.Events.ServerEvents; using MCGalaxy.Games; using MCGalaxy.Network; @@ -307,6 +308,7 @@ namespace MCGalaxy } public static bool SetMainLevel(string map) { + OnMainLevelChangingEvent.Call(ref map); string main = mainLevel != null ? mainLevel.name : Server.Config.MainLevel; if (map.CaselessEq(main)) return false; @@ -315,13 +317,13 @@ namespace MCGalaxy lvl = LevelActions.Load(Player.Console, map, false); if (lvl == null) return false; - SetMainLevel(lvl); return true; + SetMainLevel(lvl); + return true; } public static void SetMainLevel(Level lvl) { - Level oldMain = mainLevel; + Level oldMain = mainLevel; mainLevel = lvl; - Server.Config.MainLevel = lvl.name; oldMain.AutoUnload(); } diff --git a/MCGalaxy/Server/ServerProperties.cs b/MCGalaxy/Server/ServerProperties.cs index 1601da8b0..dac7586bd 100644 --- a/MCGalaxy/Server/ServerProperties.cs +++ b/MCGalaxy/Server/ServerProperties.cs @@ -26,15 +26,18 @@ namespace MCGalaxy { public static void Load() { old = new OldPerms(); - if (PropertiesFile.Read(Paths.ServerPropsFile, ref old, LineProcessor)) - Server.SettingsUpdate(); - - Database.UpdateActiveBackend(); + PropertiesFile.Read(Paths.ServerPropsFile, ref old, LineProcessor); + ApplyChanges(); + Save(); + } + + public static void ApplyChanges() { if (!Directory.Exists(Server.Config.BackupDirectory)) Server.Config.BackupDirectory = "levels/backups"; - Save(); + Server.SettingsUpdate(); + Database.UpdateActiveBackend(); Server.SetMainLevel(Server.Config.MainLevel); }