Add item name translation (#559)

* Added item name translation

* Change to more appropriate NPE catch

* Remove whitespace

* Switch from try/catch to null checking

* Update mappings

Co-authored-by: DoctorMacc <toy.fighter1@gmail.com>
This commit is contained in:
rtm516 2020-06-02 21:33:37 +01:00 committed by GitHub
parent 3d357af739
commit a91eaa7821
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 63 additions and 29 deletions

View file

@ -49,7 +49,7 @@ public class ItemEntity extends Entity {
itemPacket.setUniqueEntityId(geyserId); itemPacket.setUniqueEntityId(geyserId);
itemPacket.setFromFishing(false); itemPacket.setFromFishing(false);
itemPacket.getMetadata().putAll(metadata); itemPacket.getMetadata().putAll(metadata);
itemPacket.setItemInHand(ItemTranslator.translateToBedrock((ItemStack) entityMetadata.getValue())); itemPacket.setItemInHand(ItemTranslator.translateToBedrock(session, (ItemStack) entityMetadata.getValue()));
session.sendUpstreamPacket(itemPacket); session.sendUpstreamPacket(itemPacket);
} }

View file

@ -98,7 +98,7 @@ public class ItemFrameEntity extends Entity {
@Override @Override
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) { public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
if (entityMetadata.getId() == 7 && entityMetadata.getValue() != null) { if (entityMetadata.getId() == 7 && entityMetadata.getValue() != null) {
ItemData itemData = ItemTranslator.translateToBedrock((ItemStack) entityMetadata.getValue()); ItemData itemData = ItemTranslator.translateToBedrock(session, (ItemStack) entityMetadata.getValue());
ItemEntry itemEntry = ItemRegistry.getItem((ItemStack) entityMetadata.getValue()); ItemEntry itemEntry = ItemRegistry.getItem((ItemStack) entityMetadata.getValue());
CompoundTagBuilder builder = CompoundTag.builder(); CompoundTagBuilder builder = CompoundTag.builder();

View file

@ -59,11 +59,11 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
ItemData[] contents = new ItemData[36]; ItemData[] contents = new ItemData[36];
// Inventory // Inventory
for (int i = 9; i < 36; i++) { for (int i = 9; i < 36; i++) {
contents[i] = ItemTranslator.translateToBedrock(inventory.getItem(i)); contents[i] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
} }
// Hotbar // Hotbar
for (int i = 36; i < 45; i++) { for (int i = 36; i < 45; i++) {
contents[i - 36] = ItemTranslator.translateToBedrock(inventory.getItem(i)); contents[i - 36] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
} }
inventoryContentPacket.setContents(contents); inventoryContentPacket.setContents(contents);
session.sendUpstreamPacket(inventoryContentPacket); session.sendUpstreamPacket(inventoryContentPacket);
@ -73,7 +73,7 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
armorContentPacket.setContainerId(ContainerId.ARMOR); armorContentPacket.setContainerId(ContainerId.ARMOR);
contents = new ItemData[4]; contents = new ItemData[4];
for (int i = 5; i < 9; i++) { for (int i = 5; i < 9; i++) {
contents[i - 5] = ItemTranslator.translateToBedrock(inventory.getItem(i)); contents[i - 5] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
} }
armorContentPacket.setContents(contents); armorContentPacket.setContents(contents);
session.sendUpstreamPacket(armorContentPacket); session.sendUpstreamPacket(armorContentPacket);
@ -81,7 +81,7 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
// Offhand // Offhand
InventoryContentPacket offhandPacket = new InventoryContentPacket(); InventoryContentPacket offhandPacket = new InventoryContentPacket();
offhandPacket.setContainerId(ContainerId.OFFHAND); offhandPacket.setContainerId(ContainerId.OFFHAND);
offhandPacket.setContents(new ItemData[]{ItemTranslator.translateToBedrock(inventory.getItem(45))}); offhandPacket.setContents(new ItemData[]{ItemTranslator.translateToBedrock(session, inventory.getItem(45))});
session.sendUpstreamPacket(offhandPacket); session.sendUpstreamPacket(offhandPacket);
} }
@ -100,7 +100,7 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
if (session.getGameMode() == GameMode.CREATIVE) { if (session.getGameMode() == GameMode.CREATIVE) {
slotPacket.setItem(UNUSUABLE_CRAFTING_SPACE_BLOCK); slotPacket.setItem(UNUSUABLE_CRAFTING_SPACE_BLOCK);
}else{ }else{
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(i))); slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(i)));
} }
session.sendUpstreamPacket(slotPacket); session.sendUpstreamPacket(slotPacket);
@ -125,12 +125,12 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
slotPacket.setContainerId(ContainerId.CURSOR); slotPacket.setContainerId(ContainerId.CURSOR);
slotPacket.setSlot(slot + 27); slotPacket.setSlot(slot + 27);
} }
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(slot))); slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(slot)));
session.sendUpstreamPacket(slotPacket); session.sendUpstreamPacket(slotPacket);
} else if (slot == 45) { } else if (slot == 45) {
InventoryContentPacket offhandPacket = new InventoryContentPacket(); InventoryContentPacket offhandPacket = new InventoryContentPacket();
offhandPacket.setContainerId(ContainerId.OFFHAND); offhandPacket.setContainerId(ContainerId.OFFHAND);
offhandPacket.setContents(new ItemData[]{ItemTranslator.translateToBedrock(inventory.getItem(slot))}); offhandPacket.setContents(new ItemData[]{ItemTranslator.translateToBedrock(session, inventory.getItem(slot))});
session.sendUpstreamPacket(offhandPacket); session.sendUpstreamPacket(offhandPacket);
} }
} }

