Tools for building games mainly focused on changing block properties. And noclip.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandUtils.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Linq;
  3. using Gamecraft.Wires;
  4. using GamecraftModdingAPI;
  5. using GamecraftModdingAPI.Blocks;
  6. using GamecraftModdingAPI.Utility;
  7. using RobocraftX.CommandLine.Custom;
  8. using uREPL;
  9. namespace BlockMod
  10. {
  11. public class CommandUtils
  12. {
  13. private BlockSelections _blockSelections;
  14. public CommandUtils(BlockSelections blockSelections)
  15. {
  16. _blockSelections = blockSelections;
  17. }
  18. private void RegisterBlockCommandInternal(string name, string desc, Action<string, Block[], Block> action)
  19. {
  20. RuntimeCommands.Register<string>(name, a1 =>
  21. {
  22. action(a1, _blockSelections.blocks, _blockSelections.refBlock);
  23. }, desc);
  24. ConsoleCommands.RegisterWithChannel<string>(name, (a1, ch) =>
  25. {
  26. Console.WriteLine($"Command {name} with args {a1} and channel {ch} executing");
  27. var blks = _blockSelections.SelectBlocks(ch);
  28. action(a1, blks, blks[0]);
  29. }, ChannelType.Object, desc);
  30. }
  31. public void RegisterBlockCommand(string name, string desc, Action<float, float, float, Block[], Block> action)
  32. {
  33. RegisterBlockCommandInternal(name, desc, (args, bs, b) =>
  34. {
  35. var argsa = args.Split(' ');
  36. if (argsa.Length < 3)
  37. {
  38. Log.Error("Too few arguments. Needed arguments are: <x> <y> <z> and [id] is optional.");
  39. return;
  40. }
  41. if (!float.TryParse(argsa[0], out float x) || !float.TryParse(argsa[1], out float y) ||
  42. !float.TryParse(argsa[2], out float z))
  43. {
  44. Log.Error("Could not parse arguments as floats.");
  45. return;
  46. }
  47. if (argsa.Length > 3)
  48. {
  49. if (argsa[3].Length == 0)
  50. {
  51. Log.Error("Missing channel.");
  52. return;
  53. }
  54. var blocks = _blockSelections.SelectBlocks(argsa[3][0]);
  55. if (_blockSelections.CheckNoBlocks(blocks)) return;
  56. action(x, y, z, blocks, blocks[0]);
  57. }
  58. else if (!_blockSelections.CheckNoBlocks(bs))
  59. action(x, y, z, bs, b);
  60. });
  61. }
  62. public void RegisterBlockCommand(string name, string desc, Action<string, byte, Block[], Block> action)
  63. {
  64. RegisterBlockCommandInternal(name, desc, (args, bs, b) =>
  65. {
  66. var argsa = args.Split(' ');
  67. if (argsa.Length < 2)
  68. {
  69. Log.Error("Too few arguments. Needed arguments are: <color> <darkness> and [id] is optional.");
  70. return;
  71. }
  72. if (!byte.TryParse(argsa[1], out byte darkness))
  73. {
  74. Log.Error("Could not parse color darkness.");
  75. return;
  76. }
  77. if (argsa.Length > 2)
  78. {
  79. if (argsa[2].Length == 0)
  80. {
  81. Log.Error("Missing channel.");
  82. return;
  83. }
  84. var blocks = _blockSelections.SelectBlocks(argsa[2][0]);
  85. if (_blockSelections.CheckNoBlocks(blocks)) return;
  86. action(argsa[0], darkness, blocks, blocks[0]);
  87. }
  88. else if(!_blockSelections.CheckNoBlocks(bs))
  89. action(argsa[0], darkness, bs, b);
  90. });
  91. }
  92. }
  93. }