2014-04-04 22:03:18 +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/>.
|
|
|
|
*/
|
|
|
|
|
2011-05-01 02:04:03 +00:00
|
|
|
package com.sk89q.jnbt;
|
|
|
|
|
2014-04-03 02:08:50 +00:00
|
|
|
import com.sk89q.worldedit.world.storage.InvalidFormatException;
|
2011-05-01 02:04:03 +00:00
|
|
|
|
2014-04-04 21:26:08 +00:00
|
|
|
import java.util.Map;
|
|
|
|
|
2011-05-01 02:04:03 +00:00
|
|
|
/**
|
|
|
|
* A class which contains NBT-related utility methods.
|
2012-03-30 04:11:49 +00:00
|
|
|
*
|
2011-05-01 02:04:03 +00:00
|
|
|
* @author Graham Edgecombe
|
2012-03-30 04:11:49 +00:00
|
|
|
*
|
2011-05-01 02:04:03 +00:00
|
|
|
*/
|
|
|
|
public final class NBTUtils {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the type name of a tag.
|
2012-03-30 04:11:49 +00:00
|
|
|
*
|
2011-05-01 02:04:03 +00:00
|
|
|
* @param clazz
|
|
|
|
* The tag class.
|
|
|
|
* @return The type name.
|
|
|
|
*/
|
|
|
|
public static String getTypeName(Class<? extends Tag> clazz) {
|
|
|
|
if (clazz.equals(ByteArrayTag.class)) {
|
|
|
|
return "TAG_Byte_Array";
|
|
|
|
} else if (clazz.equals(ByteTag.class)) {
|
|
|
|
return "TAG_Byte";
|
|
|
|
} else if (clazz.equals(CompoundTag.class)) {
|
|
|
|
return "TAG_Compound";
|
|
|
|
} else if (clazz.equals(DoubleTag.class)) {
|
|
|
|
return "TAG_Double";
|
|
|
|
} else if (clazz.equals(EndTag.class)) {
|
|
|
|
return "TAG_End";
|
|
|
|
} else if (clazz.equals(FloatTag.class)) {
|
|
|
|
return "TAG_Float";
|
|
|
|
} else if (clazz.equals(IntTag.class)) {
|
|
|
|
return "TAG_Int";
|
|
|
|
} else if (clazz.equals(ListTag.class)) {
|
|
|
|
return "TAG_List";
|
|
|
|
} else if (clazz.equals(LongTag.class)) {
|
|
|
|
return "TAG_Long";
|
|
|
|
} else if (clazz.equals(ShortTag.class)) {
|
|
|
|
return "TAG_Short";
|
|
|
|
} else if (clazz.equals(StringTag.class)) {
|
|
|
|
return "TAG_String";
|
2012-03-05 06:05:17 +00:00
|
|
|
} else if (clazz.equals(IntArrayTag.class)) {
|
|
|
|
return "TAG_Int_Array";
|
2011-05-01 02:04:03 +00:00
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Invalid tag classs ("
|
|
|
|
+ clazz.getName() + ").");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the type code of a tag class.
|
2012-03-30 04:11:49 +00:00
|
|
|
*
|
2011-05-01 02:04:03 +00:00
|
|
|
* @param clazz
|
|
|
|
* The tag class.
|
|
|
|
* @return The type code.
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
* if the tag class is invalid.
|
|
|
|
*/
|
|
|
|
public static int getTypeCode(Class<? extends Tag> clazz) {
|
|
|
|
if (clazz.equals(ByteArrayTag.class)) {
|
|
|
|
return NBTConstants.TYPE_BYTE_ARRAY;
|
|
|
|
} else if (clazz.equals(ByteTag.class)) {
|
|
|
|
return NBTConstants.TYPE_BYTE;
|
|
|
|
} else if (clazz.equals(CompoundTag.class)) {
|
|
|
|
return NBTConstants.TYPE_COMPOUND;
|
|
|
|
} else if (clazz.equals(DoubleTag.class)) {
|
|
|
|
return NBTConstants.TYPE_DOUBLE;
|
|
|
|
} else if (clazz.equals(EndTag.class)) {
|
|
|
|
return NBTConstants.TYPE_END;
|
|
|
|
} else if (clazz.equals(FloatTag.class)) {
|
|
|
|
return NBTConstants.TYPE_FLOAT;
|
|
|
|
} else if (clazz.equals(IntTag.class)) {
|
|
|
|
return NBTConstants.TYPE_INT;
|
|
|
|
} else if (clazz.equals(ListTag.class)) {
|
|
|
|
return NBTConstants.TYPE_LIST;
|
|
|
|
} else if (clazz.equals(LongTag.class)) {
|
|
|
|
return NBTConstants.TYPE_LONG;
|
|
|
|
} else if (clazz.equals(ShortTag.class)) {
|
|
|
|
return NBTConstants.TYPE_SHORT;
|
|
|
|
} else if (clazz.equals(StringTag.class)) {
|
|
|
|
return NBTConstants.TYPE_STRING;
|
2012-03-05 06:05:17 +00:00
|
|
|
} else if (clazz.equals(IntArrayTag.class)) {
|
2012-03-30 04:11:49 +00:00
|
|
|
return NBTConstants.TYPE_INT_ARRAY;
|
2011-05-01 02:04:03 +00:00
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Invalid tag classs ("
|
|
|
|
+ clazz.getName() + ").");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the class of a type of tag.
|
2012-03-30 04:11:49 +00:00
|
|
|
*
|
2011-05-01 02:04:03 +00:00
|
|
|
* @param type
|
|
|
|
* The type.
|
|
|
|
* @return The class.
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
* if the tag type is invalid.
|
|
|
|
*/
|
|
|
|
public static Class<? extends Tag> getTypeClass(int type) {
|
|
|
|
switch (type) {
|
|
|
|
case NBTConstants.TYPE_END:
|
|
|
|
return EndTag.class;
|
|
|
|
case NBTConstants.TYPE_BYTE:
|
|
|
|
return ByteTag.class;
|
|
|
|
case NBTConstants.TYPE_SHORT:
|
|
|
|
return ShortTag.class;
|
|
|
|
case NBTConstants.TYPE_INT:
|
|
|
|
return IntTag.class;
|
|
|
|
case NBTConstants.TYPE_LONG:
|
|
|
|
return LongTag.class;
|
|
|
|
case NBTConstants.TYPE_FLOAT:
|
|
|
|
return FloatTag.class;
|
|
|
|
case NBTConstants.TYPE_DOUBLE:
|
|
|
|
return DoubleTag.class;
|
|
|
|
case NBTConstants.TYPE_BYTE_ARRAY:
|
|
|
|
return ByteArrayTag.class;
|
|
|
|
case NBTConstants.TYPE_STRING:
|
|
|
|
return StringTag.class;
|
|
|
|
case NBTConstants.TYPE_LIST:
|
|
|
|
return ListTag.class;
|
|
|
|
case NBTConstants.TYPE_COMPOUND:
|
|
|
|
return CompoundTag.class;
|
2012-03-05 06:05:17 +00:00
|
|
|
case NBTConstants.TYPE_INT_ARRAY:
|
|
|
|
return IntArrayTag.class;
|
2011-05-01 02:04:03 +00:00
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Invalid tag type : " + type
|
|
|
|
+ ".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default private constructor.
|
|
|
|
*/
|
|
|
|
private NBTUtils() {
|
|
|
|
|
|
|
|
}
|
2012-03-30 04:11:49 +00:00
|
|
|
|
2012-03-05 06:05:17 +00:00
|
|
|
/**
|
|
|
|
* Get child tag of a NBT structure.
|
|
|
|
*
|
|
|
|
* @param items
|
|
|
|
* @param key
|
|
|
|
* @param expected
|
|
|
|
* @return child tag
|
|
|
|
* @throws InvalidFormatException
|
|
|
|
*/
|
|
|
|
public static <T extends Tag> T getChildTag(Map<String,Tag> items, String key,
|
2014-04-04 21:26:08 +00:00
|
|
|
Class<T> expected) throws InvalidFormatException {
|
2012-03-05 06:05:17 +00:00
|
|
|
if (!items.containsKey(key)) {
|
|
|
|
throw new InvalidFormatException("Missing a \"" + key + "\" tag");
|
|
|
|
}
|
|
|
|
Tag tag = items.get(key);
|
|
|
|
if (!expected.isInstance(tag)) {
|
|
|
|
throw new InvalidFormatException(key + " tag is not of tag type " + expected.getName());
|
|
|
|
}
|
|
|
|
return expected.cast(tag);
|
|
|
|
}
|
2011-05-01 02:04:03 +00:00
|
|
|
|
|
|
|
}
|