2022-11-28 13:55:05 +08: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;
|
2015-12-12 16:48:29 +01:00
|
|
|
|
using MinecraftClient.Mapping;
|
2022-12-06 15:50:17 +08:00
|
|
|
|
using static MinecraftClient.CommandHandler.CmdResult;
|
2015-12-12 16:48:29 +01:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Commands
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Move : Command
|
|
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
public override string CmdName { get { return "move"; } }
|
2022-07-25 18:11:10 +08:00
|
|
|
|
public override string CmdUsage { get { return "move <on|off|get|up|down|east|west|north|south|center|x y z|gravity [on|off]> [-f]"; } }
|
2022-10-28 11:13:20 +08:00
|
|
|
|
public override string CmdDesc { get { return Translations.cmd_move_desc + " \"-f\": " + Translations.cmd_move_desc_force; } }
|
2015-12-12 16:48:29 +01: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))
|
|
|
|
|
|
.Then(l => l.Literal("enable")
|
|
|
|
|
|
.Executes(r => GetUsage(r.Source, "enable")))
|
|
|
|
|
|
.Then(l => l.Literal("gravity")
|
|
|
|
|
|
.Executes(r => GetUsage(r.Source, "gravity")))
|
|
|
|
|
|
.Then(l => l.Literal("direction")
|
|
|
|
|
|
.Executes(r => GetUsage(r.Source, "direction")))
|
|
|
|
|
|
.Then(l => l.Literal("center")
|
|
|
|
|
|
.Executes(r => GetUsage(r.Source, "center")))
|
|
|
|
|
|
.Then(l => l.Literal("get")
|
|
|
|
|
|
.Executes(r => GetUsage(r.Source, "get")))
|
|
|
|
|
|
.Then(l => l.Literal("location")
|
|
|
|
|
|
.Executes(r => GetUsage(r.Source, "location")))
|
|
|
|
|
|
.Then(l => l.Literal("-f")
|
|
|
|
|
|
.Executes(r => GetUsage(r.Source, "-f")))
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
dispatcher.Register(l => l.Literal(CmdName)
|
|
|
|
|
|
.Then(l => l.Literal("on")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => SetMovementEnable(r.Source, enable: true)))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("off")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => SetMovementEnable(r.Source, enable: false)))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("gravity")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => SetGravityEnable(r.Source, enable: null))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("on")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => SetGravityEnable(r.Source, enable: true)))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("off")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => SetGravityEnable(r.Source, enable: false))))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("up")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.Up, false))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("-f")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.Up, true))))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("down")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.Down, false))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("-f")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.Down, true))))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("east")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.East, false))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("-f")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.East, true))))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("west")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.West, false))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("-f")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.West, true))))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("north")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.North, false))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("-f")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.North, true))))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("south")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.South, false))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("-f")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveOnDirection(r.Source, Direction.South, true))))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("center")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveToCenter(r.Source)))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("get")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => GetCurrentLocation(r.Source)))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Argument("location", MccArguments.Location())
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveToLocation(r.Source, MccArguments.GetLocation(r, "location"), false))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("-f")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => MoveToLocation(r.Source, MccArguments.GetLocation(r, "location"), true))))
|
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)
|
2015-12-12 16:48:29 +01:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
return r.SetAndReturn(cmd switch
|
2022-07-25 14:20:24 +08:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
#pragma warning disable format // @formatter:off
|
|
|
|
|
|
"enable" => GetCmdDescTranslated(),
|
|
|
|
|
|
"gravity" => GetCmdDescTranslated(),
|
|
|
|
|
|
"direction" => GetCmdDescTranslated(),
|
|
|
|
|
|
"center" => GetCmdDescTranslated(),
|
|
|
|
|
|
"get" => GetCmdDescTranslated(),
|
|
|
|
|
|
"location" => GetCmdDescTranslated(),
|
|
|
|
|
|
"-f" => GetCmdDescTranslated(),
|
|
|
|
|
|
_ => GetCmdDescTranslated(),
|
|
|
|
|
|
#pragma warning restore format // @formatter:on
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-07-25 14:20:24 +08:00
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
private int SetMovementEnable(CmdResult r, bool enable)
|
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 (enable)
|
|
|
|
|
|
{
|
|
|
|
|
|
handler.SetTerrainEnabled(true);
|
|
|
|
|
|
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_move_enable);
|
2022-07-25 14:20:24 +08:00
|
|
|
|
}
|
2022-12-06 15:50:17 +08:00
|
|
|
|
else
|
2021-12-29 15:34:40 +01:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
handler.SetTerrainEnabled(false);
|
|
|
|
|
|
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_move_disable);
|
2021-12-29 15:34:40 +01:00
|
|
|
|
}
|
2022-12-06 15:50:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
private int SetGravityEnable(CmdResult r, bool? enable)
|
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 (enable.HasValue)
|
|
|
|
|
|
Settings.InternalConfig.GravityEnabled = enable.Value;
|
|
|
|
|
|
|
|
|
|
|
|
if (Settings.InternalConfig.GravityEnabled)
|
|
|
|
|
|
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_move_gravity_enabled);
|
|
|
|
|
|
else
|
|
|
|
|
|
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_move_gravity_disabled);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
private int GetCurrentLocation(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.GetTerrainEnabled())
|
|
|
|
|
|
return r.SetAndReturn(Status.FailNeedTerrain);
|
|
|
|
|
|
|
|
|
|
|
|
return r.SetAndReturn(Status.Done, handler.GetCurrentLocation().ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
private int MoveToCenter(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.GetTerrainEnabled())
|
|
|
|
|
|
return r.SetAndReturn(Status.FailNeedTerrain);
|
|
|
|
|
|
|
|
|
|
|
|
Location current = handler.GetCurrentLocation();
|
|
|
|
|
|
Location currentCenter = new(Math.Floor(current.X) + 0.5, current.Y, Math.Floor(current.Z) + 0.5);
|
|
|
|
|
|
handler.MoveTo(currentCenter, allowDirectTeleport: true);
|
|
|
|
|
|
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_move_walk, currentCenter, current));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
private int MoveOnDirection(CmdResult r, Direction direction, bool takeRisk)
|
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 goal = Movement.Move(handler.GetCurrentLocation(), direction);
|
|
|
|
|
|
|
|
|
|
|
|
if (!Movement.CheckChunkLoading(handler.GetWorld(), handler.GetCurrentLocation(), goal))
|
|
|
|
|
|
return r.SetAndReturn(Status.FailChunkNotLoad, string.Format(Translations.cmd_move_chunk_not_loaded, goal.X, goal.Y, goal.Z));
|
2021-12-29 15:34:40 +01:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
if (Movement.CanMove(handler.GetWorld(), handler.GetCurrentLocation(), direction))
|
2019-04-28 21:32:03 +02:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
if (handler.MoveTo(goal, allowUnsafe: takeRisk))
|
|
|
|
|
|
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_move_moving, direction.ToString()));
|
|
|
|
|
|
else
|
|
|
|
|
|
return r.SetAndReturn(Status.Fail, takeRisk ? Translations.cmd_move_dir_fail : Translations.cmd_move_suggestforce);
|
2019-04-28 21:32:03 +02:00
|
|
|
|
}
|
2022-12-06 15:50:17 +08:00
|
|
|
|
else
|
2019-04-28 21:32:03 +02:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
return r.SetAndReturn(Status.Fail, Translations.cmd_move_dir_fail);
|
2019-04-28 21:32:03 +02:00
|
|
|
|
}
|
2022-12-06 15:50:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
private int MoveToLocation(CmdResult r, Location goal, bool takeRisk)
|
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(), currentCenter = current.ToCenter();
|
|
|
|
|
|
goal.ToAbsolute(current);
|
|
|
|
|
|
|
|
|
|
|
|
if (!Movement.CheckChunkLoading(handler.GetWorld(), current, goal))
|
|
|
|
|
|
return r.SetAndReturn(Status.FailChunkNotLoad, string.Format(Translations.cmd_move_chunk_not_loaded, goal.X, goal.Y, goal.Z));
|
|
|
|
|
|
|
|
|
|
|
|
if (takeRisk || Movement.PlayerFitsHere(handler.GetWorld(), goal))
|
2022-03-06 19:37:27 +01:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
if (current.ToFloor() == goal.ToFloor())
|
|
|
|
|
|
handler.MoveTo(goal, allowDirectTeleport: true);
|
|
|
|
|
|
else if (!handler.MoveTo(goal, allowUnsafe: takeRisk))
|
|
|
|
|
|
return r.SetAndReturn(Status.Fail, takeRisk ? string.Format(Translations.cmd_move_fail, goal) : string.Format(Translations.cmd_move_suggestforce, goal));
|
|
|
|
|
|
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_move_walk, goal, current));
|
2022-03-06 19:37:27 +01:00
|
|
|
|
}
|
2022-12-06 15:50:17 +08:00
|
|
|
|
else
|
2015-12-12 16:48:29 +01:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
return r.SetAndReturn(Status.Fail, string.Format(Translations.cmd_move_suggestforce, goal));
|
2015-12-12 16:48:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|