forked from GeyserMC/Geyser
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:
parent
3d357af739
commit
a91eaa7821
13 changed files with 63 additions and 29 deletions
|
@ -49,7 +49,7 @@ public class ItemEntity extends Entity {
|
|||
itemPacket.setUniqueEntityId(geyserId);
|
||||
itemPacket.setFromFishing(false);
|
||||
itemPacket.getMetadata().putAll(metadata);
|
||||
itemPacket.setItemInHand(ItemTranslator.translateToBedrock((ItemStack) entityMetadata.getValue()));
|
||||
itemPacket.setItemInHand(ItemTranslator.translateToBedrock(session, (ItemStack) entityMetadata.getValue()));
|
||||
session.sendUpstreamPacket(itemPacket);
|
||||
}
|
||||
|
||||
|
|
|
@ -98,7 +98,7 @@ public class ItemFrameEntity extends Entity {
|
|||
@Override
|
||||
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
|
||||
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());
|
||||
CompoundTagBuilder builder = CompoundTag.builder();
|
||||
|
||||
|
|
|
@ -59,11 +59,11 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
|
|||
ItemData[] contents = new ItemData[36];
|
||||
// Inventory
|
||||
for (int i = 9; i < 36; i++) {
|
||||
contents[i] = ItemTranslator.translateToBedrock(inventory.getItem(i));
|
||||
contents[i] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
|
||||
}
|
||||
// Hotbar
|
||||
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);
|
||||
session.sendUpstreamPacket(inventoryContentPacket);
|
||||
|
@ -73,7 +73,7 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
|
|||
armorContentPacket.setContainerId(ContainerId.ARMOR);
|
||||
contents = new ItemData[4];
|
||||
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);
|
||||
session.sendUpstreamPacket(armorContentPacket);
|
||||
|
@ -81,7 +81,7 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
|
|||
// Offhand
|
||||
InventoryContentPacket offhandPacket = new InventoryContentPacket();
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -100,7 +100,7 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
|
|||
if (session.getGameMode() == GameMode.CREATIVE) {
|
||||
slotPacket.setItem(UNUSUABLE_CRAFTING_SPACE_BLOCK);
|
||||
}else{
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(i)));
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(i)));
|
||||
}
|
||||
|
||||
session.sendUpstreamPacket(slotPacket);
|
||||
|
@ -125,12 +125,12 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
|
|||
slotPacket.setContainerId(ContainerId.CURSOR);
|
||||
slotPacket.setSlot(slot + 27);
|
||||
}
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(slot)));
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(slot)));
|
||||
session.sendUpstreamPacket(slotPacket);
|
||||
} else if (slot == 45) {
|
||||
InventoryContentPacket offhandPacket = new InventoryContentPacket();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,13 +61,13 @@ public class InventoryActionDataTranslator {
|
|||
worldAction = action;
|
||||
} else if (action.getSource().getContainerId() == ContainerId.CURSOR && action.getSlot() == 0) {
|
||||
cursorAction = action;
|
||||
ItemData translatedCursor = ItemTranslator.translateToBedrock(session.getInventory().getCursor());
|
||||
ItemData translatedCursor = ItemTranslator.translateToBedrock(session, session.getInventory().getCursor());
|
||||
if (!translatedCursor.equals(action.getFromItem())) {
|
||||
refresh = true;
|
||||
}
|
||||
} else {
|
||||
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())) {
|
||||
refresh = true;
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ public class ChestInventoryUpdater extends InventoryUpdater {
|
|||
ItemData[] bedrockItems = new ItemData[paddedSize];
|
||||
for (int i = 0; i < bedrockItems.length; i++) {
|
||||
if (i < translator.size) {
|
||||
bedrockItems[i] = ItemTranslator.translateToBedrock(inventory.getItem(i));
|
||||
bedrockItems[i] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
|
||||
} else {
|
||||
bedrockItems[i] = UNUSUABLE_SPACE_BLOCK;
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ public class ChestInventoryUpdater extends InventoryUpdater {
|
|||
InventorySlotPacket slotPacket = new InventorySlotPacket();
|
||||
slotPacket.setContainerId(inventory.getId());
|
||||
slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot));
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(javaSlot)));
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(javaSlot)));
|
||||
session.sendUpstreamPacket(slotPacket);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ public class ContainerInventoryUpdater extends InventoryUpdater {
|
|||
|
||||
ItemData[] bedrockItems = new ItemData[translator.size];
|
||||
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();
|
||||
|
@ -57,7 +57,7 @@ public class ContainerInventoryUpdater extends InventoryUpdater {
|
|||
InventorySlotPacket slotPacket = new InventorySlotPacket();
|
||||
slotPacket.setContainerId(inventory.getId());
|
||||
slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot));
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(javaSlot)));
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(javaSlot)));
|
||||
session.sendUpstreamPacket(slotPacket);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ public class CursorInventoryUpdater extends InventoryUpdater {
|
|||
InventorySlotPacket slotPacket = new InventorySlotPacket();
|
||||
slotPacket.setContainerId(ContainerId.CURSOR);
|
||||
slotPacket.setSlot(bedrockSlot);
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(i)));
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(i)));
|
||||
session.sendUpstreamPacket(slotPacket);
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public class CursorInventoryUpdater extends InventoryUpdater {
|
|||
InventorySlotPacket slotPacket = new InventorySlotPacket();
|
||||
slotPacket.setContainerId(ContainerId.CURSOR);
|
||||
slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot));
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(javaSlot)));
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(javaSlot)));
|
||||
session.sendUpstreamPacket(slotPacket);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ public abstract class InventoryUpdater {
|
|||
ItemData[] bedrockItems = new ItemData[36];
|
||||
for (int i = 0; i < 36; i++) {
|
||||
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();
|
||||
contentPacket.setContainerId(ContainerId.INVENTORY);
|
||||
|
@ -52,7 +52,7 @@ public abstract class InventoryUpdater {
|
|||
InventorySlotPacket slotPacket = new InventorySlotPacket();
|
||||
slotPacket.setContainerId(ContainerId.INVENTORY);
|
||||
slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot));
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(inventory.getItem(javaSlot)));
|
||||
slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(javaSlot)));
|
||||
session.sendUpstreamPacket(slotPacket);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -27,6 +27,8 @@
|
|||
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.message.Message;
|
||||
import com.nukkitx.nbt.CompoundTagBuilder;
|
||||
import com.github.steveice10.opennbt.tag.builtin.*;
|
||||
import com.nukkitx.nbt.tag.CompoundTag;
|
||||
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.Int2ObjectOpenHashMap;
|
||||
import org.geysermc.connector.GeyserConnector;
|
||||
import org.geysermc.connector.network.session.GeyserSession;
|
||||
import org.geysermc.connector.network.translators.ItemRemapper;
|
||||
import org.geysermc.connector.utils.MessageUtils;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -115,7 +119,7 @@ public abstract class ItemTranslator {
|
|||
return itemStack;
|
||||
}
|
||||
|
||||
public static ItemData translateToBedrock(ItemStack stack) {
|
||||
public static ItemData translateToBedrock(GeyserSession session, ItemStack stack) {
|
||||
if (stack == null) {
|
||||
return ItemData.AIR;
|
||||
}
|
||||
|
@ -132,12 +136,42 @@ public abstract class ItemTranslator {
|
|||
}
|
||||
}
|
||||
|
||||
ItemData itemData;
|
||||
ItemTranslator itemStackTranslator = ITEM_STACK_TRANSLATORS.get(bedrockItem.getJavaId());
|
||||
if (itemStackTranslator != null) {
|
||||
return itemStackTranslator.translateToBedrock(itemStack, bedrockItem);
|
||||
itemData = itemStackTranslator.translateToBedrock(itemStack, bedrockItem);
|
||||
} 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() {
|
||||
|
|
|
@ -64,7 +64,7 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
|
|||
switch (recipe.getType()) {
|
||||
case CRAFTING_SHAPELESS: {
|
||||
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
|
||||
ItemData[][] inputCombinations = combinations(session, shapelessRecipeData.getIngredients());
|
||||
for (ItemData[] inputs : inputCombinations) {
|
||||
|
@ -76,7 +76,7 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
|
|||
}
|
||||
case CRAFTING_SHAPED: {
|
||||
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
|
||||
ItemData[][] inputCombinations = combinations(session, shapedRecipeData.getIngredients());
|
||||
for (ItemData[] inputs : inputCombinations) {
|
||||
|
@ -103,7 +103,7 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
|
|||
}
|
||||
Ingredient ingredient = ingredients[i];
|
||||
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())));
|
||||
Set<ItemData> optionSet = new HashSet<>(groupedByIds.size());
|
||||
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];
|
||||
for (int i = 0; i < ingredients.length; i++) {
|
||||
if (ingredients[i].getOptions().length > 0) {
|
||||
translatedItems[i] = ItemTranslator.translateToBedrock(ingredients[i].getOptions()[0]);
|
||||
translatedItems[i] = ItemTranslator.translateToBedrock(session, ingredients[i].getOptions()[0]);
|
||||
} else {
|
||||
translatedItems[i] = ItemData.AIR;
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ public class JavaEntityEquipmentTranslator extends PacketTranslator<ServerEntity
|
|||
}
|
||||
|
||||
LivingEntity livingEntity = (LivingEntity) entity;
|
||||
ItemData item = ItemTranslator.translateToBedrock(packet.getItem());
|
||||
ItemData item = ItemTranslator.translateToBedrock(session, packet.getItem());
|
||||
switch (packet.getSlot()) {
|
||||
case HELMET:
|
||||
livingEntity.setHelmet(item);
|
||||
|
|
|
@ -64,7 +64,7 @@ public class JavaSpawnParticleTranslator extends PacketTranslator<ServerSpawnPar
|
|||
break;
|
||||
case ITEM:
|
||||
ItemStack javaItem = ((ItemParticleData)packet.getParticle().getData()).getItemStack();
|
||||
ItemData bedrockItem = ItemTranslator.translateToBedrock(javaItem);
|
||||
ItemData bedrockItem = ItemTranslator.translateToBedrock(session, javaItem);
|
||||
int id = bedrockItem.getId();
|
||||
short damage = bedrockItem.getDamage();
|
||||
particle.setType(LevelEventType.PARTICLE_ITEM_BREAK);
|
||||
|
|
|
@ -104,7 +104,7 @@ public class InventoryUtils {
|
|||
InventorySlotPacket cursorPacket = new InventorySlotPacket();
|
||||
cursorPacket.setContainerId(ContainerId.CURSOR);
|
||||
cursorPacket.setSlot(0);
|
||||
cursorPacket.setItem(ItemTranslator.translateToBedrock(session.getInventory().getCursor()));
|
||||
cursorPacket.setItem(ItemTranslator.translateToBedrock(session, session.getInventory().getCursor()));
|
||||
session.sendUpstreamPacket(cursorPacket);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue