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

Last change on this file since 361 was 359, checked in by alloc, 5 years ago

Removed unnecessary try-catch-blocks from commands (command handler catches exceptions anyway and provides more detailed output)

File size: 5.5 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 int x, y, z;
61 int.TryParse (_coords [0], out x);
62 int.TryParse (_coords [1], out y);
63 int.TryParse (_coords [2], out 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> ();
84 changes.Add (bci);
85
86 GameManager.Instance.SetBlocksRPC (changes);
87
88 SdtdConsole.Instance.Output ("Land protection block at (" + v + ") removed");
89 }
90
91 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
92 try {
93 if (_senderInfo.RemoteClientInfo != null) {
94 if (_params.Count >= 1 && _params [0].EqualsCaseInsensitive ("nearby")) {
95 _params.Add (_senderInfo.RemoteClientInfo.playerId);
96 }
97 }
98
99 if (_params.Count > 0 && _params [0].EqualsCaseInsensitive ("nearby")) {
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 ClientInfo ci = ConsoleHelper.ParseParamSteamIdOnline (_params [_params.Count - 1]);
112 EntityPlayer ep = GameManager.Instance.World.Players.dict [ci.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 } catch (Exception e) {
144 Log.Out ("Error in RemoveLandProtection.Run: " + e);
145 }
146 }
147 }
148}
Note: See TracBrowser for help on using the repository browser.