Support specifying the digging duration

This commit is contained in:
BruceChen 2023-01-15 20:56:10 +08:00
parent 338f534239
commit 50dd5a3ba3
4 changed files with 84 additions and 18 deletions

View file

@ -23,8 +23,12 @@ namespace MinecraftClient.Commands
dispatcher.Register(l => l.Literal(CmdName)
.Executes(r => DigLookAt(r.Source))
.Then(l => l.Argument("Duration", Arguments.Double())
.Executes(r => DigLookAt(r.Source, Arguments.GetDouble(r, "Duration"))))
.Then(l => l.Argument("Location", MccArguments.Location())
.Executes(r => DigAt(r.Source, MccArguments.GetLocation(r, "Location"))))
.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")))))
.Then(l => l.Literal("_help")
.Executes(r => GetUsage(r.Source, string.Empty))
.Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName)))
@ -41,7 +45,7 @@ namespace MinecraftClient.Commands
});
}
private int DigAt(CmdResult r, Location blockToBreak)
private int DigAt(CmdResult r, Location blockToBreak, double duration = 0)
{
McClient handler = CmdResult.currentHandler!;
if (!handler.GetTerrainEnabled())
@ -54,7 +58,7 @@ namespace MinecraftClient.Commands
Block block = handler.GetWorld().GetBlock(blockToBreak);
if (block.Type == Material.Air)
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_no_block);
else if (handler.DigBlock(blockToBreak))
else if (handler.DigBlock(blockToBreak, duration: duration))
{
blockToBreak = blockToBreak.ToCenter();
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockToBreak.X, blockToBreak.Y, blockToBreak.Z, block.GetTypeString()));
@ -63,7 +67,7 @@ namespace MinecraftClient.Commands
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_fail);
}
private int DigLookAt(CmdResult r)
private int DigLookAt(CmdResult r, double duration = 0)
{
McClient handler = CmdResult.currentHandler!;
if (!handler.GetTerrainEnabled())
@ -74,7 +78,7 @@ namespace MinecraftClient.Commands
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);
else if (handler.DigBlock(blockLoc, lookAtBlock: false))
else if (handler.DigBlock(blockLoc, lookAtBlock: false, duration: duration))
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);

View file

@ -87,6 +87,10 @@ namespace MinecraftClient
private int playerEntityID;
private object DigLock = new();
private Tuple<Location, Direction>? LastDigPosition;
private int RemainingDiggingTime = 0;
// player health and hunger
private float playerHealth;
private int playerFoodSaturation;
@ -380,6 +384,22 @@ namespace MinecraftClient
taskToRun();
}
}
lock (DigLock)
{
if (RemainingDiggingTime > 0)
{
if (--RemainingDiggingTime == 0 && LastDigPosition != null)
{
handler.SendPlayerDigging(2, LastDigPosition.Item1, LastDigPosition.Item2, sequenceId++);
Log.Info(string.Format(Translations.cmd_dig_end, LastDigPosition.Item1));
}
else
{
DoAnimation((int)Hand.MainHand);
}
}
}
}
#region Connection Lost and Disconnect from Server
@ -1299,7 +1319,7 @@ namespace MinecraftClient
/// <returns>TRUE if the item was successfully used</returns>
public bool UseItemOnHand()
{
return InvokeOnMainThread(() => handler.SendUseItem(0, sequenceId));
return InvokeOnMainThread(() => handler.SendUseItem(0, sequenceId++));
}
/// <summary>
@ -1308,7 +1328,7 @@ namespace MinecraftClient
/// <returns>TRUE if the item was successfully used</returns>
public bool UseItemOnLeftHand()
{
return InvokeOnMainThread(() => handler.SendUseItem(1, sequenceId));
return InvokeOnMainThread(() => handler.SendUseItem(1, sequenceId++));
}
/// <summary>
@ -2193,7 +2213,7 @@ namespace MinecraftClient
/// <returns>TRUE if successfully placed</returns>
public bool PlaceBlock(Location location, Direction blockFace, Hand hand = Hand.MainHand)
{
return InvokeOnMainThread(() => handler.SendPlayerBlockPlacement((int)hand, location, blockFace, sequenceId));
return InvokeOnMainThread(() => handler.SendPlayerBlockPlacement((int)hand, location, blockFace, sequenceId++));
}
/// <summary>
@ -2202,26 +2222,44 @@ namespace MinecraftClient
/// <param name="location">Location of block to dig</param>
/// <param name="swingArms">Also perform the "arm swing" animation</param>
/// <param name="lookAtBlock">Also look at the block before digging</param>
public bool DigBlock(Location location, bool swingArms = true, bool lookAtBlock = true)
public bool DigBlock(Location location, bool swingArms = true, bool lookAtBlock = true, double duration = 0)
{
if (!GetTerrainEnabled())
return false;
if (InvokeRequired)
return InvokeOnMainThread(() => DigBlock(location, swingArms, lookAtBlock));
return InvokeOnMainThread(() => DigBlock(location, swingArms, lookAtBlock, duration));
// TODO select best face from current player location
Direction blockFace = Direction.Down;
lock (DigLock)
{
if (RemainingDiggingTime > 0 && LastDigPosition != null)
{
handler.SendPlayerDigging(1, LastDigPosition.Item1, LastDigPosition.Item2, sequenceId++);
Log.Info(string.Format(Translations.cmd_dig_cancel, LastDigPosition.Item1));
}
// Look at block before attempting to break it
if (lookAtBlock)
UpdateLocation(GetCurrentLocation(), location);
// Send dig start and dig end, will need to wait for server response to know dig result
// See https://wiki.vg/How_to_Write_a_Client#Digging for more details
return handler.SendPlayerDigging(0, location, blockFace, sequenceId)
&& (!swingArms || DoAnimation((int)Hand.MainHand))
&& handler.SendPlayerDigging(2, location, blockFace, sequenceId);
bool result = handler.SendPlayerDigging(0, location, blockFace, sequenceId++)
&& (!swingArms || DoAnimation((int)Hand.MainHand));
if (duration <= 0)
result &= handler.SendPlayerDigging(2, location, blockFace, sequenceId++);
else
{
LastDigPosition = new(location, blockFace);
RemainingDiggingTime = Settings.DoubleToTick(duration);
}
return result;
}
}
/// <summary>

View file

@ -2704,6 +2704,15 @@ namespace MinecraftClient {
}
}
/// <summary>
/// Looks up a localized string similar to Cancel mining the block located at {0}..
/// </summary>
internal static string cmd_dig_cancel {
get {
return ResourceManager.GetString("cmd.dig.cancel", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Attempt to break a block.
/// </summary>
@ -2722,6 +2731,15 @@ namespace MinecraftClient {
}
}
/// <summary>
/// Looks up a localized string similar to Mining of the block located at {0} ends..
/// </summary>
internal static string cmd_dig_end {
get {
return ResourceManager.GetString("cmd.dig.end", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Failed to start digging block..
/// </summary>

View file

@ -2028,4 +2028,10 @@ Logging in...</value>
<data name="proxy.connected" xml:space="preserve">
<value>Connected to proxy {0}:{1}</value>
</data>
<data name="cmd.dig.end" xml:space="preserve">
<value>Mining of the block located at {0} ends.</value>
</data>
<data name="cmd.dig.cancel" xml:space="preserve">
<value>Cancel mining the block located at {0}.</value>
</data>
</root>