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

Last change on this file since 477 was 455, checked in by alloc, 16 months ago

25_30_44

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