Skip to content

Commit 05ff66f

Browse files
Update for 26.1.X
1 parent 286269a commit 05ff66f

3 files changed

Lines changed: 59 additions & 59 deletions

File tree

src/main/java/meteordevelopment/meteorclient/commands/arguments/EnchantmentLevelArgumentType.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
1313
import com.mojang.brigadier.suggestion.Suggestions;
1414
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
15-
import net.minecraft.enchantment.Enchantment;
16-
import net.minecraft.registry.entry.RegistryEntry;
17-
import net.minecraft.text.Text;
15+
import net.minecraft.core.Holder;
16+
import net.minecraft.network.chat.Component;
17+
import net.minecraft.world.item.enchantment.Enchantment;
1818

1919
import java.util.Collection;
2020
import java.util.List;
2121
import java.util.concurrent.CompletableFuture;
2222

2323
public class EnchantmentLevelArgumentType implements ArgumentType<Integer> {
24-
private static final SimpleCommandExceptionType INVALID_LEVEL = new SimpleCommandExceptionType(Text.literal("Level must be at least 1"));
24+
private static final SimpleCommandExceptionType INVALID_LEVEL = new SimpleCommandExceptionType(Component.literal("Level must be at least 1"));
2525
private final String enchantmentArgName;
2626

2727
public EnchantmentLevelArgumentType(String enchantmentArgName) {
@@ -49,7 +49,7 @@ public Integer parse(StringReader reader) throws CommandSyntaxException {
4949
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
5050
try {
5151
// Try to get the enchantment from the previous argument
52-
RegistryEntry.Reference<Enchantment> enchantment =
52+
Holder.Reference<Enchantment> enchantment =
5353
RegistryEntryReferenceArgumentType.getEnchantment(context, enchantmentArgName);
5454

5555
int maxLevel = enchantment.value().getMaxLevel();
@@ -62,11 +62,11 @@ public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> cont
6262
int typedLevel = Integer.parseInt(remaining);
6363
if (typedLevel > maxLevel) {
6464
// Show error in suggestions
65-
builder.suggest(maxLevel, Text.literal("§c" + enchantName + " max level: " + maxLevel));
65+
builder.suggest(maxLevel, Component.literal("§c" + enchantName + " max level: " + maxLevel));
6666
}
6767

6868
return builder.buildFuture();
69-
} catch (NumberFormatException ignored) {
69+
} catch (NumberFormatException _) {
7070
// Command handler highlights invalid input
7171
}
7272
}
@@ -78,7 +78,7 @@ public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> cont
7878
}
7979

8080
return builder.buildFuture();
81-
} catch (Exception e) {
81+
} catch (Exception _) {
8282
return Suggestions.empty();
8383
}
8484
}

src/main/java/meteordevelopment/meteorclient/commands/commands/OptimizeEnchantsCommand.java

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
import meteordevelopment.meteorclient.commands.arguments.RegistryEntryReferenceArgumentType;
1515
import meteordevelopment.meteorclient.utils.misc.EnchantmentOptimizer;
1616
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;
17+
import net.minecraft.ChatFormatting;
18+
import net.minecraft.client.multiplayer.ClientSuggestionProvider;
19+
import net.minecraft.commands.arguments.item.ItemArgument;
20+
import net.minecraft.core.Holder;
21+
import net.minecraft.core.component.DataComponents;
22+
import net.minecraft.core.registries.Registries;
23+
import net.minecraft.network.chat.Component;
24+
import net.minecraft.network.chat.MutableComponent;
25+
import net.minecraft.world.item.Item;
26+
import net.minecraft.world.item.enchantment.Enchantment;
27+
2628

2729
import java.util.ArrayList;
2830
import java.util.List;
@@ -33,14 +35,14 @@ public OptimizeEnchantsCommand() {
3335
}
3436

