Minecraft-Console-Client/MinecraftClient/Commands/BlockInfo.cs

97 lines
4.6 KiB
C#
Raw Normal View History

using System.Text;
2022-10-26 08:54:54 +08:00
using Brigadier.NET;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
using MinecraftClient.Mapping;
using static MinecraftClient.CommandHandler.CmdResult;
namespace MinecraftClient.Commands
{
public class BlockInfo : Command
{
public override string CmdName { get { return "blockinfo"; } }
public override string CmdUsage { get { return "blockinfo <x> <y> <z> [-s]"; } }
public override string CmdDesc { get { return Translations.cmd_blockinfo_desc; } }
2022-12-11 16:30:45 +08:00
public override void RegisterCommand(CommandDispatcher<CmdResult> dispatcher)
2022-10-26 08:54:54 +08:00
{
dispatcher.Register(l => l.Literal("help")
.Then(l => l.Literal(CmdName)
.Executes(r => GetUsage(r.Source, string.Empty))
.Then(l => l.Literal("-s")
.Executes(r => GetUsage(r.Source, "-s")))
)
);
dispatcher.Register(l => l.Literal(CmdName)
2022-12-11 16:30:45 +08:00
.Executes(r => LogBlockInfo(r.Source, null, false))
.Then(l => l.Literal("-s")
2022-12-11 16:30:45 +08:00
.Executes(r => LogBlockInfo(r.Source, null, true)))
.Then(l => l.Argument("Location", MccArguments.Location())
2022-12-11 16:30:45 +08:00
.Executes(r => LogBlockInfo(r.Source, MccArguments.GetLocation(r, "Location"), false))
.Then(l => l.Literal("-s")
2022-12-11 16:30:45 +08:00
.Executes(r => LogBlockInfo(r.Source, MccArguments.GetLocation(r, "Location"), true))))
.Then(l => l.Literal("_help")
2022-12-11 17:31:37 +08:00
.Executes(r => GetUsage(r.Source, string.Empty))
.Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName)))
);
2022-10-26 08:54:54 +08:00
}
private int GetUsage(CmdResult r, string? cmd)
{
return r.SetAndReturn(cmd switch
{
#pragma warning disable format // @formatter:off
"-s" => GetCmdDescTranslated(),
_ => GetCmdDescTranslated(),
#pragma warning restore format // @formatter:on
});
}
2022-12-11 16:30:45 +08:00
private static int LogBlockInfo(CmdResult r, Location? targetBlock, bool reportSurrounding)
{
2022-12-11 16:30:45 +08:00
McClient handler = CmdResult.currentHandler!;
if (!handler.GetTerrainEnabled())
return r.SetAndReturn(Status.FailNeedTerrain);
2022-12-11 16:30:45 +08:00
if (targetBlock.HasValue)
targetBlock.Value.ToAbsolute(handler.GetCurrentLocation());
else
targetBlock = handler.GetCurrentLocation();
Block block = handler.GetWorld().GetBlock(targetBlock.Value);
handler.Log.Info($"{Translations.cmd_blockinfo_BlockType}: {block.GetTypeString()}");
if (reportSurrounding)
{
StringBuilder sb = new();
sb.AppendLine($"{Translations.cmd_blockinfo_BlocksAround}:");
2022-12-11 16:30:45 +08:00
double X = targetBlock.Value.X, Y = targetBlock.Value.Y, Z = targetBlock.Value.Z;
Block blockXPositive = handler.GetWorld().GetBlock(new Location(X + 1, Y, Z));
Block blockXNegative = handler.GetWorld().GetBlock(new Location(X - 1, Y, Z));
Block blockYPositive = handler.GetWorld().GetBlock(new Location(X, Y + 1, Z));
Block blockYNegative = handler.GetWorld().GetBlock(new Location(X, Y - 1, Z));
Block blockZPositive = handler.GetWorld().GetBlock(new Location(X, Y, Z + 1));
Block blockZNegative = handler.GetWorld().GetBlock(new Location(X, Y, Z - 1));
sb.AppendLine($"[X {Translations.cmd_blockinfo_Positive}] {Translations.cmd_blockinfo_BlockType}: {blockXPositive.GetTypeString()}");
sb.AppendLine($"[X {Translations.cmd_blockinfo_Negative}] {Translations.cmd_blockinfo_BlockType}: {blockXNegative.GetTypeString()}");
2022-10-17 16:17:23 +02:00
sb.AppendLine(" ");
2022-10-17 16:17:23 +02:00
sb.AppendLine($"[Y {Translations.cmd_blockinfo_Positive}] {Translations.cmd_blockinfo_BlockType}: {blockYPositive.GetTypeString()}");
sb.AppendLine($"[Y {Translations.cmd_blockinfo_Negative}] {Translations.cmd_blockinfo_BlockType}: {blockYNegative.GetTypeString()}");
2022-10-17 16:17:23 +02:00
sb.AppendLine(" ");
2022-10-17 16:17:23 +02:00
sb.AppendLine($"[Z {Translations.cmd_blockinfo_Positive}] {Translations.cmd_blockinfo_BlockType}: {blockZPositive.GetTypeString()}");
sb.AppendLine($"[Z {Translations.cmd_blockinfo_Negative}] {Translations.cmd_blockinfo_BlockType}: {blockZNegative.GetTypeString()}");
handler.Log.Info(sb.ToString());
}
return r.SetAndReturn(Status.Done);
}
}
}