From 6537b42de64a937c517593cc55a36ddcc0318cba Mon Sep 17 00:00:00 2001 From: Aya <31237389+tal5@users.noreply.github.com> Date: Wed, 9 Jul 2025 10:37:32 +0100 Subject: [PATCH 1/3] feature(nbt): `CompoundBinaryTag#createBuilder` --- .../java/net/kyori/adventure/nbt/CompoundBinaryTag.java | 8 ++++++++ .../net/kyori/adventure/nbt/CompoundBinaryTagImpl.java | 5 +++++ .../java/net/kyori/adventure/nbt/CompoundTagBuilder.java | 7 +++++++ 3 files changed, 20 insertions(+) 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..c2cf658b23 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java @@ -154,6 +154,14 @@ public interface CompoundBinaryTag extends BinaryTag, CompoundTagSetter(this.tags)); // explicitly copy + } + @Override public @NotNull Set keySet() { return Collections.unmodifiableSet(this.tags.keySet()); 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..b9becd40df 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 Map tags) { + this.tags = tags; + } + private Map tags() { if (this.tags == null) { this.tags = new HashMap<>(); From 50ccc7fe971a8e1eebecfc9d524cdbd6aba074f5 Mon Sep 17 00:00:00 2001 From: Aya <31237389+tal5@users.noreply.github.com> Date: Thu, 7 Aug 2025 15:02:59 +0100 Subject: [PATCH 2/3] Bump `@since` --- .../main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c2cf658b23..348b7696af 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java @@ -158,7 +158,7 @@ public interface CompoundBinaryTag extends BinaryTag, CompoundTagSetter Date: Wed, 27 Aug 2025 00:23:52 +0100 Subject: [PATCH 3/3] Switch to static factory method --- .../adventure/nbt/CompoundBinaryTag.java | 26 ++++++++++++++----- .../adventure/nbt/CompoundBinaryTagImpl.java | 8 +++--- .../adventure/nbt/CompoundTagBuilder.java | 4 +-- 3 files changed, 25 insertions(+), 13 deletions(-) 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 348b7696af..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,18 +150,21 @@ public interface CompoundBinaryTag extends BinaryTag, CompoundTagSetter type() { - return BinaryTagTypes.COMPOUND; - } - /** - * Creates a builder, pre-filled with the contents of this compound. + * 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 */ - Builder createBuilder(); + static @NotNull Builder builder(final @NotNull CompoundBinaryTag existing) { + return new CompoundTagBuilder(existing); + } + + @Override + default @NotNull BinaryTagType type() { + return BinaryTagTypes.COMPOUND; + } /** * Gets a set of all keys. @@ -170,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 0a56ab6879..8d096c6825 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTagImpl.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTagImpl.java @@ -54,13 +54,13 @@ public boolean contains(final @NotNull String key, final @NotNull BinaryTagType< } @Override - public Builder createBuilder() { - return new CompoundTagBuilder(new HashMap<>(this.tags)); // explicitly copy + public @NotNull Set keySet() { + return Collections.unmodifiableSet(this.tags.keySet()); } @Override - public @NotNull Set keySet() { - return Collections.unmodifiableSet(this.tags.keySet()); + public @NotNull Map asMap() { + return this.tags; } @Override 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 b9becd40df..7f0b46fd3a 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagBuilder.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagBuilder.java @@ -35,8 +35,8 @@ final class CompoundTagBuilder implements CompoundBinaryTag.Builder { CompoundTagBuilder() { } - CompoundTagBuilder(final Map tags) { - this.tags = tags; + CompoundTagBuilder(final CompoundBinaryTag existing) { + this.tags = new HashMap<>(existing.asMap()); // explicitly copy } private Map tags() {