| 1 | using System;
 | 
|---|
| 2 | using System.Collections.Generic;
 | 
|---|
| 3 | 
 | 
|---|
| 4 | namespace AllocsFixes.CustomCommands
 | 
|---|
| 5 | {
 | 
|---|
| 6 |         public class RemoveLandProtection : ConsoleCmdAbstract
 | 
|---|
| 7 |         {
 | 
|---|
| 8 |                 public override string GetDescription ()
 | 
|---|
| 9 |                 {
 | 
|---|
| 10 |                         return "removes the association of a land protection block to the owner";
 | 
|---|
| 11 |                 }
 | 
|---|
| 12 | 
 | 
|---|
| 13 |                 public override string GetHelp () {
 | 
|---|
| 14 |                         return "Usage:" +
 | 
|---|
| 15 |                                    "  1. removelandprotection <steamid>\n" +
 | 
|---|
| 16 |                                    "  2. removelandprotection <x> <y> <z>\n" +
 | 
|---|
| 17 |                                    "  3. removelandprotection nearby [length]\n" +
 | 
|---|
| 18 |                                    "1. Remove all land claims owned by the user with the given SteamID\n" +
 | 
|---|
| 19 |                                    "2. Remove only the claim block on the exactly given block position\n" +
 | 
|---|
| 20 |                                    "3. Remove all claims in a square with edge length of 64 (or the optionally specified size) around the executing player";
 | 
|---|
| 21 |                 }
 | 
|---|
| 22 | 
 | 
|---|
| 23 |                 public override string[] GetCommands ()
 | 
|---|
| 24 |                 {
 | 
|---|
| 25 |                         return new string[] { "removelandprotection", "rlp" };
 | 
|---|
| 26 |                 }
 | 
|---|
| 27 | 
 | 
|---|
| 28 |                 private void removeById (string _id)
 | 
|---|
| 29 |                 {
 | 
|---|
| 30 |                         try {
 | 
|---|
| 31 |                                 PersistentPlayerList ppl = GameManager.Instance.GetPersistentPlayerList ();
 | 
|---|
| 32 | 
 | 
|---|
| 33 |                                 if (_id.Length < 1 || !ppl.Players.ContainsKey (_id)) {
 | 
|---|
| 34 |                                         SdtdConsole.Instance.Output ("Not a valid Steam ID or user has never logged on. Use \"listlandprotection\" to get a list of keystones.");
 | 
|---|
| 35 |                                         return;
 | 
|---|
| 36 |                                 }
 | 
|---|
| 37 |                                 if (ppl.Players [_id].LPBlocks == null || ppl.Players [_id].LPBlocks.Count == 0) {
 | 
|---|
| 38 |                                         SdtdConsole.Instance.Output ("Player does not own any keystones. Use \"listlandprotection\" to get a list of keystones.");
 | 
|---|
| 39 |                                         return;
 | 
|---|
| 40 |                                 }
 | 
|---|
| 41 | 
 | 
|---|
| 42 |                                 List<BlockChangeInfo> changes = new List<BlockChangeInfo> ();
 | 
|---|
| 43 |                                 foreach (Vector3i pos in ppl.Players[_id].LPBlocks) {
 | 
|---|
| 44 |                                         BlockChangeInfo bci = new BlockChangeInfo (pos, new BlockValue (0), true, false);
 | 
|---|
| 45 |                                         changes.Add (bci);
 | 
|---|
| 46 |                                 }
 | 
|---|
| 47 |                                 GameManager.Instance.SetBlocksRPC (changes);
 | 
|---|
| 48 | 
 | 
|---|
| 49 |                                 SdtdConsole.Instance.Output ("Tried to remove #" + changes.Count + " land protection blocks for player \"" + _id + "\". Note "+
 | 
|---|
| 50 |                                                       "that only blocks in chunks that are currently loaded (close to any player) could be removed. "+
 | 
|---|
| 51 |                                                       "Please check for remaining blocks by running:");
 | 
|---|
| 52 |                                 SdtdConsole.Instance.Output("  listlandprotection " + _id);
 | 
|---|
| 53 |                         } catch (Exception e) {
 | 
|---|
| 54 |                                 Log.Out ("Error in RemoveLandProtection.removeById: " + e);
 | 
|---|
| 55 |                         }
 | 
|---|
| 56 |                 }
 | 
|---|
| 57 | 
 | 
|---|
| 58 |                 private void removeByPosition (List<string> _coords)
 | 
|---|
| 59 |                 {
 | 
|---|
| 60 |                         try {
 | 
|---|
| 61 |                                 int x = int.MinValue;
 | 
|---|
| 62 |                                 int.TryParse (_coords [0], out x);
 | 
|---|
| 63 |                                 int y = int.MinValue;
 | 
|---|
| 64 |                                 int.TryParse (_coords [1], out y);
 | 
|---|
| 65 |                                 int z = int.MinValue;
 | 
|---|
| 66 |                                 int.TryParse (_coords [2], out z);
 | 
|---|
| 67 | 
 | 
|---|
| 68 |                                 if (x == int.MinValue || y == int.MinValue || z == int.MinValue) {
 | 
|---|
| 69 |                                         SdtdConsole.Instance.Output ("At least one of the given coordinates is not a valid integer");
 | 
|---|
| 70 |                                         return;
 | 
|---|
| 71 |                                 }
 | 
|---|
| 72 | 
 | 
|---|
| 73 |                                 Vector3i v = new Vector3i (x, y, z);
 | 
|---|
| 74 | 
 | 
|---|
| 75 |                                 PersistentPlayerList ppl = GameManager.Instance.GetPersistentPlayerList ();
 | 
|---|
| 76 | 
 | 
|---|
| 77 |                                 Dictionary<Vector3i, PersistentPlayerData> d = ppl.m_lpBlockMap;
 | 
|---|
| 78 |                                 if (d == null || !d.ContainsKey (v)) {
 | 
|---|
| 79 |                                         SdtdConsole.Instance.Output ("No land protection block at the given position or not a valid position. Use \"listlandprotection\" to get a list of keystones.");
 | 
|---|
| 80 |                                         return;
 | 
|---|
| 81 |                                 }
 | 
|---|
| 82 | 
 | 
|---|
| 83 |                                 BlockChangeInfo bci = new BlockChangeInfo (v, new BlockValue (0), true, false);
 | 
|---|
| 84 | 
 | 
|---|
| 85 |                                 List<BlockChangeInfo> changes = new List<BlockChangeInfo> ();
 | 
|---|
| 86 |                                 changes.Add (bci);
 | 
|---|
| 87 | 
 | 
|---|
| 88 |                                 GameManager.Instance.SetBlocksRPC (changes);
 | 
|---|
| 89 | 
 | 
|---|
| 90 |                                 SdtdConsole.Instance.Output ("Land protection block at (" + v.ToString () + ") removed");
 | 
|---|
| 91 |                         } catch (Exception e) {
 | 
|---|
| 92 |                                 Log.Out ("Error in RemoveLandProtection.removeByPosition: " + e);
 | 
|---|
| 93 |                         }
 | 
|---|
| 94 |                 }
 | 
|---|
| 95 | 
 | 
|---|
| 96 |                 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo)
 | 
|---|
| 97 |                 {
 | 
|---|
| 98 |                         try {
 | 
|---|
| 99 |                                 if (_senderInfo.RemoteClientInfo != null) {
 | 
|---|
| 100 |                                         if (_params.Count >= 1 && _params [0].EqualsCaseInsensitive ("nearby")) {
 | 
|---|
| 101 |                                                 _params.Add (_senderInfo.RemoteClientInfo.playerId);
 | 
|---|
| 102 |                                         }
 | 
|---|
| 103 |                                 }
 | 
|---|
| 104 | 
 | 
|---|
| 105 |                                 if (_params.Count > 0 && _params [0].EqualsCaseInsensitive ("nearby")) {
 | 
|---|
| 106 |                                         try {
 | 
|---|
| 107 |                                                 int closeToDistance = 32;
 | 
|---|
| 108 |                                                 if (_params.Count == 3) {
 | 
|---|
| 109 |                                                         if (!int.TryParse (_params[1], out closeToDistance)) {
 | 
|---|
| 110 |                                                                 SdtdConsole.Instance.Output ("Given length is not an integer!");
 | 
|---|
| 111 |                                                                 return;
 | 
|---|
| 112 |                                                         }
 | 
|---|
| 113 |                                                         closeToDistance /= 2;
 | 
|---|
| 114 |                                                 }
 | 
|---|
| 115 |                                                 ClientInfo ci = ConsoleHelper.ParseParamSteamIdOnline (_params [_params.Count - 1]);
 | 
|---|
| 116 |                                                 EntityPlayer ep = GameManager.Instance.World.Players.dict [ci.entityId];
 | 
|---|
| 117 |                                                 Vector3i closeTo = new Vector3i (ep.GetPosition ());
 | 
|---|
| 118 |                                                 LandClaimList.PositionFilter[] posFilters = new LandClaimList.PositionFilter[] { LandClaimList.CloseToFilter2dRect (closeTo, closeToDistance) };
 | 
|---|
| 119 |                                                 Dictionary<PersistentData.Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (null, posFilters);
 | 
|---|
| 120 | 
 | 
|---|
| 121 |                                                 try {
 | 
|---|
| 122 |                                                         List<BlockChangeInfo> changes = new List<BlockChangeInfo> ();
 | 
|---|
| 123 |                                                         foreach (KeyValuePair<PersistentData.Player, List<Vector3i>> kvp in claims) {
 | 
|---|
| 124 |                                                                 foreach (Vector3i v in kvp.Value) {
 | 
|---|
| 125 |                                                                         BlockChangeInfo bci = new BlockChangeInfo (v, new BlockValue (0), true, false);
 | 
|---|
| 126 |                                                                         changes.Add (bci);
 | 
|---|
| 127 |                                                                 }
 | 
|---|
| 128 |                                                         }
 | 
|---|
| 129 |                                                         GameManager.Instance.SetBlocksRPC (changes);
 | 
|---|
| 130 |                                                 } catch (Exception e) {
 | 
|---|
| 131 |                                                         SdtdConsole.Instance.Output ("Error removing claims");
 | 
|---|
| 132 |                                                         Log.Out ("Error in RemoveLandProtection.Run: " + e);
 | 
|---|
| 133 |                                                         return;
 | 
|---|
| 134 |                                                 }
 | 
|---|
| 135 |                                         } catch (Exception e) {
 | 
|---|
| 136 |                                                 SdtdConsole.Instance.Output ("Error getting current player's position");
 | 
|---|
| 137 |                                                 Log.Out ("Error in RemoveLandProtection.Run: " + e);
 | 
|---|
| 138 |                                                 return;
 | 
|---|
| 139 |                                         }
 | 
|---|
| 140 |                                 } else if (_params.Count == 1) {
 | 
|---|
| 141 |                                         removeById (_params [0]);
 | 
|---|
| 142 |                                 } else if (_params.Count == 3) {
 | 
|---|
| 143 |                                         removeByPosition (_params);
 | 
|---|
| 144 |                                 } else {
 | 
|---|
| 145 |                                         SdtdConsole.Instance.Output ("Illegal parameters");
 | 
|---|
| 146 |                                 }
 | 
|---|
| 147 |                         } catch (Exception e) {
 | 
|---|
| 148 |                                 Log.Out ("Error in RemoveLandProtection.Run: " + e);
 | 
|---|
| 149 |                         }
 | 
|---|
| 150 |                 }
 | 
|---|
| 151 |         }
 | 
|---|
| 152 | }
 | 
|---|