3537
@Override
36-
public void build(LiteralArgumentBuilder<CommandSource> builder) {
38+
public void build(LiteralArgumentBuilder<ClientSuggestionProvider> builder) {
3739
// TODO: The optimizer supports book-only mode (item=null) for combining enchanted books,
3840
// but this command currently requires an item argument, so item will never be null.
3941
// Should we add an item-less version of the command for book-only optimization?
4042

4143
// TODO: should we restrict the available items to only those that can be enchanted?
4244
// e.g. armors, weapons, tools, books, etc.
43-
builder.then(argument("item", ItemStackArgumentType.itemStack(REGISTRY_ACCESS))
45+
builder.then(argument("item", ItemArgument.item(REGISTRY_ACCESS))
4446
.then(buildEnchantmentChain(1, 20))
4547
// TODO: what should the max depth be? Idk how many enchantments on a single item MC supports.
4648
);
@@ -50,7 +52,7 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
5052
* Recursively builds a chain of enchantment arguments.
5153
* Each enchantment requires a name and level, and can optionally chain to the next.
5254
*/
53-
private RequiredArgumentBuilder<CommandSource, ?> buildEnchantmentChain(int index, int maxDepth) {
55+
private RequiredArgumentBuilder<ClientSuggestionProvider, ?> buildEnchantmentChain(int index, int maxDepth) {
5456
String enchantArg = "enchantment" + index;
5557
String levelArg = "level" + index;
5658

@@ -71,7 +73,7 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
7173
/**
7274
* Extracts all enchantments from context and runs optimization.
7375
*/
74-
private void executeOptimization(CommandContext<CommandSource> context, int enchantmentCount) {
76+
private void executeOptimization(CommandContext<ClientSuggestionProvider> context, int enchantmentCount) {
7577
try {
7678
Item item = getItem(context);
7779
List<EnchantmentOptimizer.EnchantmentEntry> enchants = new ArrayList<>();
@@ -81,7 +83,7 @@ private void executeOptimization(CommandContext<CommandSource> context, int ench
8183
String levelArg = "level" + i;
8284

8385
try {
84-
RegistryEntry.Reference<Enchantment> enchantment =
86+
Holder.Reference<Enchantment> enchantment =
8587
RegistryEntryReferenceArgumentType.getEnchantment(context, enchantArg);
8688
int level = IntegerArgumentType.getInteger(context, levelArg);
8789

@@ -96,7 +98,7 @@ private void executeOptimization(CommandContext<CommandSource> context, int ench
9698
}
9799

98100
enchants.add(new EnchantmentOptimizer.EnchantmentEntry(enchantment, level));
99-
} catch (IllegalArgumentException e) {
101+
} catch (IllegalArgumentException _) {
100102
// Argument doesn't exist, we've reached the end
101103
break;
102104
}
@@ -113,23 +115,23 @@ private void executeOptimization(CommandContext<CommandSource> context, int ench
113115
}
114116
}
115117

116-
private Item getItem(CommandContext<CommandSource> context) {
118+
private Item getItem(CommandContext<ClientSuggestionProvider> context) {
117119
try {
118-
var itemArg = ItemStackArgumentType.getItemStackArgument(context, "item");
119-
return itemArg.getItem();
120-
} catch (Exception e) {
120+
var itemArg = ItemArgument.getItem(context, "item");
121+
return itemArg.item().value();
122+
} catch (Exception _) {
121123
return null;
122124
}
123125
}
124126

125127
private void optimize(Item item, List<EnchantmentOptimizer.EnchantmentEntry> enchants) {
126128
try {
127129
// Create optimizer from current registry
128-
var registry = mc.getNetworkHandler().getRegistryManager().getOrThrow(RegistryKeys.ENCHANTMENT);
130+
var registry = mc.getConnection().registryAccess().lookupOrThrow(Registries.ENCHANTMENT);
129131
EnchantmentOptimizer.OptimizationResult result = EnchantmentOptimizer.create(registry).optimize(item, enchants);
130132

131133
// Display header
132-
String itemName = item != null ? item.getName().getString() : "Book";
134+
String itemName = item != null ? item.components().get(DataComponents.ITEM_NAME).getString() : "Book";
133135
ChatUtils.info("=== Enchantment Optimization for %s ===", itemName);
134136
ChatUtils.info("Total Cost: (highlight)%d levels(default) (%d XP)", result.totalLevels(), result.totalXp());
135137

@@ -144,11 +146,11 @@ private void optimize(Item item, List<EnchantmentOptimizer.EnchantmentEntry> enc
144146
for (int i = 0; i < result.instructions().size(); i++) {
145147
EnchantmentOptimizer.Instruction instr = result.instructions().get(i);
146148

147-
MutableText stepText = Text.literal(String.format(" %d. ", i + 1)).formatted(Formatting.GRAY);
148-
stepText.append(Text.literal("Combine ").formatted(Formatting.GRAY));
149-
stepText.append(formatItem(instr.left()).copy().formatted(Formatting.YELLOW));
150-
stepText.append(Text.literal(" with ").formatted(Formatting.GRAY));
151-
stepText.append(formatItem(instr.right()).copy().formatted(Formatting.AQUA));
149+
MutableComponent stepText = Component.literal(String.format(" %d. ", i + 1)).withStyle(ChatFormatting.GRAY);
150+
stepText.append(Component.literal("Combine ").withStyle(ChatFormatting.GRAY));
151+
stepText.append(formatItem(instr.left()).copy().withStyle(ChatFormatting.YELLOW));
152+
stepText.append(Component.literal(" with ").withStyle(ChatFormatting.GRAY));
153+
stepText.append(formatItem(instr.right()).copy().withStyle(ChatFormatting.AQUA));
152154

153155
ChatUtils.sendMsg(stepText);
154156

@@ -164,18 +166,18 @@ private void optimize(Item item, List<EnchantmentOptimizer.EnchantmentEntry> enc
164166
}
165167
}
166168

167-
private Text formatItem(EnchantmentOptimizer.Combination comb) {
169+
private Component formatItem(EnchantmentOptimizer.Combination comb) {
168170
String baseName = comb.baseItem != null
169-
? comb.baseItem.getName().getString()
171+
? comb.baseItem.components().get(DataComponents.ITEM_NAME).getString()
170172
: "Book";
171173

172174
List<String> enchantments = new ArrayList<>();
173175
collectEnchantments(comb, enchantments);
174176

175-
if (enchantments.isEmpty()) return Text.literal(baseName);
177+
if (enchantments.isEmpty()) return Component.literal(baseName);
176178

177179
// Format: "ItemName (ench1, ench2, ench3)"
178-
return Text.literal(baseName + " (" + String.join(", ", enchantments) + ")");
180+
return Component.literal(baseName + " (" + String.join(", ", enchantments) + ")");
179181
}
180182

181183
private void collectEnchantments(EnchantmentOptimizer.Combination comb, List<String> out) {

src/main/java/meteordevelopment/meteorclient/utils/misc/EnchantmentOptimizer.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import it.unimi.dsi.fastutil.ints.*;
99
import it.unimi.dsi.fastutil.objects.Object2IntMap;
1010
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
11-
import net.minecraft.enchantment.Enchantment;
12-
import net.minecraft.item.Item;
13-
import net.minecraft.registry.Registry;
14-
import net.minecraft.registry.entry.RegistryEntry;
11+
import net.minecraft.core.Holder;
12+
import net.minecraft.core.Registry;
13+
import net.minecraft.world.item.Item;
14+
import net.minecraft.world.item.enchantment.Enchantment;
1515
import org.jspecify.annotations.NullMarked;
1616
import org.jspecify.annotations.Nullable;
1717

@@ -22,17 +22,17 @@
2222
public class EnchantmentOptimizer {
2323
private static final int MAXIMUM_MERGE_LEVELS = 39;
2424

25-
private final Object2IntMap<RegistryEntry<Enchantment>> enchantmentIds;
25+
private final Object2IntMap<Holder<Enchantment>> enchantmentIds;
2626
private final int[] enchantmentWeights;
2727
private final Map<ResultKey, Int2ObjectMap<ItemObject>> memoCache;
2828

29-
public EnchantmentOptimizer(List<RegistryEntry<Enchantment>> enchantments) {
29+
public EnchantmentOptimizer(List<Holder<Enchantment>> enchantments) {
3030
this.enchantmentIds = new Object2IntOpenHashMap<>();
3131
this.enchantmentWeights = new int[enchantments.size()];
3232
this.memoCache = new HashMap<>();
3333

3434
int id = 0;
35-
for (RegistryEntry<Enchantment> entry : enchantments) {
35+
for (Holder<Enchantment> entry : enchantments) {
3636
enchantmentIds.put(entry, id);
3737
int anvilCost = entry.value().getAnvilCost();
3838

@@ -48,7 +48,7 @@ public EnchantmentOptimizer(List<RegistryEntry<Enchantment>> enchantments) {
4848
}
4949
}
5050

51-
public record EnchantmentEntry(RegistryEntry<Enchantment> enchantment, int level) {
51+
public record EnchantmentEntry(Holder<Enchantment> enchantment, int level) {
5252
}
5353

5454
public OptimizationResult optimize(@Nullable Item item, List<EnchantmentEntry> enchants) {
@@ -59,7 +59,7 @@ public OptimizationResult optimize(@Nullable Item item, List<EnchantmentEntry> e
5959
.map(e -> {
6060
int id = enchantmentIds.getOrDefault(e.enchantment(), -1);
6161
if (id == -1) {
62-
throw new IllegalArgumentException("Unknown enchantment: " + e.enchantment().getKey().orElseThrow().getValue());
62+
throw new IllegalArgumentException("Unknown enchantment: " + e.enchantment().unwrapKey().orElseThrow().identifier());
6363
}
6464
int value = e.level() * enchantmentWeights[id];
6565
IntList ids = IntLists.singleton(id);
@@ -157,13 +157,13 @@ private ItemObject cheapestItemFromItems(ItemObject left, ItemObject right) {
157157

158158
try {
159159
normal = new MergeEnchants(left, right);
160-
} catch (MergeLevelsTooExpensiveException ignored) {
160+
} catch (MergeLevelsTooExpensiveException _) {
161161
// Ignore too expensive merges
162162
}
163163

164164
try {
165165
reversed = new MergeEnchants(right, left);
166-
} catch (MergeLevelsTooExpensiveException ignored) {
166+
} catch (MergeLevelsTooExpensiveException _) {
167167
// Ignore too expensive merges
168168
}
169169

@@ -224,7 +224,7 @@ private Int2ObjectMap<ItemObject> cheapestItemsFromDictionaries(
224224
for (Int2ObjectMap.Entry<ItemObject> entry : newWork2Item.int2ObjectEntrySet()) {
225225
cheapest.merge(entry.getIntKey(), entry.getValue(), this::compareCheapest);
226226
}
227-
} catch (MergeLevelsTooExpensiveException ignored) {
227+
} catch (MergeLevelsTooExpensiveException _) {
228228
// Ignore too expensive merges
229229
}
230230
}
@@ -321,12 +321,10 @@ public static class ItemObject {
321321

322322
public static class MergeEnchants extends ItemObject {
323323
MergeEnchants(ItemObject left, ItemObject right) {
324-
super(left.type, left.value + right.value, new IntArrayList());
325-
326324
int mergeCost = right.value + (1 << left.priorWork) - 1 + (1 << right.priorWork) - 1;
327-
if (mergeCost > MAXIMUM_MERGE_LEVELS) {
328-
throw new MergeLevelsTooExpensiveException();
329-
}
325+
if (mergeCost > MAXIMUM_MERGE_LEVELS) throw new MergeLevelsTooExpensiveException();
326+
327+
super(left.type, left.value + right.value, new IntArrayList());
330328

331329
this.enchantIds.addAll(left.enchantIds);
332330
this.enchantIds.addAll(right.enchantIds);
@@ -344,7 +342,7 @@ public static class Combination {
344342
public int value; // Total value (sum of all books)
345343
public @Nullable Item item; // For base item (only set on leaf item nodes)
346344
public @Nullable Item baseItem; // The base item for this combination tree (null = book)
347-
public @Nullable RegistryEntry<Enchantment> enchantment; // For enchanted book
345+
public @Nullable Holder<Enchantment> enchantment; // For enchanted book
348346
public int level;
349347

350348
Combination() {
@@ -358,7 +356,7 @@ public static class Combination {
358356
}
359357

360358
// For leaf book nodes
361-
Combination(RegistryEntry<Enchantment> enchantment, int level, int value) {
359+
Combination(Holder<Enchantment> enchantment, int level, int value) {
362360
this.left = null;
363361
this.right = null;
364362
this.item = null;
@@ -455,8 +453,8 @@ private static class MergeLevelsTooExpensiveException extends RuntimeException {
455453

456454
// Static factory method to create optimizer from registry
457455
public static EnchantmentOptimizer create(Registry<Enchantment> registry) {
458-
// streamEntries returns RegistryEntry.Reference, which extends RegistryEntry
459-
List<RegistryEntry<Enchantment>> enchantments = new ArrayList<>(registry.streamEntries().toList());
456+
// listElements returns Holder.Reference, which extends Holder
457+
List<Holder<Enchantment>> enchantments = new ArrayList<>(registry.listElements().toList());
460458
return new EnchantmentOptimizer(enchantments);
461459
}
462460
}

0 commit comments

Comments
 (0)