Skip to content

Commit

Permalink
Show proper names for Java-only items (GeyserMC#2159)
Browse files Browse the repository at this point in the history
Spectral arrows and knowledge books, if unnamed, will now show their correct, translated names.
  • Loading branch information
Camotoy authored Apr 26, 2021
1 parent 9de0ebd commit e7f933b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ public static void init() {
"", bedrockBlockId,
stackSize);
}
} else if (entry.getKey().equals("minecraft:spectral_arrow") || entry.getKey().equals("minecraft:knowledge_book")) {
// These items don't exist on Java, so set up a container that indicates they should have custom names
itemEntry = new TranslatableItemEntry(
entry.getKey(), bedrockIdentifier, itemIndex, bedrockId,
entry.getValue().get("bedrock_data").intValue(),
bedrockBlockId,
stackSize);
} else {
itemEntry = new ItemEntry(
entry.getKey(), bedrockIdentifier, itemIndex, bedrockId,
Expand Down Expand Up @@ -471,7 +478,6 @@ public static void init() {
}

itemNames.add("minecraft:furnace_minecart");
itemNames.add("minecraft:spectral_arrow");

if (lodestoneCompassId == 0) {
throw new RuntimeException("Lodestone compass not found in item palette!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.geysermc.connector.network.translators.chat.MessageTranslator;
import org.geysermc.connector.utils.FileUtils;
import org.geysermc.connector.utils.LanguageUtils;
import org.geysermc.connector.utils.LocaleUtils;
import org.reflections.Reflections;

import java.util.*;
Expand Down Expand Up @@ -137,8 +138,6 @@ public static ItemData translateToBedrock(GeyserSession session, ItemStack stack
nbt.put(new IntTag("map", 0));
}

ItemStack itemStack = new ItemStack(stack.getId(), stack.getAmount(), nbt);

if (nbt != null) {
for (NbtItemStackTranslator translator : NBT_TRANSLATORS) {
if (translator.acceptItem(bedrockItem)) {
Expand All @@ -147,7 +146,9 @@ public static ItemData translateToBedrock(GeyserSession session, ItemStack stack
}
}

translateDisplayProperties(session, nbt);
nbt = translateDisplayProperties(session, nbt, bedrockItem);

ItemStack itemStack = new ItemStack(stack.getId(), stack.getAmount(), nbt);

ItemData.Builder builder;
ItemTranslator itemStackTranslator = ITEM_STACK_TRANSLATORS.get(bedrockItem.getJavaId());
Expand Down Expand Up @@ -393,8 +394,20 @@ private Tag translateToJavaNBT(String name, Object object) {
* Translates the display name of the item
* @param session the Bedrock client's session
* @param tag the tag to translate
* @param itemEntry the item entry, in case it requires translation
*
* @return the new tag to use, should the current one be null
*/
public static CompoundTag translateDisplayProperties(GeyserSession session, CompoundTag tag, ItemEntry itemEntry) {
return translateDisplayProperties(session, tag, itemEntry, 'f');
}

/**
* @param translationColor if this item is not available on Java, the color that the new name should be.
* Normally, this should just be white, but for shulker boxes this should be gray.
*/
public static void translateDisplayProperties(GeyserSession session, CompoundTag tag) {
public static CompoundTag translateDisplayProperties(GeyserSession session, CompoundTag tag, ItemEntry itemEntry, char translationColor) {
boolean hasCustomName = false;
if (tag != null) {
CompoundTag display = tag.get("display");
if (display != null && display.contains("Name")) {
Expand All @@ -405,11 +418,32 @@ public static void translateDisplayProperties(GeyserSession session, CompoundTag

// Add the new name tag
display.put(new StringTag("Name", name));
// Indicate that a custom name is present
hasCustomName = true;

// Add to the new root tag
tag.put(display);
}
}

if (!hasCustomName && itemEntry instanceof TranslatableItemEntry) {
// No custom name, but we need to localize the item's name
if (tag == null) {
tag = new CompoundTag("");
}
CompoundTag display = tag.get("display");
if (display == null) {
display = new CompoundTag("display");
// Add to the new root tag
tag.put(display);
}

String translationKey = ((TranslatableItemEntry) itemEntry).getTranslationString();
// Reset formatting since Bedrock defaults to italics
display.put(new StringTag("Name", "§r§" + translationColor + LocaleUtils.getLocaleString(translationKey, session.getLocale())));
}

return tag;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/

package org.geysermc.connector.network.translators.item;

import lombok.Getter;

/**
* Used when an item should have a custom name applied, if there already isn't one.
*/
public class TranslatableItemEntry extends ItemEntry {
@Getter
private final String translationString;

public TranslatableItemEntry(String javaIdentifier, String bedrockIdentifier, int javaId, int bedrockId, int bedrockData, int bedrockBlockId, int stackSize) {
super(javaIdentifier, bedrockIdentifier, javaId, bedrockId, bedrockData, bedrockBlockId, stackSize);
this.translationString = "item." + javaIdentifier.replace(":", ".");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
import com.github.steveice10.opennbt.tag.builtin.*;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.ItemRemapper;
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.network.translators.item.ItemRegistry;
import org.geysermc.connector.network.translators.item.ItemTranslator;
import org.geysermc.connector.network.translators.item.NbtItemStackTranslator;
import org.geysermc.connector.network.translators.item.*;

@ItemRemapper
public class ShulkerBoxItemTranslator extends NbtItemStackTranslator {
Expand All @@ -54,11 +51,13 @@ public void translateToBedrock(GeyserSession session, CompoundTag itemTag, ItemE
boxItemTag.put(new StringTag("Name", boxItemEntry.getBedrockIdentifier()));
boxItemTag.put(new ShortTag("Damage", (short) boxItemEntry.getBedrockData()));
boxItemTag.put(new ByteTag("Count", ((ByteTag) itemData.get("Count")).getValue()));
if (itemData.contains("tag")) {
// Only the display name is what we have interest in, so just translate that if relevant
CompoundTag displayTag = itemData.get("tag");
ItemTranslator.translateDisplayProperties(session, displayTag);
boxItemTag.put(displayTag);
// Only the display name is what we have interest in, so just translate that if relevant
CompoundTag displayTag = itemData.get("tag");
if (displayTag == null && boxItemEntry instanceof TranslatableItemEntry) {
displayTag = new CompoundTag("tag");
}
if (displayTag != null) {
boxItemTag.put(ItemTranslator.translateDisplayProperties(session, displayTag, boxItemEntry, '7'));
}

itemsList.add(boxItemTag);
Expand Down

0 comments on commit e7f933b

Please sign in to comment.