diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 7c9b2dbd..1bcaccc0 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -58,9 +58,12 @@ namespace TShockAPI public float Type { get; set; } public float EditType { get; set; } } - public static HandlerList TileEdit = new HandlerList(); + public static HandlerList TileEdit; public static bool OnTileEdit(float x, float y, float type, float editType) { + if (TileEdit == null) + return false; + var args = new TileEditEventArgs { X = x, diff --git a/TShockAPI/HandlerList.cs b/TShockAPI/HandlerList.cs index f411efc5..225d00a7 100644 --- a/TShockAPI/HandlerList.cs +++ b/TShockAPI/HandlerList.cs @@ -11,7 +11,7 @@ namespace TShockAPI } public class HandlerList where T : EventArgs { - protected class HandlerObject + public class HandlerItem { public EventHandler Handler { get; set; } public HandlerPriority Priority { get; set; } @@ -19,10 +19,10 @@ namespace TShockAPI } protected object HandlerLock = new object(); - protected List Handlers { get; set; } + protected List Handlers { get; set; } public HandlerList() { - Handlers = new List(); + Handlers = new List(); } /// @@ -32,10 +32,15 @@ namespace TShockAPI /// Priority of the delegate /// Should the handler receive a call even if it has been handled public void Register(EventHandler handler, HandlerPriority priority = HandlerPriority.Normal, bool gethandled = false) + { + Register(Create(handler, priority, gethandled)); + } + + public void Register(HandlerItem obj) { lock (HandlerLock) { - Handlers.Add(new HandlerObject { Handler = handler, Priority = priority, GetHandled = gethandled }); + Handlers.Add(obj); Handlers = Handlers.OrderBy(h => (int)h.Priority).ToList(); } } @@ -50,11 +55,11 @@ namespace TShockAPI public void Invoke(object sender, T e) { - List handlers; + List handlers; lock (HandlerLock) { //Copy the list for invoking as to not keep it locked during the invokes - handlers = new List(Handlers); + handlers = new List(Handlers); } var hargs = e as HandledEventArgs; @@ -66,6 +71,27 @@ namespace TShockAPI } } } + + public static HandlerItem Create(EventHandler handler, HandlerPriority priority = HandlerPriority.Normal, bool gethandled = false) + { + return new HandlerItem { Handler = handler, Priority = priority, GetHandled = gethandled }; + } + public static HandlerList operator +(HandlerList hand, HandlerItem obj) + { + if (hand == null) + hand = new HandlerList(); + + hand.Register(obj); + return hand; + } + public static HandlerList operator +(HandlerList hand, EventHandler handler) + { + if (hand == null) + hand = new HandlerList(); + + hand.Register(Create(handler)); + return hand; + } } public enum HandlerPriority