source: binary-improvements/AllocsCommands/Commands/RemoveLandProtection.cs@ 356

Last change on this file since 356 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

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