Skip to content

Commit

Permalink
nbt: ability to add multiple tags to a Compound tag
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Dec 14, 2020
1 parent dccfc46 commit a1c3b62
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
13 changes: 13 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 @@ -44,6 +44,19 @@ public interface CompoundBinaryTag extends BinaryTag, CompoundTagSetter<Compound
return CompoundBinaryTagImpl.EMPTY;
}

/**
* Creates a compound tag populated with {@code tags}.
*
* <p>If {@code tags} is empty, {@link #empty()} will be returned.</p>
*
* @return a compound tag
* @since 4.4.0
*/
static @NonNull CompoundBinaryTag from(final @NonNull Map<String, BinaryTag> tags) {
if(tags.isEmpty()) return empty();
return builder().put(tags).build();
}

/**
* Creates a builder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, BinaryTag> 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)) {
Expand Down
16 changes: 13 additions & 3 deletions nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,22 @@
final class CompoundTagBuilder implements CompoundBinaryTag.Builder {
private @MonotonicNonNull Map<String, BinaryTag> tags;

@Override
public CompoundBinaryTag.@NonNull Builder put(final @NonNull String key, final @NonNull BinaryTag tag) {
private Map<String, BinaryTag> 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<String, BinaryTag> tags) {
this.tags().putAll(tags);
return this;
}

Expand Down
10 changes: 10 additions & 0 deletions nbt/src/main/java/net/kyori/adventure/nbt/CompoundTagSetter.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.Map;
import org.checkerframework.checker.nullness.qual.NonNull;

/**
Expand All @@ -42,6 +43,15 @@ public interface CompoundTagSetter<R> {
*/
@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<String, BinaryTag> tags);

/**
* Inserts a boolean.
*
Expand Down

0 comments on commit a1c3b62

Please sign in to comment.