Skip to content

Commit

Permalink
feat(plugin): improve ItemUtils#getTranslation and add 1.20.x suppo…
Browse files Browse the repository at this point in the history
…rt (#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
  • Loading branch information
iGabyTM authored Jun 23, 2023
1 parent 370c6fd commit 85a82b9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[versions]
# Minecraft
spigot = "1.19.3-R0.1-SNAPSHOT"
spigot = "1.20.1-R0.1-SNAPSHOT"

# Adventure
minimessage = "4.11.0"
adventure-platform = "4.2.0"
minimessage = "4.14.0"
adventure-platform = "4.3.0"

# Other
configurate = "4.1.2"
Expand Down
52 changes: 50 additions & 2 deletions plugin/src/main/java/at/helpch/chatchat/util/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,68 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public final class ItemUtils {

private static final LegacyComponentSerializer LEGACY_COMPONENT_SERIALIZER = LegacyComponentSerializer.legacySection();
private static Method translationKeyMethod;
private static final Map<Predicate<Material>, Function<Material, String>> translations = new LinkedHashMap<>();

static {
if (VersionHelper.IS_PAPER) {
try {
//noinspection JavaReflectionMemberAccess
ItemUtils.translationKeyMethod = Material.class.getMethod("translationKey"); // paper method
} catch (NoSuchMethodException ignored) {
}
}

translations.put(
__ -> VersionHelper.IS_PAPER && translationKeyMethod != null,
material -> {
try {
return (String) translationKeyMethod.invoke(material);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
return "null." + material.getKey().getKey();
}
}
);
translations.put(
material -> VersionHelper.HAS_SMITHING_TEMPLATE && material.name().endsWith("SMITHING_TEMPLATE"),
__ -> "item.minecraft.smithing_template"
);
translations.put(
Material::isItem,
material -> "item.minecraft." + material.getKey().getKey()
);
translations.put(
Material::isBlock,
material -> "block.minecraft." + material.getKey().getKey()
);
}

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

private static @NotNull Component getTranslation(@NotNull final Material material) {
final var type = material.isBlock() ? "block" : "item";
return Component.translatable(String.format("%s.minecraft.%s", type, material.getKey().getKey()));
for (final var entry : translations.entrySet()) {
if (entry.getKey().test(material)) {
return Component.translatable(entry.getValue().apply(material));
}
}

return Component.translatable("null." + material.getKey().getKey());
}

public static @NotNull TagResolver.@NotNull Single createItemPlaceholder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public final class VersionHelper {
public static final int V1_18_2 = 1182;
public static final int V1_19_0 = 1190;

public static final int V1_20_0 = 1200;

public static boolean HAS_SMITHING_TEMPLATE = CURRENT_VERSION >= V1_20_0;

/**
* Check if the server has access to the Paper API
* Taken from <a href="https://github.com/PaperMC/PaperLib">PaperLib</a>
Expand Down

0 comments on commit 85a82b9

Please sign in to comment.