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
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) {
[359]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);
[224]64
[359]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 }
[224]69
[359]70 Vector3i v = new Vector3i (x, y, z);
[224]71
[359]72 PersistentPlayerList ppl = GameManager.Instance.GetPersistentPlayerList ();
[224]73
[359]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 }
[224]80
[359]81 BlockChangeInfo bci = new BlockChangeInfo (v, new BlockValue (0), true, false);
[224]82
[359]83 List<BlockChangeInfo> changes = new List<BlockChangeInfo> ();
84 changes.Add (bci);
[224]85
[359]86 GameManager.Instance.SetBlocksRPC (changes);
[224]87
[359]88 SdtdConsole.Instance.Output ("Land protection block at (" + v + ") removed");
[224]89 }
90
[325]91 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
[224]92 try {
[306]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) {
[325]103 if (!int.TryParse (_params [1], out closeToDistance)) {
[306]104 SdtdConsole.Instance.Output ("Given length is not an integer!");
105 return;
106 }
[325]107
[306]108 closeToDistance /= 2;
109 }
[325]110
[306]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 ());
[325]114 LandClaimList.PositionFilter[] posFilters =
115 {LandClaimList.CloseToFilter2dRect (closeTo, closeToDistance)};
116 Dictionary<Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (null, posFilters);
[306]117
118 try {
119 List<BlockChangeInfo> changes = new List<BlockChangeInfo> ();
[325]120 foreach (KeyValuePair<Player, List<Vector3i>> kvp in claims) {
[306]121 foreach (Vector3i v in kvp.Value) {
122 BlockChangeInfo bci = new BlockChangeInfo (v, new BlockValue (0), true, false);
123 changes.Add (bci);
124 }
125 }
[325]126
[306]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) {
[224]137 removeById (_params [0]);
[230]138 } else if (_params.Count == 3) {
[224]139 removeByPosition (_params);
140 } else {
[306]141 SdtdConsole.Instance.Output ("Illegal parameters");
[224]142 }
143 } catch (Exception e) {
144 Log.Out ("Error in RemoveLandProtection.Run: " + e);
145 }
146 }
147 }
[325]148}
Note: See TracBrowser for help on using the repository browser.