Fix empty data attachments being saved. ()

* Fix empty data attachments being saved.

* Checkstyle

(cherry picked from commit b90db5748f)
This commit is contained in:
modmuss 2024-02-12 18:01:13 +00:00 committed by modmuss50
parent 2ee1668c3c
commit 6de560bbe1
2 changed files with 29 additions and 4 deletions
fabric-data-attachment-api-v1/src
main/java/net/fabricmc/fabric/impl/attachment
test/java/net/fabricmc/fabric/test/attachment

View file

@ -39,7 +39,7 @@ public class AttachmentSerializingImpl {
@SuppressWarnings("unchecked")
public static void serializeAttachmentData(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup, @Nullable IdentityHashMap<AttachmentType<?>, ?> attachments) {
if (attachments == null) {
if (attachments == null || attachments.isEmpty()) {
return;
}
@ -64,10 +64,10 @@ public class AttachmentSerializingImpl {
nbt.put(AttachmentTarget.NBT_ATTACHMENT_KEY, compound);
}
@Nullable
public static IdentityHashMap<AttachmentType<?>, Object> deserializeAttachmentData(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) {
var attachments = new IdentityHashMap<AttachmentType<?>, Object>();
if (nbt.contains(AttachmentTarget.NBT_ATTACHMENT_KEY, NbtElement.COMPOUND_TYPE)) {
var attachments = new IdentityHashMap<AttachmentType<?>, Object>();
NbtCompound compound = nbt.getCompound(AttachmentTarget.NBT_ATTACHMENT_KEY);
for (String key : compound.getKeys()) {
@ -93,9 +93,15 @@ public class AttachmentSerializingImpl {
);
}
}
if (attachments.isEmpty()) {
return null;
}
return attachments;
}
return attachments;
return null;
}
public static boolean hasPersistentAttachments(@Nullable IdentityHashMap<AttachmentType<?>, ?> map) {

View file

@ -147,6 +147,25 @@ public class CommonAttachmentTests {
assertEquals(0.5d, entry.getValue());
}
@Test
void deserializeNull() {
var nbt = new NbtCompound();
assertNull(AttachmentSerializingImpl.deserializeAttachmentData(nbt, null));
nbt.put(new Identifier("test").toString(), new NbtCompound());
assertNull(AttachmentSerializingImpl.deserializeAttachmentData(nbt, null));
}
@Test
void serializeNullOrEmpty() {
var nbt = new NbtCompound();
AttachmentSerializingImpl.serializeAttachmentData(nbt, null, null);
assertFalse(nbt.contains(AttachmentTarget.NBT_ATTACHMENT_KEY));
AttachmentSerializingImpl.serializeAttachmentData(nbt, null, new IdentityHashMap<>());
assertFalse(nbt.contains(AttachmentTarget.NBT_ATTACHMENT_KEY));
}
@Test
void testEntityCopy() {
AttachmentType<Boolean> notCopiedOnRespawn = AttachmentRegistry.create(