|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Sandbox.Game.Entities; |
| 7 | +using Sandbox.Game.World; |
| 8 | +using Torch.Commands; |
| 9 | + |
| 10 | +namespace Essentials.Commands |
| 11 | +{ |
| 12 | + [Category("cleanup")] |
| 13 | + public class CleanupModule : CommandModule |
| 14 | + { |
| 15 | + [Command("scan", "Find grids matching the given conditions: hastype, notype, hassubtype, nosubtype, blockslessthan, blocksgreaterthan, ownedby")] |
| 16 | + public void Scan() |
| 17 | + { |
| 18 | + var count = ScanConditions(Context.Args).Count(); |
| 19 | + Context.Respond($"Found {count} grids matching the given conditions."); |
| 20 | + } |
| 21 | + |
| 22 | + [Command("delete", "Delete grids matching the given conditions")] |
| 23 | + public void Delete() |
| 24 | + { |
| 25 | + var count = 0; |
| 26 | + foreach (var grid in ScanConditions(Context.Args)) |
| 27 | + { |
| 28 | + grid.Close(); |
| 29 | + count++; |
| 30 | + } |
| 31 | + |
| 32 | + Context.Respond($"Deleted {count} grids matching the given conditions."); |
| 33 | + } |
| 34 | + |
| 35 | + private IEnumerable<MyCubeGrid> ScanConditions(List<string> args) |
| 36 | + { |
| 37 | + var conditions = new List<Func<MyCubeGrid, bool>>(); |
| 38 | + |
| 39 | + for (var i = 0; i < args.Count; i += 2) |
| 40 | + { |
| 41 | + var arg = args[i]; |
| 42 | + var parameter = args[i + 1]; |
| 43 | + |
| 44 | + switch (arg) |
| 45 | + { |
| 46 | + case "hastype": |
| 47 | + conditions.Add(g => g.HasBlockType(parameter)); |
| 48 | + break; |
| 49 | + case "notype": |
| 50 | + conditions.Add(g => !g.HasBlockType(parameter)); |
| 51 | + break; |
| 52 | + case "hassubtype": |
| 53 | + conditions.Add(g => g.HasBlockSubtype(parameter)); |
| 54 | + break; |
| 55 | + case "nosubtype": |
| 56 | + conditions.Add(g => !g.HasBlockSubtype(parameter)); |
| 57 | + break; |
| 58 | + case "blockslessthan": |
| 59 | + conditions.Add(g => BlocksLessThan(g, parameter)); |
| 60 | + break; |
| 61 | + case "blocksgreaterthan": |
| 62 | + conditions.Add(g => BlocksGreaterThan(g, parameter)); |
| 63 | + break; |
| 64 | + case "ownedby": |
| 65 | + conditions.Add(g => OwnedBy(g, parameter)); |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>()) |
| 71 | + { |
| 72 | + if (conditions.TrueForAll(func => func(grid))) |
| 73 | + yield return grid; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private bool BlocksLessThan(MyCubeGrid grid, string str) |
| 78 | + { |
| 79 | + if (int.TryParse(str, out int count)) |
| 80 | + return grid.BlocksCount < count; |
| 81 | + |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + private bool BlocksGreaterThan(MyCubeGrid grid, string str) |
| 86 | + { |
| 87 | + if (int.TryParse(str, out int count)) |
| 88 | + return grid.BlocksCount > count; |
| 89 | + |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + private bool OwnedBy(MyCubeGrid grid, string str) |
| 94 | + { |
| 95 | + long identityId; |
| 96 | + |
| 97 | + if (string.Compare(str, "nobody", StringComparison.InvariantCultureIgnoreCase) == 0) |
| 98 | + { |
| 99 | + return grid.BigOwners.Count == 0; |
| 100 | + } |
| 101 | + |
| 102 | + if (string.Compare(str, "pirates", StringComparison.InvariantCultureIgnoreCase) == 0) |
| 103 | + { |
| 104 | + identityId = MyPirateAntennas.GetPiratesId(); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + var player = Utilities.GetPlayerByNameOrId(str); |
| 109 | + if (player == null) |
| 110 | + return false; |
| 111 | + |
| 112 | + identityId = player.IdentityId; |
| 113 | + } |
| 114 | + |
| 115 | + return grid.BigOwners.Contains(identityId); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments