Fix non-block items in stonecutters

Fixes #4845
This commit is contained in:
Camotoy 2024-07-26 17:22:10 -04:00
parent e994d6e1d6
commit 663e3af7c8
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
4 changed files with 30 additions and 17 deletions

View file

@ -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();
}
}

View file

@ -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, Item> 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) {

View file

@ -253,7 +253,8 @@ public class JavaUpdateRecipesTranslator extends PacketTranslator<ClientboundUpd
// We can get the correct order for button pressing
data.getValue().sort(Comparator.comparing((stoneCuttingRecipeData ->
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;

View file

@ -107,7 +107,7 @@ public class StatisticsUtils {
for (Object2IntMap.Entry<Statistic> 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<Statistic> 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<Statistic> 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<Statistic> 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<Statistic> 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) {