Implement command completion suggestions.

This commit is contained in:
BruceChen 2022-12-06 15:50:17 +08:00
parent 5d2589b10f
commit 84cf749344
115 changed files with 4684 additions and 2695 deletions

View file

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using Brigadier.NET;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
using MinecraftClient.Inventory;
namespace MinecraftClient.Commands
@ -11,80 +12,96 @@ namespace MinecraftClient.Commands
public override string CmdUsage { get { return "enchant <top|middle|bottom>"; } }
public override string CmdDesc { get { return Translations.cmd_enchant_desc; } }
public override void RegisterCommand(McClient handler, CommandDispatcher<CommandSource> dispatcher)
public override void RegisterCommand(McClient handler, CommandDispatcher<CmdResult> dispatcher)
{
dispatcher.Register(l => l.Literal("help")
.Then(l => l.Literal(CmdName)
.Executes(r => GetUsage(r.Source, string.Empty))
.Then(l => l.Literal("top")
.Executes(r => GetUsage(r.Source, "top")))
.Then(l => l.Literal("middle")
.Executes(r => GetUsage(r.Source, "middle")))
.Then(l => l.Literal("bottom")
.Executes(r => GetUsage(r.Source, "bottom")))
)
);
dispatcher.Register(l => l.Literal(CmdName)
.Then(l => l.Literal("top")
.Executes(r => DoEnchant(r.Source, handler, slotId: 0)))
.Then(l => l.Literal("middle")
.Executes(r => DoEnchant(r.Source, handler, slotId: 1)))
.Then(l => l.Literal("bottom")
.Executes(r => DoEnchant(r.Source, handler, slotId: 2)))
.Then(l => l.Literal("_help")
.Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName)))
);
}
public override string Run(McClient handler, string command, Dictionary<string, object>? localVars)
private int GetUsage(CmdResult r, string? cmd)
{
if (!handler.GetInventoryEnabled())
return Translations.error_inventoryhandling_not_enabled;
if (HasArg(command))
return r.SetAndReturn(cmd switch
{
string slot = GetArg(command).ToLower().Trim();
#pragma warning disable format // @formatter:off
"top" => GetCmdDescTranslated(),
"middle" => GetCmdDescTranslated(),
"bottom" => GetCmdDescTranslated(),
_ => GetCmdDescTranslated(),
#pragma warning restore format // @formatter:on
});
}
int slotId = slot switch
private int DoEnchant(CmdResult r, McClient handler, int slotId)
{
Container? enchantingTable = null;
foreach (var (id, container) in handler.GetInventories())
{
if (container.Type == ContainerType.Enchantment)
{
"top" => 0,
"middle" => 1,
"bottom" => 2,
_ => -1
};
if (slotId == -1)
return Translations.cmd_enchant_invalid_slot;
Container? enchantingTable = null;
foreach (var (id, container) in handler.GetInventories())
{
if (container.Type == ContainerType.Enchantment)
{
enchantingTable = container;
break;
}
enchantingTable = container;
break;
}
if (enchantingTable == null)
return Translations.cmd_enchant_enchanting_table_not_opened;
int[] emptySlots = enchantingTable.GetEmpytSlots();
if (emptySlots.Contains(0))
return Translations.cmd_enchant_enchanting_no_item;
if (emptySlots.Contains(1))
return Translations.cmd_enchant_enchanting_no_lapis;
Item lapisSlot = enchantingTable.Items[1];
if (lapisSlot.Type != ItemType.LapisLazuli)
return Translations.cmd_enchant_enchanting_no_lapis;
if (lapisSlot.Count < 3)
return Translations.cmd_enchant_enchanting_no_lapis;
EnchantmentData? enchantment = handler.GetLastEnchantments();
if (enchantment == null)
return Translations.cmd_enchant_no_enchantments;
short requiredLevel = slotId switch
{
0 => enchantment.TopEnchantmentLevelRequirement,
1 => enchantment.MiddleEnchantmentLevelRequirement,
2 => enchantment.BottomEnchantmentLevelRequirement,
_ => 9999
};
if (handler.GetLevel() < requiredLevel)
return string.Format(Translations.cmd_enchant_no_levels, handler.GetLevel(), requiredLevel);
return handler.ClickContainerButton(enchantingTable.ID, slotId) ? Translations.cmd_enchant_clicked : Translations.cmd_enchant_not_clicked;
}
return GetCmdDescTranslated();
if (enchantingTable == null)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_enchant_enchanting_table_not_opened);
int[] emptySlots = enchantingTable.GetEmpytSlots();
if (emptySlots.Contains(0))
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_enchant_enchanting_no_item);
if (emptySlots.Contains(1))
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_enchant_enchanting_no_lapis);
Item lapisSlot = enchantingTable.Items[1];
if (lapisSlot.Type != ItemType.LapisLazuli || lapisSlot.Count < 3)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_enchant_enchanting_no_lapis);
EnchantmentData? enchantment = handler.GetLastEnchantments();
if (enchantment == null)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_enchant_no_enchantments);
short requiredLevel = slotId switch
{
0 => enchantment.TopEnchantmentLevelRequirement,
1 => enchantment.MiddleEnchantmentLevelRequirement,
2 => enchantment.BottomEnchantmentLevelRequirement,
_ => 9999
};
if (handler.GetLevel() < requiredLevel)
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_enchant_no_levels, handler.GetLevel(), requiredLevel));
else
{
if (handler.ClickContainerButton(enchantingTable.ID, slotId))
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_enchant_clicked);
else
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_enchant_not_clicked);
}
}
}
}