Skip to content

Commit 85a82b9

Browse files
authored
feat(plugin): improve ItemUtils#getTranslation and add 1.20.x support (#210)
* feat(plugin): improve `ItemUtils#getTranslation` and add 1.20.x support * feat: update adventure minimessage 4.11.0 -> 4.14.0 platform 4.2.0 -> 4.3.0
1 parent 370c6fd commit 85a82b9

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

gradle/libs.versions.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[versions]
22
# Minecraft
3-
spigot = "1.19.3-R0.1-SNAPSHOT"
3+
spigot = "1.20.1-R0.1-SNAPSHOT"
44

55
# Adventure
6-
minimessage = "4.11.0"
7-
adventure-platform = "4.2.0"
6+
minimessage = "4.14.0"
7+
adventure-platform = "4.3.0"
88

99
# Other
1010
configurate = "4.1.2"

plugin/src/main/java/at/helpch/chatchat/util/ItemUtils.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,68 @@
1414
import org.jetbrains.annotations.NotNull;
1515
import org.jetbrains.annotations.Nullable;
1616

17+
import java.lang.reflect.InvocationTargetException;
18+
import java.lang.reflect.Method;
1719
import java.util.ArrayList;
1820
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;
1925
import java.util.stream.Collectors;
2026

2127
public final class ItemUtils {
28+
2229
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+
}
2366

2467
private ItemUtils() {
2568
throw new AssertionError("Util classes are not to be instantiated!");
2669
}
2770

2871
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());
3179
}
3280

3381
public static @NotNull TagResolver.@NotNull Single createItemPlaceholder(

plugin/src/main/java/at/helpch/chatchat/util/VersionHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public final class VersionHelper {
2828
public static final int V1_18_2 = 1182;
2929
public static final int V1_19_0 = 1190;
3030

31+
public static final int V1_20_0 = 1200;
32+
33+
public static boolean HAS_SMITHING_TEMPLATE = CURRENT_VERSION >= V1_20_0;
34+
3135
/**
3236
* Check if the server has access to the Paper API
3337
* Taken from <a href="https://github.com/PaperMC/PaperLib">PaperLib</a>

0 commit comments

Comments
 (0)