diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/ProtocolSnapshotTo1_12_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/ProtocolSnapshotTo1_12_2.java index cfffd8771..95f444124 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/ProtocolSnapshotTo1_12_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/ProtocolSnapshotTo1_12_2.java @@ -41,6 +41,10 @@ public class ProtocolSnapshotTo1_12_2 extends Protocol { MappingData.init(); } + public static String jsonTextToLegacy(String value) { + return TextComponent.toLegacyText(ComponentSerializer.parse(value)); + } + @Override protected void registerPackets() { // Register grouped packet changes diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/data/MappingData.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/data/MappingData.java index 5b285ba77..350e1ceb5 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/data/MappingData.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/data/MappingData.java @@ -1,5 +1,7 @@ package us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.data; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -14,9 +16,11 @@ import java.util.Map; public class MappingData { public static Map oldToNewBlocks = new HashMap<>(); public static Map oldToNewItems = new HashMap<>(); + public static Map newToOldItems = new HashMap<>(); public static Map blockTags = new HashMap<>(); public static Map itemTags = new HashMap<>(); public static Map fluidTags = new HashMap<>(); + public static BiMap oldEnchantmentsIds = HashBiMap.create(); public static void init() { JsonObject mapping1_12 = loadData("mapping-1.12.json"); @@ -27,9 +31,14 @@ public class MappingData { mapIdentifiers(oldToNewBlocks, mapping1_12.getAsJsonObject("blocks"), mapping1_13.getAsJsonObject("blocks")); System.out.println("Loading item mapping..."); mapIdentifiers(oldToNewItems, mapping1_12.getAsJsonObject("items"), mapping1_13.getAsJsonObject("items")); + System.out.println("Loading new to old item mapping..."); + mapIdentifiers(newToOldItems, mapping1_13.getAsJsonObject("items"), mapping1_12.getAsJsonObject("items")); + System.out.println("Loading new tags..."); loadTags(blockTags, mapping1_13.getAsJsonObject("block_tags")); loadTags(itemTags, mapping1_13.getAsJsonObject("item_tags")); loadTags(fluidTags, mapping1_13.getAsJsonObject("fluid_tags")); + System.out.println("Loading enchantments..."); + loadEnchantments(oldEnchantmentsIds, mapping1_12.getAsJsonObject("enchantments")); } private static void mapIdentifiers(Map output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { @@ -64,6 +73,12 @@ public class MappingData { } } + public static void loadEnchantments(Map output, JsonObject enchantments) { + for (Map.Entry enchantment : enchantments.entrySet()) { + output.put(Short.parseShort(enchantment.getKey()), enchantment.getValue().getAsString()); + } + } + public static JsonObject loadData(String name) { InputStream stream = MappingData.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name); InputStreamReader reader = new InputStreamReader(stream); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/packets/InventoryPackets.java index af854e967..dfd4c5e2c 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/packets/InventoryPackets.java @@ -1,8 +1,6 @@ package us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.packets; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.opennbt.tag.builtin.IntTag; -import com.github.steveice10.opennbt.tag.builtin.StringTag; +import com.github.steveice10.opennbt.tag.builtin.*; import com.google.common.base.Optional; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.item.Item; @@ -11,6 +9,7 @@ import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.ProtocolSnapshotTo1_12_2; import us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.data.MappingData; import us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.data.SoundSource; import us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.data.SpawnEggRewriter; @@ -212,6 +211,7 @@ public class InventoryPackets { int originalId = (item.getId() << 16 | item.getData() & 0xFFFF); tag.put(new IntTag(NBT_TAG_NAME, originalId)); + // NBT changes if (isDamageable(item.getId())) { tag.put(new IntTag("Damage", item.getData())); } @@ -230,13 +230,45 @@ public class InventoryPackets { } } + // Display Name now uses JSON + if (tag.get("display") instanceof CompoundTag) { + if (((CompoundTag) tag.get("display")).get("Name") instanceof StringTag) { + StringTag name = ((CompoundTag) tag.get("display")).get("Name"); + name.setValue( + ProtocolSnapshotTo1_12_2.legacyTextToJson( + name.getValue() + ) + ); + } + } + + // ench is now Enchantments and now uses identifiers + if (tag.get("ench") instanceof ListTag) { + ListTag ench = tag.get("ench"); + ListTag enchantments = new ListTag("Enchantments", CompoundTag.class); + for (Tag enchEntry : ench) { + if (enchEntry instanceof CompoundTag) { + CompoundTag enchantmentEntry = new CompoundTag(""); + enchantmentEntry.put(new StringTag("id", + MappingData.oldEnchantmentsIds.get( + (Short) ((CompoundTag) enchEntry).get("id").getValue() + ) + )); + enchantmentEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchEntry).get("lvl").getValue())); + enchantments.add(enchantmentEntry); + } + } + tag.remove("ench"); + tag.put(enchantments); + } + int rawId = (item.getId() << 4 | item.getData() & 0xF); // Handle SpawnEggs if (item.getId() == 383) { - if (tag.contains("EntityTag")) { + if (tag.get("EntityTag") instanceof CompoundTag) { CompoundTag entityTag = tag.get("EntityTag"); - if (entityTag.contains("id") && entityTag.get("id") instanceof StringTag) { + if (entityTag.get("id") instanceof StringTag) { StringTag identifier = entityTag.get("id"); rawId = SpawnEggRewriter.getSpawnEggId(identifier.getValue()); } else { @@ -271,36 +303,33 @@ public class InventoryPackets { CompoundTag tag = item.getTag(); + // Use tag to get original ID and data if (tag != null) { // Check for valid tag - if (tag.contains(NBT_TAG_NAME)) { - if (tag.get(NBT_TAG_NAME) instanceof IntTag) { - rawId = (Integer) tag.get(NBT_TAG_NAME).getValue(); - // Remove the tag - tag.remove(NBT_TAG_NAME); - gotRawIdFromTag = true; - } + if (tag.get(NBT_TAG_NAME) instanceof IntTag) { + rawId = (Integer) tag.get(NBT_TAG_NAME).getValue(); + // Remove the tag + tag.remove(NBT_TAG_NAME); + gotRawIdFromTag = true; } } + if (rawId == null) { - for (Map.Entry entry : MappingData.oldToNewItems.entrySet()) { - if (entry.getValue() == item.getId()) { - int oldId = entry.getKey(); - // Handle spawn eggs - Optional eggEntityId = SpawnEggRewriter.getEntityId(oldId); - if (eggEntityId.isPresent()) { - rawId = 383 << 16; - if (tag == null) - item.setTag(tag = new CompoundTag("tag")); - if (!tag.contains("EntityTag")) { - CompoundTag entityTag = new CompoundTag("EntityTag"); - entityTag.put(new StringTag("id", eggEntityId.get())); - tag.put(entityTag); - } - } else { - rawId = (oldId >> 4) << 16 | oldId & 0xF; + Integer oldId = MappingData.newToOldItems.get((int) item.getId()); + if (oldId != null) { + // Handle spawn eggs + Optional eggEntityId = SpawnEggRewriter.getEntityId(oldId); + if (eggEntityId.isPresent()) { + rawId = 383 << 16; + if (tag == null) + item.setTag(tag = new CompoundTag("tag")); + if (!tag.contains("EntityTag")) { + CompoundTag entityTag = new CompoundTag("EntityTag"); + entityTag.put(new StringTag("id", eggEntityId.get())); + tag.put(entityTag); } - break; + } else { + rawId = (oldId >> 4) << 16 | oldId & 0xF; } } } @@ -313,6 +342,7 @@ public class InventoryPackets { item.setId((short) (rawId >> 16)); item.setData((short) (rawId & 0xFFFF)); + // NBT changes if (tag != null) { if (isDamageable(item.getId())) { if (tag.get("Damage") instanceof IntTag) { @@ -339,6 +369,41 @@ public class InventoryPackets { } } } + + // Display Name now uses JSON + if (tag.get("display") instanceof CompoundTag) { + if (((CompoundTag) tag.get("display")).get("Name") instanceof StringTag) { + StringTag name = ((CompoundTag) tag.get("display")).get("Name"); + name.setValue( + ProtocolSnapshotTo1_12_2.jsonTextToLegacy( + name.getValue() + ) + ); + } + } + + // ench is now Enchantments and now uses identifiers + if (tag.get("Enchantments") instanceof ListTag) { + ListTag enchantments = tag.get("Enchantments"); + ListTag ench = new ListTag("ench", CompoundTag.class); + for (Tag enchantmentEntry : enchantments) { + if (enchantmentEntry instanceof CompoundTag) { + CompoundTag enchEntry = new CompoundTag(""); + enchEntry.put( + new ShortTag( + "id", + MappingData.oldEnchantmentsIds.inverse().get( + (String) ((CompoundTag) enchantmentEntry).get("id").getValue() + ) + ) + ); + enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue())); + ench.add(enchEntry); + } + } + tag.remove("Enchantment"); + tag.put(ench); + } } } diff --git a/common/src/main/resources/assets/viaversion/data/mapping-1.12.json b/common/src/main/resources/assets/viaversion/data/mapping-1.12.json index 9a3962bc1..8fa34e10e 100644 --- a/common/src/main/resources/assets/viaversion/data/mapping-1.12.json +++ b/common/src/main/resources/assets/viaversion/data/mapping-1.12.json @@ -2377,5 +2377,37 @@ "25100328": "minecraft:zombie_horse_spawn_egg", "25100329": "minecraft:zombie_pigman_spawn_egg", "25100330": "minecraft:zombie_villager_spawn_egg" + }, + "enchantments": { + "0": "minecraft:protection", + "1": "minecraft:fire_protection", + "2": "minecraft:feather_falling", + "3": "minecraft:blast_protection", + "4": "minecraft:projectile_protection", + "5": "minecraft:respiration", + "6": "minecraft:aqua_affinity", + "7": "minecraft:thorns", + "8": "minecraft:depth_strider", + "9": "minecraft:frost_walker", + "10": "minecraft:binding_curse", + "16": "minecraft:sharpness", + "17": "minecraft:smite", + "18": "minecraft:bane_of_arthropods", + "19": "minecraft:knockback", + "20": "minecraft:fire_aspect", + "21": "minecraft:looting", + "22": "minecraft:sweeping", + "32": "minecraft:efficiency", + "33": "minecraft:silk_touch", + "34": "minecraft:unbreaking", + "35": "minecraft:fortune", + "48": "minecraft:power", + "49": "minecraft:punch", + "50": "minecraft:flame", + "51": "minecraft:infinity", + "61": "minecraft:luck_of_the_sea", + "62": "minecraft:lure", + "70": "minecraft:mending", + "71": "minecraft:vanishing_curse" } } \ No newline at end of file