|
14 | 14 | import org.jetbrains.annotations.NotNull;
|
15 | 15 | import org.jetbrains.annotations.Nullable;
|
16 | 16 |
|
| 17 | +import java.lang.reflect.InvocationTargetException; |
| 18 | +import java.lang.reflect.Method; |
17 | 19 | import java.util.ArrayList;
|
18 | 20 | import java.util.Collections;
|
| 21 | +import java.util.LinkedHashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.Function; |
| 24 | +import java.util.function.Predicate; |
19 | 25 | import java.util.stream.Collectors;
|
20 | 26 |
|
21 | 27 | public final class ItemUtils {
|
| 28 | + |
22 | 29 | private static final LegacyComponentSerializer LEGACY_COMPONENT_SERIALIZER = LegacyComponentSerializer.legacySection();
|
| 30 | + private static Method translationKeyMethod; |
| 31 | + private static final Map<Predicate<Material>, Function<Material, String>> translations = new LinkedHashMap<>(); |
| 32 | + |
| 33 | + static { |
| 34 | + if (VersionHelper.IS_PAPER) { |
| 35 | + try { |
| 36 | + //noinspection JavaReflectionMemberAccess |
| 37 | + ItemUtils.translationKeyMethod = Material.class.getMethod("translationKey"); // paper method |
| 38 | + } catch (NoSuchMethodException ignored) { |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + translations.put( |
| 43 | + __ -> VersionHelper.IS_PAPER && translationKeyMethod != null, |
| 44 | + material -> { |
| 45 | + try { |
| 46 | + return (String) translationKeyMethod.invoke(material); |
| 47 | + } catch (InvocationTargetException | IllegalAccessException e) { |
| 48 | + e.printStackTrace(); |
| 49 | + return "null." + material.getKey().getKey(); |
| 50 | + } |
| 51 | + } |
| 52 | + ); |
| 53 | + translations.put( |
| 54 | + material -> VersionHelper.HAS_SMITHING_TEMPLATE && material.name().endsWith("SMITHING_TEMPLATE"), |
| 55 | + __ -> "item.minecraft.smithing_template" |
| 56 | + ); |
| 57 | + translations.put( |
| 58 | + Material::isItem, |
| 59 | + material -> "item.minecraft." + material.getKey().getKey() |
| 60 | + ); |
| 61 | + translations.put( |
| 62 | + Material::isBlock, |
| 63 | + material -> "block.minecraft." + material.getKey().getKey() |
| 64 | + ); |
| 65 | + } |
23 | 66 |
|
24 | 67 | private ItemUtils() {
|
25 | 68 | throw new AssertionError("Util classes are not to be instantiated!");
|
26 | 69 | }
|
27 | 70 |
|
28 | 71 | private static @NotNull Component getTranslation(@NotNull final Material material) {
|
29 |
| - final var type = material.isBlock() ? "block" : "item"; |
30 |
| - return Component.translatable(String.format("%s.minecraft.%s", type, material.getKey().getKey())); |
| 72 | + for (final var entry : translations.entrySet()) { |
| 73 | + if (entry.getKey().test(material)) { |
| 74 | + return Component.translatable(entry.getValue().apply(material)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return Component.translatable("null." + material.getKey().getKey()); |
31 | 79 | }
|
32 | 80 |
|
33 | 81 | public static @NotNull TagResolver.@NotNull Single createItemPlaceholder(
|
|
0 commit comments