Skip to content

Commit 5026c2c

Browse files
authored
Fix constant list Variable Card deserialization failure after world reload (#1639)
Closes #1639
1 parent 8cbaafd commit 5026c2c

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

src/main/java/org/cyclops/integrateddynamics/core/evaluate/variable/ValueTypeListProxyMaterializedFactory.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.cyclops.integrateddynamics.core.evaluate.variable;
22

33
import com.google.common.collect.ImmutableList;
4+
import net.minecraft.nbt.ByteTag;
45
import net.minecraft.nbt.CompoundTag;
56
import net.minecraft.nbt.IntTag;
67
import net.minecraft.nbt.ListTag;
8+
import net.minecraft.nbt.LongTag;
79
import net.minecraft.nbt.Tag;
810
import net.minecraft.resources.ResourceLocation;
911
import org.cyclops.integrateddynamics.Reference;
@@ -67,12 +69,31 @@ public ValueTypeListProxyMaterialized<IValueType<IValue>, IValue> deserialize(Va
6769
if (!tag.contains("valueType", Tag.TAG_STRING)) {
6870
throw new IValueTypeListProxyFactoryTypeRegistry.SerializationException(String.format("Could not deserialize the materialized list value '%s' as it is missing a valueType.", value));
6971
}
70-
// This tag rewrite needed for loading variables in advancement icons
72+
// These tag rewrites are needed for loading variables in advancement icons,
73+
// and also to handle the case where Minecraft's NbtOps codec converts
74+
// ListTag<ByteTag> -> ByteArrayTag, ListTag<IntTag> -> IntArrayTag, ListTag<LongTag> -> LongArrayTag
75+
// during serialization (e.g. when saving item NBT).
7176
if (tag.contains("values", Tag.TAG_BYTE_ARRAY)) {
7277
byte[] byteArray = tag.getByteArray("values");
7378
ListTag list = new ListTag();
7479
for (byte b : byteArray) {
75-
list.add(IntTag.valueOf(b));
80+
list.add(ByteTag.valueOf(b));
81+
}
82+
tag.put("values", list);
83+
}
84+
if (tag.contains("values", Tag.TAG_INT_ARRAY)) {
85+
int[] intArray = tag.getIntArray("values");
86+
ListTag list = new ListTag();
87+
for (int i : intArray) {
88+
list.add(IntTag.valueOf(i));
89+
}
90+
tag.put("values", list);
91+
}
92+
if (tag.contains("values", Tag.TAG_LONG_ARRAY)) {
93+
long[] longArray = tag.getLongArray("values");
94+
ListTag list = new ListTag();
95+
for (long l : longArray) {
96+
list.add(LongTag.valueOf(l));
7697
}
7798
tag.put("values", list);
7899
}

src/test/java/org/cyclops/integrateddynamics/core/evaluate/variable/TestValueTypeListProxyFactories.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.cyclops.integrateddynamics.core.evaluate.variable;
22

33
import com.google.common.collect.Lists;
4+
import net.minecraft.nbt.ByteArrayTag;
45
import net.minecraft.nbt.CompoundTag;
6+
import net.minecraft.nbt.IntArrayTag;
7+
import net.minecraft.nbt.LongArrayTag;
58
import net.minecraft.nbt.Tag;
69
import org.cyclops.cyclopscore.helper.CyclopsCoreInstance;
710
import org.cyclops.integrateddynamics.ModBaseMocked;
@@ -62,6 +65,48 @@ public void testMaterialized() throws IValueTypeListProxyFactoryTypeRegistry.Ser
6265
));
6366
}
6467

68+
@Test
69+
public void testMaterializedIntegerListFromIntArrayTag() throws IValueTypeListProxyFactoryTypeRegistry.SerializationException {
70+
// Simulate codec round-trip: ListTag<IntTag> -> IntArrayTag (as done by Minecraft's NbtOps)
71+
ValueTypeListProxyMaterialized<?, ?> proxy = new ValueTypeListProxyMaterialized<>(
72+
ValueTypes.INTEGER,
73+
Lists.newArrayList(ValueTypeInteger.ValueInteger.of(42))
74+
);
75+
Tag serialized = ValueTypeListProxyFactories.REGISTRY.serialize(ValueDeseralizationContextMocked.get(), proxy);
76+
CompoundTag tag = (CompoundTag) serialized;
77+
tag.put("values", new IntArrayTag(new int[]{42}));
78+
IValueTypeListProxy<?, ?> proxyNew = ValueTypeListProxyFactories.REGISTRY.deserialize(ValueDeseralizationContextMocked.get(), tag);
79+
assertThat(proxyNew, equalTo(proxy));
80+
}
81+
82+
@Test
83+
public void testMaterializedLongListFromLongArrayTag() throws IValueTypeListProxyFactoryTypeRegistry.SerializationException {
84+
// Simulate codec round-trip: ListTag<LongTag> -> LongArrayTag (as done by Minecraft's NbtOps)
85+
ValueTypeListProxyMaterialized<?, ?> proxy = new ValueTypeListProxyMaterialized<>(
86+
ValueTypes.LONG,
87+
Lists.newArrayList(ValueTypeLong.ValueLong.of(123L))
88+
);
89+
Tag serialized = ValueTypeListProxyFactories.REGISTRY.serialize(ValueDeseralizationContextMocked.get(), proxy);
90+
CompoundTag tag = (CompoundTag) serialized;
91+
tag.put("values", new LongArrayTag(new long[]{123L}));
92+
IValueTypeListProxy<?, ?> proxyNew = ValueTypeListProxyFactories.REGISTRY.deserialize(ValueDeseralizationContextMocked.get(), tag);
93+
assertThat(proxyNew, equalTo(proxy));
94+
}
95+
96+
@Test
97+
public void testMaterializedBooleanListFromByteArrayTag() throws IValueTypeListProxyFactoryTypeRegistry.SerializationException {
98+
// Simulate codec round-trip: ListTag<ByteTag> -> ByteArrayTag (as done by Minecraft's NbtOps)
99+
ValueTypeListProxyMaterialized<?, ?> proxy = new ValueTypeListProxyMaterialized<>(
100+
ValueTypes.BOOLEAN,
101+
Lists.newArrayList(ValueTypeBoolean.ValueBoolean.of(true))
102+
);
103+
Tag serialized = ValueTypeListProxyFactories.REGISTRY.serialize(ValueDeseralizationContextMocked.get(), proxy);
104+
CompoundTag tag = (CompoundTag) serialized;
105+
tag.put("values", new ByteArrayTag(new byte[]{(byte) 1}));
106+
IValueTypeListProxy<?, ?> proxyNew = ValueTypeListProxyFactories.REGISTRY.deserialize(ValueDeseralizationContextMocked.get(), tag);
107+
assertThat(proxyNew, equalTo(proxy));
108+
}
109+
65110
@Test
66111
public void testNbtKeys() throws IValueTypeListProxyFactoryTypeRegistry.SerializationException {
67112
testFactoryType(new ValueTypeListProxyNbtKeys(

0 commit comments

Comments
 (0)