Refactoring to asynchronous. (partially completed)

This commit is contained in:
BruceChen 2022-12-20 22:41:14 +08:00
parent 7ee08092d4
commit 096ea0c70c
72 changed files with 6033 additions and 5080 deletions

View file

@ -49,7 +49,7 @@ namespace MinecraftClient.Commands
private static int DoAnimation(CmdResult r, bool mainhand)
{
McClient handler = CmdResult.currentHandler!;
return r.SetAndReturn(handler.DoAnimation(mainhand ? 1 : 0));
return r.SetAndReturn(handler.DoAnimation(mainhand ? 1 : 0).Result);
}
}
}

View file

@ -57,7 +57,7 @@ namespace MinecraftClient.Commands
private static int DoLeaveBed(CmdResult r)
{
McClient handler = CmdResult.currentHandler!;
return r.SetAndReturn(Translations.cmd_bed_leaving, handler.SendEntityAction(Protocol.EntityActionType.LeaveBed));
return r.SetAndReturn(Translations.cmd_bed_leaving, handler.SendEntityAction(Protocol.EntityActionType.LeaveBed).Result);
}
private static int DoSleepBedWithRadius(CmdResult r, double radius)
@ -137,7 +137,7 @@ namespace MinecraftClient.Commands
handler.Log.Info(string.Format(Translations.cmd_bed_moving, bedLocation.X, bedLocation.Y, bedLocation.Z));
bool res = handler.PlaceBlock(bedLocation, Direction.Down);
bool res = handler.PlaceBlock(bedLocation, Direction.Down).Result;
handler.Log.Info(string.Format(
Translations.cmd_bed_trying_to_use,
@ -174,7 +174,7 @@ namespace MinecraftClient.Commands
blockCenter.X,
blockCenter.Y,
blockCenter.Z,
handler.PlaceBlock(block, Direction.Down) ? Translations.cmd_bed_in : Translations.cmd_bed_not_in
handler.PlaceBlock(block, Direction.Down).Result ? Translations.cmd_bed_in : Translations.cmd_bed_not_in
));
}
}

View file

@ -77,7 +77,7 @@ namespace MinecraftClient.Commands
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_bots_noloaded);
else
{
handler.UnloadAllBots();
handler.UnloadAllBots().Wait();
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_bots_unloaded_all);
}
}
@ -88,7 +88,7 @@ namespace MinecraftClient.Commands
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_bots_notfound, botName));
else
{
handler.BotUnLoad(bot);
handler.BotUnLoad(bot).Wait();
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_bots_unloaded, botName));
}
}

View file

@ -44,7 +44,7 @@ namespace MinecraftClient.Commands
if (!handler.GetInventoryEnabled())
return r.SetAndReturn(Status.FailNeedInventory);
if (handler.ChangeSlot((short)(slot - 1)))
if (handler.ChangeSlot((short)(slot - 1)).Result)
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_changeSlot_changed, slot));
else
return r.SetAndReturn(Status.Fail, Translations.cmd_changeSlot_fail);

View file

@ -54,7 +54,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).Result)
{
blockToBreak = blockToBreak.ToCenter();
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockToBreak.X, blockToBreak.Y, blockToBreak.Z, block.GetTypeString()));
@ -74,7 +74,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).Result)
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

@ -58,7 +58,7 @@ namespace MinecraftClient.Commands
var p = inventories[inventoryId];
int[] targetItems = p.SearchItem(itemType);
foreach (int slot in targetItems)
handler.DoWindowAction(inventoryId, slot, WindowActionType.DropItemStack);
handler.DoWindowAction(inventoryId, slot, WindowActionType.DropItemStack).Wait();
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dropItem_dropped, Item.GetTypeString(itemType), inventoryId));
}

View file

@ -99,7 +99,7 @@ namespace MinecraftClient.Commands
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_enchant_no_levels, handler.GetLevel(), requiredLevel));
else
{
if (handler.ClickContainerButton(enchantingTable.ID, slotId))
if (handler.ClickContainerButton(enchantingTable.ID, slotId).Result)
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_enchant_clicked);
else
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_enchant_not_clicked);

View file

@ -166,12 +166,12 @@ namespace MinecraftClient.Commands
{
if (action == ActionType.Attack)
{
handler.InteractEntity(entity2.Key, InteractType.Attack);
handler.InteractEntity(entity2.Key, InteractType.Attack).Wait();
actionst = Translations.cmd_entityCmd_attacked;
}
else if (action == ActionType.Use)
{
handler.InteractEntity(entity2.Key, InteractType.Interact);
handler.InteractEntity(entity2.Key, InteractType.Interact).Wait();
actionst = Translations.cmd_entityCmd_used;
}
actioncount++;
@ -311,10 +311,10 @@ namespace MinecraftClient.Commands
switch (action)
{
case ActionType.Attack:
handler.InteractEntity(entity.ID, InteractType.Attack);
handler.InteractEntity(entity.ID, InteractType.Attack).Wait();
return Translations.cmd_entityCmd_attacked;
case ActionType.Use:
handler.InteractEntity(entity.ID, InteractType.Interact);
handler.InteractEntity(entity.ID, InteractType.Interact).Wait();
return Translations.cmd_entityCmd_used;
case ActionType.List:
return GetEntityInfoDetailed(handler, entity);

View file

@ -48,11 +48,5 @@ namespace MinecraftClient.Commands
Program.Exit(code);
return r.SetAndReturn(CmdResult.Status.Done);
}
internal static string DoExit(string command)
{
Program.Exit();
return string.Empty;
}
}
}

