Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport to 1.21.1: Avoid swallowing errors in case of AttachmentType Serialization failure #1955

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,17 @@ public final CompoundTag serializeAttachments(HolderLookup.Provider provider) {
CompoundTag tag = null;
for (var entry : attachments.entrySet()) {
var type = entry.getKey();
var key = NeoForgeRegistries.ATTACHMENT_TYPES.getKey(type);
if (type.serializer != null) {
Tag serialized = ((IAttachmentSerializer<?, Object>) type.serializer).write(entry.getValue(), provider);
if (serialized != null) {
if (tag == null)
tag = new CompoundTag();
tag.put(NeoForgeRegistries.ATTACHMENT_TYPES.getKey(type).toString(), serialized);
try {
Tag serialized = ((IAttachmentSerializer<?, Object>) type.serializer).write(entry.getValue(), provider);
if (serialized != null) {
if (tag == null)
tag = new CompoundTag();
tag.put(key.toString(), serialized);
}
} catch (Exception exception) {
LOGGER.error("Failed to serialize data attachment {}. Skipping.", key, exception);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.google.common.base.Predicates;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -198,17 +199,25 @@ public Builder<T> serialize(Codec<T> codec) {
*/
public Builder<T> serialize(Codec<T> codec, Predicate<? super T> shouldSerialize) {
Objects.requireNonNull(codec);
// TODO: better error handling
return serialize(new IAttachmentSerializer<>() {
@Override
public T read(IAttachmentHolder holder, Tag tag, HolderLookup.Provider provider) {
return codec.parse(provider.createSerializationContext(NbtOps.INSTANCE), tag).result().get();
final DataResult<T> parsingResult = codec.parse(provider.createSerializationContext(NbtOps.INSTANCE), tag);
return parsingResult.getOrThrow(msg -> buildException("read", msg));
}

@Nullable
@Override
public Tag write(T attachment, HolderLookup.Provider provider) {
return shouldSerialize.test(attachment) ? codec.encodeStart(provider.createSerializationContext(NbtOps.INSTANCE), attachment).result().get() : null;
if (!shouldSerialize.test(attachment)) {
return null;
}
final DataResult<Tag> encodingResult = codec.encodeStart(provider.createSerializationContext(NbtOps.INSTANCE), attachment);
return encodingResult.getOrThrow(msg -> buildException("write", msg));
}

private RuntimeException buildException(final String operation, final String error) {
return new IllegalStateException("Unable to " + operation + " attachment due to an internal codec error: " + error);
}
});
}
Expand Down
Loading