From 663e3af7c8f10e5ae9e4fc5d76ad32b8730b26c9 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Fri, 26 Jul 2024 17:22:10 -0400 Subject: [PATCH] Fix non-block items in stonecutters Fixes #4845 --- .../geysermc/geyser/item/type/BlockItem.java | 13 ++++++++++++ .../org/geysermc/geyser/item/type/Item.java | 11 +++++++--- .../java/JavaUpdateRecipesTranslator.java | 3 ++- .../geysermc/geyser/util/StatisticsUtils.java | 20 +++++++------------ 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/item/type/BlockItem.java b/core/src/main/java/org/geysermc/geyser/item/type/BlockItem.java index 30a31a100..c57e58469 100644 --- a/core/src/main/java/org/geysermc/geyser/item/type/BlockItem.java +++ b/core/src/main/java/org/geysermc/geyser/item/type/BlockItem.java @@ -28,6 +28,9 @@ package org.geysermc.geyser.item.type; import org.geysermc.geyser.level.block.type.Block; public class BlockItem extends Item { + // If item is instanceof ItemNameBlockItem + private final boolean treatLikeBlock; + public BlockItem(Builder builder, Block block, Block... otherBlocks) { super(block.javaIdentifier().value(), builder); @@ -36,6 +39,7 @@ public class BlockItem extends Item { for (Block otherBlock : otherBlocks) { registerBlock(otherBlock, this); } + treatLikeBlock = true; } // Use this constructor if the item name is not the same as its primary block @@ -46,5 +50,14 @@ public class BlockItem extends Item { for (Block otherBlock : otherBlocks) { registerBlock(otherBlock, this); } + treatLikeBlock = false; + } + + @Override + public String translationKey() { + if (!treatLikeBlock) { + return super.translationKey(); + } + return "block." + this.javaIdentifier.namespace() + "." + this.javaIdentifier.value(); } } diff --git a/core/src/main/java/org/geysermc/geyser/item/type/Item.java b/core/src/main/java/org/geysermc/geyser/item/type/Item.java index 2417177ce..a8a477025 100644 --- a/core/src/main/java/org/geysermc/geyser/item/type/Item.java +++ b/core/src/main/java/org/geysermc/geyser/item/type/Item.java @@ -25,6 +25,7 @@ package org.geysermc.geyser.item.type; +import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -59,7 +60,7 @@ import java.util.Map; public class Item { private static final Map BLOCK_TO_ITEM = new HashMap<>(); - private final String javaIdentifier; + protected final Key javaIdentifier; private int javaId = -1; private final int stackSize; private final int attackDamage; @@ -68,7 +69,7 @@ public class Item { private final boolean glint; public Item(String javaIdentifier, Builder builder) { - this.javaIdentifier = MinecraftKey.key(javaIdentifier).asString().intern(); + this.javaIdentifier = MinecraftKey.key(javaIdentifier); this.stackSize = builder.stackSize; this.maxDamage = builder.maxDamage; this.attackDamage = builder.attackDamage; @@ -77,7 +78,7 @@ public class Item { } public String javaIdentifier() { - return javaIdentifier; + return javaIdentifier.asString(); } public int javaId() { @@ -108,6 +109,10 @@ public class Item { return false; } + public String translationKey() { + return "item." + javaIdentifier.namespace() + "." + javaIdentifier.value(); + } + /* Translation methods to Bedrock and back */ public ItemData.Builder translateToBedrock(int count, DataComponents components, ItemMapping mapping, ItemMappings mappings) { diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/JavaUpdateRecipesTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/JavaUpdateRecipesTranslator.java index fd8981552..7c36c505b 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/JavaUpdateRecipesTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/JavaUpdateRecipesTranslator.java @@ -253,7 +253,8 @@ public class JavaUpdateRecipesTranslator extends PacketTranslator Registries.JAVA_ITEMS.get().get(stoneCuttingRecipeData.getResult().getId()) - .javaIdentifier()))); + // See RecipeManager#getRecipesFor as of 1.21 + .translationKey()))); // Now that it's sorted, let's translate these recipes int buttonId = 0; diff --git a/core/src/main/java/org/geysermc/geyser/util/StatisticsUtils.java b/core/src/main/java/org/geysermc/geyser/util/StatisticsUtils.java index 72fcb4fa6..9847c0cfc 100644 --- a/core/src/main/java/org/geysermc/geyser/util/StatisticsUtils.java +++ b/core/src/main/java/org/geysermc/geyser/util/StatisticsUtils.java @@ -107,7 +107,7 @@ public class StatisticsUtils { for (Object2IntMap.Entry entry : session.getStatistics().object2IntEntrySet()) { if (entry.getKey() instanceof BreakItemStatistic statistic) { - String item = itemRegistry.get(statistic.getId()).javaIdentifier(); + Item item = itemRegistry.get(statistic.getId()); content.add(getItemTranslateKey(item, language) + ": " + entry.getIntValue()); } } @@ -117,7 +117,7 @@ public class StatisticsUtils { for (Object2IntMap.Entry entry : session.getStatistics().object2IntEntrySet()) { if (entry.getKey() instanceof CraftItemStatistic statistic) { - String item = itemRegistry.get(statistic.getId()).javaIdentifier(); + Item item = itemRegistry.get(statistic.getId()); content.add(getItemTranslateKey(item, language) + ": " + entry.getIntValue()); } } @@ -127,7 +127,7 @@ public class StatisticsUtils { for (Object2IntMap.Entry entry : session.getStatistics().object2IntEntrySet()) { if (entry.getKey() instanceof UseItemStatistic statistic) { - String item = itemRegistry.get(statistic.getId()).javaIdentifier(); + Item item = itemRegistry.get(statistic.getId()); content.add(getItemTranslateKey(item, language) + ": " + entry.getIntValue()); } } @@ -137,7 +137,7 @@ public class StatisticsUtils { for (Object2IntMap.Entry entry : session.getStatistics().object2IntEntrySet()) { if (entry.getKey() instanceof PickupItemStatistic statistic) { - String item = itemRegistry.get(statistic.getId()).javaIdentifier(); + Item item = itemRegistry.get(statistic.getId()); content.add(getItemTranslateKey(item, language) + ": " + entry.getIntValue()); } } @@ -147,7 +147,7 @@ public class StatisticsUtils { for (Object2IntMap.Entry entry : session.getStatistics().object2IntEntrySet()) { if (entry.getKey() instanceof DropItemStatistic statistic) { - String item = itemRegistry.get(statistic.getId()).javaIdentifier(); + Item item = itemRegistry.get(statistic.getId()); content.add(getItemTranslateKey(item, language) + ": " + entry.getIntValue()); } } @@ -208,14 +208,8 @@ public class StatisticsUtils { * @param language the language to search in * @return the full name of the item */ - private static String getItemTranslateKey(String item, String language) { - item = item.replace("minecraft:", "item.minecraft."); - String translatedItem = MinecraftLocale.getLocaleString(item, language); - if (translatedItem.equals(item)) { - // Didn't translate; must be a block - translatedItem = MinecraftLocale.getLocaleString(item.replace("item.", "block."), language); - } - return translatedItem; + private static String getItemTranslateKey(Item item, String language) { + return MinecraftLocale.getLocaleString(item.translationKey(), language); } private static String translate(String keys, String locale) {