diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriter.java index ecd0213c..32afe523 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriter.java @@ -5,19 +5,24 @@ import nl.matsv.viabackwards.api.data.MappedItem; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.rewriters.IdRewriteFunction; import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import us.myles.viaversion.libs.opennbt.tag.builtin.ListTag; import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag; +import us.myles.viaversion.libs.opennbt.tag.builtin.Tag; public abstract class ItemRewriter extends ItemRewriterBase { private final MappedItemFunction mappedItemFunction; + private final TranslatableRewriter translatableRewriter; - protected ItemRewriter(T protocol, IdRewriteFunction oldRewriter, IdRewriteFunction newRewriter, MappedItemFunction mappedItemFunction) { + protected ItemRewriter(T protocol, TranslatableRewriter translatableRewriter, IdRewriteFunction oldRewriter, IdRewriteFunction newRewriter, MappedItemFunction mappedItemFunction) { super(protocol, oldRewriter, newRewriter, true); + this.translatableRewriter = translatableRewriter; this.mappedItemFunction = mappedItemFunction; } - protected ItemRewriter(T protocol, MappedItemFunction mappedItemFunction) { + protected ItemRewriter(T protocol, TranslatableRewriter translatableRewriter, MappedItemFunction mappedItemFunction) { super(protocol, true); + this.translatableRewriter = translatableRewriter; this.mappedItemFunction = mappedItemFunction; } @@ -25,32 +30,62 @@ public abstract class ItemRewriter extends ItemRewr public Item handleItemToClient(Item item) { if (item == null) return null; + CompoundTag tag = null; + boolean textChanged = false; + if (translatableRewriter != null + && item.getTag() != null && (tag = item.getTag().get("display")) != null) { + // Handle name and lore components + StringTag name = tag.get("Name"); + if (name != null) { + String newValue = translatableRewriter.processText(name.getValue()); + if (name.getValue().equals(newValue)) { + textChanged = true; + } + name.setValue(newValue); + } + + ListTag lore = tag.get("Lore"); + if (lore != null) { + for (Tag loreEntry : lore) { + StringTag stringTag = (StringTag) loreEntry; + String newValue = translatableRewriter.processText(stringTag.getValue()); + if (stringTag.getValue().equals(newValue)) { + textChanged = true; + } + stringTag.setValue(newValue); + } + } + } + + if (textChanged) { + // Backup data for toServer + item.getTag().put(createViaNBT(item)); + } + MappedItem data = mappedItemFunction.get(item.getIdentifier()); if (data == null) { // Just rewrite the id return super.handleItemToClient(item); } - if (item.getTag() == null) { - item.setTag(new CompoundTag("")); + // Backup data for toServer if not already done above + if (!textChanged) { + if (item.getTag() == null) { + item.setTag(new CompoundTag("")); + } + item.getTag().put(createViaNBT(item)); } - // Backup data for toServer - item.getTag().put(createViaNBT(item)); - - // Also includes the already mapped id + // Set remapped id item.setIdentifier(data.getId()); - // Set custom name - CompoundTag tag = item.getTag().get("display"); + // Set custom name - only done if there is no original one if (tag == null) { item.getTag().put(tag = new CompoundTag("display")); } - // Only set name if there is no original one if (!tag.contains("Name")) { tag.put(new StringTag("Name", data.getJsonName())); } - return item; } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java index 712e93e5..bd73b793 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java @@ -45,11 +45,6 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol { Via.getManager().getProviders().register(BackwardsBlockEntityProvider.class, new BackwardsBlockEntityProvider()); }); - (blockItemPackets = new BlockItemPackets1_13(this)).register(); - new EntityPackets1_13(this).register(); - new PlayerPacket1_13(this).register(); - new SoundPackets1_13(this).register(); - TranslatableRewriter translatableRewriter = new TranslatableRewriter(this) { @Override protected void handleTranslate(JsonObject root, String translate) { @@ -68,6 +63,11 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol { translatableRewriter.registerTitle(0x4B, 0x48); translatableRewriter.registerPlayerList(0x4E, 0x4A); + (blockItemPackets = new BlockItemPackets1_13(this, translatableRewriter)).register(); + new EntityPackets1_13(this).register(); + new PlayerPacket1_13(this).register(); + new SoundPackets1_13(this).register(); + // Thanks to https://wiki.vg/index.php?title=Pre-release_protocol&oldid=14150 out(State.PLAY, 0x11, -1, cancel()); // Declare Commands TODO NEW diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java index f9d0ea8c..ed2d6b1f 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java @@ -13,6 +13,7 @@ package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets; import com.google.common.primitives.Ints; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter; +import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.block_entity_handlers.FlowerPotHandler; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings; @@ -48,8 +49,8 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It private final Map enchantmentMappings = new HashMap<>(); private final String NBT_TAG_NAME; - public BlockItemPackets1_13(Protocol1_12_2To1_13 protocol) { - super(protocol, id -> BackwardsMappings.itemMappings.getMappedItem(id)); + public BlockItemPackets1_13(Protocol1_12_2To1_13 protocol, TranslatableRewriter translatableRewriter) { + super(protocol, translatableRewriter, id -> BackwardsMappings.itemMappings.getMappedItem(id)); NBT_TAG_NAME = "ViaBackwards|" + protocol.getClass().getSimpleName() + "|Part2"; } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java index 586300bd..568ecaad 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java @@ -29,13 +29,6 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol { protected void registerPackets() { executeAsyncAfterLoaded(Protocol1_14To1_13_2.class, BackwardsMappings::init); - blockItemPackets = new BlockItemPackets1_14(this); - blockItemPackets.register(); - entityPackets = new EntityPackets1_14(this); - entityPackets.register(); - new PlayerPackets1_14(this).register(); - new SoundPackets1_14(this).register(); - TranslatableRewriter translatableRewriter = new TranslatableRewriter(this); translatableRewriter.registerBossBar(0x0C, 0x0C); translatableRewriter.registerChatMessage(0x0E, 0x0E); @@ -45,6 +38,13 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol { translatableRewriter.registerTitle(0x4F, 0x4B); translatableRewriter.registerPing(); + blockItemPackets = new BlockItemPackets1_14(this, translatableRewriter); + blockItemPackets.register(); + entityPackets = new EntityPackets1_14(this); + entityPackets.register(); + new PlayerPackets1_14(this).register(); + new SoundPackets1_14(this).register(); + registerOutgoing(State.PLAY, 0x15, 0x16); registerOutgoing(State.PLAY, 0x18, 0x19); registerOutgoing(State.PLAY, 0x1B, 0x1C); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java index 80c8902c..72a99f95 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java @@ -5,6 +5,7 @@ import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter; import nl.matsv.viabackwards.api.rewriters.RecipeRewriter; +import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.RecipeRewriter1_14; @@ -47,8 +48,8 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It private EnchantmentRewriter enchantmentRewriter; - public BlockItemPackets1_14(Protocol1_13_2To1_14 protocol) { - super(protocol, BlockItemPackets1_14::getOldItemId, BlockItemPackets1_14::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); + public BlockItemPackets1_14(Protocol1_13_2To1_14 protocol, TranslatableRewriter translatableRewriter) { + super(protocol, translatableRewriter, BlockItemPackets1_14::getOldItemId, BlockItemPackets1_14::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); } @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java index df6494a9..7b20af37 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java @@ -23,7 +23,7 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class BlockItemPackets1_15 extends nl.matsv.viabackwards.api.rewriters.ItemRewriter { public BlockItemPackets1_15(Protocol1_14_4To1_15 protocol) { - super(protocol, BlockItemPackets1_15::getOldItemId, BlockItemPackets1_15::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); + super(protocol, null, BlockItemPackets1_15::getOldItemId, BlockItemPackets1_15::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); } @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java index 9f02d4e4..861df43f 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java @@ -23,16 +23,13 @@ import java.util.UUID; public class Protocol1_15_2To1_16 extends BackwardsProtocol { private BlockItemPackets1_16 blockItemPackets; + private TranslatableRewriter translatableRewriter; @Override protected void registerPackets() { executeAsyncAfterLoaded(Protocol1_16To1_15_2.class, BackwardsMappings::init); - (blockItemPackets = new BlockItemPackets1_16(this)).register(); - EntityPackets1_16 entityPackets = new EntityPackets1_16(this); - entityPackets.register(); - - TranslatableRewriter translatableRewriter = new TranslatableRewriter1_16(this); + translatableRewriter = new TranslatableRewriter1_16(this); translatableRewriter.registerBossBar(0x0D, 0x0D); translatableRewriter.registerCombatEvent(0x33, 0x33); translatableRewriter.registerDisconnect(0x1B, 0x1B); @@ -40,6 +37,10 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol { translatableRewriter.registerTitle(0x50, 0x50); translatableRewriter.registerPing(); + (blockItemPackets = new BlockItemPackets1_16(this, translatableRewriter)).register(); + EntityPackets1_16 entityPackets = new EntityPackets1_16(this); + entityPackets.register(); + // Chat Message registerOutgoing(State.PLAY, 0x0F, 0x0F, new PacketRemapper() { @Override @@ -207,4 +208,8 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol { public BlockItemPackets1_16 getBlockItemPackets() { return blockItemPackets; } + + public TranslatableRewriter getTranslatableRewriter() { + return translatableRewriter; + } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java index 3c3b25bc..de418fd1 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java @@ -2,6 +2,7 @@ package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.packets; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter; +import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.RecipeRewriter1_15; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.data.BackwardsMappings; @@ -31,8 +32,9 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It private EnchantmentRewriter enchantmentRewriter; - public BlockItemPackets1_16(Protocol1_15_2To1_16 protocol) { - super(protocol, BlockItemPackets1_16::getOldItemId, BlockItemPackets1_16::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); + public BlockItemPackets1_16(Protocol1_15_2To1_16 protocol, TranslatableRewriter translatableRewriter) { + super(protocol, translatableRewriter, + BlockItemPackets1_16::getOldItemId, BlockItemPackets1_16::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); } @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java index ddf568f8..687cfaa8 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java @@ -157,6 +157,11 @@ public class EntityPackets1_16 extends EntityRewriter { } else if (type == MetaType1_14.PARTICLE) { Particle particle = (Particle) meta.getValue(); particle.setId(ParticleMapping.getOldId(particle.getId())); + } else if (type == MetaType1_14.OptChat) { + String text = meta.getCastedValue(); + if (text != null) { + meta.setValue(protocol.getTranslatableRewriter().processText(text)); + } } return meta; });