diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java index 78e1afa45a..8bdda0d365 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java @@ -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; @@ -149,6 +150,17 @@ public interface CompoundBinaryTag extends BinaryTag, CompoundTagSetter type() { return BinaryTagTypes.COMPOUND; @@ -162,6 +174,14 @@ public interface CompoundBinaryTag extends BinaryTag, CompoundTagSetter keySet(); + /** + * Returns an unmodifiable map representing the contents of this compound. + * + * @return an unmodifiable map of contents + * @since 4.25.0 + */ + @Unmodifiable @NotNull Map asMap(); + /** * Gets a tag. * diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTagImpl.java b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTagImpl.java index f3a6d3d908..8d096c6825 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTagImpl.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTagImpl.java @@ -58,6 +58,11 @@ public boolean contains(final @NotNull String key, final @NotNull BinaryTagType< return Collections.unmodifiableSet(this.tags.keySet()); } + @Override + public @NotNull Map asMap() { + return this.tags; + } + @Override public @Nullable BinaryTag get(final String key) { return this.tags.get(key); diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagBuilder.java b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagBuilder.java index 9d2f3c43b9..7f0b46fd3a 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagBuilder.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagBuilder.java @@ -32,6 +32,13 @@ final class CompoundTagBuilder implements CompoundBinaryTag.Builder { private @Nullable Map tags; + CompoundTagBuilder() { + } + + CompoundTagBuilder(final CompoundBinaryTag existing) { + this.tags = new HashMap<>(existing.asMap()); // explicitly copy + } + private Map tags() { if (this.tags == null) { this.tags = new HashMap<>();