2022-12-06 15:50:17 +08:00
|
|
|
|
using System.Linq;
|
2022-10-26 08:54:54 +08:00
|
|
|
|
using Brigadier.NET;
|
2022-12-06 15:50:17 +08:00
|
|
|
|
using Brigadier.NET.Builder;
|
|
|
|
|
|
using MinecraftClient.CommandHandler;
|
2022-10-12 19:51:01 +02:00
|
|
|
|
using MinecraftClient.Inventory;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Commands
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Enchant : Command
|
|
|
|
|
|
{
|
|
|
|
|
|
public override string CmdName { get { return "enchant"; } }
|
|
|
|
|
|
public override string CmdUsage { get { return "enchant <top|middle|bottom>"; } }
|
2022-10-28 11:13:20 +08:00
|
|
|
|
public override string CmdDesc { get { return Translations.cmd_enchant_desc; } }
|
2022-10-12 19:51:01 +02:00
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
public override void RegisterCommand(CommandDispatcher<CmdResult> dispatcher)
|
2022-10-26 08:54:54 +08:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
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")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => DoEnchant(r.Source, slotId: 0)))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("middle")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => DoEnchant(r.Source, slotId: 1)))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("bottom")
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => DoEnchant(r.Source, slotId: 2)))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("_help")
|
|
|
|
|
|
.Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName)))
|
|
|
|
|
|
);
|
2022-10-26 08:54:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
private int GetUsage(CmdResult r, string? cmd)
|
2022-10-12 19:51:01 +02:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
return r.SetAndReturn(cmd switch
|
2022-10-12 19:51:01 +02:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
#pragma warning disable format // @formatter:off
|
|
|
|
|
|
"top" => GetCmdDescTranslated(),
|
|
|
|
|
|
"middle" => GetCmdDescTranslated(),
|
|
|
|
|
|
"bottom" => GetCmdDescTranslated(),
|
|
|
|
|
|
_ => GetCmdDescTranslated(),
|
|
|
|
|
|
#pragma warning restore format // @formatter:on
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-10-12 19:51:01 +02:00
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
private int DoEnchant(CmdResult r, int slotId)
|
2022-12-06 15:50:17 +08:00
|
|
|
|
{
|
2022-12-11 16:30:45 +08:00
|
|
|
|
McClient handler = CmdResult.currentHandler!;
|
2022-12-06 15:50:17 +08:00
|
|
|
|
Container? enchantingTable = null;
|
2022-10-12 19:51:01 +02:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
foreach (var (id, container) in handler.GetInventories())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (container.Type == ContainerType.Enchantment)
|
2022-10-12 19:51:01 +02:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
enchantingTable = container;
|
|
|
|
|
|
break;
|
2022-10-12 19:51:01 +02:00
|
|
|
|
}
|
2022-12-06 15:50:17 +08:00
|
|
|
|
}
|
2022-10-12 19:51:01 +02:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
if (enchantingTable == null)
|
|
|
|
|
|
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_enchant_enchanting_table_not_opened);
|
2022-10-14 15:33:33 +02:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
int[] emptySlots = enchantingTable.GetEmpytSlots();
|
2022-10-14 15:33:33 +02:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
if (emptySlots.Contains(0))
|
|
|
|
|
|
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_enchant_enchanting_no_item);
|
2022-10-14 15:33:33 +02:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
if (emptySlots.Contains(1))
|
|
|
|
|
|
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_enchant_enchanting_no_lapis);
|
2022-10-14 15:33:33 +02:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
Item lapisSlot = enchantingTable.Items[1];
|
2022-10-17 15:14:55 +02:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
if (lapisSlot.Type != ItemType.LapisLazuli || lapisSlot.Count < 3)
|
|
|
|
|
|
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_enchant_enchanting_no_lapis);
|
2022-10-17 15:14:55 +02:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
EnchantmentData? enchantment = handler.GetLastEnchantments();
|
2022-10-17 15:14:55 +02:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
if (enchantment == null)
|
|
|
|
|
|
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_enchant_no_enchantments);
|
2022-10-17 15:14:55 +02:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
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);
|
2022-10-12 19:51:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|