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

59 lines
2 KiB
C#
Raw Normal View History

using Brigadier.NET;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
namespace MinecraftClient.Commands
{
public class Sneak : Command
{
2023-05-16 23:19:31 +02:00
public override string CmdName => "sneak";
public override string CmdUsage => "sneak";
public override string CmdDesc => Translations.cmd_sneak_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))
)
);
dispatcher.Register(l => l.Literal(CmdName)
2022-12-11 16:30:45 +08:00
.Executes(r => DoSneak(r.Source))
.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
_ => GetCmdDescTranslated(),
#pragma warning restore format // @formatter:on
});
}
2022-12-11 16:30:45 +08:00
private int DoSneak(CmdResult r)
{
2023-05-16 23:19:31 +02:00
var handler = CmdResult.currentHandler!;
if (handler.IsSneaking)
{
2023-05-16 23:19:31 +02:00
if (!handler.SendEntityAction(Protocol.EntityActionType.StopSneaking))
return r.SetAndReturn(CmdResult.Status.Fail);
2023-05-16 23:19:31 +02:00
handler.IsSneaking = false;
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_sneak_off);
}
2023-05-16 23:19:31 +02:00
if (!handler.SendEntityAction(Protocol.EntityActionType.StartSneaking))
return r.SetAndReturn(CmdResult.Status.Fail);
handler.IsSneaking = true;
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_sneak_on);
}
}
}