From 5816b5badd2046518c8c132693e0dd67633728d7 Mon Sep 17 00:00:00 2001 From: high Date: Fri, 30 Dec 2011 21:36:40 -0500 Subject: [PATCH] Added + operator to HandlerList. Now it works just like events meaning you don't have to initialize it. Note that means you also cannot call 'Register' unless you create an instance first. You can do 'TileEdit += HandlerList.Create(test, priority, true);'. Which 'Create' takes the same parameters as Register. Or if you don't need the parameters you can do 'TileEdit += test;'. --- TShockAPI/GetDataHandlers.cs | 5 ++++- TShockAPI/HandlerList.cs | 38 ++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 7 deletions(-) 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