Skip to content

Commit

Permalink
Improve OnLevelLoadEvent to also have path and 'cancel' argument
Browse files Browse the repository at this point in the history
Before there was no way to tell which path the map was being loaded from, and had to rely on a global 'Level.cancelload' variable
  • Loading branch information
UnknownShadow200 committed Nov 16, 2020
1 parent cd5c6bf commit 061f2f9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
11 changes: 7 additions & 4 deletions MCGalaxy/Events/LevelEvents.cs
Expand Up @@ -29,12 +29,15 @@ public sealed class OnLevelLoadedEvent : IEvent<OnLevelLoaded> {
}
}

public delegate void OnLevelLoad(string level);
public delegate void OnLevelLoad(string name, string path, ref bool cancel);
public sealed class OnLevelLoadEvent : IEvent<OnLevelLoad> {

public static void Call(string name) {
if (handlers.Count == 0) return;
CallCommon(pl => pl(name));
public static void Call(string name, string path, ref bool cancel) {
IEvent<OnLevelLoad>[] items = handlers.Items;
for (int i = 0; i < items.Length; i++) {
try { items[i].method(name, path, ref cancel); }
catch (Exception ex) { LogHandlerException(ex, items[i]); }
}
}
}

Expand Down
1 change: 0 additions & 1 deletion MCGalaxy/Levels/Level.Fields.cs
Expand Up @@ -59,7 +59,6 @@ public sealed partial class Level : IDisposable {
get { return Math.Max(10000, (int)(Server.Config.DrawReloadThreshold * Width * Height * Length)); }
}

public static bool cancelload;
public bool cancelsave;
public bool cancelunload;
public bool Changed;
Expand Down
5 changes: 3 additions & 2 deletions MCGalaxy/Levels/Level.cs
Expand Up @@ -267,8 +267,9 @@ public sealed partial class Level : IDisposable {
public static Level Load(string name) { return Load(name, LevelInfo.MapPath(name)); }

public static Level Load(string name, string path) {
OnLevelLoadEvent.Call(name);
if (cancelload) { cancelload = false; return null; }
bool cancel = false;
OnLevelLoadEvent.Call(name, path, ref cancel);
if (cancel) return null;

if (!File.Exists(path)) {
Logger.Log(LogType.Warning, "Attempted to load level {0}, but {1} does not exist.", name, path);
Expand Down

0 comments on commit 061f2f9

Please sign in to comment.