From aa262bb0a56bc4ca2ae9ac6cc57c8f1743f73cd7 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Wed, 28 Feb 2024 22:46:33 +0100 Subject: [PATCH] Rename itemdata to be more generic --- .../viaversion/api/data/MappingData.java | 2 +- .../viaversion/api/data/MappingDataBase.java | 4 ++-- .../StructuredData.java} | 11 +++++---- .../api/minecraft/item/DataItem.java | 3 ++- .../api/minecraft/item/DynamicItem.java | 12 ++++++---- .../viaversion/api/minecraft/item/Item.java | 3 ++- .../api/type/types/item/ItemType1_20_5.java | 22 +++++++++--------- ...mDataType.java => StructuredDataType.java} | 20 ++++++++-------- .../api/type/types/version/Types1_20_5.java | 4 ++-- .../BlockItemPacketRewriter1_20_5.java | 6 ++--- .../viaversion/data/identifiers-1.20.3.nbt | Bin 4719 -> 4722 bytes .../viaversion/data/identifiers-1.20.5.nbt | Bin 4791 -> 4794 bytes 12 files changed, 46 insertions(+), 41 deletions(-) rename api/src/main/java/com/viaversion/viaversion/api/minecraft/{item/ItemData.java => data/StructuredData.java} (84%) rename api/src/main/java/com/viaversion/viaversion/api/type/types/item/{ItemDataType.java => StructuredDataType.java} (80%) diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/MappingData.java b/api/src/main/java/com/viaversion/viaversion/api/data/MappingData.java index 015233e80..66389e99a 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/data/MappingData.java +++ b/api/src/main/java/com/viaversion/viaversion/api/data/MappingData.java @@ -115,7 +115,7 @@ public interface MappingData { @Nullable FullMappings getRecipeSerializerMappings(); - FullMappings getItemDataSerializerMappings(); + FullMappings getDataComponentSerializerMappings(); @Nullable Mappings getPaintingMappings(); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java index c605e2d24..0857fdc0e 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java +++ b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java @@ -87,7 +87,7 @@ public class MappingDataBase implements MappingData { entityMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "entities"); argumentTypeMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "argumenttypes"); recipeSerializerMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "recipe_serializers"); - itemDataSerializerMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "item_serializers"); + itemDataSerializerMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "data_component_type"); final ListTag unmappedParticles = unmappedIdentifierData.get("particles"); final ListTag mappedParticles = mappedIdentifierData.get("particles"); @@ -241,7 +241,7 @@ public class MappingDataBase implements MappingData { } @Override - public @Nullable FullMappings getItemDataSerializerMappings() { + public @Nullable FullMappings getDataComponentSerializerMappings() { return itemDataSerializerMappings; } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/ItemData.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredData.java similarity index 84% rename from api/src/main/java/com/viaversion/viaversion/api/minecraft/item/ItemData.java rename to api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredData.java index b9d01736d..9ea4e5821 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/ItemData.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredData.java @@ -20,27 +20,28 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package com.viaversion.viaversion.api.minecraft.item; +package com.viaversion.viaversion.api.minecraft.data; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.util.IdHolder; import com.viaversion.viaversion.util.Unit; import io.netty.buffer.ByteBuf; -public final class ItemData implements IdHolder { +public final class StructuredData implements IdHolder { private final Type type; private T value; private int id; - public ItemData(final Type type, final T value, final int id) { + public StructuredData(final Type type, final T value, final int id) { this.type = type; this.value = value; this.id = id; } - public static ItemData empty(final int id) { - return new ItemData<>(Type.UNIT, Unit.INSTANCE, id); + public static StructuredData empty(final int id) { + // Indicates empty structures, whereas an empty optional is used to remove default values + return new StructuredData<>(Type.UNIT, Unit.INSTANCE, id); } public boolean isEmpty() { diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/DataItem.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/DataItem.java index c0e11fbdd..29368f82c 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/DataItem.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/DataItem.java @@ -24,6 +24,7 @@ package com.viaversion.viaversion.api.minecraft.item; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.google.gson.annotations.SerializedName; +import com.viaversion.viaversion.api.minecraft.data.StructuredData; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import java.util.Objects; @@ -95,7 +96,7 @@ public class DataItem implements Item { } @Override - public Int2ObjectMap>> itemData() { + public Int2ObjectMap>> itemData() { return new Int2ObjectOpenHashMap<>(); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/DynamicItem.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/DynamicItem.java index fe0c9a011..9036c0adc 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/DynamicItem.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/DynamicItem.java @@ -23,13 +23,14 @@ package com.viaversion.viaversion.api.minecraft.item; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.api.minecraft.data.StructuredData; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import java.util.Optional; import org.checkerframework.checker.nullness.qual.Nullable; public class DynamicItem implements Item { - private final Int2ObjectMap>> data; + private final Int2ObjectMap>> data; private int identifier; private byte amount; @@ -37,7 +38,7 @@ public class DynamicItem implements Item { this(0, (byte) 0, new Int2ObjectOpenHashMap<>()); } - public DynamicItem(int identifier, byte amount, Int2ObjectMap>> data) { + public DynamicItem(int identifier, byte amount, Int2ObjectMap>> data) { this.identifier = identifier; this.amount = amount; this.data = data; @@ -77,15 +78,16 @@ public class DynamicItem implements Item { } @Override - public Int2ObjectMap>> itemData() { + public Int2ObjectMap>> itemData() { return data; } - public void addData(ItemData data) { + public void addData(StructuredData data) { this.data.put(data.id(), Optional.of(data)); } - public void addMarkerData(int id) { + public void removeDefault(int id) { + // Empty optional to override the Minecraft default this.data.put(id, Optional.empty()); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/Item.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/Item.java index 5684647b4..f71c40cb6 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/Item.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/Item.java @@ -23,6 +23,7 @@ package com.viaversion.viaversion.api.minecraft.item; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.api.minecraft.data.StructuredData; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import java.util.Optional; import org.checkerframework.checker.nullness.qual.Nullable; @@ -90,7 +91,7 @@ public interface Item { */ void setTag(@Nullable CompoundTag tag); - Int2ObjectMap>> itemData(); + Int2ObjectMap>> itemData(); /** * Returns a copy of the item. diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/item/ItemType1_20_5.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/item/ItemType1_20_5.java index 8e3fce433..b79e25829 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/item/ItemType1_20_5.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/item/ItemType1_20_5.java @@ -24,7 +24,7 @@ package com.viaversion.viaversion.api.type.types.item; import com.viaversion.viaversion.api.minecraft.item.DynamicItem; import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.minecraft.item.ItemData; +import com.viaversion.viaversion.api.minecraft.data.StructuredData; import com.viaversion.viaversion.api.type.Type; import io.netty.buffer.ByteBuf; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; @@ -34,9 +34,9 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class ItemType1_20_5 extends Type { - private final Type> dataType; + private final Type> dataType; - public ItemType1_20_5(final Type> itemDataType) { + public ItemType1_20_5(final Type> itemDataType) { super(Item.class); this.dataType = itemDataType; } @@ -49,20 +49,20 @@ public class ItemType1_20_5 extends Type { } final int id = Type.VAR_INT.readPrimitive(buffer); - final Int2ObjectMap>> data = readData(buffer); + final Int2ObjectMap>> data = readData(buffer); return new DynamicItem(id, amount, data); } - private Int2ObjectMap>> readData(final ByteBuf buffer) throws Exception { + private Int2ObjectMap>> readData(final ByteBuf buffer) throws Exception { final int valuesSize = Type.VAR_INT.readPrimitive(buffer); final int markersSize = Type.VAR_INT.readPrimitive(buffer); if (valuesSize == 0 && markersSize == 0) { return new Int2ObjectOpenHashMap<>(); } - final Int2ObjectMap>> map = new Int2ObjectOpenHashMap<>(valuesSize + markersSize); + final Int2ObjectMap>> map = new Int2ObjectOpenHashMap<>(valuesSize + markersSize); for (int i = 0; i < valuesSize; i++) { - final ItemData value = dataType.read(buffer); + final StructuredData value = dataType.read(buffer); map.put(value.id(), Optional.of(value)); } @@ -83,10 +83,10 @@ public class ItemType1_20_5 extends Type { buffer.writeByte(object.amount()); Type.VAR_INT.writePrimitive(buffer, object.identifier()); - final Int2ObjectMap>> data = object.itemData(); + final Int2ObjectMap>> data = object.itemData(); int valuesSize = 0; int markersSize = 0; - for (final Int2ObjectMap.Entry>> entry : data.int2ObjectEntrySet()) { + for (final Int2ObjectMap.Entry>> entry : data.int2ObjectEntrySet()) { if (entry.getValue().isPresent()) { valuesSize++; } else { @@ -97,12 +97,12 @@ public class ItemType1_20_5 extends Type { Type.VAR_INT.writePrimitive(buffer, valuesSize); Type.VAR_INT.writePrimitive(buffer, markersSize); - for (final Int2ObjectMap.Entry>> entry : data.int2ObjectEntrySet()) { + for (final Int2ObjectMap.Entry>> entry : data.int2ObjectEntrySet()) { if (entry.getValue().isPresent()) { dataType.write(buffer, entry.getValue().get()); } } - for (final Int2ObjectMap.Entry>> entry : data.int2ObjectEntrySet()) { + for (final Int2ObjectMap.Entry>> entry : data.int2ObjectEntrySet()) { if (!entry.getValue().isPresent()) { Type.VAR_INT.writePrimitive(buffer, entry.getIntKey()); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/item/ItemDataType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/item/StructuredDataType.java similarity index 80% rename from api/src/main/java/com/viaversion/viaversion/api/type/types/item/ItemDataType.java rename to api/src/main/java/com/viaversion/viaversion/api/type/types/item/StructuredDataType.java index 6548dffa5..522e1d8ae 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/item/ItemDataType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/item/StructuredDataType.java @@ -23,39 +23,39 @@ package com.viaversion.viaversion.api.type.types.item; import com.viaversion.viaversion.api.data.FullMappings; -import com.viaversion.viaversion.api.minecraft.item.ItemData; +import com.viaversion.viaversion.api.minecraft.data.StructuredData; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.type.Type; import io.netty.buffer.ByteBuf; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; -public class ItemDataType extends Type> { +public class StructuredDataType extends Type> { private final Int2ObjectMap> types = new Int2ObjectOpenHashMap<>(); - public ItemDataType() { - super(ItemData.class); + public StructuredDataType() { + super(StructuredData.class); } @Override - public void write(final ByteBuf buffer, final ItemData object) throws Exception { + public void write(final ByteBuf buffer, final StructuredData object) throws Exception { Type.VAR_INT.writePrimitive(buffer, object.id()); object.write(buffer); } @Override - public ItemData read(final ByteBuf buffer) throws Exception { + public StructuredData read(final ByteBuf buffer) throws Exception { final int id = Type.VAR_INT.readPrimitive(buffer); final Type type = this.types.get(id); if (type != null) { - return readItemData(buffer, type, id); + return readData(buffer, type, id); } throw new IllegalArgumentException("Unknown item data type id: " + id); } - private ItemData readItemData(final ByteBuf buffer, final Type type, final int id) throws Exception { - return new ItemData<>(type, type.read(buffer), id); + private StructuredData readData(final ByteBuf buffer, final Type type, final int id) throws Exception { + return new StructuredData<>(type, type.read(buffer), id); } public DataFiller filler(final Protocol protocol) { @@ -72,7 +72,7 @@ public class ItemDataType extends Type> { private final boolean useMappedNames; private DataFiller(final Protocol protocol, final boolean useMappedNames) { - this.mappings = protocol.getMappingData().getItemDataSerializerMappings(); + this.mappings = protocol.getMappingData().getDataComponentSerializerMappings(); this.useMappedNames = useMappedNames; } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Types1_20_5.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Types1_20_5.java index 7f3fc6da5..1d27a6343 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Types1_20_5.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Types1_20_5.java @@ -28,7 +28,7 @@ import com.viaversion.viaversion.api.minecraft.metadata.types.MetaTypes1_20_5; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.ArrayType; import com.viaversion.viaversion.api.type.types.item.ItemType1_20_5; -import com.viaversion.viaversion.api.type.types.item.ItemDataType; +import com.viaversion.viaversion.api.type.types.item.StructuredDataType; import com.viaversion.viaversion.api.type.types.metadata.MetaListType; import com.viaversion.viaversion.api.type.types.metadata.MetadataType; import com.viaversion.viaversion.api.type.types.misc.ParticleType; @@ -38,7 +38,7 @@ public final class Types1_20_5 { // Only safe to use after protocol loading public static final ParticleType PARTICLE = new ParticleType(); - public static final ItemDataType ITEM_DATA = new ItemDataType(); + public static final StructuredDataType ITEM_DATA = new StructuredDataType(); public static final Type ITEM = new ItemType1_20_5(ITEM_DATA); public static final Type ITEM_ARRAY = new ArrayType<>(ITEM); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java index f6e5cb070..f78159dc7 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java @@ -25,7 +25,7 @@ import com.viaversion.viaversion.api.minecraft.Particle; import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.DynamicItem; import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.minecraft.item.ItemData; +import com.viaversion.viaversion.api.minecraft.data.StructuredData; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_20_2; import com.viaversion.viaversion.api.type.types.version.Types1_20_3; @@ -202,11 +202,11 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter(type, value, id)); + item.addData(new StructuredData<>(type, value, id)); } private int serializerId(final String type) { - return protocol.getMappingData().getItemDataSerializerMappings().mappedId(type); + return protocol.getMappingData().getDataComponentSerializerMappings().mappedId(type); } @Override diff --git a/common/src/main/resources/assets/viaversion/data/identifiers-1.20.3.nbt b/common/src/main/resources/assets/viaversion/data/identifiers-1.20.3.nbt index 64ae6e14bded8a26e45a73aac09f38c3c69ad8f9..d5353a88efa08bc24687c122ad903cc03f5390b3 100644 GIT binary patch delta 33 ocmaE_@=0Yw1HXuHN@7W3d~$wnL4ICpUP*jOWkKrZiTwAN0O_g?umAu6 delta 30 lcmeyQ@?K>_1HYg^W=U#pd~s?~W@1ieRccZ3<|+L5m;l2>40`|o diff --git a/common/src/main/resources/assets/viaversion/data/identifiers-1.20.5.nbt b/common/src/main/resources/assets/viaversion/data/identifiers-1.20.5.nbt index 5d3435dfc1a5eac7cdc84418e132c7e0f9bd2cd1..84cbd575c2523c74d4f8eaa516d89dfd0d94a2d6 100644 GIT binary patch delta 33 pcmdn4x=VG#34Rgbl*E$6_~iWDg8aPHyps5m%7WC*H~H@|0RZIa4blJr delta 30 mcmdm`x?Od{34TF=%#zgH_~O)}%*33`s??(5&3E|kF#!O+^bDN<