mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2026-08-15 13:04:36 +00:00
More parts switched to async
This commit is contained in:
parent
297d7dbc30
commit
4d800a31fc
6 changed files with 295 additions and 73 deletions
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MinecraftClient.Protocol.Handlers.Forge;
|
||||
using MinecraftClient.Protocol.Message;
|
||||
using MinecraftClient.Scripting;
|
||||
|
|
@ -21,6 +22,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
|
||||
private readonly ForgeInfo? forgeInfo = forgeInfo;
|
||||
private FMLHandshakeClientState fmlHandshakeState = FMLHandshakeClientState.START;
|
||||
private DateTime? pendingServerDataAckAt;
|
||||
private bool ForgeEnabled() { return forgeInfo is not null; }
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -40,12 +42,17 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
/// </summary>
|
||||
/// <returns>Whether the handshake was successful.</returns>
|
||||
public bool CompleteForgeHandshake()
|
||||
{
|
||||
return CompleteForgeHandshakeAsync().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public async Task<bool> CompleteForgeHandshakeAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (ForgeEnabled() && forgeInfo!.Version == FMLVersion.FML)
|
||||
{
|
||||
while (fmlHandshakeState != FMLHandshakeClientState.DONE)
|
||||
{
|
||||
(int packetID, Queue<byte> packetData) = protocol18.ReadNextPacket();
|
||||
(int packetID, Queue<byte> packetData) = await protocol18.ReadNextPacketAsync(cancellationToken);
|
||||
|
||||
if (packetID == 0x40) // Disconnect
|
||||
{
|
||||
|
|
@ -56,12 +63,31 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
{
|
||||
// Send back regular packet to the vanilla protocol handler
|
||||
protocol18.HandlePacket(packetID, packetData);
|
||||
await FlushPendingHandshakeActionsAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task FlushPendingHandshakeActionsAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!pendingServerDataAckAt.HasValue)
|
||||
return;
|
||||
|
||||
TimeSpan delay = pendingServerDataAckAt.Value - DateTime.UtcNow;
|
||||
if (delay > TimeSpan.Zero)
|
||||
await Task.Delay(delay, cancellationToken);
|
||||
|
||||
if (Settings.Config.Logging.DebugMessages)
|
||||
ConsoleIO.WriteLineFormatted("§8" + Translations.forge_accept, acceptnewlines: true);
|
||||
|
||||
SendForgeHandshakePacket(FMLHandshakeDiscriminator.HandshakeAck,
|
||||
new byte[] { (byte)FMLHandshakeClientState.WAITINGSERVERDATA });
|
||||
|
||||
pendingServerDataAckAt = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read Forge VarShort field
|
||||
/// </summary>
|
||||
|
|
@ -145,16 +171,9 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
if (discriminator != FMLHandshakeDiscriminator.ModList)
|
||||
return false;
|
||||
|
||||
Thread.Sleep(2000);
|
||||
|
||||
if (Settings.Config.Logging.DebugMessages)
|
||||
ConsoleIO.WriteLineFormatted("§8" + Translations.forge_accept, acceptnewlines: true);
|
||||
// Tell the server that yes, we are OK with the mods it has
|
||||
// even though we don't actually care what mods it has.
|
||||
|
||||
SendForgeHandshakePacket(FMLHandshakeDiscriminator.HandshakeAck,
|
||||
new byte[] { (byte)FMLHandshakeClientState.WAITINGSERVERDATA });
|
||||
|
||||
pendingServerDataAckAt = DateTime.UtcNow.AddSeconds(2);
|
||||
fmlHandshakeState = FMLHandshakeClientState.WAITINGSERVERCOMPLETE;
|
||||
return false;
|
||||
case FMLHandshakeClientState.WAITINGSERVERCOMPLETE:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue