Skip to content
Open
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
20 changes: 20 additions & 0 deletions nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -149,6 +150,17 @@ public interface CompoundBinaryTag extends BinaryTag, CompoundTagSetter<Compound
return new CompoundTagBuilder();
}

/**
* Creates a builder, pre-filled with the contents of {@code existing}.
*
* @param existing the compound to pre-fill the builder with
* @return a new builder
* @since 4.25.0
*/
static @NotNull Builder builder(final @NotNull CompoundBinaryTag existing) {
return new CompoundTagBuilder(existing);
}

@Override
default @NotNull BinaryTagType<CompoundBinaryTag> type() {
return BinaryTagTypes.COMPOUND;
Expand All @@ -162,6 +174,14 @@ public interface CompoundBinaryTag extends BinaryTag, CompoundTagSetter<Compound
*/
@NotNull Set<String> keySet();

/**
* Returns an unmodifiable map representing the contents of this compound.
*
* @return an unmodifiable map of contents
* @since 4.25.0
*/
@Unmodifiable @NotNull Map<String, BinaryTag> asMap();

/**
* Gets a tag.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public boolean contains(final @NotNull String key, final @NotNull BinaryTagType<
return Collections.unmodifiableSet(this.tags.keySet());
}

@Override
public @NotNull Map<String, BinaryTag> asMap() {
return this.tags;
}

@Override
public @Nullable BinaryTag get(final String key) {
return this.tags.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
final class CompoundTagBuilder implements CompoundBinaryTag.Builder {
private @Nullable Map<String, BinaryTag> tags;

CompoundTagBuilder() {
}

CompoundTagBuilder(final CompoundBinaryTag existing) {
this.tags = new HashMap<>(existing.asMap()); // explicitly copy
}

private Map<String, BinaryTag> tags() {
if (this.tags == null) {
this.tags = new HashMap<>();
Expand Down