View file

@ -159,7 +159,7 @@ namespace MinecraftClient.Commands
if (handler.GetGamemode() == 1)
{
if (handler.DoCreativeGive(slot, itemType, count, null))
if (handler.DoCreativeGive(slot, itemType, count, null).Result)
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_inventory_creative_done, itemType, count, slot));
else
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_inventory_creative_fail);
@ -178,7 +178,7 @@ namespace MinecraftClient.Commands
if (handler.GetGamemode() == 1)
{
if (handler.DoCreativeGive(slot, ItemType.Null, 0, null))
if (handler.DoCreativeGive(slot, ItemType.Null, 0, null).Result)
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_inventory_creative_delete, slot));
else
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_inventory_creative_fail);
@ -279,7 +279,7 @@ namespace MinecraftClient.Commands
if (inventory == null)
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_inventory_not_exist, inventoryId));
if (handler.CloseInventory(inventoryId.Value))
if (handler.CloseInventory(inventoryId.Value).Result)
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_inventory_close, inventoryId));
else
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_inventory_close_fail, inventoryId));
@ -355,7 +355,9 @@ namespace MinecraftClient.Commands
};
handler.Log.Info(string.Format(Translations.cmd_inventory_clicking, keyName, slot, inventoryId));
return r.SetAndReturn(handler.DoWindowAction(inventoryId.Value, slot, actionType));
var task = handler.DoWindowAction(inventoryId.Value, slot, actionType);
task.Wait();
return r.SetAndReturn(task.Result);
}
private int DoDropAction(CmdResult r, int? inventoryId, int slot, WindowActionType actionType)
@ -379,7 +381,7 @@ namespace MinecraftClient.Commands
if (!inventory.Items.ContainsKey(slot))
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_inventory_no_item, slot));
if (handler.DoWindowAction(inventoryId.Value, slot, actionType))
if (handler.DoWindowAction(inventoryId.Value, slot, actionType).Result)
{
if (actionType == WindowActionType.DropItemStack)
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_inventory_drop_stack, slot));

View file

@ -63,7 +63,7 @@ namespace MinecraftClient.Commands
}
}
Program.Restart(keepAccountAndServerSettings: true);
return String.Empty;
return string.Empty;
}
}
}

View file

@ -40,7 +40,7 @@ namespace MinecraftClient.Commands
{
McClient handler = CmdResult.currentHandler!;
handler.Log.Info(Translations.cmd_reload_started);
handler.ReloadSettings();
handler.ReloadSettings().Wait();
handler.Log.Warn(Translations.cmd_reload_warning1);
handler.Log.Warn(Translations.cmd_reload_warning2);
handler.Log.Warn(Translations.cmd_reload_warning3);

View file

@ -39,7 +39,7 @@ namespace MinecraftClient.Commands
private int DoRespawn(CmdResult r)
{
McClient handler = CmdResult.currentHandler!;
handler.SendRespawnPacket();
handler.SendRespawnPacket().Wait();
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_respawn_done);
}
}

View file

@ -37,7 +37,7 @@ namespace MinecraftClient.Commands
private int DoSendText(CmdResult r, string command)
{
McClient handler = CmdResult.currentHandler!;
handler.SendText(command);
handler.SendText(command).Wait();
return r.SetAndReturn(CmdResult.Status.Done);
}
}

View file

@ -42,7 +42,7 @@ namespace MinecraftClient.Commands
McClient handler = CmdResult.currentHandler!;
if (sneaking)
{
var result = handler.SendEntityAction(Protocol.EntityActionType.StopSneaking);
var result = handler.SendEntityAction(Protocol.EntityActionType.StopSneaking).Result;
if (result)
sneaking = false;
if (result)
@ -52,7 +52,7 @@ namespace MinecraftClient.Commands
}
else
{
var result = handler.SendEntityAction(Protocol.EntityActionType.StartSneaking);
var result = handler.SendEntityAction(Protocol.EntityActionType.StartSneaking).Result;
if (result)
sneaking = true;
if (result)

View file

@ -1,4 +1,5 @@
using Brigadier.NET;
using System.Threading.Tasks;
using Brigadier.NET;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
@ -71,7 +72,7 @@ namespace MinecraftClient.Commands
private static int CheckUpdate(CmdResult r)
{
UpgradeHelper.CheckUpdate(forceUpdate: true);
Task.Run(async () => { await UpgradeHelper.CheckUpdate(forceUpdate: true); });
return r.SetAndReturn(CmdResult.Status.Done, Translations.mcc_update_start);
}
}

View file

@ -43,7 +43,7 @@ namespace MinecraftClient.Commands
if (!handler.GetInventoryEnabled())
return r.SetAndReturn(Status.FailNeedInventory);
handler.UseItemOnHand();
handler.UseItemOnHand().Wait();
return r.SetAndReturn(Status.Done, Translations.cmd_useitem_use);
}
}

View file

@ -48,7 +48,7 @@ namespace MinecraftClient.Commands
Location current = handler.GetCurrentLocation();
block = block.ToAbsolute(current).ToFloor();
Location blockCenter = block.ToCenter();
bool res = handler.PlaceBlock(block, Direction.Down);
bool res = handler.PlaceBlock(block, Direction.Down).Result;
return r.SetAndReturn(string.Format(Translations.cmd_useblock_use, blockCenter.X, blockCenter.Y, blockCenter.Z, res ? "succeeded" : "failed"), res);
}
}