2022-12-06 15:50:17 +08:00
|
|
|
|
using Brigadier.NET;
|
|
|
|
|
|
using Brigadier.NET.Builder;
|
|
|
|
|
|
using MinecraftClient.CommandHandler;
|
2026-03-27 16:43:53 +01:00
|
|
|
|
using MinecraftClient.Inventory;
|
|
|
|
|
|
using MinecraftClient.Mapping;
|
2022-12-06 15:50:17 +08:00
|
|
|
|
using static MinecraftClient.CommandHandler.CmdResult;
|
2020-03-22 20:12:06 +08:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Commands
|
|
|
|
|
|
{
|
|
|
|
|
|
class UseItem : Command
|
|
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
public override string CmdName { get { return "useitem"; } }
|
2026-03-27 16:43:53 +01:00
|
|
|
|
public override string CmdUsage { get { return "useitem [x] [y] [z]"; } }
|
2022-10-28 11:13:20 +08:00
|
|
|
|
public override string CmdDesc { get { return Translations.cmd_useitem_desc; } }
|
2020-03-22 20:12:06 +08: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 => DoUseItem(r.Source))
|
2026-03-27 16:43:53 +01:00
|
|
|
|
.Then(l => l.Argument("Location", MccArguments.Location())
|
|
|
|
|
|
.Executes(r => DoUseItemAtLocation(r.Source, MccArguments.GetLocation(r, "Location"))))
|
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-03-22 20:12:06 +08:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
return r.SetAndReturn(cmd switch
|
2020-03-29 18:41:26 +02:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
#pragma warning disable format // @formatter:off
|
|
|
|
|
|
_ => GetCmdDescTranslated(),
|
|
|
|
|
|
#pragma warning restore format // @formatter:on
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
private int DoUseItem(CmdResult r)
|
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.GetInventoryEnabled())
|
|
|
|
|
|
return r.SetAndReturn(Status.FailNeedInventory);
|
|
|
|
|
|
|
2026-03-27 16:43:53 +01:00
|
|
|
|
if (handler.GetTerrainEnabled())
|
|
|
|
|
|
{
|
|
|
|
|
|
const double maxDistance = 4.5;
|
|
|
|
|
|
var raycast = RaycastHelper.RaycastBlock(handler, maxDistance, false);
|
|
|
|
|
|
if (raycast.Item1 && raycast.Item3.Type != Material.Air)
|
|
|
|
|
|
{
|
|
|
|
|
|
handler.PlaceBlock(raycast.Item2, Direction.Up, lookAtBlock: true);
|
|
|
|
|
|
handler.DoAnimation((int)Hand.MainHand);
|
|
|
|
|
|
return r.SetAndReturn(Status.Done, Translations.cmd_useitem_use);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
handler.UseItemOnHand();
|
|
|
|
|
|
return r.SetAndReturn(Status.Done, Translations.cmd_useitem_use);
|
2020-03-22 20:12:06 +08:00
|
|
|
|
}
|
2026-03-27 16:43:53 +01:00
|
|
|
|
|
|
|
|
|
|
private int DoUseItemAtLocation(CmdResult r, Location block)
|
|
|
|
|
|
{
|
|
|
|
|
|
McClient handler = CmdResult.currentHandler!;
|
|
|
|
|
|
if (!handler.GetTerrainEnabled())
|
|
|
|
|
|
return r.SetAndReturn(Status.FailNeedTerrain);
|
|
|
|
|
|
|
|
|
|
|
|
Location current = handler.GetCurrentLocation();
|
|
|
|
|
|
block = block.ToAbsolute(current).ToFloor();
|
|
|
|
|
|
handler.PlaceBlock(block, Direction.Up, lookAtBlock: true);
|
|
|
|
|
|
handler.DoAnimation((int)Hand.MainHand);
|
|
|
|
|
|
return r.SetAndReturn(Status.Done, Translations.cmd_useitem_use);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-22 20:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|