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

Last change on this file since 400 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 5.5 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 public override string GetDescription () {
11 return "removes the association of a land protection block to the owner";
12 }
13
14 public 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 public 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 ("Tried to remove #" + changes.Count +
53 " land protection blocks for player \"" + _id + "\". Note " +
54 "that only blocks in chunks that are currently loaded (close to any player) could be removed. " +
55 "Please check for remaining blocks by running:");
56 SdtdConsole.Instance.Output (" listlandprotection " + _id);
57 } catch (Exception e) {
58 Log.Out ("Error in RemoveLandProtection.removeById: " + e);
59 }
60 }
61
62 private void removeByPosition (List<string> _coords) {
63 int.TryParse (_coords [0], out int x);
64 int.TryParse (_coords [1], out int y);
65 int.TryParse (_coords [2], out int z);
66
67 if (x == int.MinValue || y == int.MinValue || z == int.MinValue) {
68 SdtdConsole.Instance.Output ("At least one of the given coordinates is not a valid integer");
69 return;
70 }
71
72 Vector3i v = new Vector3i (x, y, z);
73
74 PersistentPlayerList ppl = GameManager.Instance.GetPersistentPlayerList ();
75
76 Dictionary<Vector3i, PersistentPlayerData> d = ppl.m_lpBlockMap;
77 if (d == null || !d.ContainsKey (v)) {
78 SdtdConsole.Instance.Output (
79 "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> {bci};
86
87 GameManager.Instance.SetBlocksRPC (changes);
88
89 SdtdConsole.Instance.Output ("Land protection block at (" + v + ") removed");
90 }
91
92 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
93 if (_params.Count > 0 && _params [0].EqualsCaseInsensitive ("nearby")) {
94 if (_senderInfo.RemoteClientInfo != null) {
95 _params.Add (_senderInfo.RemoteClientInfo.entityId.ToString ());
96 } else if (_senderInfo.IsLocalGame && !GameManager.IsDedicatedServer) {
97 _params.Add (GameManager.Instance.World.GetPrimaryPlayerId ().ToString ());
98 }
99
100 try {
101 int closeToDistance = 32;
102 if (_params.Count == 3) {
103 if (!int.TryParse (_params [1], out closeToDistance)) {
104 SdtdConsole.Instance.Output ("Given length is not an integer!");
105 return;
106 }
107
108 closeToDistance /= 2;
109 }
110
111 int entityId = int.Parse (_params [_params.Count - 1]);
112 EntityPlayer ep = GameManager.Instance.World.Players.dict [entityId];
113 Vector3i closeTo = new Vector3i (ep.GetPosition ());
114 LandClaimList.PositionFilter[] posFilters =
115 {LandClaimList.CloseToFilter2dRect (closeTo, closeToDistance)};
116 Dictionary<Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (null, posFilters);
117
118 try {
119 List<BlockChangeInfo> changes = new List<BlockChangeInfo> ();
120 foreach (KeyValuePair<Player, List<Vector3i>> kvp in claims) {
121 foreach (Vector3i v in kvp.Value) {
122 BlockChangeInfo bci = new BlockChangeInfo (v, new BlockValue (0), true, false);
123 changes.Add (bci);
124 }
125 }
126
127 GameManager.Instance.SetBlocksRPC (changes);
128 } catch (Exception e) {
129 SdtdConsole.Instance.Output ("Error removing claims");
130 Log.Out ("Error in RemoveLandProtection.Run: " + e);
131 }
132 } catch (Exception e) {
133 SdtdConsole.Instance.Output ("Error getting current player's position");
134 Log.Out ("Error in RemoveLandProtection.Run: " + e);
135 }
136 } else if (_params.Count == 1) {
137 removeById (_params [0]);
138 } else if (_params.Count == 3) {
139 removeByPosition (_params);
140 } else {
141 SdtdConsole.Instance.Output ("Illegal parameters");
142 }
143 }
144 }
145}
Note: See TracBrowser for help on using the repository browser.