From a1c3b62a17f9a6f6715e0381872eafea8f2e60b0 Mon Sep 17 00:00:00 2001
From: Riley Park
Date: Mon, 14 Dec 2020 11:12:19 -0800
Subject: [PATCH] nbt: ability to add multiple tags to a Compound tag
---
.../kyori/adventure/nbt/CompoundBinaryTag.java | 13 +++++++++++++
.../adventure/nbt/CompoundBinaryTagImpl.java | 5 +++++
.../kyori/adventure/nbt/CompoundTagBuilder.java | 16 +++++++++++++---
.../kyori/adventure/nbt/CompoundTagSetter.java | 10 ++++++++++
4 files changed, 41 insertions(+), 3 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 c0753a6520..debd0e6fa1 100644
--- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java
+++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java
@@ -44,6 +44,19 @@ public interface CompoundBinaryTag extends BinaryTag, CompoundTagSetterIf {@code tags} is empty, {@link #empty()} will be returned.
+ *
+ * @return a compound tag
+ * @since 4.4.0
+ */
+ static @NonNull CompoundBinaryTag from(final @NonNull Map tags) {
+ if(tags.isEmpty()) return empty();
+ return builder().put(tags).build();
+ }
+
/**
* Creates a builder.
*
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 6dcac22125..ddf62223b0 100644
--- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTagImpl.java
+++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTagImpl.java
@@ -66,6 +66,11 @@ public boolean contains(final @NonNull String key, final @NonNull BinaryTagType<
return this.edit(map -> map.put(key, tag));
}
+ @Override
+ public @NonNull CompoundBinaryTag put(final @NonNull Map tags) {
+ return this.edit(map -> map.putAll(tags));
+ }
+
@Override
public byte getByte(final @NonNull String key, final byte defaultValue) {
if(this.contains(key, BinaryTagTypes.BYTE)) {
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 cef75b71df..0f687ed8fe 100644
--- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagBuilder.java
+++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagBuilder.java
@@ -31,12 +31,22 @@
final class CompoundTagBuilder implements CompoundBinaryTag.Builder {
private @MonotonicNonNull Map tags;
- @Override
- public CompoundBinaryTag.@NonNull Builder put(final @NonNull String key, final @NonNull BinaryTag tag) {
+ private Map tags() {
if(this.tags == null) {
this.tags = new HashMap<>();
}
- this.tags.put(key, tag);
+ return this.tags;
+ }
+
+ @Override
+ public CompoundBinaryTag.@NonNull Builder put(final @NonNull String key, final @NonNull BinaryTag tag) {
+ this.tags().put(key, tag);
+ return this;
+ }
+
+ @Override
+ public CompoundBinaryTag.@NonNull Builder put(final @NonNull Map tags) {
+ this.tags().putAll(tags);
return this;
}
diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagSetter.java b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagSetter.java
index b5e48aa8d5..06fd229e17 100644
--- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagSetter.java
+++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagSetter.java
@@ -23,6 +23,7 @@
*/
package net.kyori.adventure.nbt;
+import java.util.Map;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
@@ -42,6 +43,15 @@ public interface CompoundTagSetter {
*/
@NonNull R put(final @NonNull String key, final @NonNull BinaryTag tag);
+ /**
+ * Inserts some tag.
+ *
+ * @param tags the tags
+ * @return a compound tag
+ * @since 4.4.0
+ */
+ @NonNull R put(final @NonNull Map tags);
+
/**
* Inserts a boolean.
*