context: #region References
  using System;
  using System.Collections.Generic;
  using System.Linq;
  
  using Server.Commands;
  using Server.Engines.CityLoyalty;
  using Server.Factions;
  using Server.Gumps;
  using Server.Mobiles;
  using Server.Network;
  using Server.Spells;
  #endregion
  
  namespace Server.Items
  {
  	public class PublicMoongate : Item
  	{
  		public static List<PublicMoongate> Moongates { get; private set; }
  		
  		static PublicMoongate()
  		{
  			Moongates = new List<PublicMoongate>();
  		}
  
  		public static void Initialize()
  		{
  			CommandSystem.Register("MoonGen", AccessLevel.Administrator, MoonGen_OnCommand);
  			CommandSystem.Register("MoonGenDelete", AccessLevel.Administrator, MoonGenDelete_OnCommand);
  		}
  
  		[Usage("MoonGen")]
  		[Description("Generates public moongates. Removes all old moongates.")]
  		public static void MoonGen_OnCommand(CommandEventArgs e)
  		{
  			DeleteAll();
  
  			var count = 0;
  
  			if (!Siege.SiegeShard)
  			{
  				count += MoonGen(PMList.Trammel);
  			}
  
  			count += MoonGen(PMList.Felucca);
  			count += MoonGen(PMList.Ilshenar);
  			count += MoonGen(PMList.Malas);
  			count += MoonGen(PMList.Tokuno);
  			count += MoonGen(PMList.TerMur);
  
  			World.Broadcast(0x35, true, "{0} moongates generated.", count);
  		}
  
  		private static int MoonGen(PMList list)
  		{
  			foreach (var entry in list.Entries)
  			{
  				var o = new PublicMoongate();
  
  				o.MoveToWorld(entry.Location, list.Map);
  
  				if (entry.Number == 1060642) // Umbra
  				{
  					o.Hue = 0x497;
  				}
  			}
  
  			return list.Entries.Length;
  		}
  
  		[Usage("MoonGenDelete")]
  		[Description("Removes all public moongates.")]
  		public static void MoonGenDelete_OnCommand(CommandEventArgs e)
  		{
  			DeleteAll();
  		}
  
  		public static void DeleteAll()
  		{
  			var count = Moongates.Count;
  
  			var index = count;
  
  			while (--index >= 0)
  			{
  				if(index < Moongates.Count)
  					Moongates[index].Delete();
  			}
  
  			Moongates.Clear();
  
  			if (count > 0)
  			{
  				World.Broadcast(0x35, true, "{0:#,0} moongates removed.", count);
  			}
  		}
  
  		public static IEnumerable<PublicMoongate> FindGates(Map map)
  		{
  			PublicMoongate o;
  
  			var i = Moongates.Count;
  
  			while (--i >= 0)
  			{
  				o = Moongates[i];
  
  				if (o == null || o.Deleted)
  				{
  					Moongates.RemoveAt(i);
  				}
  				else if (o.Map == map)
  				{
  					yield return o;
  				}
  			}
  		}
  		
  		public override string DefaultName { get { return "Moongate"; } }
  
  		public override bool HandlesOnMovement { get { return true; } }
  		public override bool ForceShowProperties { get { return true; } }
  
  		[Constructable]
  		public PublicMoongate()
  			: base(0xF6C)
  		{
  			Movable = false;
  			Light = LightType.Circle300;
  
  			Moongates.Add(this);
  		}
  
  		public PublicMoongate(Serial serial)
  			: base(serial)
  		{
  			Moongates.Add(this);
  		}
  
  		public override void OnDelete()
  		{
  			base.OnDelete();
  
  			Moongates.Remove(this);
  		}
  
  		public override void OnAfterDelete()
  		{
  			base.OnAfterDelete();
  
  			Moongates.Remove(this);
  		}
  
  		public override void OnDoubleClick(Mobile m)
  		{
  			UseGate(m);
  		}
  
  		public override bool OnMoveOver(Mobile m)
  		{
  			if (m.Player && m.CanSee(this))
  			{
  				UseGate(m);
  			}
  
  			return m.Map == Map && m.InRange(this, 1);
  		}
  
  		public override void OnMovement(Mobile m, Point3D oldLocation)
  		{
  			if (m.Player && !Utility.InRange(m.Location, Location, 1) && Utility.InRange(oldLocation, Location, 1))
  			{
  				m.CloseGump(typeof(MoongateGump));
  			}
  		}
  
  		public virtual bool CanUseGate(Mobile m, bool message)
  		{
  			if (m.IsStaff())
  			{
  				//Staff can always use a gate!
  				return true;
  			}
  			
  			if (m.Criminal)
  			{
  				// Thou'rt a criminal and cannot escape so easily.
  				m.SendLocalizedMessage(1005561, "", 0x22); 
  				return false;
  			}
  			
  			if (SpellHelper.CheckCombat(m))
  			{
  				// Wouldst thou flee during the heat of battle??
  				m.SendLocalizedMessage(1005564, "", 0x22); 
  				return false;
  			}
  			
  			if (m.Spell != null)
  			{
  				// You are too busy to do that at the moment.
  				m.SendLocalizedMessage(1049616); 
  				return false;
  			}
  			
  
gt: 			if (m.Holding != null)