Add support for specifying channels in consoles

Although it doesn't work unless it receives an object signal
This commit is contained in:
Norbi Peti 2020-07-10 01:37:35 +02:00
parent 20cd8b7a50
commit 62a0c2d242

View file

@ -20,6 +20,7 @@ namespace BlockMod
{ {
private Block[] blocks = new Block[0]; private Block[] blocks = new Block[0];
private Block refBlock; private Block refBlock;
public void OnApplicationStart() public void OnApplicationStart()
{ {
Main.Init(); Main.Init();
@ -63,6 +64,7 @@ namespace BlockMod
RegisterBlockCommand("colorBlocks", "Colors the blocks you're looking at", RegisterBlockCommand("colorBlocks", "Colors the blocks you're looking at",
(color, darkness, blocks, refBlock) => (color, darkness, blocks, refBlock) =>
{ {
if (!Enum.TryParse(color, true, out BlockColors clr)) if (!Enum.TryParse(color, true, out BlockColors clr))
{ {
Logging.CommandLogWarning("Color " + color + " not found"); Logging.CommandLogWarning("Color " + color + " not found");
@ -72,7 +74,8 @@ namespace BlockMod
block.Color = new BlockColor {Color = clr, Darkness = darkness}; block.Color = new BlockColor {Color = clr, Darkness = darkness};
}); });
CommandBuilder.Builder("selectBlocksLookedAt", "Selects blocks (1 or more) to change. Only works in build mode, however the blocks can be changed afterwards." + CommandBuilder.Builder("selectBlocksLookedAt",
"Selects blocks (1 or more) to change. Only works in build mode, however the blocks can be changed afterwards." +
" Paramter: whether one (true) or all connected (false) blocks should be selected.") " Paramter: whether one (true) or all connected (false) blocks should be selected.")
.Action<bool>(single => .Action<bool>(single =>
{ {
@ -91,10 +94,7 @@ namespace BlockMod
refBlock = blocks.Length > 0 ? blocks[0] : null; refBlock = blocks.Length > 0 ? blocks[0] : null;
}).Build(); }).Build();
ConsoleCommands.RegisterWithChannel("selectSendSignal", ch => ConsoleCommands.RegisterWithChannel("selectSendSignal", ch => { }, ChannelType.Object,
{
}, ChannelType.Object,
"Sends a signal for selecting a given object ID for a command block."); "Sends a signal for selecting a given object ID for a command block.");
RegisterBlockCommand("pushBlocks", "Adds velocity to the selected blocks. Only works in simulation.", RegisterBlockCommand("pushBlocks", "Adds velocity to the selected blocks. Only works in simulation.",
@ -143,7 +143,8 @@ namespace BlockMod
$"- Rotation: {rot.x:F}° {rot.y:F}° {rot.z:F}°\n" + $"- Rotation: {rot.x:F}° {rot.y:F}° {rot.z:F}°\n" +
$"- Color: {block.Color.Color} darkness: {block.Color.Darkness}\n" + $"- Color: {block.Color.Color} darkness: {block.Color.Darkness}\n" +
$"- Scale: {scale.x:F} {scale.y:F} {scale.z:F}\n" + $"- Scale: {scale.x:F} {scale.y:F} {scale.z:F}\n" +
$"- Label: {block.Label}"; $"- Label: {block.Label}\n" +
$"- ID: {block.Id}";
} }
private static string GetBodyInfoInSimMode() private static string GetBodyInfoInSimMode()
@ -182,35 +183,94 @@ namespace BlockMod
return blocks; return blocks;
} }
private void RegisterBlockCommand(string name, string desc, Action<string, byte, Block[], Block> action) private Block[] SelectBlocks(char id)
{ {
RuntimeCommands.Register<string, byte>(name, (a1, a2) => var blocks = ObjectIdentifier.GetByID(id).SelectMany(oid => oid.GetConnectedCubes())
.ToArray();
return blocks;
}
private void RegisterBlockCommandInternal(string name, string desc, Action<string, Block[], Block> action)
{ {
if (CheckNoBlocks(blocks)) return; RuntimeCommands.Register<string>(name, a1 =>
action(a1, a2, blocks, refBlock); {
action(a1, blocks, refBlock);
}, desc); }, desc);
ConsoleCommands.RegisterWithChannel<string, byte>(name, (a1, a2, ch) => ConsoleCommands.RegisterWithChannel<string>(name, (a1, ch) =>
{ {
Console.WriteLine($"Command {name} with args {a1} and channel {ch} executing");
var blks = SelectBlocks(ch); var blks = SelectBlocks(ch);
if (CheckNoBlocks(blks)) return; action(a1, blks, blks[0]);
action(a1, a2, blks, blks[0]);
}, ChannelType.Object, desc); }, ChannelType.Object, desc);
} }
private void RegisterBlockCommand(string name, string desc, Action<float, float, float, Block[], Block> action) private void RegisterBlockCommand(string name, string desc, Action<float, float, float, Block[], Block> action)
{ {
RuntimeCommands.Register<float, float, float>(name, (x, y, z) => RegisterBlockCommandInternal(name, desc, (args, bs, b) =>
{ {
var argsa = args.Split(' ');
if (argsa.Length < 3)
{
Log.Error("Too few arguments. Needed arguments are: <x> <y> <z> 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 = SelectBlocks(argsa[3][0]);
if (CheckNoBlocks(blocks)) return; if (CheckNoBlocks(blocks)) return;
action(x, y, z, blocks, refBlock); action(x, y, z, blocks, blocks[0]);
}, }
desc); else if (!CheckNoBlocks(bs))
ConsoleCommands.RegisterWithChannel<float, float, float>(name, (x, y, z, ch) => action(x, y, z, bs, b);
});
}
private void RegisterBlockCommand(string name, string desc, Action<string, byte, Block[], Block> action)
{ {
var blks = SelectBlocks(ch); RegisterBlockCommandInternal(name, desc, (args, bs, b) =>
if (CheckNoBlocks(blks)) return; {
action(x, y, z, blks, blks[0]); var argsa = args.Split(' ');
}, ChannelType.Object, desc); if (argsa.Length < 2)
{
Log.Error("Too few arguments. Needed arguments are: <color> <darkness> 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 = SelectBlocks(argsa[2][0]);
if (CheckNoBlocks(blocks)) return;
action(argsa[0], darkness, blocks, blocks[0]);
}
else if(!CheckNoBlocks(bs))
action(argsa[0], darkness, bs, b);
});
} }
public void OnApplicationQuit() public void OnApplicationQuit()