using System; using System.Linq; using Gamecraft.Wires; using GamecraftModdingAPI; using GamecraftModdingAPI.Blocks; using GamecraftModdingAPI.Utility; using RobocraftX.CommandLine.Custom; using uREPL; namespace BlockMod { public class CommandUtils { private BlockSelections _blockSelections; public CommandUtils(BlockSelections blockSelections) { _blockSelections = blockSelections; } private void RegisterBlockCommandInternal(string name, string desc, Action action) { RuntimeCommands.Register(name, a1 => { action(a1, _blockSelections.blocks, _blockSelections.refBlock); }, desc); ConsoleCommands.RegisterWithChannel(name, (a1, ch) => { Console.WriteLine($"Command {name} with args {a1} and channel {ch} executing"); var blks = _blockSelections.SelectBlocks(ch); action(a1, blks, blks[0]); }, ChannelType.Object, desc); } public void RegisterBlockCommand(string name, string desc, Action action) { RegisterBlockCommandInternal(name, desc, (args, bs, b) => { var argsa = args.Split(' '); if (argsa.Length < 3) { Log.Error("Too few arguments. Needed arguments are: and [id] is optional."); return; } if (!float.TryParse(argsa[0], out float x) || !float.TryParse(argsa[1], out float y) || !float.TryParse(argsa[2], out float z)) { Log.Error("Could not parse arguments as floats."); return; } if (argsa.Length > 3) { if (argsa[3].Length == 0) { Log.Error("Missing channel."); return; } var blocks = _blockSelections.SelectBlocks(argsa[3][0]); if (_blockSelections.CheckNoBlocks(blocks)) return; action(x, y, z, blocks, blocks[0]); } else if (!_blockSelections.CheckNoBlocks(bs)) action(x, y, z, bs, b); }); } public void RegisterBlockCommand(string name, string desc, Action action) { RegisterBlockCommandInternal(name, desc, (args, bs, b) => { var argsa = args.Split(' '); if (argsa.Length < 2) { Log.Error("Too few arguments. Needed arguments are: and [id] is optional."); return; } if (!byte.TryParse(argsa[1], out byte darkness)) { Log.Error("Could not parse color darkness."); return; } if (argsa.Length > 2) { if (argsa[2].Length == 0) { Log.Error("Missing channel."); return; } var blocks = _blockSelections.SelectBlocks(argsa[2][0]); if (_blockSelections.CheckNoBlocks(blocks)) return; action(argsa[0], darkness, blocks, blocks[0]); } else if(!_blockSelections.CheckNoBlocks(bs)) action(argsa[0], darkness, bs, b); }); } } }