From 6d996a93c839b03b10434306417d9825b2574118 Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Tue, 17 Jul 2018 17:40:39 -0300 Subject: [PATCH] Do not create unnecessary tag --- .../data/MappingData.java | 5 +- .../packets/InventoryPackets.java | 93 ++++++++++--------- .../assets/viaversion/data/mapping-1.12.json | 2 +- 3 files changed, 50 insertions(+), 50 deletions(-) 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 712d2096e..53a134aca 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 @@ -15,8 +15,7 @@ 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 BiMap oldToNewItems = HashBiMap.create(); public static Map blockTags = new HashMap<>(); public static Map itemTags = new HashMap<>(); public static Map fluidTags = new HashMap<>(); @@ -32,8 +31,6 @@ 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")); 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 9bf5766cd..c03d6b773 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 @@ -263,66 +263,63 @@ public class InventoryPackets { // TODO Rewrite identifiers public static void toClient(Item item) { if (item == null) return; - - // create tag CompoundTag tag = item.getTag(); - if (tag == null) { - item.setTag(tag = new CompoundTag("tag")); - } // Save original id int originalId = (item.getId() << 16 | item.getData() & 0xFFFF); - tag.put(new IntTag(NBT_TAG_NAME, originalId)); - // NBT changes + // NBT Additions if (isDamageable(item.getId())) { + if (tag == null) item.setTag(tag = new CompoundTag("tag")); tag.put(new IntTag("Damage", item.getData())); } - if (item.getId() == 358) { // map + if (tag == null) item.setTag(tag = new CompoundTag("tag")); tag.put(new IntTag("map", item.getData())); } - if (item.getId() == 442) { // shield - if (tag.get("BlockEntityTag") instanceof CompoundTag) { - CompoundTag blockEntityTag = tag.get("BlockEntityTag"); - if (blockEntityTag.get("Base") instanceof IntTag) { - IntTag base = blockEntityTag.get("Base"); - base.setValue(15 - base.getValue()); // invert color id + // NBT Changes + if (tag != null) { + // Invert shield color id + if (item.getId() == 442) { + if (tag.get("BlockEntityTag") instanceof CompoundTag) { + CompoundTag blockEntityTag = tag.get("BlockEntityTag"); + if (blockEntityTag.get("Base") instanceof IntTag) { + IntTag base = blockEntityTag.get("Base"); + base.setValue(15 - base.getValue()); + } } } - } - - // 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() + // 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() ) - )); - enchantmentEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchEntry).get("lvl").getValue())); - enchantments.add(enchantmentEntry); + ); } } - tag.remove("ench"); - tag.put(enchantments); + // 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); @@ -345,7 +342,13 @@ public class InventoryPackets { } if (!MappingData.oldToNewItems.containsKey(rawId)) { - if (MappingData.oldToNewItems.containsKey(rawId & ~0xF)) { + if (!isDamageable(item.getId()) && item.getId() != 358) { // Map + if (tag == null) item.setTag(tag = new CompoundTag("tag")); + tag.put(new IntTag(NBT_TAG_NAME, originalId)); // Data will be lost, saving original id + } + if (item.getId() == 31 && item.getData() == 0) { // Shrub was removed + rawId = MappingData.oldToNewItems.get(512); // Dead Bush + } else if (MappingData.oldToNewItems.containsKey(rawId & ~0xF)) { rawId &= ~0xF; // Remove data } else { System.out.println("FAILED TO GET 1.13 ITEM FOR " + item.getId()); // TODO: Make this nicer etc, perhaps fix issues with mapping :T @@ -378,7 +381,7 @@ public class InventoryPackets { } if (rawId == null) { - Integer oldId = MappingData.newToOldItems.get((int) item.getId()); + Integer oldId = MappingData.oldToNewItems.inverse().get((int) item.getId()); if (oldId != null) { // Handle spawn eggs Optional eggEntityId = SpawnEggRewriter.getEntityId(oldId); 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 29822180c..300730002 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 @@ -204,7 +204,7 @@ "476": "minecraft:sticky_piston[extended=true,facing=west]", "477": "minecraft:sticky_piston[extended=true,facing=east]", "480": "minecraft:cobweb", - "496": "minecraft:dead_bush", + //"496": "minecraft:dead_bush", RIP Dead Bush "497": "minecraft:grass", "498": "minecraft:fern", "512": "minecraft:dead_bush",