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) dispatcher.Register(l => l.Literal(CmdName)
.Executes(r => DigLookAt(r.Source)) .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()) .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") .Then(l => l.Literal("_help")
.Executes(r => GetUsage(r.Source, string.Empty)) .Executes(r => GetUsage(r.Source, string.Empty))
.Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName))) .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!; McClient handler = CmdResult.currentHandler!;
if (!handler.GetTerrainEnabled()) if (!handler.GetTerrainEnabled())
@ -54,7 +58,7 @@ namespace MinecraftClient.Commands
Block block = handler.GetWorld().GetBlock(blockToBreak); Block block = handler.GetWorld().GetBlock(blockToBreak);
if (block.Type == Material.Air) if (block.Type == Material.Air)
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_no_block); 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(); blockToBreak = blockToBreak.ToCenter();
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockToBreak.X, blockToBreak.Y, blockToBreak.Z, block.GetTypeString())); 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); 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!; McClient handler = CmdResult.currentHandler!;
if (!handler.GetTerrainEnabled()) if (!handler.GetTerrainEnabled())
@ -74,7 +78,7 @@ namespace MinecraftClient.Commands
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_too_far); return r.SetAndReturn(Status.Fail, Translations.cmd_dig_too_far);
else if (block.Type == Material.Air) else if (block.Type == Material.Air)
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_no_block); 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())); return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockLoc.X, blockLoc.Y, blockLoc.Z, block.GetTypeString()));
else else
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_fail); return r.SetAndReturn(Status.Fail, Translations.cmd_dig_fail);

View file

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

View file

@ -2028,4 +2028,10 @@ Logging in...</value>
<data name="proxy.connected" xml:space="preserve"> <data name="proxy.connected" xml:space="preserve">
<value>Connected to proxy {0}:{1}</value> <value>Connected to proxy {0}:{1}</value>
</data> </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> </root>