From bb98765601117049550a0c96de2923676043596d Mon Sep 17 00:00:00 2001 From: BugTeaON Date: Sat, 17 May 2025 11:17:48 +0800 Subject: [PATCH 01/10] Add some code comments, remove the incorrect prefix correctly in potion name, fix potion name in tipped arrows (refs #5255 #5176) --- .../translator/item/ItemTranslator.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java index 04f5f5bb308..87eabfb3003 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java @@ -45,6 +45,7 @@ import org.geysermc.geyser.item.Items; import org.geysermc.geyser.item.TooltipOptions; import org.geysermc.geyser.item.components.Rarity; +import org.geysermc.geyser.item.type.ArrowItem; import org.geysermc.geyser.item.type.Item; import org.geysermc.geyser.item.type.PotionItem; import org.geysermc.geyser.level.block.type.Block; @@ -183,7 +184,9 @@ public static ItemData translateToBedrock(GeyserSession session, ItemStack stack PotionContents potionContents = components.get(DataComponentTypes.POTION_CONTENTS); // Make custom effect information visible when shown in tooltip if (potionContents != null && tooltip.showInTooltip(DataComponentTypes.POTION_CONTENTS)) { - customName += getPotionEffectInfo(potionContents, session.locale()); // TODO should this be done with lore instead? + // Add to the name (use '\n' to wrap lines) instead of the lore + // to make it show in HUD like the effect information of vanilla Bedrock potion + customName += getPotionEffectInfo(potionContents, session.locale()); } nbtBuilder.setCustomName(customName); @@ -379,6 +382,7 @@ public static String getPotionEffectInfo(PotionContents contents, String languag } Component component = Component.text() .resetStyle() + // Use blue to distinguish the gray vanilla Bedrock effect .color((negativeEffectList.contains(effect)) ? NamedTextColor.RED : NamedTextColor.BLUE) .append(appendTranslatable) .build(); @@ -400,7 +404,21 @@ public static String getPotionName(PotionContents contents, ItemMapping mapping, } if (!contents.getCustomEffects().isEmpty()) { // Make a name when has custom effects - String potionName = potion == null ? "empty" : potion.toString().toLowerCase(Locale.ROOT); + // because the custom effect information is display from the second line of the name. + // if name is not set, the custom effect information will not be displayed. + String potionName; + if (potion != null) { + potionName = potion.toString().toLowerCase(Locale.ROOT); + // Remove the incorrect prefix + // for example, potion "long_leaping" should use "leaping" in translatable name + if (potionName.startsWith("strong_")) { + potionName = potionName.substring(7); + } else if (potionName.startsWith("long_")) { + potionName = potionName.substring(5); + } + } else { + potionName = "empty"; + } return MessageTranslator.convertMessage(Component.translatable(mapping.getJavaItem().translationKey() + ".effect." + potionName), language); } return null; @@ -538,7 +556,7 @@ public static String getCustomName(GeyserSession session, DataComponents compone } if (!customNameOnly) { - if (mapping.getJavaItem() instanceof PotionItem) { + if (mapping.getJavaItem() instanceof PotionItem || mapping.getJavaItem() instanceof ArrowItem) { PotionContents potionContents = components.get(DataComponentTypes.POTION_CONTENTS); if (potionContents != null) { String potionName = getPotionName(potionContents, mapping, session.locale()); // TODO also test this From 989eb71c863df2d5822189d456a9e2aa0e45dad6 Mon Sep 17 00:00:00 2001 From: BugTeaON Date: Sat, 17 May 2025 22:38:46 +0800 Subject: [PATCH 02/10] Fix tipped arrow always using name "Tipped Arrow" and not affected by potion components --- .../geyser/registry/populator/ItemRegistryPopulator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/geysermc/geyser/registry/populator/ItemRegistryPopulator.java b/core/src/main/java/org/geysermc/geyser/registry/populator/ItemRegistryPopulator.java index bdf9c285e99..8e1793a1e13 100644 --- a/core/src/main/java/org/geysermc/geyser/registry/populator/ItemRegistryPopulator.java +++ b/core/src/main/java/org/geysermc/geyser/registry/populator/ItemRegistryPopulator.java @@ -257,7 +257,7 @@ public static void populate() { Set javaOnlyItems = new ObjectOpenHashSet<>(); Collections.addAll(javaOnlyItems, Items.SPECTRAL_ARROW, Items.DEBUG_STICK, - Items.KNOWLEDGE_BOOK, Items.TIPPED_ARROW); + Items.KNOWLEDGE_BOOK); if (!customItemsAllowed) { javaOnlyItems.add(Items.FURNACE_MINECART); } From d038c66c6808e85ac9dbaca86a98eee209c41bdd Mon Sep 17 00:00:00 2001 From: BugTeaON Date: Sat, 17 May 2025 22:56:37 +0800 Subject: [PATCH 03/10] Fix the wrong Bedrock ID of tipped arrows in the tooltip of shulker box --- .../main/java/org/geysermc/geyser/item/type/ShulkerBoxItem.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/geysermc/geyser/item/type/ShulkerBoxItem.java b/core/src/main/java/org/geysermc/geyser/item/type/ShulkerBoxItem.java index e2e910b1744..dadd16944f5 100644 --- a/core/src/main/java/org/geysermc/geyser/item/type/ShulkerBoxItem.java +++ b/core/src/main/java/org/geysermc/geyser/item/type/ShulkerBoxItem.java @@ -87,7 +87,7 @@ public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNul if (potionContents != null) { Potion potion = Potion.getByJavaId(potionContents.getPotionId()); if (potion != null) { - bedrockData = potion.getBedrockId(); + bedrockData = boxMapping.getJavaItem() instanceof ArrowItem ? potion.tippedArrowId() : potion.getBedrockId(); } } } From c0023618e4135c2b86c4681870e484fff5b2f96f Mon Sep 17 00:00:00 2001 From: BugTeaON Date: Sat, 17 May 2025 23:43:20 +0800 Subject: [PATCH 04/10] Is necessary to check the type of Item to improve performance, like getting potion name in the getPotionName method? --- .../geyser/translator/item/ItemTranslator.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java index 87eabfb3003..f67e0e64274 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java @@ -181,12 +181,14 @@ public static ItemData translateToBedrock(GeyserSession session, ItemStack stack Rarity rarity = Rarity.fromId(components.getOrDefault(DataComponentTypes.RARITY, 0)); String customName = getCustomName(session, customComponents, bedrockItem, rarity.getColor(), false, false); if (customName != null) { - PotionContents potionContents = components.get(DataComponentTypes.POTION_CONTENTS); - // Make custom effect information visible when shown in tooltip - if (potionContents != null && tooltip.showInTooltip(DataComponentTypes.POTION_CONTENTS)) { - // Add to the name (use '\n' to wrap lines) instead of the lore - // to make it show in HUD like the effect information of vanilla Bedrock potion - customName += getPotionEffectInfo(potionContents, session.locale()); + if (javaItem instanceof PotionItem || javaItem instanceof ArrowItem) { + PotionContents potionContents = components.get(DataComponentTypes.POTION_CONTENTS); + // Make custom effect information visible when shown in tooltip + if (potionContents != null && tooltip.showInTooltip(DataComponentTypes.POTION_CONTENTS)) { + // Add to the name (use '\n' to wrap lines) instead of the lore + // to make it show in HUD like the effect information of vanilla Bedrock potion + customName += getPotionEffectInfo(potionContents, session.locale()); + } } nbtBuilder.setCustomName(customName); From 02666f6a2d112cb41b6065260f84e2ba49985620 Mon Sep 17 00:00:00 2001 From: BugTeaON Date: Sun, 18 May 2025 11:45:34 +0800 Subject: [PATCH 05/10] Revert "Is necessary to check the type of Item to improve performance, like getting potion name in the getPotionName method?" This reverts commit c0023618e4135c2b86c4681870e484fff5b2f96f. --- .../geyser/translator/item/ItemTranslator.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java index f67e0e64274..87eabfb3003 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java @@ -181,14 +181,12 @@ public static ItemData translateToBedrock(GeyserSession session, ItemStack stack Rarity rarity = Rarity.fromId(components.getOrDefault(DataComponentTypes.RARITY, 0)); String customName = getCustomName(session, customComponents, bedrockItem, rarity.getColor(), false, false); if (customName != null) { - if (javaItem instanceof PotionItem || javaItem instanceof ArrowItem) { - PotionContents potionContents = components.get(DataComponentTypes.POTION_CONTENTS); - // Make custom effect information visible when shown in tooltip - if (potionContents != null && tooltip.showInTooltip(DataComponentTypes.POTION_CONTENTS)) { - // Add to the name (use '\n' to wrap lines) instead of the lore - // to make it show in HUD like the effect information of vanilla Bedrock potion - customName += getPotionEffectInfo(potionContents, session.locale()); - } + PotionContents potionContents = components.get(DataComponentTypes.POTION_CONTENTS); + // Make custom effect information visible when shown in tooltip + if (potionContents != null && tooltip.showInTooltip(DataComponentTypes.POTION_CONTENTS)) { + // Add to the name (use '\n' to wrap lines) instead of the lore + // to make it show in HUD like the effect information of vanilla Bedrock potion + customName += getPotionEffectInfo(potionContents, session.locale()); } nbtBuilder.setCustomName(customName); From abf57ca7beab9810a3c5a685244a3e99ba3ba639 Mon Sep 17 00:00:00 2001 From: BugTeaON Date: Sun, 18 May 2025 22:52:29 +0800 Subject: [PATCH 06/10] Able to display custom potion effects in all items; Optimize potion name when potion tooltip is hidden --- .../translator/item/ItemTranslator.java | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java index 87eabfb3003..a800a8ebfce 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java @@ -45,9 +45,9 @@ import org.geysermc.geyser.item.Items; import org.geysermc.geyser.item.TooltipOptions; import org.geysermc.geyser.item.components.Rarity; -import org.geysermc.geyser.item.type.ArrowItem; import org.geysermc.geyser.item.type.Item; import org.geysermc.geyser.item.type.PotionItem; +import org.geysermc.geyser.item.type.TippedArrowItem; import org.geysermc.geyser.level.block.type.Block; import org.geysermc.geyser.registry.BlockRegistries; import org.geysermc.geyser.registry.Registries; @@ -392,23 +392,27 @@ public static String getPotionEffectInfo(PotionContents contents, String languag return finalText.toString(); } - public static String getPotionName(PotionContents contents, ItemMapping mapping, String language) { - String customPotionName = contents.getCustomName(); - Potion potion = Potion.getByJavaId(contents.getPotionId()); - - if (customPotionName != null) { - // "custom_name" tag in "potion_contents" component - return MessageTranslator.convertMessage( - Component.translatable(mapping.getJavaItem().translationKey() + ".effect." + customPotionName), - language); + public static String getPotionName(PotionContents contents, ItemMapping mapping, + boolean isPotionItem, boolean customOnly, String language) { + if (isPotionItem) { + String customPotionName = contents.getCustomName(); + if (customPotionName != null) { + // "custom_name" tag in "potion_contents" component + return MinecraftLocale.getLocaleString(mapping.getJavaItem().translationKey() + ".effect." + customPotionName, language); + } } - if (!contents.getCustomEffects().isEmpty()) { + + if (!customOnly && !contents.getCustomEffects().isEmpty()) { + if (!isPotionItem) { + return MinecraftLocale.getLocaleString(mapping.getJavaItem().translationKey(), language); + } // Make a name when has custom effects // because the custom effect information is display from the second line of the name. // if name is not set, the custom effect information will not be displayed. String potionName; + Potion potion = Potion.getByJavaId(contents.getPotionId()); if (potion != null) { - potionName = potion.toString().toLowerCase(Locale.ROOT); + potionName = potion.name().toLowerCase(Locale.ROOT); // Remove the incorrect prefix // for example, potion "long_leaping" should use "leaping" in translatable name if (potionName.startsWith("strong_")) { @@ -419,8 +423,9 @@ public static String getPotionName(PotionContents contents, ItemMapping mapping, } else { potionName = "empty"; } - return MessageTranslator.convertMessage(Component.translatable(mapping.getJavaItem().translationKey() + ".effect." + potionName), language); + return MinecraftLocale.getLocaleString(mapping.getJavaItem().translationKey() + ".effect." + potionName, language); } + return null; } @@ -542,7 +547,7 @@ public static ItemDefinition getBedrockItemDefinition(GeyserSession session, @No * Normally, this should just be white, but for shulker boxes this should be gray. */ public static String getCustomName(GeyserSession session, DataComponents components, ItemMapping mapping, - char translationColor, boolean customNameOnly, boolean includeAll) { + char translationColor, boolean customNameOnly, boolean shulkerBoxTooltip) { if (components != null) { // If the tooltip is hidden entirely, return an empty custom name if (TooltipOptions.hideTooltip(components)) { @@ -556,17 +561,18 @@ public static String getCustomName(GeyserSession session, DataComponents compone } if (!customNameOnly) { - if (mapping.getJavaItem() instanceof PotionItem || mapping.getJavaItem() instanceof ArrowItem) { - PotionContents potionContents = components.get(DataComponentTypes.POTION_CONTENTS); - if (potionContents != null) { - String potionName = getPotionName(potionContents, mapping, session.locale()); // TODO also test this - if (potionName != null) { - return ChatColor.RESET + ChatColor.ESCAPE + translationColor + potionName; - } + PotionContents potionContents = components.get(DataComponentTypes.POTION_CONTENTS); + if (potionContents != null) { + boolean isPotionItem = mapping.getJavaItem() instanceof PotionItem || mapping.getJavaItem() instanceof TippedArrowItem; + boolean customOnly = shulkerBoxTooltip || + !TooltipOptions.fromComponents(components).showInTooltip(DataComponentTypes.POTION_CONTENTS); + String potionName = getPotionName(potionContents, mapping, isPotionItem, customOnly, session.locale()); // TODO also test this + if (potionName != null) { + return ChatColor.RESET + ChatColor.ESCAPE + translationColor + potionName; } } - if (includeAll) { + if (shulkerBoxTooltip) { // Fix book title display in tooltips of shulker box WrittenBookContent bookContent = components.get(DataComponentTypes.WRITTEN_BOOK_CONTENT); if (bookContent != null) { From f804eb1e49a530e2bb13a60898e6e3947d0e65e4 Mon Sep 17 00:00:00 2001 From: BugTeaON Date: Thu, 22 May 2025 22:49:48 +0800 Subject: [PATCH 07/10] Ensure potion displays the same name both its own tooltip and shulker box tooltip --- .../org/geysermc/geyser/item/type/ShulkerBoxItem.java | 4 ++-- .../geyser/translator/item/ItemTranslator.java | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/item/type/ShulkerBoxItem.java b/core/src/main/java/org/geysermc/geyser/item/type/ShulkerBoxItem.java index dadd16944f5..b1ee0abaa71 100644 --- a/core/src/main/java/org/geysermc/geyser/item/type/ShulkerBoxItem.java +++ b/core/src/main/java/org/geysermc/geyser/item/type/ShulkerBoxItem.java @@ -82,12 +82,12 @@ public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNul bedrockData = 0; } else { // Manual checks for potions/tipped arrows - if (boxMapping.getJavaItem() instanceof PotionItem || boxMapping.getJavaItem() instanceof ArrowItem) { + if (boxMapping.getJavaItem() instanceof PotionItem || boxMapping.getJavaItem() instanceof TippedArrowItem) { PotionContents potionContents = boxComponents.get(DataComponentTypes.POTION_CONTENTS); if (potionContents != null) { Potion potion = Potion.getByJavaId(potionContents.getPotionId()); if (potion != null) { - bedrockData = boxMapping.getJavaItem() instanceof ArrowItem ? potion.tippedArrowId() : potion.getBedrockId(); + bedrockData = boxMapping.getJavaItem() instanceof TippedArrowItem ? potion.tippedArrowId() : potion.getBedrockId(); } } } diff --git a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java index a800a8ebfce..227bb801603 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java @@ -356,7 +356,7 @@ public static String getPotionEffectInfo(PotionContents contents, String languag MobEffectDetails details = effectInstance.getDetails(); int amplifier = details.getAmplifier(); int durations = details.getDuration(); - TranslatableComponent appendTranslatable = Component.translatable("effect.minecraft." + effect.toString().toLowerCase(Locale.ROOT)); + TranslatableComponent appendTranslatable = Component.translatable("effect.minecraft." + effect.name().toLowerCase(Locale.ROOT)); if (amplifier != 0) { appendTranslatable = Component.translatable("potion.withAmplifier", appendTranslatable, @@ -547,7 +547,7 @@ public static ItemDefinition getBedrockItemDefinition(GeyserSession session, @No * Normally, this should just be white, but for shulker boxes this should be gray. */ public static String getCustomName(GeyserSession session, DataComponents components, ItemMapping mapping, - char translationColor, boolean customNameOnly, boolean shulkerBoxTooltip) { + char translationColor, boolean customNameOnly, boolean includeAll) { if (components != null) { // If the tooltip is hidden entirely, return an empty custom name if (TooltipOptions.hideTooltip(components)) { @@ -564,15 +564,14 @@ public static String getCustomName(GeyserSession session, DataComponents compone PotionContents potionContents = components.get(DataComponentTypes.POTION_CONTENTS); if (potionContents != null) { boolean isPotionItem = mapping.getJavaItem() instanceof PotionItem || mapping.getJavaItem() instanceof TippedArrowItem; - boolean customOnly = shulkerBoxTooltip || - !TooltipOptions.fromComponents(components).showInTooltip(DataComponentTypes.POTION_CONTENTS); - String potionName = getPotionName(potionContents, mapping, isPotionItem, customOnly, session.locale()); // TODO also test this + boolean customOnly = !TooltipOptions.fromComponents(components).showInTooltip(DataComponentTypes.POTION_CONTENTS); + String potionName = getPotionName(potionContents, mapping, isPotionItem, customOnly, session.locale()); if (potionName != null) { return ChatColor.RESET + ChatColor.ESCAPE + translationColor + potionName; } } - if (shulkerBoxTooltip) { + if (includeAll) { // Fix book title display in tooltips of shulker box WrittenBookContent bookContent = components.get(DataComponentTypes.WRITTEN_BOOK_CONTENT); if (bookContent != null) { From 19f202ac0dcc88be4cab69f3b39f129898750ca7 Mon Sep 17 00:00:00 2001 From: BugTeaON Date: Sat, 24 May 2025 00:52:01 +0800 Subject: [PATCH 08/10] Add `minecraft:potion_duration_scale` component support (new feature since 1.21.5) --- .../geyser/translator/item/ItemTranslator.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java index 227bb801603..da177742236 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java @@ -186,7 +186,7 @@ public static ItemData translateToBedrock(GeyserSession session, ItemStack stack if (potionContents != null && tooltip.showInTooltip(DataComponentTypes.POTION_CONTENTS)) { // Add to the name (use '\n' to wrap lines) instead of the lore // to make it show in HUD like the effect information of vanilla Bedrock potion - customName += getPotionEffectInfo(potionContents, session.locale()); + customName += getPotionEffectInfo(potionContents, components.getOrDefault(DataComponentTypes.POTION_DURATION_SCALE, 1f), session.locale()); } nbtBuilder.setCustomName(customName); @@ -348,22 +348,23 @@ private static String attributeToLore(GeyserSession session, int attribute, Item Effect.INFESTED ); - public static String getPotionEffectInfo(PotionContents contents, String language) { + public static String getPotionEffectInfo(PotionContents contents, float durationScale, String language) { StringBuilder finalText = new StringBuilder(); List effectInstanceList = contents.getCustomEffects(); for (MobEffectInstance effectInstance : effectInstanceList) { Effect effect = effectInstance.getEffect(); MobEffectDetails details = effectInstance.getDetails(); int amplifier = details.getAmplifier(); - int durations = details.getDuration(); + int duration = details.getDuration(); TranslatableComponent appendTranslatable = Component.translatable("effect.minecraft." + effect.name().toLowerCase(Locale.ROOT)); if (amplifier != 0) { appendTranslatable = Component.translatable("potion.withAmplifier", appendTranslatable, Component.translatable("potion.potency." + amplifier)); } - if (durations > 20) { - int seconds = durations / 20; + if (duration > 20) { + int scaledDuration = (int) (duration * durationScale); + int seconds = scaledDuration / 20; int secondsFormat = seconds % 60; int minutes = seconds / 60; int minutesFormat = minutes % 60; @@ -375,7 +376,7 @@ public static String getPotionEffectInfo(PotionContents contents, String languag appendTranslatable = Component.translatable("potion.withDuration", appendTranslatable, Component.text(text)); - } else if (durations == -1) { + } else if (duration == -1) { appendTranslatable = Component.translatable("potion.withDuration", appendTranslatable, Component.translatable("effect.duration.infinite")); From 31259a22aef62ab3c2399cee33b4988defaa90c0 Mon Sep 17 00:00:00 2001 From: BugTeaON Date: Tue, 5 Aug 2025 22:03:06 +0800 Subject: [PATCH 09/10] Improve access to potion names --- .../translator/item/ItemTranslator.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java index fb33303b979..f5490941496 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java @@ -420,20 +420,14 @@ public static String getPotionEffectInfo(PotionContents contents, float duration return finalText.toString(); } - public static String getPotionName(PotionContents contents, ItemMapping mapping, - boolean isPotionItem, boolean customOnly, String language) { - if (isPotionItem) { - String customPotionName = contents.getCustomName(); - if (customPotionName != null) { - // "custom_name" tag in "potion_contents" component - return MinecraftLocale.getLocaleString(mapping.getJavaItem().translationKey() + ".effect." + customPotionName, language); - } + public static String getPotionName(PotionContents contents, ItemMapping mapping, boolean includeDefault, String language) { + String customPotionName = contents.getCustomName(); + if (customPotionName != null) { + // "custom_name" tag in "potion_contents" component + return MinecraftLocale.getLocaleString(mapping.getJavaItem().translationKey() + ".effect." + customPotionName, language); } - if (!customOnly && !contents.getCustomEffects().isEmpty()) { - if (!isPotionItem) { - return MinecraftLocale.getLocaleString(mapping.getJavaItem().translationKey(), language); - } + if (includeDefault && !contents.getCustomEffects().isEmpty()) { // Make a name when has custom effects // because the custom effect information is display from the second line of the name. // if name is not set, the custom effect information will not be displayed. @@ -589,17 +583,23 @@ public static String getCustomName(GeyserSession session, DataComponents compone } if (!customNameOnly) { + boolean forceName = false; PotionContents potionContents = components.get(DataComponentTypes.POTION_CONTENTS); if (potionContents != null) { - boolean isPotionItem = mapping.getJavaItem() instanceof PotionItem || mapping.getJavaItem() instanceof TippedArrowItem; - boolean customOnly = !TooltipOptions.fromComponents(components).showInTooltip(DataComponentTypes.POTION_CONTENTS); - String potionName = getPotionName(potionContents, mapping, isPotionItem, customOnly, session.locale()); - if (potionName != null) { - return ChatColor.RESET + ChatColor.ESCAPE + translationColor + potionName; + boolean showPotionEffects = TooltipOptions.fromComponents(components).showInTooltip(DataComponentTypes.POTION_CONTENTS); + if (mapping.getJavaItem() instanceof PotionItem || mapping.getJavaItem() instanceof TippedArrowItem) { + // Get name in "potion_contents" component + String potionName = getPotionName(potionContents, mapping, showPotionEffects, session.locale()); + if (potionName != null) { + return ChatColor.RESET + ChatColor.ESCAPE + translationColor + potionName; + } + } else { + // hold the custom effect information (reason for this is mentioned in getPotionName method) + forceName = showPotionEffects && !potionContents.getCustomEffects().isEmpty(); } } - if (includeAll) { + if (includeAll || forceName) { // Fix book title display in tooltips of shulker box WrittenBookContent bookContent = components.get(DataComponentTypes.WRITTEN_BOOK_CONTENT); if (bookContent != null) { @@ -613,6 +613,11 @@ public static String getCustomName(GeyserSession session, DataComponents compone // behavior as of 1.21 return ChatColor.RESET + ChatColor.ESCAPE + translationColor + MessageTranslator.convertMessage(customName, session.locale()); } + + if (forceName) { + String translationKey = mapping.getJavaItem().translationKey(); + return ChatColor.RESET + ChatColor.ESCAPE + translationColor + MinecraftLocale.getLocaleString(translationKey, session.locale()); + } } } From e313a7ab70a3a493fc05b80fd81ac4637861569e Mon Sep 17 00:00:00 2001 From: BugTeaON Date: Tue, 5 Aug 2025 23:32:10 +0800 Subject: [PATCH 10/10] Improve a little of code --- .../geyser/translator/item/ItemTranslator.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java index f5490941496..1e03910a25d 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/item/ItemTranslator.java @@ -427,7 +427,7 @@ public static String getPotionName(PotionContents contents, ItemMapping mapping, return MinecraftLocale.getLocaleString(mapping.getJavaItem().translationKey() + ".effect." + customPotionName, language); } - if (includeDefault && !contents.getCustomEffects().isEmpty()) { + if (includeDefault) { // Make a name when has custom effects // because the custom effect information is display from the second line of the name. // if name is not set, the custom effect information will not be displayed. @@ -586,16 +586,15 @@ public static String getCustomName(GeyserSession session, DataComponents compone boolean forceName = false; PotionContents potionContents = components.get(DataComponentTypes.POTION_CONTENTS); if (potionContents != null) { - boolean showPotionEffects = TooltipOptions.fromComponents(components).showInTooltip(DataComponentTypes.POTION_CONTENTS); + // hold the custom effect information (reason for this is mentioned in getPotionName method) + forceName = TooltipOptions.fromComponents(components).showInTooltip(DataComponentTypes.POTION_CONTENTS) + && !potionContents.getCustomEffects().isEmpty(); + // Get name in "potion_contents" component for vanilla potion items if (mapping.getJavaItem() instanceof PotionItem || mapping.getJavaItem() instanceof TippedArrowItem) { - // Get name in "potion_contents" component - String potionName = getPotionName(potionContents, mapping, showPotionEffects, session.locale()); + String potionName = getPotionName(potionContents, mapping, forceName, session.locale()); if (potionName != null) { return ChatColor.RESET + ChatColor.ESCAPE + translationColor + potionName; } - } else { - // hold the custom effect information (reason for this is mentioned in getPotionName method) - forceName = showPotionEffects && !potionContents.getCustomEffects().isEmpty(); } }