View file

@ -61,13 +61,13 @@ public class InventoryActionDataTranslator {
worldAction = action; worldAction = action;
} else if (action.getSource().getContainerId() == ContainerId.CURSOR && action.getSlot() == 0) { } else if (action.getSource().getContainerId() == ContainerId.CURSOR && action.getSlot() == 0) {
cursorAction = action; cursorAction = action;
ItemData translatedCursor = ItemTranslator.translateToBedrock(session.getInventory().getCursor()); ItemData translatedCursor = ItemTranslator.translateToBedrock(session, session.getInventory().getCursor());
if (!translatedCursor.equals(action.getFromItem())) { if (!translatedCursor.equals(action.getFromItem())) {
refresh = true; refresh = true;
} }
} else { } else {
containerAction = action; containerAction = action;
ItemData translatedItem = ItemTranslator.translateToBedrock(inventory.getItem(translator.bedrockSlotToJava(action))); ItemData translatedItem = ItemTranslator.translateToBedrock(session, inventory.getItem(translator.bedrockSlotToJava(action)));
if (!translatedItem.equals(action.getFromItem())) { if (!translatedItem.equals(action.getFromItem())) {
refresh = true; refresh = true;
} }

View file

@ -49,7 +49,7 @@ public class ChestInventoryUpdater extends InventoryUpdater {
ItemData[] bedrockItems = new ItemData[paddedSize]; ItemData[] bedrockItems = new ItemData[paddedSize];
for (int i = 0; i < bedrockItems.length; i++) { for (int i = 0; i < bedrockItems.length; i++) {
if (i < translator.size) { if (i < translator.size) {
bedrockItems[i] = ItemTranslator.translateToBedrock(inventory.getItem(i)); bedrockItems[i] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
} else { } else {
bedrockItems[i] = UNUSUABLE_SPACE_BLOCK; bedrockItems[i] = UNUSUABLE_SPACE_BLOCK;
} }
@ -69,7 +69,7 @@ public class ChestInventoryUpdater extends InventoryUpdater {
InventorySlotPacket slotPacket = new InventorySlotPacket(); InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(inventory.getId()); slotPacket.setContainerId(inventory.getId());
slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot)); slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot));
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(javaSlot))); slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(javaSlot)));
session.sendUpstreamPacket(slotPacket); session.sendUpstreamPacket(slotPacket);
return true; return true;
} }

View file

