From 5ce1e850c8f3217e3686203edc602dda2ad6c87f Mon Sep 17 00:00:00 2001 From: KennyTV Date: Thu, 2 Jul 2020 15:57:34 +0200 Subject: [PATCH] Rewrite item nbt attributes --- .../Protocol1_16_2To1_16_1.java | 2 +- .../packets/EntityPackets.java | 8 +-- .../data/MappingData.java | 7 +-- .../packets/InventoryPackets.java | 58 +++++++++++++++++++ 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java index 4cca292c3..c6b2f5a5c 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java @@ -50,7 +50,7 @@ public class Protocol1_16_2To1_16_1 extends Protocol { short gamemode = wrapper.read(Type.UNSIGNED_BYTE); - boolean hardcore = (gamemode & 0x08) != 0; + wrapper.write(Type.BOOLEAN, (gamemode & 0x08) != 0); // Hardcore + gamemode &= ~0x08; - wrapper.write(Type.BOOLEAN, hardcore); // Hardcore wrapper.write(Type.UNSIGNED_BYTE, gamemode); }); map(Type.BYTE); // Previous Gamemode @@ -45,7 +45,7 @@ public class EntityPackets { ClientWorld clientChunks = wrapper.user().get(ClientWorld.class); String dimension = wrapper.get(Type.STRING, 0); clientChunks.setEnvironment(dimension); - wrapper.user().get(EntityTracker1_16.class).addEntity(wrapper.get(Type.INT, 0), Entity1_16_2Types.EntityType.PLAYER); + wrapper.user().get(EntityTracker1_16_2.class).addEntity(wrapper.get(Type.INT, 0), Entity1_16_2Types.EntityType.PLAYER); }); } }); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/data/MappingData.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/data/MappingData.java index 410091a76..a70744838 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/data/MappingData.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/data/MappingData.java @@ -1,17 +1,16 @@ package us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; import com.google.gson.JsonObject; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.Mappings; import us.myles.ViaVersion.util.Int2IntBiMap; -import java.util.HashMap; -import java.util.Map; - public class MappingData { public static Int2IntBiMap oldToNewItems = new Int2IntBiMap(); - public static Map attributeMappings = new HashMap<>(); + public static BiMap attributeMappings = HashBiMap.create(); public static Mappings blockMappings; public static Mappings blockStateMappings; public static Mappings soundMappings; diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java index 4bc322390..e08dbed0f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java @@ -2,6 +2,8 @@ package us.myles.ViaVersion.protocols.protocol1_16to1_15_2.packets; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.IntArrayTag; +import com.github.steveice10.opennbt.tag.builtin.ListTag; +import com.github.steveice10.opennbt.tag.builtin.LongTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import us.myles.ViaVersion.api.Via; @@ -105,6 +107,7 @@ public class InventoryPackets { } } + oldToNewAttributes(item); item.setIdentifier(getNewItemId(item.getIdentifier())); } @@ -125,6 +128,61 @@ public class InventoryPackets { } } } + + newToOldAttributes(item); + } + + public static void oldToNewAttributes(Item item) { + if (item.getTag() == null) return; + + ListTag attributes = item.getTag().get("AttributeModifiers"); + if (attributes == null) return; + + for (Tag tag : attributes) { + CompoundTag attribute = (CompoundTag) tag; + rewriteAttributeName(attribute, "AttributeName", false); + rewriteAttributeName(attribute, "Name", false); + Tag leastTag = attribute.get("UUIDLeast"); + if (leastTag != null) { + Tag mostTag = attribute.get("UUIDMost"); + int[] uuidIntArray = UUIDIntArrayType.bitsToIntArray(((Number) leastTag.getValue()).longValue(), ((Number) mostTag.getValue()).longValue()); + attribute.put(new IntArrayTag("UUID", uuidIntArray)); + } + } + } + + public static void newToOldAttributes(Item item) { + if (item.getTag() == null) return; + + ListTag attributes = item.getTag().get("AttributeModifiers"); + if (attributes == null) return; + + for (Tag tag : attributes) { + CompoundTag attribute = (CompoundTag) tag; + rewriteAttributeName(attribute, "AttributeName", true); + rewriteAttributeName(attribute, "Name", true); + IntArrayTag uuidTag = attribute.get("UUID"); + if (uuidTag != null) { + UUID uuid = UUIDIntArrayType.uuidFromIntArray(uuidTag.getValue()); + attribute.put(new LongTag("UUIDLeast", uuid.getLeastSignificantBits())); + attribute.put(new LongTag("UUIDMost", uuid.getMostSignificantBits())); + } + } + } + + public static void rewriteAttributeName(CompoundTag compoundTag, String entryName, boolean inverse) { + StringTag attributeNameTag = compoundTag.get("AttributeName"); + if (attributeNameTag == null) return; + + String attributeName = attributeNameTag.getValue(); + if (inverse && !attributeName.startsWith("minecraft:")) { + attributeName = "minecraft:" + attributeName; + } + + String mappedAttribute = (inverse ? MappingData.attributeMappings.inverse() : MappingData.attributeMappings).get(attributeName); + if (mappedAttribute == null) return; + + attributeNameTag.setValue(mappedAttribute); } public static int getNewItemId(int id) {