2014-04-06 08:41:28 +00:00
|
|
|
/*
|
|
|
|
* 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 Lesser 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 Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.forge;
|
|
|
|
|
2018-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.jnbt.ByteArrayTag;
|
|
|
|
import com.sk89q.jnbt.ByteTag;
|
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
|
|
|
import com.sk89q.jnbt.DoubleTag;
|
|
|
|
import com.sk89q.jnbt.EndTag;
|
|
|
|
import com.sk89q.jnbt.FloatTag;
|
|
|
|
import com.sk89q.jnbt.IntArrayTag;
|
|
|
|
import com.sk89q.jnbt.IntTag;
|
|
|
|
import com.sk89q.jnbt.ListTag;
|
|
|
|
import com.sk89q.jnbt.LongTag;
|
|
|
|
import com.sk89q.jnbt.ShortTag;
|
|
|
|
import com.sk89q.jnbt.StringTag;
|
|
|
|
import com.sk89q.jnbt.Tag;
|
|
|
|
import net.minecraft.nbt.NBTBase;
|
|
|
|
import net.minecraft.nbt.NBTTagByte;
|
|
|
|
import net.minecraft.nbt.NBTTagByteArray;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.nbt.NBTTagDouble;
|
|
|
|
import net.minecraft.nbt.NBTTagEnd;
|
|
|
|
import net.minecraft.nbt.NBTTagFloat;
|
|
|
|
import net.minecraft.nbt.NBTTagInt;
|
|
|
|
import net.minecraft.nbt.NBTTagIntArray;
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
import net.minecraft.nbt.NBTTagLong;
|
|
|
|
import net.minecraft.nbt.NBTTagShort;
|
|
|
|
import net.minecraft.nbt.NBTTagString;
|
2014-04-06 08:41:28 +00:00
|
|
|
|
2018-07-30 13:26:06 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2014-04-06 08:41:28 +00:00
|
|
|
import java.util.Map.Entry;
|
2018-07-30 13:26:06 +00:00
|
|
|
import java.util.Set;
|
2014-04-06 08:41:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts between JNBT and Minecraft NBT classes.
|
|
|
|
*/
|
|
|
|
final class NBTConverter {
|
|
|
|
|
|
|
|
private NBTConverter() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTBase toNative(Tag tag) {
|
|
|
|
if (tag instanceof IntArrayTag) {
|
|
|
|
return toNative((IntArrayTag) tag);
|
|
|
|
|
|
|
|
} else if (tag instanceof ListTag) {
|
|
|
|
return toNative((ListTag) tag);
|
|
|
|
|
|
|
|
} else if (tag instanceof LongTag) {
|
|
|
|
return toNative((LongTag) tag);
|
|
|
|
|
|
|
|
} else if (tag instanceof StringTag) {
|
|
|
|
return toNative((StringTag) tag);
|
|
|
|
|
|
|
|
} else if (tag instanceof IntTag) {
|
|
|
|
return toNative((IntTag) tag);
|
|
|
|
|
|
|
|
} else if (tag instanceof ByteTag) {
|
|
|
|
return toNative((ByteTag) tag);
|
|
|
|
|
|
|
|
} else if (tag instanceof ByteArrayTag) {
|
|
|
|
return toNative((ByteArrayTag) tag);
|
|
|
|
|
|
|
|
} else if (tag instanceof CompoundTag) {
|
|
|
|
return toNative((CompoundTag) tag);
|
|
|
|
|
|
|
|
} else if (tag instanceof FloatTag) {
|
|
|
|
return toNative((FloatTag) tag);
|
|
|
|
|
|
|
|
} else if (tag instanceof ShortTag) {
|
|
|
|
return toNative((ShortTag) tag);
|
|
|
|
|
|
|
|
} else if (tag instanceof DoubleTag) {
|
|
|
|
return toNative((DoubleTag) tag);
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Can't convert tag of type " + tag.getClass().getCanonicalName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagIntArray toNative(IntArrayTag tag) {
|
|
|
|
int[] value = tag.getValue();
|
2014-10-12 19:27:51 +00:00
|
|
|
return new NBTTagIntArray(Arrays.copyOf(value, value.length));
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagList toNative(ListTag tag) {
|
2014-10-12 19:27:51 +00:00
|
|
|
NBTTagList list = new NBTTagList();
|
2014-04-06 08:41:28 +00:00
|
|
|
for (Tag child : tag.getValue()) {
|
|
|
|
if (child instanceof EndTag) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
list.appendTag(toNative(child));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagLong toNative(LongTag tag) {
|
2014-10-12 19:27:51 +00:00
|
|
|
return new NBTTagLong(tag.getValue());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagString toNative(StringTag tag) {
|
2014-10-12 19:27:51 +00:00
|
|
|
return new NBTTagString(tag.getValue());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagInt toNative(IntTag tag) {
|
2014-10-12 19:27:51 +00:00
|
|
|
return new NBTTagInt(tag.getValue());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagByte toNative(ByteTag tag) {
|
2014-10-12 19:27:51 +00:00
|
|
|
return new NBTTagByte(tag.getValue());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagByteArray toNative(ByteArrayTag tag) {
|
|
|
|
byte[] value = tag.getValue();
|
2014-10-12 19:27:51 +00:00
|
|
|
return new NBTTagByteArray(Arrays.copyOf(value, value.length));
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagCompound toNative(CompoundTag tag) {
|
2014-10-12 19:27:51 +00:00
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
2014-04-06 08:41:28 +00:00
|
|
|
for (Entry<String, Tag> child : tag.getValue().entrySet()) {
|
|
|
|
compound.setTag(child.getKey(), toNative(child.getValue()));
|
|
|
|
}
|
|
|
|
return compound;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagFloat toNative(FloatTag tag) {
|
2014-10-12 19:27:51 +00:00
|
|
|
return new NBTTagFloat(tag.getValue());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagShort toNative(ShortTag tag) {
|
2014-10-12 19:27:51 +00:00
|
|
|
return new NBTTagShort(tag.getValue());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagDouble toNative(DoubleTag tag) {
|
2014-10-12 19:27:51 +00:00
|
|
|
return new NBTTagDouble(tag.getValue());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Tag fromNative(NBTBase other) {
|
|
|
|
if (other instanceof NBTTagIntArray) {
|
|
|
|
return fromNative((NBTTagIntArray) other);
|
|
|
|
|
|
|
|
} else if (other instanceof NBTTagList) {
|
|
|
|
return fromNative((NBTTagList) other);
|
|
|
|
|
|
|
|
} else if (other instanceof NBTTagEnd) {
|
|
|
|
return fromNative((NBTTagEnd) other);
|
|
|
|
|
|
|
|
} else if (other instanceof NBTTagLong) {
|
|
|
|
return fromNative((NBTTagLong) other);
|
|
|
|
|
|
|
|
} else if (other instanceof NBTTagString) {
|
|
|
|
return fromNative((NBTTagString) other);
|
|
|
|
|
|
|
|
} else if (other instanceof NBTTagInt) {
|
|
|
|
return fromNative((NBTTagInt) other);
|
|
|
|
|
|
|
|
} else if (other instanceof NBTTagByte) {
|
|
|
|
return fromNative((NBTTagByte) other);
|
|
|
|
|
|
|
|
} else if (other instanceof NBTTagByteArray) {
|
|
|
|
return fromNative((NBTTagByteArray) other);
|
|
|
|
|
|
|
|
} else if (other instanceof NBTTagCompound) {
|
|
|
|
return fromNative((NBTTagCompound) other);
|
|
|
|
|
|
|
|
} else if (other instanceof NBTTagFloat) {
|
|
|
|
return fromNative((NBTTagFloat) other);
|
|
|
|
|
|
|
|
} else if (other instanceof NBTTagShort) {
|
|
|
|
return fromNative((NBTTagShort) other);
|
|
|
|
|
|
|
|
} else if (other instanceof NBTTagDouble) {
|
|
|
|
return fromNative((NBTTagDouble) other);
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Can't convert other of type " + other.getClass().getCanonicalName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IntArrayTag fromNative(NBTTagIntArray other) {
|
2015-01-16 03:39:15 +00:00
|
|
|
int[] value = other.getIntArray();
|
2014-10-12 19:27:51 +00:00
|
|
|
return new IntArrayTag(Arrays.copyOf(value, value.length));
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static ListTag fromNative(NBTTagList other) {
|
2016-11-18 06:17:04 +00:00
|
|
|
other = other.copy();
|
2014-04-06 08:41:28 +00:00
|
|
|
List<Tag> list = new ArrayList<Tag>();
|
|
|
|
Class<? extends Tag> listClass = StringTag.class;
|
2014-10-16 04:55:59 +00:00
|
|
|
int tags = other.tagCount();
|
|
|
|
for (int i = 0; i < tags; i++) {
|
2014-10-12 19:27:51 +00:00
|
|
|
Tag child = fromNative(other.removeTag(0));
|
2014-04-06 08:41:28 +00:00
|
|
|
list.add(child);
|
|
|
|
listClass = child.getClass();
|
|
|
|
}
|
2014-10-12 19:27:51 +00:00
|
|
|
return new ListTag(listClass, list);
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static EndTag fromNative(NBTTagEnd other) {
|
|
|
|
return new EndTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static LongTag fromNative(NBTTagLong other) {
|
2015-01-16 03:39:15 +00:00
|
|
|
return new LongTag(other.getLong());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static StringTag fromNative(NBTTagString other) {
|
2015-01-16 03:39:15 +00:00
|
|
|
return new StringTag(other.getString());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static IntTag fromNative(NBTTagInt other) {
|
2015-01-16 03:39:15 +00:00
|
|
|
return new IntTag(other.getInt());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static ByteTag fromNative(NBTTagByte other) {
|
2015-01-16 03:39:15 +00:00
|
|
|
return new ByteTag(other.getByte());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static ByteArrayTag fromNative(NBTTagByteArray other) {
|
2015-01-16 03:39:15 +00:00
|
|
|
byte[] value = other.getByteArray();
|
2014-10-12 19:27:51 +00:00
|
|
|
return new ByteArrayTag(Arrays.copyOf(value, value.length));
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static CompoundTag fromNative(NBTTagCompound other) {
|
2016-03-19 16:53:52 +00:00
|
|
|
Set<String> tags = other.getKeySet();
|
2014-04-06 08:41:28 +00:00
|
|
|
Map<String, Tag> map = new HashMap<String, Tag>();
|
2014-10-12 19:27:51 +00:00
|
|
|
for (String tagName : tags) {
|
|
|
|
map.put(tagName, fromNative(other.getTag(tagName)));
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
2014-10-12 19:27:51 +00:00
|
|
|
return new CompoundTag(map);
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static FloatTag fromNative(NBTTagFloat other) {
|
2015-01-16 03:39:15 +00:00
|
|
|
return new FloatTag(other.getFloat());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static ShortTag fromNative(NBTTagShort other) {
|
2015-01-16 03:39:15 +00:00
|
|
|
return new ShortTag(other.getShort());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static DoubleTag fromNative(NBTTagDouble other) {
|
2015-01-16 03:39:15 +00:00
|
|
|
return new DoubleTag(other.getDouble());
|
2014-04-06 08:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|