Skip to content

Commit

Permalink
Adjust NbtSerializer to prevent issues with equality and hashcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon-Seeker committed Nov 11, 2024
1 parent 805f90b commit 2f31189
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void writeBytes(SerializationContext ctx, byte[] bytes) {
this.consume(new NbtByteArray(bytes));
}

private final Set<NbtElement> encodedOptionals = Collections.newSetFromMap(new WeakHashMap<>());
private final Set<IdentityHolder<NbtElement>> encodedOptionals = Collections.newSetFromMap(new WeakHashMap<>());

@Override
public <V> void writeOptional(SerializationContext ctx, Endec<V> endec, Optional<V> optional) {
Expand All @@ -115,7 +115,7 @@ public <V> void writeOptional(SerializationContext ctx, Endec<V> endec, Optional

var compound = encoded.require("optional representation");

encodedOptionals.add(compound);
encodedOptionals.add(new IdentityHolder<>(compound));
frameData.setValue(compound);
});

Expand Down Expand Up @@ -177,7 +177,7 @@ public <F> Struct field(String name, SerializationContext ctx, Endec<F> endec, F

var element = encoded.require("struct field");

if (mayOmit && NbtSerializer.this.encodedOptionals.contains(element)) {
if (mayOmit && NbtSerializer.this.encodedOptionals.contains(new IdentityHolder<>(element))) {
var nbtCompound = (NbtCompound) element;

if(!nbtCompound.getBoolean("present")) return;
Expand Down

0 comments on commit 2f31189

Please sign in to comment.