@ -40,7 +40,7 @@ public class ContainerInventoryUpdater extends InventoryUpdater {
ItemData[] bedrockItems = new ItemData[translator.size]; ItemData[] bedrockItems = new ItemData[translator.size];
for (int i = 0; i < bedrockItems.length; i++) { for (int i = 0; i < bedrockItems.length; i++) {
bedrockItems[translator.javaSlotToBedrock(i)] = ItemTranslator.translateToBedrock(inventory.getItem(i)); bedrockItems[translator.javaSlotToBedrock(i)] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
} }
InventoryContentPacket contentPacket = new InventoryContentPacket(); InventoryContentPacket contentPacket = new InventoryContentPacket();
@ -57,7 +57,7 @@ public class ContainerInventoryUpdater extends InventoryUpdater {
InventorySlotPacket slotPacket = new InventorySlotPacket(); InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(inventory.getId()); slotPacket.setContainerId(inventory.getId());
slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot)); slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot));
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(javaSlot))); slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(javaSlot)));
session.sendUpstreamPacket(slotPacket); session.sendUpstreamPacket(slotPacket);
return true; return true;
} }

View file

@ -44,7 +44,7 @@ public class CursorInventoryUpdater extends InventoryUpdater {
InventorySlotPacket slotPacket = new InventorySlotPacket(); InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(ContainerId.CURSOR); slotPacket.setContainerId(ContainerId.CURSOR);
slotPacket.setSlot(bedrockSlot); slotPacket.setSlot(bedrockSlot);
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(i))); slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(i)));
session.sendUpstreamPacket(slotPacket); session.sendUpstreamPacket(slotPacket);
} }
} }
@ -57,7 +57,7 @@ public class CursorInventoryUpdater extends InventoryUpdater {
InventorySlotPacket slotPacket = new InventorySlotPacket(); InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(ContainerId.CURSOR); slotPacket.setContainerId(ContainerId.CURSOR);
slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot)); slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot));
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(javaSlot))); slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(javaSlot)));
session.sendUpstreamPacket(slotPacket); session.sendUpstreamPacket(slotPacket);
return true; return true;
} }

View file

@ -39,7 +39,7 @@ public abstract class InventoryUpdater {
ItemData[] bedrockItems = new ItemData[36]; ItemData[] bedrockItems = new ItemData[36];
for (int i = 0; i < 36; i++) { for (int i = 0; i < 36; i++) {
final int offset = i < 9 ? 27 : -9; final int offset = i < 9 ? 27 : -9;
bedrockItems[i] = ItemTranslator.translateToBedrock(inventory.getItem(translator.size + i + offset)); bedrockItems[i] = ItemTranslator.translateToBedrock(session, inventory.getItem(translator.size + i + offset));
} }
InventoryContentPacket contentPacket = new InventoryContentPacket(); InventoryContentPacket contentPacket = new InventoryContentPacket();
contentPacket.setContainerId(ContainerId.INVENTORY); contentPacket.setContainerId(ContainerId.INVENTORY);
@ -52,7 +52,7 @@ public abstract class InventoryUpdater {
InventorySlotPacket slotPacket = new InventorySlotPacket(); InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(ContainerId.INVENTORY); slotPacket.setContainerId(ContainerId.INVENTORY);
slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot)); slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot));
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(javaSlot))); slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(javaSlot)));
session.sendUpstreamPacket(slotPacket); session.sendUpstreamPacket(slotPacket);
return true; return true;
} }

View file

