From 3365963851ce9ee2fef9e954988be2186a227741 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Fri, 29 Jan 2021 19:03:11 +1000 Subject: [PATCH] Migrate to adventure-nbt --- buildSrc/src/main/kotlin/LibsConfig.kt | 3 +- .../com/sk89q/jnbt/AdventureNBTConverter.java | 210 ++++++++++++++++++ .../java/com/sk89q/jnbt/ByteArrayTag.java | 22 +- .../src/main/java/com/sk89q/jnbt/ByteTag.java | 22 +- .../main/java/com/sk89q/jnbt/CompoundTag.java | 23 ++ .../com/sk89q/jnbt/CompoundTagBuilder.java | 3 + .../main/java/com/sk89q/jnbt/DoubleTag.java | 22 +- .../src/main/java/com/sk89q/jnbt/EndTag.java | 3 + .../main/java/com/sk89q/jnbt/FloatTag.java | 22 +- .../main/java/com/sk89q/jnbt/IntArrayTag.java | 22 +- .../src/main/java/com/sk89q/jnbt/IntTag.java | 22 +- .../src/main/java/com/sk89q/jnbt/ListTag.java | 33 +++ .../java/com/sk89q/jnbt/ListTagBuilder.java | 3 + .../java/com/sk89q/jnbt/LongArrayTag.java | 22 +- .../src/main/java/com/sk89q/jnbt/LongTag.java | 22 +- .../java/com/sk89q/jnbt/NBTConstants.java | 6 +- .../java/com/sk89q/jnbt/NBTInputStream.java | 3 + .../java/com/sk89q/jnbt/NBTOutputStream.java | 3 + .../main/java/com/sk89q/jnbt/NBTUtils.java | 4 +- .../main/java/com/sk89q/jnbt/NamedTag.java | 3 + .../main/java/com/sk89q/jnbt/ShortTag.java | 22 +- .../main/java/com/sk89q/jnbt/StringTag.java | 22 +- .../src/main/java/com/sk89q/jnbt/Tag.java | 3 + .../com/sk89q/worldedit/LocalSession.java | 12 +- .../com/sk89q/worldedit/blocks/BaseItem.java | 18 ++ .../sk89q/worldedit/entity/BaseEntity.java | 22 +- .../factory/parser/DefaultItemParser.java | 83 +++++-- .../extent/world/SurvivalModeExtent.java | 3 +- .../worldedit/world/block/BaseBlock.java | 58 +++-- .../worldedit/world/block/BlockState.java | 10 + .../world/block/BlockStateHolder.java | 14 ++ .../src/main/resources/lang/strings.json | 2 + worldedit-libs/core/build.gradle.kts | 1 + 33 files changed, 658 insertions(+), 85 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/jnbt/AdventureNBTConverter.java diff --git a/buildSrc/src/main/kotlin/LibsConfig.kt b/buildSrc/src/main/kotlin/LibsConfig.kt index 29c5bfa3a8..959b39f9cd 100644 --- a/buildSrc/src/main/kotlin/LibsConfig.kt +++ b/buildSrc/src/main/kotlin/LibsConfig.kt @@ -28,7 +28,8 @@ fun Project.applyLibrariesConfiguration() { val relocations = mapOf( "net.kyori.text" to "com.sk89q.worldedit.util.formatting.text", - "net.kyori.minecraft" to "com.sk89q.worldedit.util.kyori" + "net.kyori.minecraft" to "com.sk89q.worldedit.util.kyori", + "net.kyori.adventure.nbt" to "com.sk89q.worldedit.util.nbt" ) tasks.register("jar") { diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/AdventureNBTConverter.java b/worldedit-core/src/main/java/com/sk89q/jnbt/AdventureNBTConverter.java new file mode 100644 index 0000000000..fb018413f9 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/AdventureNBTConverter.java @@ -0,0 +1,210 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.jnbt; + +import com.sk89q.worldedit.util.nbt.BinaryTag; +import com.sk89q.worldedit.util.nbt.ByteArrayBinaryTag; +import com.sk89q.worldedit.util.nbt.ByteBinaryTag; +import com.sk89q.worldedit.util.nbt.CompoundBinaryTag; +import com.sk89q.worldedit.util.nbt.DoubleBinaryTag; +import com.sk89q.worldedit.util.nbt.EndBinaryTag; +import com.sk89q.worldedit.util.nbt.FloatBinaryTag; +import com.sk89q.worldedit.util.nbt.IntArrayBinaryTag; +import com.sk89q.worldedit.util.nbt.IntBinaryTag; +import com.sk89q.worldedit.util.nbt.ListBinaryTag; +import com.sk89q.worldedit.util.nbt.LongArrayBinaryTag; +import com.sk89q.worldedit.util.nbt.LongBinaryTag; +import com.sk89q.worldedit.util.nbt.ShortBinaryTag; +import com.sk89q.worldedit.util.nbt.StringBinaryTag; + +/** + * Converts between JNBT and Adventure-NBT classes. + * + * @deprecated JNBT is being removed in WE8. + */ +@Deprecated +public class AdventureNBTConverter { + + private AdventureNBTConverter() { + + } + + public static BinaryTag toAdventure(Tag tag) { + if (tag instanceof IntArrayTag) { + return toAdventure((IntArrayTag) tag); + } else if (tag instanceof ListTag) { + return toAdventure((ListTag) tag); + } else if (tag instanceof LongTag) { + return toAdventure((LongTag) tag); + } else if (tag instanceof LongArrayTag) { + return toAdventure((LongArrayTag) tag); + } else if (tag instanceof StringTag) { + return toAdventure((StringTag) tag); + } else if (tag instanceof IntTag) { + return toAdventure((IntTag) tag); + } else if (tag instanceof ByteTag) { + return toAdventure((ByteTag) tag); + } else if (tag instanceof ByteArrayTag) { + return toAdventure((ByteArrayTag) tag); + } else if (tag instanceof CompoundTag) { + return toAdventure((CompoundTag) tag); + } else if (tag instanceof FloatTag) { + return toAdventure((FloatTag) tag); + } else if (tag instanceof ShortTag) { + return toAdventure((ShortTag) tag); + } else if (tag instanceof DoubleTag) { + return toAdventure((DoubleTag) tag); + } else { + throw new IllegalArgumentException("Can't convert tag of type " + tag.getClass().getCanonicalName()); + } + } + + private static DoubleBinaryTag toAdventure(DoubleTag tag) { + return tag.toAdventure(); + } + + private static ShortBinaryTag toAdventure(ShortTag tag) { + return tag.toAdventure(); + } + + private static FloatBinaryTag toAdventure(FloatTag tag) { + return tag.toAdventure(); + } + + private static ByteArrayBinaryTag toAdventure(ByteArrayTag tag) { + return tag.toAdventure(); + } + + private static ByteBinaryTag toAdventure(ByteTag tag) { + return tag.toAdventure(); + } + + private static IntBinaryTag toAdventure(IntTag tag) { + return tag.toAdventure(); + } + + private static StringBinaryTag toAdventure(StringTag tag) { + return tag.toAdventure(); + } + + private static LongArrayBinaryTag toAdventure(LongArrayTag tag) { + return tag.toAdventure(); + } + + private static LongBinaryTag toAdventure(LongTag tag) { + return tag.toAdventure(); + } + + private static IntArrayBinaryTag toAdventure(IntArrayTag tag) { + return tag.toAdventure(); + } + + public static ListBinaryTag toAdventure(ListTag tag) { + return tag.toAdventure(); + } + + public static CompoundBinaryTag toAdventure(CompoundTag tag) { + return tag.toAdventure(); + } + + public static Tag fromAdventure(BinaryTag other) { + if (other instanceof IntArrayBinaryTag) { + return fromAdventure((IntArrayBinaryTag) other); + } else if (other instanceof ListBinaryTag) { + return fromAdventure((ListBinaryTag) other); + } else if (other instanceof EndBinaryTag) { + return fromAdventure(); + } else if (other instanceof LongBinaryTag) { + return fromAdventure((LongBinaryTag) other); + } else if (other instanceof LongArrayBinaryTag) { + return fromAdventure((LongArrayBinaryTag) other); + } else if (other instanceof StringBinaryTag) { + return fromAdventure((StringBinaryTag) other); + } else if (other instanceof IntBinaryTag) { + return fromAdventure((IntBinaryTag) other); + } else if (other instanceof ByteBinaryTag) { + return fromAdventure((ByteBinaryTag) other); + } else if (other instanceof ByteArrayBinaryTag) { + return fromAdventure((ByteArrayBinaryTag) other); + } else if (other instanceof CompoundBinaryTag) { + return fromAdventure((CompoundBinaryTag) other); + } else if (other instanceof FloatBinaryTag) { + return fromAdventure((FloatBinaryTag) other); + } else if (other instanceof ShortBinaryTag) { + return fromAdventure((ShortBinaryTag) other); + } else if (other instanceof DoubleBinaryTag) { + return fromAdventure((DoubleBinaryTag) other); + } else { + throw new IllegalArgumentException("Can't convert other of type " + other.getClass().getCanonicalName()); + } + } + + public static DoubleTag fromAdventure(DoubleBinaryTag other) { + return new DoubleTag(other); + } + + public static ShortTag fromAdventure(ShortBinaryTag other) { + return new ShortTag(other); + } + + public static FloatTag fromAdventure(FloatBinaryTag other) { + return new FloatTag(other); + } + + public static CompoundTag fromAdventure(CompoundBinaryTag other) { + return new CompoundTag(other); + } + + public static ByteArrayTag fromAdventure(ByteArrayBinaryTag other) { + return new ByteArrayTag(other); + } + + public static ByteTag fromAdventure(ByteBinaryTag other) { + return new ByteTag(other); + } + + public static IntTag fromAdventure(IntBinaryTag other) { + return new IntTag(other); + } + + public static StringTag fromAdventure(StringBinaryTag other) { + return new StringTag(other); + } + + public static LongArrayTag fromAdventure(LongArrayBinaryTag other) { + return new LongArrayTag(other); + } + + public static LongTag fromAdventure(LongBinaryTag other) { + return new LongTag(other); + } + + public static EndTag fromAdventure() { + return new EndTag(); + } + + public static ListTag fromAdventure(ListBinaryTag other) { + return new ListTag(other); + } + + public static IntArrayTag fromAdventure(IntArrayBinaryTag other) { + return new IntArrayTag(other); + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/ByteArrayTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/ByteArrayTag.java index 78a9d7521b..733ad86ae3 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/ByteArrayTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/ByteArrayTag.java @@ -19,14 +19,19 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.ByteArrayBinaryTag; + import java.util.Locale; /** * The {@code TAG_Byte_Array} tag. + * + * @deprecated Use {@link ByteArrayBinaryTag}. */ +@Deprecated public final class ByteArrayTag extends Tag { - private final byte[] value; + private final ByteArrayBinaryTag innerTag; /** * Creates the tag with an empty name. @@ -35,18 +40,27 @@ public final class ByteArrayTag extends Tag { */ public ByteArrayTag(byte[] value) { super(); - this.value = value; + this.innerTag = ByteArrayBinaryTag.of(value); + } + + ByteArrayTag(ByteArrayBinaryTag adventureTag) { + super(); + this.innerTag = adventureTag; + } + + ByteArrayBinaryTag toAdventure() { + return this.innerTag; } @Override public byte[] getValue() { - return value; + return innerTag.value(); } @Override public String toString() { StringBuilder hex = new StringBuilder(); - for (byte b : value) { + for (byte b : innerTag.value()) { String hexDigits = Integer.toHexString(b).toUpperCase(Locale.ROOT); if (hexDigits.length() == 1) { hex.append("0"); diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/ByteTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/ByteTag.java index e7fe1cc407..495b8c2d6d 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/ByteTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/ByteTag.java @@ -19,12 +19,17 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.ByteBinaryTag; + /** * The {@code TAG_Byte} tag. + * + * @deprecated Use {@link ByteBinaryTag}. */ +@Deprecated public final class ByteTag extends Tag { - private final byte value; + private final ByteBinaryTag innerTag; /** * Creates the tag with an empty name. @@ -33,17 +38,26 @@ public final class ByteTag extends Tag { */ public ByteTag(byte value) { super(); - this.value = value; + this.innerTag = ByteBinaryTag.of(value); + } + + ByteTag(ByteBinaryTag adventureTag) { + super(); + this.innerTag = adventureTag; + } + + ByteBinaryTag toAdventure() { + return this.innerTag; } @Override public Byte getValue() { - return value; + return innerTag.value(); } @Override public String toString() { - return "TAG_Byte(" + value + ")"; + return "TAG_Byte(" + innerTag.value() + ")"; } } diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/CompoundTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/CompoundTag.java index 3a382effec..448400c55e 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/CompoundTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/CompoundTag.java @@ -19,14 +19,20 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.CompoundBinaryTag; + import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; /** * The {@code TAG_Compound} tag. + * + * @deprecated Use {@link com.sk89q.worldedit.util.nbt.CompoundBinaryTag}. */ +@Deprecated public final class CompoundTag extends Tag { private final Map value; @@ -41,6 +47,23 @@ public CompoundTag(Map value) { this.value = Collections.unmodifiableMap(value); } + CompoundTag(CompoundBinaryTag adventureTag) { + Set tags = adventureTag.keySet(); + Map map = new HashMap<>(); + for (String tagName : tags) { + map.put(tagName, AdventureNBTConverter.fromAdventure(adventureTag.get(tagName))); + } + this.value = Collections.unmodifiableMap(map); + } + + CompoundBinaryTag toAdventure() { + CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder(); + for (Map.Entry child : getValue().entrySet()) { + builder.put(child.getKey(), AdventureNBTConverter.toAdventure(child.getValue())); + } + return builder.build(); + } + /** * Returns whether this compound tag contains the given key. * diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/CompoundTagBuilder.java b/worldedit-core/src/main/java/com/sk89q/jnbt/CompoundTagBuilder.java index 7267a72e0a..941fe9fa71 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/CompoundTagBuilder.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/CompoundTagBuilder.java @@ -26,7 +26,10 @@ /** * Helps create compound tags. + * + * @deprecated Use {@link com.sk89q.worldedit.util.nbt.CompoundBinaryTag.Builder}. */ +@Deprecated public class CompoundTagBuilder { private final Map entries; diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/DoubleTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/DoubleTag.java index b579308571..746382ac38 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/DoubleTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/DoubleTag.java @@ -19,12 +19,17 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.DoubleBinaryTag; + /** * The {@code TAG_Double} tag. + * + * @deprecated Use {@link DoubleBinaryTag}. */ +@Deprecated public final class DoubleTag extends Tag { - private final double value; + private final DoubleBinaryTag innerTag; /** * Creates the tag with an empty name. @@ -33,17 +38,26 @@ public final class DoubleTag extends Tag { */ public DoubleTag(double value) { super(); - this.value = value; + this.innerTag = DoubleBinaryTag.of(value); + } + + DoubleTag(DoubleBinaryTag adventureTag) { + super(); + this.innerTag = adventureTag; + } + + DoubleBinaryTag toAdventure() { + return this.innerTag; } @Override public Double getValue() { - return value; + return innerTag.value(); } @Override public String toString() { - return "TAG_Double(" + value + ")"; + return "TAG_Double(" + innerTag.value() + ")"; } } diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/EndTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/EndTag.java index c6fc635983..ec95724b80 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/EndTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/EndTag.java @@ -21,7 +21,10 @@ /** * The {@code TAG_End} tag. + * + * @deprecated Use {@link com.sk89q.worldedit.util.nbt.EndBinaryTag}. */ +@Deprecated public final class EndTag extends Tag { @Override diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/FloatTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/FloatTag.java index e1281ea131..468bf40082 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/FloatTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/FloatTag.java @@ -19,12 +19,17 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.FloatBinaryTag; + /** * The {@code TAG_Float} tag. + * + * @deprecated Use {@link FloatBinaryTag}. */ +@Deprecated public final class FloatTag extends Tag { - private final float value; + private final FloatBinaryTag innerTag; /** * Creates the tag with an empty name. @@ -33,17 +38,26 @@ public final class FloatTag extends Tag { */ public FloatTag(float value) { super(); - this.value = value; + this.innerTag = FloatBinaryTag.of(value); + } + + FloatTag(FloatBinaryTag adventureTag) { + super(); + this.innerTag = adventureTag; + } + + FloatBinaryTag toAdventure() { + return this.innerTag; } @Override public Float getValue() { - return value; + return innerTag.value(); } @Override public String toString() { - return "TAG_Float(" + value + ")"; + return "TAG_Float(" + innerTag.value() + ")"; } } diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/IntArrayTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/IntArrayTag.java index 39c6217359..65dedbf64c 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/IntArrayTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/IntArrayTag.java @@ -19,16 +19,21 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.IntArrayBinaryTag; + import java.util.Locale; import static com.google.common.base.Preconditions.checkNotNull; /** * The {@code TAG_Int_Array} tag. + * + * @deprecated Use {@link IntArrayBinaryTag}. */ +@Deprecated public final class IntArrayTag extends Tag { - private final int[] value; + private final IntArrayBinaryTag innerTag; /** * Creates the tag with an empty name. @@ -38,18 +43,27 @@ public final class IntArrayTag extends Tag { public IntArrayTag(int[] value) { super(); checkNotNull(value); - this.value = value; + this.innerTag = IntArrayBinaryTag.of(value); + } + + IntArrayTag(IntArrayBinaryTag adventureTag) { + super(); + this.innerTag = adventureTag; + } + + IntArrayBinaryTag toAdventure() { + return this.innerTag; } @Override public int[] getValue() { - return value; + return innerTag.value(); } @Override public String toString() { StringBuilder hex = new StringBuilder(); - for (int b : value) { + for (int b : innerTag.value()) { String hexDigits = Integer.toHexString(b).toUpperCase(Locale.ROOT); if (hexDigits.length() == 1) { hex.append("0"); diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/IntTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/IntTag.java index 359cf85b9b..336abe6f2c 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/IntTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/IntTag.java @@ -19,12 +19,17 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.IntBinaryTag; + /** * The {@code TAG_Int} tag. + * + * @deprecated Use {@link IntBinaryTag}. */ +@Deprecated public final class IntTag extends Tag { - private final int value; + private final IntBinaryTag innerTag; /** * Creates the tag with an empty name. @@ -33,17 +38,26 @@ public final class IntTag extends Tag { */ public IntTag(int value) { super(); - this.value = value; + this.innerTag = IntBinaryTag.of(value); + } + + IntTag(IntBinaryTag adventureTag) { + super(); + this.innerTag = adventureTag; + } + + IntBinaryTag toAdventure() { + return this.innerTag; } @Override public Integer getValue() { - return value; + return innerTag.value(); } @Override public String toString() { - return "TAG_Int(" + value + ")"; + return "TAG_Int(" + innerTag.value() + ")"; } } diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/ListTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/ListTag.java index 8134a3f74e..ad21023a9c 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/ListTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/ListTag.java @@ -19,6 +19,10 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.BinaryTag; +import com.sk89q.worldedit.util.nbt.ListBinaryTag; + +import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.annotation.Nullable; @@ -27,7 +31,10 @@ /** * The {@code TAG_List} tag. + * + * @deprecated Use {@link com.sk89q.worldedit.util.nbt.ListBinaryTag}. */ +@Deprecated public final class ListTag extends Tag { private final Class type; @@ -46,6 +53,32 @@ public ListTag(Class type, List value) { this.value = Collections.unmodifiableList(value); } + ListTag(ListBinaryTag adventureTag) { + super(); + List list = new ArrayList<>(); + Class listClass = StringTag.class; + int tags = adventureTag.size(); + for (int i = 0; i < tags; i++) { + Tag child = AdventureNBTConverter.fromAdventure(adventureTag.get(0)); + list.add(child); + listClass = child.getClass(); + } + + this.type = listClass; + this.value = Collections.unmodifiableList(list); + } + + ListBinaryTag toAdventure() { + ListBinaryTag.Builder builder = ListBinaryTag.builder(); + for (Tag child : getValue()) { + if (child instanceof EndTag) { + continue; + } + builder.add(AdventureNBTConverter.toAdventure(child)); + } + return builder.build(); + } + /** * Gets the type of item in this list. * diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/ListTagBuilder.java b/worldedit-core/src/main/java/com/sk89q/jnbt/ListTagBuilder.java index b98c667e50..ad3a6e25cf 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/ListTagBuilder.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/ListTagBuilder.java @@ -28,7 +28,10 @@ /** * Helps create list tags. + * + * @deprecated Use {@link com.sk89q.worldedit.util.nbt.ListBinaryTag.Builder}. */ +@Deprecated public class ListTagBuilder { private final Class type; diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/LongArrayTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/LongArrayTag.java index c2e9688d08..f0f76b5c3c 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/LongArrayTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/LongArrayTag.java @@ -19,16 +19,21 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.LongArrayBinaryTag; + import java.util.Locale; import static com.google.common.base.Preconditions.checkNotNull; /** * The {@code TAG_Long_Array} tag. + * + * @deprecated Use {@link LongArrayBinaryTag}. */ +@Deprecated public class LongArrayTag extends Tag { - private final long[] value; + private final LongArrayBinaryTag innerTag; /** * Creates the tag with an empty name. @@ -38,18 +43,27 @@ public class LongArrayTag extends Tag { public LongArrayTag(long[] value) { super(); checkNotNull(value); - this.value = value; + this.innerTag = LongArrayBinaryTag.of(value); + } + + LongArrayTag(LongArrayBinaryTag adventureTag) { + super(); + this.innerTag = adventureTag; + } + + LongArrayBinaryTag toAdventure() { + return this.innerTag; } @Override public long[] getValue() { - return value; + return innerTag.value(); } @Override public String toString() { StringBuilder hex = new StringBuilder(); - for (long b : value) { + for (long b : innerTag.value()) { String hexDigits = Long.toHexString(b).toUpperCase(Locale.ROOT); if (hexDigits.length() == 1) { hex.append("0"); diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/LongTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/LongTag.java index 8f894520a2..10c4da68b1 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/LongTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/LongTag.java @@ -19,12 +19,17 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.LongBinaryTag; + /** * The {@code TAG_Long} tag. + * + * @deprecated Use {@link LongBinaryTag}. */ +@Deprecated public final class LongTag extends Tag { - private final long value; + private final LongBinaryTag innerTag; /** * Creates the tag with an empty name. @@ -33,17 +38,26 @@ public final class LongTag extends Tag { */ public LongTag(long value) { super(); - this.value = value; + this.innerTag = LongBinaryTag.of(value); + } + + LongTag(LongBinaryTag adventureTag) { + super(); + this.innerTag = adventureTag; + } + + LongBinaryTag toAdventure() { + return this.innerTag; } @Override public Long getValue() { - return value; + return innerTag.value(); } @Override public String toString() { - return "TAG_Long(" + value + ")"; + return "TAG_Long(" + innerTag.value() + ")"; } } diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/NBTConstants.java b/worldedit-core/src/main/java/com/sk89q/jnbt/NBTConstants.java index db6689cb52..9d65f5e79d 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/NBTConstants.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/NBTConstants.java @@ -20,13 +20,17 @@ package com.sk89q.jnbt; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; /** * A class which holds constant values. + * + * @deprecated JNBT is being removed for adventure-nbt in WorldEdit 8. */ +@Deprecated public final class NBTConstants { - public static final Charset CHARSET = Charset.forName("UTF-8"); + public static final Charset CHARSET = StandardCharsets.UTF_8; public static final int TYPE_END = 0; public static final int TYPE_BYTE = 1; diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/NBTInputStream.java b/worldedit-core/src/main/java/com/sk89q/jnbt/NBTInputStream.java index 35a0bb62e1..1b41180851 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/NBTInputStream.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/NBTInputStream.java @@ -38,7 +38,10 @@ * found at * https://minecraft.gamepedia.com/NBT_format. *

+ * + * @deprecated JNBT is being removed for adventure-nbt in WorldEdit 8. */ +@Deprecated public final class NBTInputStream implements Closeable { private final DataInputStream is; diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/NBTOutputStream.java b/worldedit-core/src/main/java/com/sk89q/jnbt/NBTOutputStream.java index 98ccb72db0..ae92946ae0 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/NBTOutputStream.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/NBTOutputStream.java @@ -37,7 +37,10 @@ * found at * https://minecraft.gamepedia.com/NBT_format. *

+ * + * @deprecated JNBT is being removed for adventure-nbt in WorldEdit 8. */ +@Deprecated public final class NBTOutputStream implements Closeable { /** diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/NBTUtils.java b/worldedit-core/src/main/java/com/sk89q/jnbt/NBTUtils.java index c6e53e598c..52d21ca451 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/NBTUtils.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/NBTUtils.java @@ -29,7 +29,9 @@ /** * A class which contains NBT-related utility methods. * + * @deprecated JNBT is being removed for adventure-nbt in WorldEdit 8. */ +@Deprecated public final class NBTUtils { /** @@ -72,7 +74,7 @@ public static String getTypeName(Class clazz) { } else if (clazz.equals(LongArrayTag.class)) { return "TAG_Long_Array"; } else { - throw new IllegalArgumentException("Invalid tag classs (" + throw new IllegalArgumentException("Invalid tag class (" + clazz.getName() + ")."); } } diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/NamedTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/NamedTag.java index c7089e3f48..80742783c8 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/NamedTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/NamedTag.java @@ -23,7 +23,10 @@ /** * A tag that has a name. + * + * @deprecated JNBT is being removed for adventure-nbt in WorldEdit 8. */ +@Deprecated public class NamedTag { private final String name; diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/ShortTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/ShortTag.java index f8224ab0ed..6df155375f 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/ShortTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/ShortTag.java @@ -19,12 +19,17 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.ShortBinaryTag; + /** * The {@code TAG_Short} tag. + * + * @deprecated Use {@link ShortBinaryTag}. */ +@Deprecated public final class ShortTag extends Tag { - private final short value; + private final ShortBinaryTag innerTag; /** * Creates the tag with an empty name. @@ -33,17 +38,26 @@ public final class ShortTag extends Tag { */ public ShortTag(short value) { super(); - this.value = value; + this.innerTag = ShortBinaryTag.of(value); + } + + ShortTag(ShortBinaryTag adventureTag) { + super(); + this.innerTag = adventureTag; + } + + ShortBinaryTag toAdventure() { + return this.innerTag; } @Override public Short getValue() { - return value; + return innerTag.value(); } @Override public String toString() { - return "TAG_Short(" + value + ")"; + return "TAG_Short(" + innerTag.value() + ")"; } } diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/StringTag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/StringTag.java index 3727f8d8e1..78eae3011a 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/StringTag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/StringTag.java @@ -19,14 +19,19 @@ package com.sk89q.jnbt; +import com.sk89q.worldedit.util.nbt.StringBinaryTag; + import static com.google.common.base.Preconditions.checkNotNull; /** * The {@code TAG_String} tag. + * + * @deprecated Use {@link StringBinaryTag}. */ +@Deprecated public final class StringTag extends Tag { - private final String value; + private final StringBinaryTag innerTag; /** * Creates the tag with an empty name. @@ -36,17 +41,26 @@ public final class StringTag extends Tag { public StringTag(String value) { super(); checkNotNull(value); - this.value = value; + this.innerTag = StringBinaryTag.of(value); + } + + StringTag(StringBinaryTag adventureTag) { + super(); + this.innerTag = adventureTag; + } + + StringBinaryTag toAdventure() { + return this.innerTag; } @Override public String getValue() { - return value; + return innerTag.value(); } @Override public String toString() { - return "TAG_String(" + value + ")"; + return "TAG_String(" + innerTag.value() + ")"; } } diff --git a/worldedit-core/src/main/java/com/sk89q/jnbt/Tag.java b/worldedit-core/src/main/java/com/sk89q/jnbt/Tag.java index a94b3b3bb3..53c84727f2 100644 --- a/worldedit-core/src/main/java/com/sk89q/jnbt/Tag.java +++ b/worldedit-core/src/main/java/com/sk89q/jnbt/Tag.java @@ -21,7 +21,10 @@ /** * Represents a NBT tag. + * + * @deprecated JNBT is being removed for adventure-nbt in WorldEdit 8. */ +@Deprecated public abstract class Tag { /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java index 837db9498c..dbfb69c012 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -23,8 +23,7 @@ import com.sk89q.jchronic.Options; import com.sk89q.jchronic.utils.Span; import com.sk89q.jchronic.utils.Time; -import com.sk89q.jnbt.IntTag; -import com.sk89q.jnbt.Tag; +import com.sk89q.jnbt.AdventureNBTConverter; import com.sk89q.worldedit.command.tool.BlockTool; import com.sk89q.worldedit.command.tool.BrushTool; import com.sk89q.worldedit.command.tool.InvalidToolBindException; @@ -51,6 +50,7 @@ import com.sk89q.worldedit.util.Countable; import com.sk89q.worldedit.util.SideEffectSet; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; +import com.sk89q.worldedit.util.nbt.CompoundBinaryTag; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockState; @@ -805,11 +805,11 @@ public void updateServerCUI(Actor actor) { BaseBlock block = ServerCUIHandler.createStructureBlock(player); if (block != null) { // If it's null, we don't need to do anything. The old was already removed. - Map tags = block.getNbtData().getValue(); + CompoundBinaryTag tags = AdventureNBTConverter.toAdventure(block.getNbtData()); BlockVector3 tempCuiTemporaryBlock = BlockVector3.at( - ((IntTag) tags.get("x")).getValue(), - ((IntTag) tags.get("y")).getValue(), - ((IntTag) tags.get("z")).getValue() + tags.getInt("x"), + tags.getInt("y"), + tags.getInt("z") ); if (cuiTemporaryBlock != null && !tempCuiTemporaryBlock.equals(cuiTemporaryBlock)) { // Update the existing block if it's the same location diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java index a365bc4b14..e564449162 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java @@ -19,10 +19,14 @@ package com.sk89q.worldedit.blocks; +import com.sk89q.jnbt.AdventureNBTConverter; import com.sk89q.jnbt.CompoundTag; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.util.nbt.TagStringIO; import com.sk89q.worldedit.world.NbtValued; import com.sk89q.worldedit.world.item.ItemType; +import java.io.IOException; import javax.annotation.Nullable; import static com.google.common.base.Preconditions.checkNotNull; @@ -94,4 +98,18 @@ public CompoundTag getNbtData() { public void setNbtData(@Nullable CompoundTag nbtData) { this.nbtData = nbtData; } + + @Override + public String toString() { + String nbtString = ""; + if (hasNbtData()) { + try { + nbtString = TagStringIO.get().asString(AdventureNBTConverter.toAdventure(getNbtData())); + } catch (IOException e) { + WorldEdit.logger.error("Failed to parse NBT of Item", e); + } + } + + return getType().getId() + nbtString; + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/entity/BaseEntity.java b/worldedit-core/src/main/java/com/sk89q/worldedit/entity/BaseEntity.java index 393023df81..8cc1b5b533 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/entity/BaseEntity.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/entity/BaseEntity.java @@ -19,7 +19,9 @@ package com.sk89q.worldedit.entity; +import com.sk89q.jnbt.AdventureNBTConverter; import com.sk89q.jnbt.CompoundTag; +import com.sk89q.worldedit.util.nbt.CompoundBinaryTag; import com.sk89q.worldedit.world.NbtValued; import com.sk89q.worldedit.world.entity.EntityType; @@ -43,19 +45,33 @@ public class BaseEntity implements NbtValued { private final EntityType type; - private CompoundTag nbtData; + @Nullable + private CompoundBinaryTag nbtData; /** * Create a new base entity. * * @param type the entity type * @param nbtData NBT data + * @deprecated Use {@link BaseEntity#BaseEntity(EntityType, CompoundBinaryTag)} */ + @Deprecated public BaseEntity(EntityType type, CompoundTag nbtData) { this(type); setNbtData(nbtData); } + /** + * Create a new base entity. + * + * @param type the entity type + * @param nbtData NBT data + */ + public BaseEntity(EntityType type, CompoundBinaryTag nbtData) { + this(type); + setNbtData(nbtData == null ? null : AdventureNBTConverter.fromAdventure(nbtData)); + } + /** * Create a new base entity with no NBT data. * @@ -84,12 +100,12 @@ public boolean hasNbtData() { @Nullable @Override public CompoundTag getNbtData() { - return nbtData; + return nbtData == null ? null : AdventureNBTConverter.fromAdventure(nbtData); } @Override public void setNbtData(@Nullable CompoundTag nbtData) { - this.nbtData = nbtData; + this.nbtData = nbtData == null ? null : AdventureNBTConverter.toAdventure(nbtData); } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultItemParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultItemParser.java index 186fff5560..aefe15e0d1 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultItemParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultItemParser.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.extension.factory.parser; +import com.sk89q.jnbt.AdventureNBTConverter; +import com.sk89q.jnbt.CompoundTag; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItemStack; @@ -32,10 +34,12 @@ import com.sk89q.worldedit.util.HandSide; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; +import com.sk89q.worldedit.util.nbt.TagStringIO; import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemTypes; import com.sk89q.worldedit.world.registry.LegacyMapper; +import java.io.IOException; import java.util.Locale; import java.util.stream.Stream; @@ -52,44 +56,85 @@ public Stream getSuggestions(String input) { @Override public BaseItem parseFromInput(String input, ParserContext context) throws InputParseException { + ItemType itemType; + CompoundTag itemNbtData = null; + BaseItem item = null; + // Legacy matcher if (context.isTryingLegacy()) { try { String[] split = input.split(":"); - ItemType type; if (split.length == 0) { throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.invalid-colon")); } else if (split.length == 1) { - type = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(split[0])); + itemType = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(split[0])); } else { - type = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(split[0]), Integer.parseInt(split[1])); + itemType = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(split[0]), Integer.parseInt(split[1])); } - if (type != null) { - item = new BaseItem(type); + if (itemType != null) { + item = new BaseItem(itemType); } } catch (NumberFormatException ignored) { } } - if ("hand".equalsIgnoreCase(input)) { - return getItemInHand(context.requireActor(), HandSide.MAIN_HAND); - } else if ("offhand".equalsIgnoreCase(input)) { - return getItemInHand(context.requireActor(), HandSide.OFF_HAND); - } - if (item == null) { - ItemType type = ItemTypes.get(input.toLowerCase(Locale.ROOT)); - if (type != null) { - item = new BaseItem(type); + String typeString; + String nbtString = null; + int nbtStart = input.indexOf('{'); + + if (nbtStart == -1) { + typeString = input; + } else { + typeString = input.substring(0, nbtStart); + if (nbtStart + 1 >= input.length()) { + throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.hanging-lbracket", TextComponent.of(nbtStart))); + } + int stateEnd = input.lastIndexOf('}'); + if (stateEnd < 0) { + throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.missing-rbracket")); + } + nbtString = input.substring(nbtStart); } - } - if (item == null) { - throw new NoMatchException(TranslatableComponent.of("worldedit.error.no-match", TextComponent.of(input))); - } else { - return item; + if ("hand".equalsIgnoreCase(typeString)) { + BaseItemStack heldItem = getItemInHand(context.requireActor(), HandSide.MAIN_HAND); + itemType = heldItem.getType(); + itemNbtData = heldItem.getNbtData(); + } else if ("offhand".equalsIgnoreCase(typeString)) { + BaseItemStack heldItem = getItemInHand(context.requireActor(), HandSide.OFF_HAND); + itemType = heldItem.getType(); + itemNbtData = heldItem.getNbtData(); + } else { + itemType = ItemTypes.get(typeString.toLowerCase(Locale.ROOT)); + } + + if (itemType == null) { + throw new NoMatchException(TranslatableComponent.of("worldedit.error.unknown-item", TextComponent.of(input))); + } + + if (nbtString != null) { + try { + CompoundTag otherTag = AdventureNBTConverter.fromAdventure(TagStringIO.get().asCompound(nbtString)); + if (itemNbtData == null) { + itemNbtData = otherTag; + } else { + itemNbtData = itemNbtData.createBuilder().putAll(otherTag.getValue()).build(); + } + } catch (IOException e) { + throw new NoMatchException(TranslatableComponent.of( + "worldedit.error.invalid-nbt", + TextComponent.of(input), + TextComponent.of(e.getMessage()) + )); + } + } + + item = new BaseItem(itemType, itemNbtData); } + + return item; } private BaseItemStack getItemInHand(Actor actor, HandSide handSide) throws InputParseException { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/world/SurvivalModeExtent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/world/SurvivalModeExtent.java index 2cb6b02c82..21c287cd79 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/world/SurvivalModeExtent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/world/SurvivalModeExtent.java @@ -23,6 +23,7 @@ import com.sk89q.worldedit.extent.AbstractDelegateExtent; import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.util.nbt.CompoundBinaryTag; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BlockStateHolder; @@ -95,7 +96,7 @@ public > boolean setBlock(BlockVector3 location, B } else { // Can't be an inlined check due to inconsistent generic return type if (stripNbt) { - return super.setBlock(location, block.toBaseBlock(null)); + return super.setBlock(location, block.toBaseBlock((CompoundBinaryTag) null)); } else { return super.setBlock(location, block); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java index a7d50f6b1c..79d63da7c5 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java @@ -19,12 +19,15 @@ package com.sk89q.worldedit.world.block; +import com.sk89q.jnbt.AdventureNBTConverter; import com.sk89q.jnbt.CompoundTag; -import com.sk89q.jnbt.StringTag; -import com.sk89q.jnbt.Tag; +import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.blocks.TileEntityBlock; import com.sk89q.worldedit.registry.state.Property; +import com.sk89q.worldedit.util.nbt.CompoundBinaryTag; +import com.sk89q.worldedit.util.nbt.TagStringIO; +import java.io.IOException; import java.util.Map; import java.util.Objects; import javax.annotation.Nullable; @@ -44,7 +47,7 @@ public class BaseBlock implements BlockStateHolder, TileEntityBlock { private final BlockState blockState; @Nullable - private final CompoundTag nbtData; + private final CompoundBinaryTag nbtData; /** * Construct a block with a state. @@ -62,7 +65,19 @@ protected BaseBlock(BlockState blockState) { * @param state The block state * @param nbtData NBT data, which must be provided */ + @Deprecated protected BaseBlock(BlockState state, CompoundTag nbtData) { + this(state, AdventureNBTConverter.toAdventure(checkNotNull(nbtData))); + } + + + /** + * Construct a block with the given ID, data value and NBT data structure. + * + * @param state The block state + * @param nbtData NBT data, which must be provided + */ + protected BaseBlock(BlockState state, CompoundBinaryTag nbtData) { checkNotNull(nbtData); this.blockState = state; this.nbtData = nbtData; @@ -106,22 +121,17 @@ public boolean hasNbtData() { @Override public String getNbtId() { - CompoundTag nbtData = getNbtData(); + CompoundBinaryTag nbtData = this.nbtData; if (nbtData == null) { return ""; } - Tag idTag = nbtData.getValue().get("id"); - if (idTag instanceof StringTag) { - return ((StringTag) idTag).getValue(); - } else { - return ""; - } + return nbtData.getString("id"); } @Nullable @Override public CompoundTag getNbtData() { - return this.nbtData; + return this.nbtData == null ? null : AdventureNBTConverter.fromAdventure(this.nbtData); } @Override @@ -167,8 +177,22 @@ public BaseBlock toBaseBlock() { return this; } + @Deprecated @Override public BaseBlock toBaseBlock(CompoundTag compoundTag) { + if (compoundTag == null) { + return this.blockState.toBaseBlock(); + } else if (AdventureNBTConverter.toAdventure(compoundTag) == this.nbtData) { + // The above reference equality check is fine, as JNBT classes contain an internal + // Adventure reference. + return this; + } else { + return new BaseBlock(this.blockState, compoundTag); + } + } + + @Override + public BaseBlock toBaseBlock(CompoundBinaryTag compoundTag) { if (compoundTag == null) { return this.blockState.toBaseBlock(); } else if (compoundTag == this.nbtData) { @@ -189,8 +213,16 @@ public int hashCode() { @Override public String toString() { - // TODO use a json serializer for the NBT data - return blockState.getAsString() + (hasNbtData() ? "{hasNbt}" : ""); + String nbtString = ""; + if (hasNbtData()) { + try { + nbtString = TagStringIO.get().asString(nbtData); + } catch (IOException e) { + WorldEdit.logger.error("Failed to parse NBT of Block", e); + } + } + + return blockState.getAsString() + nbtString; } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java index fdbdd92d42..be4584b03b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java @@ -28,6 +28,7 @@ import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.internal.block.BlockStateIdAccess; import com.sk89q.worldedit.registry.state.Property; +import com.sk89q.worldedit.util.nbt.CompoundBinaryTag; import java.util.Collections; import java.util.Comparator; @@ -210,6 +211,7 @@ public BaseBlock toBaseBlock() { return this.emptyBaseBlock; } + @Deprecated @Override public BaseBlock toBaseBlock(CompoundTag compoundTag) { if (compoundTag == null) { @@ -218,6 +220,14 @@ public BaseBlock toBaseBlock(CompoundTag compoundTag) { return new BaseBlock(this, compoundTag); } + @Override + public BaseBlock toBaseBlock(CompoundBinaryTag compoundTag) { + if (compoundTag == null) { + return toBaseBlock(); + } + return new BaseBlock(this, compoundTag); + } + /** * Internal method used for creating the initial BlockState. * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockStateHolder.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockStateHolder.java index 93aaa55ed4..875da7895d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockStateHolder.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockStateHolder.java @@ -19,10 +19,12 @@ package com.sk89q.worldedit.world.block; +import com.sk89q.jnbt.AdventureNBTConverter; import com.sk89q.jnbt.CompoundTag; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.registry.state.Property; +import com.sk89q.worldedit.util.nbt.CompoundBinaryTag; import java.util.Locale; import java.util.Map; @@ -88,9 +90,21 @@ public interface BlockStateHolder> extends Pattern * * @param compoundTag The NBT Data to apply * @return The BaseBlock + * @deprecated Use {@link BlockStateHolder#toBaseBlock(CompoundBinaryTag)}. */ + @Deprecated BaseBlock toBaseBlock(CompoundTag compoundTag); + /** + * Gets a {@link BaseBlock} from this BlockStateHolder. + * + * @param compoundTag The NBT Data to apply + * @return The BaseBlock + */ + default BaseBlock toBaseBlock(CompoundBinaryTag compoundTag) { + return toBaseBlock(AdventureNBTConverter.fromAdventure(compoundTag)); + } + @Override default BaseBlock applyBlock(BlockVector3 position) { return toBaseBlock(); diff --git a/worldedit-core/src/main/resources/lang/strings.json b/worldedit-core/src/main/resources/lang/strings.json index 2ed98270c1..a48412d5d7 100644 --- a/worldedit-core/src/main/resources/lang/strings.json +++ b/worldedit-core/src/main/resources/lang/strings.json @@ -331,6 +331,7 @@ "worldedit.error.invalid-number.matches": "Number expected; string \"{0}\" given.", "worldedit.error.incomplete-region": "Make a region selection first.", "worldedit.error.unknown-block": "Block name '{0}' was not recognized.", + "worldedit.error.unknown-item": "Item name '{0}' was not recognized.", "worldedit.error.unknown-entity": "Entity name '{0}' was not recognized.", "worldedit.error.unknown-mob": "Mob name '{0}' was not recognized.", "worldedit.error.unknown-biome": "Biome name '{0}' was not recognized.", @@ -372,6 +373,7 @@ "worldedit.error.parser.missing-random-type": "Missing the type after the % symbol for '{0}'", "worldedit.error.parser.clipboard.missing-coordinates": "Clipboard offset needs x,y,z coordinates.", "worldedit.error.parser.player-only": "Input '{0}' requires a player!", + "worldedit.error.parser.invalid-nbt": "Invalid NBT Data in input: '{0}'. Error: {1}", "worldedit.error.disabled": "This functionality is disabled (see WorldEdit configuration).", "worldedit.error.unknown": "Unknown error occurred: {0}", "worldedit.error.missing-extent": "No Extent is known", diff --git a/worldedit-libs/core/build.gradle.kts b/worldedit-libs/core/build.gradle.kts index d669216d6d..26fdba8be4 100644 --- a/worldedit-libs/core/build.gradle.kts +++ b/worldedit-libs/core/build.gradle.kts @@ -13,4 +13,5 @@ dependencies { "shade"("org.enginehub.piston:core:${Versions.PISTON}") "shade"("org.enginehub.piston.core-ap:runtime:${Versions.PISTON}") "shade"("org.enginehub.piston:default-impl:${Versions.PISTON}") + "shade"("net.kyori:adventure-nbt:4.4.0") }