source: binary-improvements2/CommandExtensions/src/Commands/RemoveLandProtection.cs@ 405

Last change on this file since 405 was 405, checked in by alloc, 21 months ago

Refactored console commands for A21 caching

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