2020-06-20 21:30:23 +02:00
|
|
|
|
using System;
|
2022-10-26 08:54:54 +08:00
|
|
|
|
using Brigadier.NET;
|
2022-12-06 15:50:17 +08:00
|
|
|
|
using Brigadier.NET.Builder;
|
|
|
|
|
|
using MinecraftClient.CommandHandler;
|
2020-06-20 21:30:23 +02:00
|
|
|
|
using MinecraftClient.Mapping;
|
2022-12-06 15:50:17 +08:00
|
|
|
|
using static MinecraftClient.CommandHandler.CmdResult;
|
2020-06-20 21:30:23 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Commands
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Dig : Command
|
|
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
public override string CmdName { get { return "dig"; } }
|
|
|
|
|
|
public override string CmdUsage { get { return "dig <x> <y> <z>"; } }
|
2022-10-28 11:13:20 +08:00
|
|
|
|
public override string CmdDesc { get { return Translations.cmd_dig_desc; } }
|
2020-06-20 21:30:23 +02:00
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
public override void RegisterCommand(CommandDispatcher<CmdResult> dispatcher)
|
2022-10-26 08:54:54 +08:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
dispatcher.Register(l => l.Literal("help")
|
|
|
|
|
|
.Then(l => l.Literal(CmdName)
|
|
|
|
|
|
.Executes(r => GetUsage(r.Source, string.Empty))
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
dispatcher.Register(l => l.Literal(CmdName)
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => DigLookAt(r.Source))
|
2023-01-15 20:56:10 +08:00
|
|
|
|
.Then(l => l.Argument("Duration", Arguments.Double())
|
|
|
|
|
|
.Executes(r => DigLookAt(r.Source, Arguments.GetDouble(r, "Duration"))))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Argument("Location", MccArguments.Location())
|
2023-01-15 20:56:10 +08:00
|
|
|
|
.Executes(r => DigAt(r.Source, MccArguments.GetLocation(r, "Location")))
|
|
|
|
|
|
.Then(l => l.Argument("Duration", Arguments.Double())
|
|
|
|
|
|
.Executes(r => DigAt(r.Source, MccArguments.GetLocation(r, "Location"), Arguments.GetDouble(r, "Duration")))))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("_help")
|
2022-12-11 17:31:37 +08:00
|
|
|
|
.Executes(r => GetUsage(r.Source, string.Empty))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName)))
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private int GetUsage(CmdResult r, string? cmd)
|
|
|
|
|
|
{
|
|
|
|
|
|
return r.SetAndReturn(cmd switch
|
|
|
|
|
|
{
|
|
|
|
|
|
#pragma warning disable format // @formatter:off
|
|
|
|
|
|
_ => GetCmdDescTranslated(),
|
|
|
|
|
|
#pragma warning restore format // @formatter:on
|
|
|
|
|
|
});
|
2022-10-26 08:54:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-15 20:56:10 +08:00
|
|
|
|
private int DigAt(CmdResult r, Location blockToBreak, double duration = 0)
|
2020-06-20 21:30:23 +02:00
|
|
|
|
{
|
2022-12-11 16:30:45 +08:00
|
|
|
|
McClient handler = CmdResult.currentHandler!;
|
2020-06-20 21:30:23 +02:00
|
|
|
|
if (!handler.GetTerrainEnabled())
|
2022-12-06 15:50:17 +08:00
|
|
|
|
return r.SetAndReturn(Status.FailNeedTerrain);
|
2020-06-20 21:30:23 +02:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
Location current = handler.GetCurrentLocation();
|
|
|
|
|
|
blockToBreak = blockToBreak.ToAbsolute(current);
|
|
|
|
|
|
if (blockToBreak.DistanceSquared(current.EyesLocation()) > 25)
|
|
|
|
|
|
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_too_far);
|
|
|
|
|
|
Block block = handler.GetWorld().GetBlock(blockToBreak);
|
|
|
|
|
|
if (block.Type == Material.Air)
|
|
|
|
|
|
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_no_block);
|
2023-01-15 20:56:10 +08:00
|
|
|
|
else if (handler.DigBlock(blockToBreak, duration: duration))
|
2022-10-02 13:49:36 +08:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
blockToBreak = blockToBreak.ToCenter();
|
|
|
|
|
|
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockToBreak.X, blockToBreak.Y, blockToBreak.Z, block.GetTypeString()));
|
2022-10-02 13:49:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
2022-12-06 15:50:17 +08:00
|
|
|
|
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_fail);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-15 20:56:10 +08:00
|
|
|
|
private int DigLookAt(CmdResult r, double duration = 0)
|
2022-12-06 15:50:17 +08:00
|
|
|
|
{
|
2022-12-11 16:30:45 +08:00
|
|
|
|
McClient handler = CmdResult.currentHandler!;
|
2022-12-06 15:50:17 +08:00
|
|
|
|
if (!handler.GetTerrainEnabled())
|
|
|
|
|
|
return r.SetAndReturn(Status.FailNeedTerrain);
|
|
|
|
|
|
|
|
|
|
|
|
(bool hasBlock, Location blockLoc, Block block) = RaycastHelper.RaycastBlock(handler, 4.5, false);
|
|
|
|
|
|
if (!hasBlock)
|
|
|
|
|
|
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_too_far);
|
|
|
|
|
|
else if (block.Type == Material.Air)
|
|
|
|
|
|
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_no_block);
|
2023-01-15 20:56:10 +08:00
|
|
|
|
else if (handler.DigBlock(blockLoc, lookAtBlock: false, duration: duration))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockLoc.X, blockLoc.Y, blockLoc.Z, block.GetTypeString()));
|
|
|
|
|
|
else
|
|
|
|
|
|
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_fail);
|
2020-06-20 21:30:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|