public static void AfterNetworkSetup()
 {
     new Thread(() => {
         Thread.CurrentThread.IsBackground = true;
         Log.Write("Started kingdom spawner thread");
         while (true)
         {
             Thread.Sleep(DelayBetweenPlacingAttempts);
             try {
                 if (KingdomsTracker.Count < MaxNumberOfKingdoms)
                 {
                     for (int c = 0; c < NumOfSpotsToCheckPerAttempt; c++)
                     {
                         var kingdomPosition = GetRandomSpot(MaxRangeFromSpawn);
                         var farmSize        = 1 + Pipliz.Random.Next(NpcFarmBuilder.MAX_SIZE);
                         var npcKingdom      = NpcFarm.Create(kingdomPosition, farmSize);
                         var closestBanner   = BannerTracker.GetClosest(kingdomPosition, MinDistanceToBanners);
                         if (closestBanner == null)
                         {
                             LoadChunksBlocking(npcKingdom.GetPrimaryChunkPositions());
                             if (npcKingdom.IsAreaClear())
                             {
                                 LoadChunksBlocking(npcKingdom.GetTotalChunkPositions());
                                 npcKingdom.InitNew();
                                 if (KingdomsTracker.Count >= MaxNumberOfKingdoms)
                                 {
                                     Log.Write($"Reached maximum number ({MaxNumberOfKingdoms}) of kingdoms");
                                 }
                                 break;
                             }
                             try {
                                 currentlyUsedChunksLock.EnterWriteLock();
                                 currentlyUsedChunks.Clear();
                             } finally {
                                 if (currentlyUsedChunksLock.IsWriteLockHeld)
                                 {
                                     currentlyUsedChunksLock.ExitWriteLock();
                                 }
                             }
                         }
                         Thread.Sleep(DelayBetweenSpotChecks);
                     }
                 }
             } catch (Exception exception) {
                 Log.WriteError($"Exception in kingdom update thread; {exception.Message}");
             }
         }
     }).Start();
 }
 public bool TryDoCommand(Players.Player causedBy, string chattext)
 {
     try {
         if (!Permissions.PermissionsManager.CheckAndWarnPermission(causedBy, KingdomsModEntries.MOD_PREFIX + "farm"))
         {
             return(true);
         }
         var m = Regex.Match(chattext, @"/farm( (?<size>\d+))?");
         if (!m.Success)
         {
             Chat.Send(causedBy, "Command didn't match, use /farm [size]");
             return(true);
         }
         string strSize = m.Groups ["size"].Value;
         int    size;
         if (strSize.Length > 0)
         {
             if (!int.TryParse(strSize, out size))
             {
                 Chat.Send(causedBy, "Could not parse size");
                 return(true);
             }
             if (size < 1 || size > NpcFarmBuilder.MAX_SIZE)
             {
                 Chat.Send(causedBy, $"Size is out of range; min: 1; max: {NpcFarmBuilder.MAX_SIZE}");
                 return(true);
             }
         }
         else
         {
             size = 1 + Pipliz.Random.Next(NpcFarmBuilder.MAX_SIZE);
         }
         var farmPosition = ToFarmPosition(causedBy.Position);
         NpcFarm.Create(farmPosition, size).InitNew();
         Chat.Send(causedBy, $"You placed a farm at {farmPosition} with size {size}");
     } catch (Exception exception) {
         Log.WriteError($"Exception while parsing command; {exception.Message} - {exception.StackTrace}");
     }
     return(true);
 }