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

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

More cleanup, allocation improvements

File size: 5.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using AllocsFixes.PersistentData;
4
5namespace AllocsFixes.CustomCommands {
6 public class RemoveLandProtection : ConsoleCmdAbstract {
7 public override string GetDescription () {
8 return "removes the association of a land protection block to the owner";
9 }
10
11 public override string GetHelp () {
12 return "Usage:" +
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";
19 }
20
21 public override string[] GetCommands () {
22 return new[] {"removelandprotection", "rlp"};
23 }
24
25 private void removeById (string _id) {
26 try {
27 PersistentPlayerList ppl = GameManager.Instance.GetPersistentPlayerList ();
28
29 if (_id.Length < 1 || !ppl.Players.ContainsKey (_id)) {
30 SdtdConsole.Instance.Output (
31 "Not a valid Steam ID or user has never logged on. Use \"listlandprotection\" to get a list of keystones.");
32 return;
33 }
34
35 if (ppl.Players [_id].LPBlocks == null || ppl.Players [_id].LPBlocks.Count == 0) {
36 SdtdConsole.Instance.Output (
37 "Player does not own any keystones. Use \"listlandprotection\" to get a list of keystones.");
38 return;
39 }
40
41 List<BlockChangeInfo> changes = new List<BlockChangeInfo> ();
42 foreach (Vector3i pos in ppl.Players [_id].LPBlocks) {
43 BlockChangeInfo bci = new BlockChangeInfo (pos, new BlockValue (0), true, false);
44 changes.Add (bci);
45 }
46
47 GameManager.Instance.SetBlocksRPC (changes);
48
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);
54 } catch (Exception e) {
55 Log.Out ("Error in RemoveLandProtection.removeById: " + e);
56 }
57 }
58
59 private void removeByPosition (List<string> _coords) {
60 try {
61 int x, y, z;
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) {
67 SdtdConsole.Instance.Output ("At least one of the given coordinates is not a valid integer");
68 return;
69 }
70
71 Vector3i v = new Vector3i (x, y, z);
72
73 PersistentPlayerList ppl = GameManager.Instance.GetPersistentPlayerList ();
74
75 Dictionary<Vector3i, PersistentPlayerData> d = ppl.m_lpBlockMap;
76 if (d == null || !d.ContainsKey (v)) {
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.");
79 return;
80 }
81
82 BlockChangeInfo bci = new BlockChangeInfo (v, new BlockValue (0), true, false);
83
84 List<BlockChangeInfo> changes = new List<BlockChangeInfo> ();
85 changes.Add (bci);
86
87 GameManager.Instance.SetBlocksRPC (changes);
88
89 SdtdConsole.Instance.Output ("Land protection block at (" + v + ") removed");
90 } catch (Exception e) {
91 Log.Out ("Error in RemoveLandProtection.removeByPosition: " + e);
92 }
93 }
94
95 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
96 try {
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) {
107 if (!int.TryParse (_params [1], out closeToDistance)) {
108 SdtdConsole.Instance.Output ("Given length is not an integer!");
109 return;
110 }
111
112 closeToDistance /= 2;
113 }
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 =
119 {LandClaimList.CloseToFilter2dRect (closeTo, closeToDistance)};
120 Dictionary<Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (null, posFilters);
121
122 try {
123 List<BlockChangeInfo> changes = new List<BlockChangeInfo> ();
124 foreach (KeyValuePair<Player, List<Vector3i>> kvp in claims) {
125 foreach (Vector3i v in kvp.Value) {
126 BlockChangeInfo bci = new BlockChangeInfo (v, new BlockValue (0), true, false);
127 changes.Add (bci);
128 }
129 }
130
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) {
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}
Note: See TracBrowser for help on using the repository browser.