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 497b59a5..27c70239 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 @@ -38,9 +38,12 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import us.myles.viaversion.libs.opennbt.conversion.ConverterRegistry; import us.myles.viaversion.libs.opennbt.tag.builtin.*; +import java.util.*; + public class BlockItemPackets1_13 extends BlockItemRewriter { private static String NBT_TAG_NAME; + private static Map enchantmentMappings = new HashMap<>(); public static int toOldId(int oldId) { if (oldId < 0) { @@ -57,7 +60,7 @@ public class BlockItemPackets1_13 extends BlockItemRewriter lore = new ArrayList<>(); for (Tag enchantmentEntry : enchantments) { if (enchantmentEntry instanceof CompoundTag) { CompoundTag enchEntry = new CompoundTag(""); String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue(); - Short oldId = MappingData.oldEnchantmentsIds.inverse().get(newId); - if (oldId == null && newId.startsWith("viaversion:legacy/")) { - oldId = Short.valueOf(newId.substring(18)); + if(enchantmentMappings.containsKey(newId)){ + lore.add(new StringTag("", enchantmentMappings.get(newId))); + noMapped.add(enchantmentEntry); + }else{ + Short oldId = MappingData.oldEnchantmentsIds.inverse().get(newId); + if (oldId == null && newId.startsWith("viaversion:legacy/")) { + oldId = Short.valueOf(newId.substring(18)); + } + enchEntry.put( + new ShortTag( + "id", + oldId + ) + ); + enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue())); + enchantments.add(enchEntry); } - enchEntry.put( - new ShortTag( - "id", - oldId - ) - ); - enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue())); - ench.add(enchEntry); } } tag.remove("Enchantment"); + tag.put(noMapped); tag.put(ench); + + if(!lore.isEmpty()){ + CompoundTag display = tag.get("display"); + if (display==null) { + tag.put(display = new CompoundTag("display")); + tag.put(new ByteTag(NBT_TAG_NAME + "|noDisplay")); + } + ListTag loreTag = display.get("Lore"); + if (loreTag==null){ + display.put(loreTag = new ListTag("Lore", StringTag.class)); + } + + ListTag oldLore = new ListTag(NBT_TAG_NAME + "|OldLore", StringTag.class); + Iterator iterator = lore.iterator(); + while(iterator.hasNext()){ + oldLore.add(iterator.next().clone()); + } + display.put(oldLore); + + lore.addAll(loreTag.getValue()); + loreTag.setValue(lore); + } } if (tag.get("StoredEnchantments") instanceof ListTag) { ListTag storedEnch = tag.get("StoredEnchantments"); + ListTag noMapped = new ListTag(NBT_TAG_NAME + "|StoredEnchantments", CompoundTag.class); ListTag newStoredEnch = new ListTag("StoredEnchantments", CompoundTag.class); + List lore = new ArrayList<>(); for (Tag enchantmentEntry : storedEnch) { if (enchantmentEntry instanceof CompoundTag) { CompoundTag enchEntry = new CompoundTag(""); String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue(); - Short oldId = MappingData.oldEnchantmentsIds.inverse().get(newId); - if (oldId == null && newId.startsWith("viaversion:legacy/")) { - oldId = Short.valueOf(newId.substring(18)); + if(enchantmentMappings.containsKey(newId)){ + lore.add(new StringTag("", enchantmentMappings.get(newId))); + noMapped.add(enchantmentEntry); + }else{ + Short oldId = MappingData.oldEnchantmentsIds.inverse().get(newId); + if (oldId == null && newId.startsWith("viaversion:legacy/")) { + oldId = Short.valueOf(newId.substring(18)); + } + enchEntry.put( + new ShortTag( + "id", + oldId + ) + ); + enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue())); + newStoredEnch.add(enchEntry); } - enchEntry.put( - new ShortTag( - "id", - oldId - ) - ); - enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue())); - newStoredEnch.add(enchEntry); } } tag.remove("StoredEnchantments"); + tag.put(noMapped); tag.put(newStoredEnch); + + if(!lore.isEmpty()){ + CompoundTag display = tag.get("display"); + if (display==null) { + tag.put(display = new CompoundTag("display")); + tag.put(new ByteTag(NBT_TAG_NAME + "|noDisplay")); + } + ListTag loreTag = display.get("Lore"); + if (loreTag==null){ + display.put(loreTag = new ListTag("Lore", StringTag.class)); + } + + ListTag oldLore = new ListTag(NBT_TAG_NAME + "|OldLore", StringTag.class); + Iterator iterator = lore.iterator(); + while(iterator.hasNext()){ + oldLore.add(iterator.next().clone()); + } + display.put(oldLore); + + lore.addAll(loreTag.getValue()); + loreTag.setValue(lore); + } } if (tag.get(NBT_TAG_NAME + "|CanPlaceOn") instanceof ListTag) { tag.put(ConverterRegistry.convertToTag( @@ -720,7 +786,6 @@ public class BlockItemPackets1_13 extends BlockItemRewriter iterator = oldLore.iterator(); + + while (iterator.hasNext()){ + loreTag.add(iterator.next()); + } + display.remove("Lore"); + display.put(loreTag); + display.remove(NBT_TAG_NAME + "|OldLore"); + } } } // ench is now Enchantments and now uses identifiers @@ -795,6 +877,13 @@ public class BlockItemPackets1_13 extends BlockItemRewriter iterator = noMapped.iterator(); + while (iterator.hasNext()){ + enchantments.add(iterator.next()); + } + } tag.remove("ench"); tag.put(enchantments); } @@ -816,6 +905,13 @@ public class BlockItemPackets1_13 extends BlockItemRewriter iterator = noMapped.iterator(); + while (iterator.hasNext()){ + newStoredEnch.add(iterator.next()); + } + } tag.remove("StoredEnchantments"); tag.put(newStoredEnch); } @@ -920,4 +1016,9 @@ public class BlockItemPackets1_13 extends BlockItemRewriter