Fix empty data attachments being saved. (#3588)

* Fix empty data attachments being saved.

* Checkstyle
This commit is contained in:
modmuss 2024-02-12 18:01:13 +00:00
parent 41e516f9db
commit de0fd6d132
2 changed files with 29 additions and 4 deletions

View file

@ -37,7 +37,7 @@ public class AttachmentSerializingImpl {
@SuppressWarnings("unchecked")
public static void serializeAttachmentData(NbtCompound nbt, @Nullable IdentityHashMap<AttachmentType<?>, ?> attachments) {
if (attachments == null) {
if (attachments == null || attachments.isEmpty()) {
return;
}
@ -61,10 +61,10 @@ public class AttachmentSerializingImpl {
nbt.put(AttachmentTarget.NBT_ATTACHMENT_KEY, compound);
}
@Nullable
public static IdentityHashMap<AttachmentType<?>, Object> deserializeAttachmentData(NbtCompound nbt) {
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()) {
@ -89,9 +89,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));
nbt.put(new Identifier("test").toString(), new NbtCompound());
assertNull(AttachmentSerializingImpl.deserializeAttachmentData(nbt));
}
@Test
void serializeNullOrEmpty() {
var nbt = new NbtCompound();
AttachmentSerializingImpl.serializeAttachmentData(nbt, null);
assertFalse(nbt.contains(AttachmentTarget.NBT_ATTACHMENT_KEY));
AttachmentSerializingImpl.serializeAttachmentData(nbt, new IdentityHashMap<>());
assertFalse(nbt.contains(AttachmentTarget.NBT_ATTACHMENT_KEY));
}
@Test
void testEntityCopy() {
AttachmentType<Boolean> notCopiedOnRespawn = AttachmentRegistry.create(