@ -27,6 +27,8 @@
package org.geysermc.connector.network.translators.item; package org.geysermc.connector.network.translators.item;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.github.steveice10.mc.protocol.data.message.Message;
import com.nukkitx.nbt.CompoundTagBuilder;
import com.github.steveice10.opennbt.tag.builtin.*; import com.github.steveice10.opennbt.tag.builtin.*;
import com.nukkitx.nbt.tag.CompoundTag; import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.nbt.tag.Tag; import com.nukkitx.nbt.tag.Tag;
@ -34,7 +36,9 @@ import com.nukkitx.protocol.bedrock.data.ItemData;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.ItemRemapper; import org.geysermc.connector.network.translators.ItemRemapper;
import org.geysermc.connector.utils.MessageUtils;
import org.reflections.Reflections; import org.reflections.Reflections;
import java.util.ArrayList; import java.util.ArrayList;
@ -115,7 +119,7 @@ public abstract class ItemTranslator {
return itemStack; return itemStack;
} }
public static ItemData translateToBedrock(ItemStack stack) { public static ItemData translateToBedrock(GeyserSession session, ItemStack stack) {
if (stack == null) { if (stack == null) {
return ItemData.AIR; return ItemData.AIR;
} }
@ -132,12 +136,42 @@ public abstract class ItemTranslator {
} }
} }
ItemData itemData;
ItemTranslator itemStackTranslator = ITEM_STACK_TRANSLATORS.get(bedrockItem.getJavaId()); ItemTranslator itemStackTranslator = ITEM_STACK_TRANSLATORS.get(bedrockItem.getJavaId());
if (itemStackTranslator != null) { if (itemStackTranslator != null) {
return itemStackTranslator.translateToBedrock(itemStack, bedrockItem); itemData = itemStackTranslator.translateToBedrock(itemStack, bedrockItem);
} else { } else {
return DEFAULT_TRANSLATOR.translateToBedrock(itemStack, bedrockItem); itemData = DEFAULT_TRANSLATOR.translateToBedrock(itemStack, bedrockItem);
} }
// Get the display name of the item
CompoundTag tag = itemData.getTag();
if (tag != null) {
CompoundTag display = tag.getCompound("display");
if (display != null) {
String name = display.getString("Name");
// Check if its a message to translate
if (MessageUtils.isMessage(name)) {
// Get the translated name
name = MessageUtils.getTranslatedBedrockMessage(Message.fromString(name), session.getClientData().getLanguageCode());
// Build the new display tag
CompoundTagBuilder displayBuilder = display.toBuilder();
displayBuilder.stringTag("Name", name);
// Build the new root tag
CompoundTagBuilder builder = tag.toBuilder();
builder.tag(displayBuilder.build("display"));
// Create a new item with the original data + updated name
itemData = ItemData.of(itemData.getId(), itemData.getDamage(), itemData.getCount(), builder.buildRootTag());
}
}
}
return itemData;
} }
private static final ItemTranslator DEFAULT_TRANSLATOR = new ItemTranslator() { private static final ItemTranslator DEFAULT_TRANSLATOR = new ItemTranslator() {

View file

@ -64,7 +64,7 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
switch (recipe.getType()) { switch (recipe.getType()) {
case CRAFTING_SHAPELESS: { case CRAFTING_SHAPELESS: {
ShapelessRecipeData shapelessRecipeData = (ShapelessRecipeData) recipe.getData(); ShapelessRecipeData shapelessRecipeData = (ShapelessRecipeData) recipe.getData();
ItemData output = ItemTranslator.translateToBedrock(shapelessRecipeData.getResult()); ItemData output = ItemTranslator.translateToBedrock(session, shapelessRecipeData.getResult());
output = ItemData.of(output.getId(), output.getDamage(), output.getCount()); //strip NBT output = ItemData.of(output.getId(), output.getDamage(), output.getCount()); //strip NBT
ItemData[][] inputCombinations = combinations(session, shapelessRecipeData.getIngredients()); ItemData[][] inputCombinations = combinations(session, shapelessRecipeData.getIngredients());
for (ItemData[] inputs : inputCombinations) { for (ItemData[] inputs : inputCombinations) {
@ -76,7 +76,7 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
} }
case CRAFTING_SHAPED: { case CRAFTING_SHAPED: {
ShapedRecipeData shapedRecipeData = (ShapedRecipeData) recipe.getData(); ShapedRecipeData shapedRecipeData = (ShapedRecipeData) recipe.getData();
ItemData output = ItemTranslator.translateToBedrock(shapedRecipeData.getResult()); ItemData output = ItemTranslator.translateToBedrock(session, shapedRecipeData.getResult());
output = ItemData.of(output.getId(), output.getDamage(), output.getCount()); //strip NBT output = ItemData.of(output.getId(), output.getDamage(), output.getCount()); //strip NBT
ItemData[][] inputCombinations = combinations(session, shapedRecipeData.getIngredients()); ItemData[][] inputCombinations = combinations(session, shapedRecipeData.getIngredients());
for (ItemData[] inputs : inputCombinations) { for (ItemData[] inputs : inputCombinations) {
@ -103,7 +103,7 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
} }
Ingredient ingredient = ingredients[i]; Ingredient ingredient = ingredients[i];
Map<GroupedItem, List<ItemData>> groupedByIds = Arrays.stream(ingredient.getOptions()) Map<GroupedItem, List<ItemData>> groupedByIds = Arrays.stream(ingredient.getOptions())
.map(ItemTranslator::translateToBedrock) .map(item -> ItemTranslator.translateToBedrock(session, item))
.collect(Collectors.groupingBy(item -> new GroupedItem(item.getId(), item.getCount(), item.getTag()))); .collect(Collectors.groupingBy(item -> new GroupedItem(item.getId(), item.getCount(), item.getTag())));
Set<ItemData> optionSet = new HashSet<>(groupedByIds.size()); Set<ItemData> optionSet = new HashSet<>(groupedByIds.size());
for (Map.Entry<GroupedItem, List<ItemData>> entry : groupedByIds.entrySet()) { for (Map.Entry<GroupedItem, List<ItemData>> entry : groupedByIds.entrySet()) {
@ -136,7 +136,7 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
ItemData[] translatedItems = new ItemData[ingredients.length]; ItemData[] translatedItems = new ItemData[ingredients.length];
for (int i = 0; i < ingredients.length; i++) { for (int i = 0; i < ingredients.length; i++) {
if (ingredients[i].getOptions().length > 0) { if (ingredients[i].getOptions().length > 0) {
translatedItems[i] = ItemTranslator.translateToBedrock(ingredients[i].getOptions()[0]); translatedItems[i] = ItemTranslator.translateToBedrock(session, ingredients[i].getOptions()[0]);
} else { } else {
translatedItems[i] = ItemData.AIR; translatedItems[i] = ItemData.AIR;
} }

View file

@ -55,7 +55,7 @@ public class JavaEntityEquipmentTranslator extends PacketTranslator<ServerEntity
} }
LivingEntity livingEntity = (LivingEntity) entity; LivingEntity livingEntity = (LivingEntity) entity;
ItemData item = ItemTranslator.translateToBedrock(packet.getItem()); ItemData item = ItemTranslator.translateToBedrock(session, packet.getItem());
switch (packet.getSlot()) { switch (packet.getSlot()) {
case HELMET: case HELMET:
livingEntity.setHelmet(item); livingEntity.setHelmet(item);

View file

@ -64,7 +64,7 @@ public class JavaSpawnParticleTranslator extends PacketTranslator<ServerSpawnPar
break; break;
case ITEM: case ITEM:
ItemStack javaItem = ((ItemParticleData)packet.getParticle().getData()).getItemStack(); ItemStack javaItem = ((ItemParticleData)packet.getParticle().getData()).getItemStack();
ItemData bedrockItem = ItemTranslator.translateToBedrock(javaItem); ItemData bedrockItem = ItemTranslator.translateToBedrock(session, javaItem);
int id = bedrockItem.getId(); int id = bedrockItem.getId();
short damage = bedrockItem.getDamage(); short damage = bedrockItem.getDamage();
particle.setType(LevelEventType.PARTICLE_ITEM_BREAK); particle.setType(LevelEventType.PARTICLE_ITEM_BREAK);

View file

@ -104,7 +104,7 @@ public class InventoryUtils {
InventorySlotPacket cursorPacket = new InventorySlotPacket(); InventorySlotPacket cursorPacket = new InventorySlotPacket();
cursorPacket.setContainerId(ContainerId.CURSOR); cursorPacket.setContainerId(ContainerId.CURSOR);
cursorPacket.setSlot(0); cursorPacket.setSlot(0);
cursorPacket.setItem(ItemTranslator.translateToBedrock(session.getInventory().getCursor())); cursorPacket.setItem(ItemTranslator.translateToBedrock(session, session.getInventory().getCursor()));
session.sendUpstreamPacket(cursorPacket); session.sendUpstreamPacket(cursorPacket);
} }