Skip to content

Commit 663e3af

Browse files
committed
Fix non-block items in stonecutters
Fixes #4845
1 parent e994d6e commit 663e3af

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

core/src/main/java/org/geysermc/geyser/item/type/BlockItem.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import org.geysermc.geyser.level.block.type.Block;
2929

3030
public class BlockItem extends Item {
31+
// If item is instanceof ItemNameBlockItem
32+
private final boolean treatLikeBlock;
33+
3134
public BlockItem(Builder builder, Block block, Block... otherBlocks) {
3235
super(block.javaIdentifier().value(), builder);
3336

@@ -36,6 +39,7 @@ public BlockItem(Builder builder, Block block, Block... otherBlocks) {
3639
for (Block otherBlock : otherBlocks) {
3740
registerBlock(otherBlock, this);
3841
}
42+
treatLikeBlock = true;
3943
}
4044

4145
// Use this constructor if the item name is not the same as its primary block
@@ -46,5 +50,14 @@ public BlockItem(String javaIdentifier, Builder builder, Block block, Block... o
4650
for (Block otherBlock : otherBlocks) {
4751
registerBlock(otherBlock, this);
4852
}
53+
treatLikeBlock = false;
54+
}
55+
56+
@Override
57+
public String translationKey() {
58+
if (!treatLikeBlock) {
59+
return super.translationKey();
60+
}
61+
return "block." + this.javaIdentifier.namespace() + "." + this.javaIdentifier.value();
4962
}
5063
}

core/src/main/java/org/geysermc/geyser/item/type/Item.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package org.geysermc.geyser.item.type;
2727

28+
import net.kyori.adventure.key.Key;
2829
import net.kyori.adventure.text.Component;
2930
import org.checkerframework.checker.nullness.qual.NonNull;
3031
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -59,7 +60,7 @@
5960

6061
public class Item {
6162
private static final Map<Block, Item> BLOCK_TO_ITEM = new HashMap<>();
62-
private final String javaIdentifier;
63+
protected final Key javaIdentifier;
6364
private int javaId = -1;
6465
private final int stackSize;
6566
private final int attackDamage;
@@ -68,7 +69,7 @@ public class Item {
6869
private final boolean glint;
6970

7071
public Item(String javaIdentifier, Builder builder) {
71-
this.javaIdentifier = MinecraftKey.key(javaIdentifier).asString().intern();
72+
this.javaIdentifier = MinecraftKey.key(javaIdentifier);
7273
this.stackSize = builder.stackSize;
7374
this.maxDamage = builder.maxDamage;
7475
this.attackDamage = builder.attackDamage;
@@ -77,7 +78,7 @@ public Item(String javaIdentifier, Builder builder) {
7778
}
7879

7980
public String javaIdentifier() {
80-
return javaIdentifier;
81+
return javaIdentifier.asString();
8182
}
8283

8384
public int javaId() {
@@ -108,6 +109,10 @@ public boolean isValidRepairItem(Item other) {
108109
return false;
109110
}
110111

112+
public String translationKey() {
113+
return "item." + javaIdentifier.namespace() + "." + javaIdentifier.value();
114+
}
115+
111116
/* Translation methods to Bedrock and back */
112117

113118
public ItemData.Builder translateToBedrock(int count, DataComponents components, ItemMapping mapping, ItemMappings mappings) {

core/src/main/java/org/geysermc/geyser/translator/protocol/java/JavaUpdateRecipesTranslator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ public void translate(GeyserSession session, ClientboundUpdateRecipesPacket pack
253253
// We can get the correct order for button pressing
254254
data.getValue().sort(Comparator.comparing((stoneCuttingRecipeData ->
255255
Registries.JAVA_ITEMS.get().get(stoneCuttingRecipeData.getResult().getId())
256-
.javaIdentifier())));
256+
// See RecipeManager#getRecipesFor as of 1.21
257+
.translationKey())));
257258

258259
// Now that it's sorted, let's translate these recipes
259260
int buttonId = 0;

core/src/main/java/org/geysermc/geyser/util/StatisticsUtils.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {
107107

108108
for (Object2IntMap.Entry<Statistic> entry : session.getStatistics().object2IntEntrySet()) {
109109
if (entry.getKey() instanceof BreakItemStatistic statistic) {
110-
String item = itemRegistry.get(statistic.getId()).javaIdentifier();
110+
Item item = itemRegistry.get(statistic.getId());
111111
content.add(getItemTranslateKey(item, language) + ": " + entry.getIntValue());
112112
}
113113
}
@@ -117,7 +117,7 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {
117117

118118
for (Object2IntMap.Entry<Statistic> entry : session.getStatistics().object2IntEntrySet()) {
119119
if (entry.getKey() instanceof CraftItemStatistic statistic) {
120-
String item = itemRegistry.get(statistic.getId()).javaIdentifier();
120+
Item item = itemRegistry.get(statistic.getId());
121121
content.add(getItemTranslateKey(item, language) + ": " + entry.getIntValue());
122122
}
123123
}
@@ -127,7 +127,7 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {
127127

128128
for (Object2IntMap.Entry<Statistic> entry : session.getStatistics().object2IntEntrySet()) {
129129
if (entry.getKey() instanceof UseItemStatistic statistic) {
130-
String item = itemRegistry.get(statistic.getId()).javaIdentifier();
130+
Item item = itemRegistry.get(statistic.getId());
131131
content.add(getItemTranslateKey(item, language) + ": " + entry.getIntValue());
132132
}
133133
}
@@ -137,7 +137,7 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {
137137

138138
for (Object2IntMap.Entry<Statistic> entry : session.getStatistics().object2IntEntrySet()) {
139139
if (entry.getKey() instanceof PickupItemStatistic statistic) {
140-
String item = itemRegistry.get(statistic.getId()).javaIdentifier();
140+
Item item = itemRegistry.get(statistic.getId());
141141
content.add(getItemTranslateKey(item, language) + ": " + entry.getIntValue());
142142
}
143143
}
@@ -147,7 +147,7 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {
147147

148148
for (Object2IntMap.Entry<Statistic> entry : session.getStatistics().object2IntEntrySet()) {
149149
if (entry.getKey() instanceof DropItemStatistic statistic) {
150-
String item = itemRegistry.get(statistic.getId()).javaIdentifier();
150+
Item item = itemRegistry.get(statistic.getId());
151151
content.add(getItemTranslateKey(item, language) + ": " + entry.getIntValue());
152152
}
153153
}
@@ -208,14 +208,8 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {
208208
* @param language the language to search in
209209
* @return the full name of the item
210210
*/
211-
private static String getItemTranslateKey(String item, String language) {
212-
item = item.replace("minecraft:", "item.minecraft.");
213-
String translatedItem = MinecraftLocale.getLocaleString(item, language);
214-
if (translatedItem.equals(item)) {
215-
// Didn't translate; must be a block
216-
translatedItem = MinecraftLocale.getLocaleString(item.replace("item.", "block."), language);
217-
}
218-
return translatedItem;
211+
private static String getItemTranslateKey(Item item, String language) {
212+
return MinecraftLocale.getLocaleString(item.translationKey(), language);
219213
}
220214

221215
private static String translate(String keys, String locale) {

0 commit comments

Comments
 (0)