|
9 | 9 | import java.util.HashMap; |
10 | 10 | import java.util.List; |
11 | 11 | import java.util.Map; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.stream.Collectors; |
12 | 14 |
|
13 | 15 | public class TierManager { |
14 | 16 |
|
15 | | - public static final Map<TagKey<Block>, PickaxeItem> TAG_LOOK_UP = new HashMap<>(); |
| 17 | + private static final Map<TagKey<Block>, List<PickaxeItem>> TAG_LOOK_UP = new HashMap<>(); |
16 | 18 |
|
17 | 19 | public static void setup() { |
18 | 20 | TAG_LOOK_UP.clear(); |
| 21 | + |
19 | 22 | List<PickaxeItem> pickaxeItems = GeneratedMaterialManager.getRegistry().stream() |
20 | 23 | .filter(PickaxeItem.class::isInstance) |
21 | 24 | .map(PickaxeItem.class::cast) |
22 | | - .filter(pickaxe -> pickaxe.getTier() != null) |
23 | | - .filter(pickaxe -> pickaxe.getTier().getIncorrectBlocksForDrops() != null) |
| 25 | + .filter(p -> p.getTier() != null && p.getTier().getIncorrectBlocksForDrops() != null) |
24 | 26 | .toList(); |
25 | 27 |
|
26 | | - for (PickaxeItem pickaxe : pickaxeItems) { |
27 | | - TagKey<Block> tagKey = pickaxe.getTier().getIncorrectBlocksForDrops(); |
28 | | - if (tagKey != null) { |
29 | | - TAG_LOOK_UP.put(tagKey, pickaxe); |
30 | | - } |
| 28 | + TAG_LOOK_UP.putAll( |
| 29 | + pickaxeItems.stream() |
| 30 | + .collect(Collectors.groupingBy( |
| 31 | + p -> p.getTier().getIncorrectBlocksForDrops() |
| 32 | + )) |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the preferred pickaxe for this tag, preferring Mojang tools. |
| 38 | + */ |
| 39 | + public static Optional<PickaxeItem> getPreferredPickaxe(TagKey<Block> tag) { |
| 40 | + List<PickaxeItem> candidates = TAG_LOOK_UP.get(tag); |
| 41 | + if (candidates == null || candidates.isEmpty()) { |
| 42 | + return Optional.empty(); |
31 | 43 | } |
| 44 | + |
| 45 | + // prefer namespace "minecraft" |
| 46 | + return candidates.stream() |
| 47 | + .sorted((a, b) -> { |
| 48 | + boolean aVanilla = isMojangItem(a); |
| 49 | + boolean bVanilla = isMojangItem(b); |
| 50 | + if (aVanilla && !bVanilla) return -1; |
| 51 | + if (!aVanilla && bVanilla) return 1; |
| 52 | + return a.getName(a.getDefaultInstance()).getString() |
| 53 | + .compareToIgnoreCase(b.getName(b.getDefaultInstance()).getString()); |
| 54 | + }) |
| 55 | + .findFirst(); |
| 56 | + } |
| 57 | + |
| 58 | + public static List<PickaxeItem> getAllPickaxes(TagKey<Block> tag) { |
| 59 | + return TAG_LOOK_UP.getOrDefault(tag, List.of()); |
| 60 | + } |
| 61 | + |
| 62 | + private static boolean isMojangItem(PickaxeItem item) { |
| 63 | + // for 1.20+, use item.builtInRegistryHolder().key().location().getNamespace() |
| 64 | + return item.arch$registryName() != null |
| 65 | + && "minecraft".equals(item.arch$registryName().getNamespace()); |
32 | 66 | } |
33 | 67 |
|
34 | 68 |
|
|
0 commit comments