|
| 1 | +/* |
| 2 | + * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client). |
| 3 | + * Copyright (c) Meteor Development. |
| 4 | + */ |
| 5 | + |
| 6 | +package meteordevelopment.meteorclient.commands.commands; |
| 7 | + |
| 8 | +import com.mojang.brigadier.arguments.IntegerArgumentType; |
| 9 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 10 | +import com.mojang.brigadier.builder.RequiredArgumentBuilder; |
| 11 | +import com.mojang.brigadier.context.CommandContext; |
| 12 | +import meteordevelopment.meteorclient.commands.Command; |
| 13 | +import meteordevelopment.meteorclient.commands.arguments.EnchantmentLevelArgumentType; |
| 14 | +import meteordevelopment.meteorclient.commands.arguments.RegistryEntryReferenceArgumentType; |
| 15 | +import meteordevelopment.meteorclient.utils.misc.EnchantmentOptimizer; |
| 16 | +import meteordevelopment.meteorclient.utils.player.ChatUtils; |
| 17 | +import net.minecraft.command.CommandSource; |
| 18 | +import net.minecraft.command.argument.ItemStackArgumentType; |
| 19 | +import net.minecraft.enchantment.Enchantment; |
| 20 | +import net.minecraft.item.Item; |
| 21 | +import net.minecraft.registry.RegistryKeys; |
| 22 | +import net.minecraft.registry.entry.RegistryEntry; |
| 23 | +import net.minecraft.text.MutableText; |
| 24 | +import net.minecraft.text.Text; |
| 25 | +import net.minecraft.util.Formatting; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +public class EnchantOptimizeCommand extends Command { |
| 31 | + |
| 32 | + public EnchantOptimizeCommand() { |
| 33 | + super("enchant-optimize", "Calculates the optimal order to apply enchantments for minimum XP cost.", "eopt"); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void build(LiteralArgumentBuilder<CommandSource> builder) { |
| 38 | + // TODO: The optimizer supports book-only mode (item=null) for combining enchanted books, |
| 39 | + // but this command currently requires an item argument, so item will never be null. |
| 40 | + // Should we add an item-less version of the command for book-only optimization? |
| 41 | + |
| 42 | + // TODO: should we restrict the available items to only those that can be enchanted? |
| 43 | + // e.g. armors, weapons, tools, books, etc. |
| 44 | + builder.then(argument("item", ItemStackArgumentType.itemStack(REGISTRY_ACCESS)) |
| 45 | + .then(buildEnchantmentChain(1, 20)) |
| 46 | + // TODO: what should the max depth be? Idk how many enchantments on a single item MC supports. |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Recursively builds a chain of enchantment arguments. |
| 52 | + * Each enchantment requires a name and level, and can optionally chain to the next. |
| 53 | + */ |
| 54 | + private RequiredArgumentBuilder<CommandSource, ?> buildEnchantmentChain(int index, int maxDepth) { |
| 55 | + String enchantArg = "enchantment" + index; |
| 56 | + String levelArg = "level" + index; |
| 57 | + |
| 58 | + var enchantmentArg = argument(enchantArg, RegistryEntryReferenceArgumentType.enchantment()); |
| 59 | + var levelArgBuilder = argument(levelArg, EnchantmentLevelArgumentType.enchantmentLevel(enchantArg)) |
| 60 | + .executes(context -> { |
| 61 | + executeOptimization(context, index); |
| 62 | + return SINGLE_SUCCESS; |
| 63 | + }); |
| 64 | + |
| 65 | + if (index < maxDepth) { |
| 66 | + levelArgBuilder.then(buildEnchantmentChain(index + 1, maxDepth)); |
| 67 | + } |
| 68 | + |
| 69 | + return enchantmentArg.then(levelArgBuilder); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Extracts all enchantments from context and runs optimization. |
| 74 | + */ |
| 75 | + private void executeOptimization(CommandContext<CommandSource> context, int enchantmentCount) { |
| 76 | + try { |
| 77 | + Item item = getItem(context); |
| 78 | + List<EnchantmentOptimizer.EnchantmentEntry> enchants = new ArrayList<>(); |
| 79 | + |
| 80 | + for (int i = 1; i <= enchantmentCount; i++) { |
| 81 | + String enchantArg = "enchantment" + i; |
| 82 | + String levelArg = "level" + i; |
| 83 | + |
| 84 | + try { |
| 85 | + RegistryEntry.Reference<Enchantment> enchantment = |
| 86 | + RegistryEntryReferenceArgumentType.getEnchantment(context, enchantArg); |
| 87 | + int level = IntegerArgumentType.getInteger(context, levelArg); |
| 88 | + |
| 89 | + // TODO: keep validation or allow arbitary levels? MC supports up to 255. |
| 90 | + // Validate level against enchantment's max level |
| 91 | + int maxLevel = enchantment.value().getMaxLevel(); |
| 92 | + if (level > maxLevel) { |
| 93 | + String enchantName = enchantment.value().description().getString(); |
| 94 | + error("Enchantment (highlight)%s(default) has max level (highlight)%d(default), but you specified (highlight)%d(default).", |
| 95 | + enchantName, maxLevel, level); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + enchants.add(new EnchantmentOptimizer.EnchantmentEntry(enchantment, level)); |
| 100 | + } catch (IllegalArgumentException e) { |
| 101 | + // Argument doesn't exist, we've reached the end |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (enchants.isEmpty()) { |
| 107 | + error("No enchantments specified."); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + optimize(item, enchants); |
| 112 | + } catch (Exception e) { |
| 113 | + error("Failed to parse enchantments: %s", e.getMessage()); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private Item getItem(CommandContext<CommandSource> context) { |
| 118 | + try { |
| 119 | + var itemArg = ItemStackArgumentType.getItemStackArgument(context, "item"); |
| 120 | + return itemArg.getItem(); |
| 121 | + } catch (Exception e) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private void optimize(Item item, List<EnchantmentOptimizer.EnchantmentEntry> enchants) { |
| 127 | + try { |
| 128 | + // Create optimizer from current registry |
| 129 | + var registry = mc.getNetworkHandler().getRegistryManager().getOrThrow(RegistryKeys.ENCHANTMENT); |
| 130 | + EnchantmentOptimizer.OptimizationResult result = EnchantmentOptimizer.create(registry).optimize(item, enchants); |
| 131 | + |
| 132 | + // Display header |
| 133 | + String itemName = item != null ? item.getName().getString() : "Book"; |
| 134 | + ChatUtils.info("=== Enchantment Optimization for %s ===", itemName); |
| 135 | + info("Total Cost: (highlight)%d levels(default) (%d XP)", result.totalLevels(), result.totalXp()); |
| 136 | + |
| 137 | + if (result.instructions().isEmpty()) { |
| 138 | + info("No combinations needed - single enchantment only."); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + info("Steps:"); |
| 143 | + |
| 144 | + // Display steps |
| 145 | + for (int i = 0; i < result.instructions().size(); i++) { |
| 146 | + EnchantmentOptimizer.Instruction instr = result.instructions().get(i); |
| 147 | + |
| 148 | + MutableText stepText = Text.literal(String.format(" %d. ", i + 1)).formatted(Formatting.GRAY); |
| 149 | + stepText.append(Text.literal("Combine ").formatted(Formatting.GRAY)); |
| 150 | + stepText.append(formatItem(instr.left()).copy().formatted(Formatting.YELLOW)); |
| 151 | + stepText.append(Text.literal(" with ").formatted(Formatting.GRAY)); |
| 152 | + stepText.append(formatItem(instr.right()).copy().formatted(Formatting.AQUA)); |
| 153 | + |
| 154 | + ChatUtils.sendMsg(stepText); |
| 155 | + |
| 156 | + info(" Cost: (highlight)%d levels(default) (%d XP), Prior Work Penalty: %d", |
| 157 | + instr.levels(), |
| 158 | + instr.xp(), |
| 159 | + instr.priorWorkPenalty() |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + } catch (Exception e) { |
| 164 | + error("Failed to optimize enchantments: %s", e.getMessage()); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private Text formatItem(EnchantmentOptimizer.Combination comb) { |
| 169 | + if (comb.item != null) { |
| 170 | + return comb.item.getName(); |
| 171 | + } |
| 172 | + |
| 173 | + if (comb.enchantment != null) { |
| 174 | + String enchName = comb.enchantment.value().description().getString(); |
| 175 | + String level = romanNumeral(comb.level); |
| 176 | + return Text.literal(enchName + " " + level + " Book"); |
| 177 | + } |
| 178 | + |
| 179 | + return Text.literal("Combined Item"); |
| 180 | + } |
| 181 | + |
| 182 | + private String romanNumeral(int num) { |
| 183 | + return switch (num) { |
| 184 | + case 1 -> "I"; |
| 185 | + case 2 -> "II"; |
| 186 | + case 3 -> "III"; |
| 187 | + case 4 -> "IV"; |
| 188 | + case 5 -> "V"; |
| 189 | + case 6 -> "VI"; |
| 190 | + case 7 -> "VII"; |
| 191 | + case 8 -> "VIII"; |
| 192 | + case 9 -> "IX"; |
| 193 | + case 10 -> "X"; |
| 194 | + default -> String.valueOf(num); |
| 195 | + }; |
| 196 | + } |
| 197 | +} |
0 commit comments