Skip to content

Commit

Permalink
Fix non-block items in stonecutters
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Jul 26, 2024
1 parent e994d6e commit 663e3af
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
13 changes: 13 additions & 0 deletions core/src/main/java/org/geysermc/geyser/item/type/BlockItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
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);

Expand All @@ -36,6 +39,7 @@ public BlockItem(Builder builder, Block block, Block... otherBlocks) {
for (Block otherBlock : otherBlocks) {
registerBlock(otherBlock, this);
}
treatLikeBlock = true;
}

// Use this constructor if the item name is not the same as its primary block
Expand All @@ -46,5 +50,14 @@ public BlockItem(String javaIdentifier, Builder builder, Block block, Block... o
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();
}
}
11 changes: 8 additions & 3 deletions core/src/main/java/org/geysermc/geyser/item/type/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,7 +60,7 @@

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;
Expand All @@ -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;
Expand All @@ -77,7 +78,7 @@ public Item(String javaIdentifier, Builder builder) {
}

public String javaIdentifier() {
return javaIdentifier;
return javaIdentifier.asString();
}

public int javaId() {
Expand Down Expand Up @@ -108,6 +109,10 @@ public boolean isValidRepairItem(Item other) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ public void translate(GeyserSession session, ClientboundUpdateRecipesPacket pack
// 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;
Expand Down
20 changes: 7 additions & 13 deletions core/src/main/java/org/geysermc/geyser/util/StatisticsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {

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());
}
}
Expand All @@ -117,7 +117,7 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {

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());
}
}
Expand All @@ -127,7 +127,7 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {

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());
}
}
Expand All @@ -137,7 +137,7 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {

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());
}
}
Expand All @@ -147,7 +147,7 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {

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());
}
}
Expand Down Expand Up @@ -208,14 +208,8 @@ public static void buildAndSendStatisticsMenu(GeyserSession session) {
* @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) {
Expand Down

0 comments on commit 663e3af

Please sign in to comment.