2026-06-05 20:26:29 +02:00
|
|
|
using System;
|
2026-03-20 02:35:52 +08:00
|
|
|
using Brigadier.NET;
|
2022-12-06 15:50:17 +08:00
|
|
|
using Brigadier.NET.Builder;
|
|
|
|
|
using MinecraftClient.CommandHandler;
|
2026-03-29 22:38:47 +08:00
|
|
|
using MinecraftClient.Inventory;
|
2022-10-02 18:31:08 +08:00
|
|
|
using MinecraftClient.Mapping;
|
2022-12-06 15:50:17 +08:00
|
|
|
using static MinecraftClient.CommandHandler.CmdResult;
|
2020-06-20 17:57:07 +05:00
|
|
|
|
|
|
|
|
namespace MinecraftClient.Commands
|
|
|
|
|
{
|
|
|
|
|
class Useblock : Command
|
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
public override string CmdName { get { return "useblock"; } }
|
2026-03-29 22:38:47 +08:00
|
|
|
public override string CmdUsage { get { return "useblock <x> <y> <z> [mainhand|offhand]"; } }
|
2022-10-28 11:13:20 +08:00
|
|
|
public override string CmdDesc { get { return Translations.cmd_useblock_desc; } }
|
2020-06-20 17:57:07 +05: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)
|
|
|
|
|
.Then(l => l.Argument("Location", MccArguments.Location())
|
2026-03-29 22:38:47 +08:00
|
|
|
.Executes(r => UseBlockAtLocation(r.Source, MccArguments.GetLocation(r, "Location"), Hand.MainHand))
|
|
|
|
|
.Then(l => l.Literal("mainhand")
|
|
|
|
|
.Executes(r => UseBlockAtLocation(r.Source, MccArguments.GetLocation(r, "Location"), Hand.MainHand)))
|
|
|
|
|
.Then(l => l.Literal("offhand")
|
|
|
|
|
.Executes(r => UseBlockAtLocation(r.Source, MccArguments.GetLocation(r, "Location"), Hand.OffHand))))
|
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)))
|
|
|
|
|
);
|
2022-10-26 08:54:54 +08:00
|
|
|
}
|
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
private int GetUsage(CmdResult r, string? cmd)
|
2020-06-20 17:57:07 +05:00
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
return r.SetAndReturn(cmd switch
|
2020-06-20 17:57:07 +05:00
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
#pragma warning disable format // @formatter:off
|
|
|
|
|
_ => GetCmdDescTranslated(),
|
|
|
|
|
#pragma warning restore format // @formatter:on
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 22:38:47 +08:00
|
|
|
private int UseBlockAtLocation(CmdResult r, Location block, Hand hand)
|
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);
|
|
|
|
|
|
|
|
|
|
Location current = handler.GetCurrentLocation();
|
|
|
|
|
block = block.ToAbsolute(current).ToFloor();
|
|
|
|
|
Location blockCenter = block.ToCenter();
|
2026-06-05 20:26:29 +02:00
|
|
|
bool res = handler.PlaceBlock(block, GetFaceNearestPlayer(current, blockCenter), hand, lookAtBlock: true);
|
2022-12-06 15:50:17 +08:00
|
|
|
return r.SetAndReturn(string.Format(Translations.cmd_useblock_use, blockCenter.X, blockCenter.Y, blockCenter.Z, res ? "succeeded" : "failed"), res);
|
2020-06-20 17:57:07 +05:00
|
|
|
}
|
2026-06-05 20:26:29 +02:00
|
|
|
|
|
|
|
|
private static Direction GetFaceNearestPlayer(Location playerLocation, Location blockCenter)
|
|
|
|
|
{
|
|
|
|
|
double dx = playerLocation.X - blockCenter.X;
|
|
|
|
|
double dy = playerLocation.Y - blockCenter.Y;
|
|
|
|
|
double dz = playerLocation.Z - blockCenter.Z;
|
|
|
|
|
|
|
|
|
|
double absX = Math.Abs(dx);
|
|
|
|
|
double absY = Math.Abs(dy);
|
|
|
|
|
double absZ = Math.Abs(dz);
|
|
|
|
|
|
|
|
|
|
if (absX >= absY && absX >= absZ)
|
|
|
|
|
return dx >= 0 ? Direction.East : Direction.West;
|
|
|
|
|
|
|
|
|
|
if (absY >= absZ)
|
|
|
|
|
return dy >= 0 ? Direction.Up : Direction.Down;
|
|
|
|
|
|
|
|
|
|
return dz >= 0 ? Direction.South : Direction.North;
|
|
|
|
|
}
|
2020-06-20 17:57:07 +05:00
|
|
|
}
|
|
|
|
|
}
|