Skip to content

Commit

Permalink
Fix empty data attachments being saved. (#3588)
Browse files Browse the repository at this point in the history
* Fix empty data attachments being saved.

* Checkstyle

(cherry picked from commit b90db57)
  • Loading branch information
modmuss50 committed Feb 12, 2024
1 parent 2ee1668 commit 6de560b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -64,10 +64,10 @@ public static void serializeAttachmentData(NbtCompound nbt, RegistryWrapper.Wrap
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()) {
Expand All @@ -93,9 +93,15 @@ public static IdentityHashMap<AttachmentType<?>, Object> deserializeAttachmentDa
);
}
}

if (attachments.isEmpty()) {
return null;
}

return attachments;
}

return attachments;
return null;
}

public static boolean hasPersistentAttachments(@Nullable IdentityHashMap<AttachmentType<?>, ?> map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,25 @@ void testStaticReadWrite() {
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(
Expand Down

0 comments on commit 6de560b

Please sign in to comment.