diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EnchantmentRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EnchantmentRewriter.java index a72246fe..2b4b8943 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EnchantmentRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EnchantmentRewriter.java @@ -1,6 +1,13 @@ package nl.matsv.viabackwards.api.rewriters; -import us.myles.viaversion.libs.opennbt.tag.builtin.*; +import us.myles.ViaVersion.api.minecraft.item.Item; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; +import us.myles.viaversion.libs.opennbt.tag.builtin.ByteTag; +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.ShortTag; +import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag; +import us.myles.viaversion.libs.opennbt.tag.builtin.Tag; import java.util.ArrayList; import java.util.HashMap; @@ -11,15 +18,45 @@ public class EnchantmentRewriter { private final Map enchantmentMappings = new HashMap<>(); private final String nbtTagName; + private final boolean jsonFormat; - public EnchantmentRewriter(final String nbtTagName) { + public EnchantmentRewriter(String nbtTagName, boolean jsonFormat) { this.nbtTagName = nbtTagName; + this.jsonFormat = jsonFormat; + } + + public EnchantmentRewriter(String nbtTagName) { + this(nbtTagName, true); } public void registerEnchantment(String key, String replacementLore) { enchantmentMappings.put(key, replacementLore); } + public void handleToClient(Item item) { + CompoundTag tag = item.getTag(); + if (tag == null) return; + + if (tag.get("Enchantments") instanceof ListTag) { + rewriteEnchantmentsToClient(tag, false); + } + if (tag.get("StoredEnchantments") instanceof ListTag) { + rewriteEnchantmentsToClient(tag, true); + } + } + + public void handleToServer(Item item) { + CompoundTag tag = item.getTag(); + if (tag == null) return; + + if (tag.contains(nbtTagName + "|Enchantments")) { + rewriteEnchantmentsToServer(tag, false); + } + if (tag.contains(nbtTagName + "|StoredEnchantments")) { + rewriteEnchantmentsToServer(tag, true); + } + } + public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant) { String key = storedEnchant ? "StoredEnchantments" : "Enchantments"; ListTag enchantments = tag.get(key); @@ -31,7 +68,12 @@ public class EnchantmentRewriter { if (enchantmentName != null) { enchantments.remove(enchantmentEntry); Number level = (Number) ((CompoundTag) enchantmentEntry).get("lvl").getValue(); - lore.add(new StringTag("", enchantmentName + " " + getRomanNumber(level.intValue()))); + String loreValue = enchantmentName + " " + getRomanNumber(level.intValue()); + if (jsonFormat) { + loreValue = ChatRewriter.legacyTextToJson(loreValue); + } + + lore.add(new StringTag("", loreValue)); remappedEnchantments.add(enchantmentEntry); } } 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 914d80f6..80c8902c 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 @@ -539,7 +539,7 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It @Override protected void registerRewrites() { - enchantmentRewriter = new EnchantmentRewriter(nbtTagName); + enchantmentRewriter = new EnchantmentRewriter(nbtTagName, false); enchantmentRewriter.registerEnchantment("minecraft:multishot", "§7Multishot"); enchantmentRewriter.registerEnchantment("minecraft:quick_charge", "§7Quick Charge"); enchantmentRewriter.registerEnchantment("minecraft:piercing", "§7Piercing"); @@ -550,8 +550,8 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It if (item == null) return null; super.handleItemToClient(item); - CompoundTag tag; - if ((tag = item.getTag()) != null) { + CompoundTag tag = item.getTag(); + if (tag != null) { // Display Name now uses JSON if (tag.get("display") instanceof CompoundTag) { CompoundTag display = tag.get("display"); @@ -575,12 +575,7 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It } } - if (tag.get("Enchantments") instanceof ListTag) { - enchantmentRewriter.rewriteEnchantmentsToClient(tag, false); - } - if (tag.get("StoredEnchantments") instanceof ListTag) { - enchantmentRewriter.rewriteEnchantmentsToClient(tag, true); - } + enchantmentRewriter.handleToClient(item); } return item; } @@ -590,8 +585,8 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It if (item == null) return null; super.handleItemToServer(item); - CompoundTag tag; - if ((tag = item.getTag()) != null) { + CompoundTag tag = item.getTag(); + if (tag != null) { // Display Lore now uses JSON if (tag.get("display") instanceof CompoundTag) { CompoundTag display = tag.get("display"); @@ -606,12 +601,7 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It } } - if (tag.contains(nbtTagName + "|Enchantments")) { - enchantmentRewriter.rewriteEnchantmentsToServer(tag, false); - } - if (tag.contains(nbtTagName + "|StoredEnchantments")) { - enchantmentRewriter.rewriteEnchantmentsToServer(tag, true); - } + enchantmentRewriter.handleToServer(item); } return item; } 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 5c7c056f..3c3b25bc 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 @@ -1,6 +1,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.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; @@ -28,6 +29,8 @@ import java.util.UUID; public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.ItemRewriter { + private EnchantmentRewriter enchantmentRewriter; + public BlockItemPackets1_16(Protocol1_15_2To1_16 protocol) { super(protocol, BlockItemPackets1_16::getOldItemId, BlockItemPackets1_16::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); } @@ -196,6 +199,27 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It // Spawn particle blockRewriter.registerSpawnParticle(Type.DOUBLE, 0x24, 0x24, 3, 23, 32, BlockItemPackets1_16::getNewParticleId, this::handleItemToClient, Type.FLAT_VAR_INT_ITEM); + + // Window Property + protocol.registerOutgoing(State.PLAY, 0x16, 0x16, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.UNSIGNED_BYTE); // Window id + map(Type.SHORT); // Property + map(Type.SHORT); // Value + handler(wrapper -> { + short property = wrapper.get(Type.SHORT, 0); + if (property >= 4 && property <= 6) { // Enchantment id + short enchantmentId = wrapper.get(Type.SHORT, 1); + if (enchantmentId > 11) { // soul_speed + wrapper.set(Type.SHORT, 1, --enchantmentId); + } else if (enchantmentId == 11) { + wrapper.set(Type.SHORT, 1, (short) 9); + } + } + }); + } + }); } public static int getNewParticleId(int id) { @@ -223,14 +247,20 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It return id; } + @Override + protected void registerRewrites() { + enchantmentRewriter = new EnchantmentRewriter(nbtTagName); + enchantmentRewriter.registerEnchantment("minecraft:soul_speed", "§7Soul Speed"); + } + @Override public Item handleItemToClient(Item item) { if (item == null) return null; super.handleItemToClient(item); - if (item.getIdentifier() == 771 && item.getTag() != null) { - CompoundTag tag = item.getTag(); + CompoundTag tag = item.getTag(); + if (item.getIdentifier() == 771 && tag != null) { Tag ownerTag = tag.get("SkullOwner"); if (ownerTag instanceof CompoundTag) { CompoundTag ownerCompundTag = (CompoundTag) ownerTag; @@ -241,6 +271,8 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It } } } + + enchantmentRewriter.handleToClient(item); return item; } @@ -251,8 +283,8 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It int identifier = item.getIdentifier(); super.handleItemToServer(item); - if (identifier == 771 && item.getTag() != null) { - CompoundTag tag = item.getTag(); + CompoundTag tag = item.getTag(); + if (identifier == 771 && tag != null) { Tag ownerTag = tag.get("SkullOwner"); if (ownerTag instanceof CompoundTag) { CompoundTag ownerCompundTag = (CompoundTag) ownerTag; @@ -263,6 +295,8 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It } } } + + enchantmentRewriter.handleToServer(item); return item; }