Skip to content

Commit

Permalink
fix #1070 (nbt): ListBinaryTag is weakly immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Apr 29, 2024
1 parent ec3fe59 commit 2e612aa
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public double getDouble(final @NotNull String key, final double defaultValue) {
private CompoundBinaryTag edit(final Consumer<Map<String, BinaryTag>> consumer) {
final Map<String, BinaryTag> tags = new HashMap<>(this.tags);
consumer.accept(tags);
return new CompoundBinaryTagImpl(tags);
return new CompoundBinaryTagImpl(new HashMap<>(tags)); // explicitly copy

This comment has been minimized.

Copy link
@RealBauHD

RealBauHD Apr 29, 2024

Contributor

Isn't it unnecessary here, as the previously created map is not exposed anywhere? Same goes for ListBinaryTagImpl#edit

This comment has been minimized.

Copy link
@kashike

kashike Apr 29, 2024

Author Member

It's not necessary, no, but it doesn't hurt anything.

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ private Map<String, BinaryTag> tags() {
@Override
public @NotNull CompoundBinaryTag build() {
if (this.tags == null) return CompoundBinaryTag.empty();
return new CompoundBinaryTagImpl(new HashMap<>(this.tags));
return new CompoundBinaryTagImpl(new HashMap<>(this.tags)); // explicitly copy
}
}
3 changes: 2 additions & 1 deletion nbt/src/main/java/net/kyori/adventure/nbt/ListBinaryTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package net.kyori.adventure.nbt;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
Expand Down Expand Up @@ -99,7 +100,7 @@ public interface ListBinaryTag extends ListTagSetter<ListBinaryTag, BinaryTag>,
static @NotNull ListBinaryTag listBinaryTag(final @NotNull BinaryTagType<? extends BinaryTag> type, final @NotNull List<BinaryTag> tags) {
if (tags.isEmpty()) return empty();
if (type == BinaryTagTypes.END) throw new IllegalArgumentException("Cannot create a list of " + BinaryTagTypes.END);
return new ListBinaryTagImpl(type, tags);
return new ListBinaryTagImpl(type, new ArrayList<>(tags)); // explicitly copy
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private ListBinaryTag edit(final Consumer<List<BinaryTag>> consumer, final @Null
if (maybeElementType != null && elementType == BinaryTagTypes.END) {
elementType = maybeElementType;
}
return new ListBinaryTagImpl(elementType, tags);
return new ListBinaryTagImpl(elementType, new ArrayList<>(tags)); // explicitly copy
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ final class ListTagBuilder<T extends BinaryTag> implements ListBinaryTag.Builder
@Override
public @NotNull ListBinaryTag build() {
if (this.tags == null) return ListBinaryTag.empty();
return new ListBinaryTagImpl(this.elementType, new ArrayList<>(this.tags));
return new ListBinaryTagImpl(this.elementType, new ArrayList<>(this.tags)); // explicitly copy
}
}

0 comments on commit 2e612aa

Please sign in to comment.