Skip to content

Commit

Permalink
Do not swallow errors in case of AttachmentType RW failure
Browse files Browse the repository at this point in the history
Signed-off-by: TheSilkMiner <[email protected]>
  • Loading branch information
TheSilkMiner committed Feb 11, 2025
1 parent 83c257a commit a0130d3
Showing 1 changed file with 13 additions and 3 deletions.
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,26 @@ 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) {
// TODO: Maybe find a way to let the user know *which* attachment has exploded?
return new IllegalStateException("Unable to " + operation + " attachment due to an internal codec error: " + error);
}
});
}
Expand Down

0 comments on commit a0130d3

Please sign in to comment.