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 c7dfc68f..b7b87e76 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 @@ -698,133 +698,12 @@ public class BlockItemPackets1_13 extends BlockItemRewriter lore = new ArrayList<>(); - boolean dummyEnchatment = true; - for (Tag enchantmentEntry : enchantments.clone()) { - if (enchantmentEntry instanceof CompoundTag) { - CompoundTag enchEntry = new CompoundTag(""); - String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue(); - if (enchantmentMappings.containsKey(newId)) { - lore.add(new StringTag("", enchantmentMappings.get(newId))); - noMapped.add(enchantmentEntry); - } else if (!newId.isEmpty()) { - dummyEnchatment = false; - 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())); - ench.add(enchEntry); - } - } - } - if (dummyEnchatment && enchantments.size() > 0) { - ByteTag hideFlags = tag.get("HideFlags"); - if (hideFlags == null) { - hideFlags = new ByteTag("HideFlags"); - tag.put(new ByteTag(NBT_TAG_NAME + "|noHideFlags")); - } - tag.put(new ByteTag(NBT_TAG_NAME + "|dummyEnchatment", hideFlags.getValue())); - - CompoundTag enchEntry = new CompoundTag(""); - enchEntry.put(new ShortTag("id", (short) 0)); - enchEntry.put(new ShortTag("lvl", (short) 1)); - - byte value = hideFlags.getValue(); - hideFlags.setValue(value |= 1); - tag.put(hideFlags); - - 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); - } + rewriteEnchantmentsToClient(tag, false); } 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(); - if (enchantmentMappings.containsKey(newId)) { - lore.add(new StringTag("", enchantmentMappings.get(newId) + " " + getRomanNumber((Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue()))); - 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); - } - } - } - 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); - } + rewriteEnchantmentsToClient(tag, true); } + if (tag.get(NBT_TAG_NAME + "|CanPlaceOn") instanceof ListTag) { tag.put(ConverterRegistry.convertToTag( "CanPlaceOn", @@ -877,6 +756,74 @@ public class BlockItemPackets1_13 extends BlockItemRewriter lore = new ArrayList<>(); + for (Tag enchantmentEntry : enchantments.clone()) { + CompoundTag enchEntry = new CompoundTag(""); + String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue(); + if (enchantmentMappings.containsKey(newId)) { + lore.add(new StringTag("", enchantmentMappings.get(newId) + " " + + getRomanNumber((Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue()))); + noMapped.add(enchantmentEntry); + } else if (!newId.isEmpty()) { + 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())); + newEnchantments.add(enchEntry); + } + } + + if (!lore.isEmpty()) { + if (!storedEnch && newEnchantments.size() == 0) { + IntTag hideFlags = tag.get("HideFlags"); + if (hideFlags == null) { + hideFlags = new IntTag("HideFlags"); + } else { + tag.put(new IntTag(NBT_TAG_NAME + "|OldHideFlags", hideFlags.getValue())); + } + + CompoundTag enchEntry = new CompoundTag(""); + enchEntry.put(new ShortTag("id", (short) 0)); + enchEntry.put(new ShortTag("lvl", (short) 1)); + + hideFlags.setValue(hideFlags.getValue() | 1); + tag.put(hideFlags); + + newEnchantments.add(enchEntry); + } + + tag.put(noMapped); + + CompoundTag display = tag.get("display"); + if (display == null) { + tag.put(display = new CompoundTag("display")); + } + 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); + for (Tag value : lore) { + oldLore.add(value.clone()); + } + display.put(oldLore); + + lore.addAll(loreTag.getValue()); + loreTag.setValue(lore); + } + + tag.remove("Enchantments"); + tag.put(newEnchantments); + } + @Override protected Item handleItemToServer(Item item) { if (item == null) return null; @@ -949,116 +896,12 @@ public class BlockItemPackets1_13 extends BlockItemRewriter iterator = noMapped.iterator(); - while (iterator.hasNext()) { - enchantments.add(iterator.next()); - } - tag.remove(NBT_TAG_NAME + "|Enchantments"); - } - tag.remove("ench"); - tag.put(enchantments); + rewriteEnchantmentsToServer(tag, false); } if (tag.get("StoredEnchantments") instanceof ListTag) { - ListTag storedEnch = tag.get("StoredEnchantments"); - ListTag newStoredEnch = new ListTag("StoredEnchantments", CompoundTag.class); - for (Tag enchEntry : storedEnch) { - if (enchEntry instanceof CompoundTag) { - CompoundTag enchantmentEntry = new CompoundTag(""); - short oldId = ((Number) ((CompoundTag) enchEntry).get("id").getValue()).shortValue(); - String newId = MappingData.oldEnchantmentsIds.get(oldId); - if (newId == null) { - newId = "viaversion:legacy/" + oldId; - } - enchantmentEntry.put(new StringTag("id", - newId - )); - enchantmentEntry.put(new ShortTag("lvl", ((Number) ((CompoundTag) enchEntry).get("lvl").getValue()).shortValue())); - newStoredEnch.add(enchantmentEntry); - } - } - if (tag.get(NBT_TAG_NAME + "|Enchantments") instanceof ListTag) { - ListTag noMapped = tag.get(NBT_TAG_NAME + "|StoredEnchantments"); - Iterator iterator = noMapped.iterator(); - while (iterator.hasNext()) { - newStoredEnch.add(iterator.next()); - } - } - tag.remove("StoredEnchantments"); - tag.put(newStoredEnch); - } - if (tag.get("CanPlaceOn") instanceof ListTag) { - ListTag old = tag.get("CanPlaceOn"); - ListTag newCanPlaceOn = new ListTag("CanPlaceOn", StringTag.class); - tag.put(ConverterRegistry.convertToTag(NBT_TAG_NAME + "|CanPlaceOn", ConverterRegistry.convertToValue(old))); // There will be data losing - for (Tag oldTag : old) { - Object value = oldTag.getValue(); - String[] newValues = BlockIdData.blockIdMapping.get(value instanceof String - ? ((String) value).replace("minecraft:", "") - : null); - if (newValues != null) { - for (String newValue : newValues) { - newCanPlaceOn.add(new StringTag("", newValue)); - } - } else { - newCanPlaceOn.add(oldTag); - } - } - tag.put(newCanPlaceOn); - } - if (tag.get("CanDestroy") instanceof ListTag) { - ListTag old = tag.get("CanDestroy"); - ListTag newCanDestroy = new ListTag("CanDestroy", StringTag.class); - tag.put(ConverterRegistry.convertToTag(NBT_TAG_NAME + "|CanDestroy", ConverterRegistry.convertToValue(old))); // There will be data losing - for (Tag oldTag : old) { - Object value = oldTag.getValue(); - String[] newValues = BlockIdData.blockIdMapping.get(value instanceof String - ? ((String) value).replace("minecraft:", "") - : null); - if (newValues != null) { - for (String newValue : newValues) { - newCanDestroy.add(new StringTag("", newValue)); - } - } else { - newCanDestroy.add(oldTag); - } - } - tag.put(newCanDestroy); + rewriteEnchantmentsToServer(tag, true); } + // Handle SpawnEggs if (item.getId() == 383) { if (tag.get("EntityTag") instanceof CompoundTag) { @@ -1110,6 +953,68 @@ public class BlockItemPackets1_13 extends BlockItemRewriter