|
| 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.Entities.Cube; |
| 8 | +using Sandbox.ModAPI; |
| 9 | +using Torch.Commands; |
| 10 | + |
| 11 | +namespace Essentials.Commands |
| 12 | +{ |
| 13 | + [Category("blocks")] |
| 14 | + public class BlocksModule : CommandModule |
| 15 | + { |
| 16 | + [Command("on type", "Turn on all blocks of the given type.")] |
| 17 | + public void OnType(string type) |
| 18 | + { |
| 19 | + var count = 0; |
| 20 | + foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>()) |
| 21 | + { |
| 22 | + foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>()) |
| 23 | + { |
| 24 | + var blockType = block.BlockDefinition.Id.TypeId.ToString().Substring(16); |
| 25 | + if (block != null && string.Compare(type, blockType, StringComparison.InvariantCultureIgnoreCase) == 0) |
| 26 | + { |
| 27 | + block.Enabled = true; |
| 28 | + count++; |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + Context.Respond($"Enabled {count} blocks of type {type}."); |
| 34 | + } |
| 35 | + |
| 36 | + [Command("on subtype", "Turn on all blocks of the given subtype.")] |
| 37 | + public void OnSubtype(string subtype) |
| 38 | + { |
| 39 | + var count = 0; |
| 40 | + foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>()) |
| 41 | + { |
| 42 | + foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>()) |
| 43 | + { |
| 44 | + var blockType = block.BlockDefinition.Id.SubtypeName; |
| 45 | + if (block != null && string.Compare(subtype, blockType, StringComparison.InvariantCultureIgnoreCase) == 0) |
| 46 | + { |
| 47 | + block.Enabled = true; |
| 48 | + count++; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Context.Respond($"Enabled {count} blocks of subtype {subtype}."); |
| 54 | + } |
| 55 | + |
| 56 | + [Command("off type", "Turn off all blocks of the given type.")] |
| 57 | + public void OffType(string type) |
| 58 | + { |
| 59 | + var count = 0; |
| 60 | + foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>()) |
| 61 | + { |
| 62 | + foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>()) |
| 63 | + { |
| 64 | + var blockType = block.BlockDefinition.Id.TypeId.ToString().Substring(16); |
| 65 | + if (block != null && string.Compare(type, blockType, StringComparison.InvariantCultureIgnoreCase) == 0) |
| 66 | + { |
| 67 | + block.Enabled = false; |
| 68 | + count++; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + Context.Respond($"Disabled {count} blocks of type {type}."); |
| 75 | + } |
| 76 | + |
| 77 | + [Command("off subtype", "Turn off all blocks of the given subtype.")] |
| 78 | + public void OffSubtype(string subtype) |
| 79 | + { |
| 80 | + var count = 0; |
| 81 | + foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>()) |
| 82 | + { |
| 83 | + foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>()) |
| 84 | + { |
| 85 | + var blockType = block.BlockDefinition.Id.SubtypeName; |
| 86 | + if (block != null && string.Compare(subtype, blockType, StringComparison.InvariantCultureIgnoreCase) == 0) |
| 87 | + { |
| 88 | + block.Enabled = false; |
| 89 | + count++; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + Context.Respond($"Disabled {count} blocks of subtype {subtype}."); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments