Skip to content

Commit

Permalink
Migrate to adventure-nbt
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Feb 3, 2021
1 parent 476342a commit 3365963
Show file tree
Hide file tree
Showing 33 changed files with 658 additions and 85 deletions.
3 changes: 2 additions & 1 deletion buildSrc/src/main/kotlin/LibsConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShadowJar>("jar") {
Expand Down
210 changes: 210 additions & 0 deletions worldedit-core/src/main/java/com/sk89q/jnbt/AdventureNBTConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <https://www.gnu.org/licenses/>.
*/

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);
}
}
22 changes: 18 additions & 4 deletions worldedit-core/src/main/java/com/sk89q/jnbt/ByteArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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");
Expand Down
22 changes: 18 additions & 4 deletions worldedit-core/src/main/java/com/sk89q/jnbt/ByteTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() + ")";
}

}
23 changes: 23 additions & 0 deletions worldedit-core/src/main/java/com/sk89q/jnbt/CompoundTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Tag> value;
Expand All @@ -41,6 +47,23 @@ public CompoundTag(Map<String, Tag> value) {
this.value = Collections.unmodifiableMap(value);
}

CompoundTag(CompoundBinaryTag adventureTag) {
Set<String> tags = adventureTag.keySet();
Map<String, Tag> 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<String, Tag> child : getValue().entrySet()) {
builder.put(child.getKey(), AdventureNBTConverter.toAdventure(child.getValue()));
}
return builder.build();
}

/**
* Returns whether this compound tag contains the given key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Tag> entries;
Expand Down
Loading

0 comments on commit 3365963

Please sign in to comment.