From 1c005ad1ec4bff3d5856b1e395eb5624af5a8601 Mon Sep 17 00:00:00 2001 From: Steveice10 Date: Tue, 25 Mar 2014 20:16:23 -0700 Subject: [PATCH] 1.4: Refactor packages, remove generics from ListTag, add value<->tag conversion system, merge NBTFileIO and NBTIO --- pom.xml | 2 +- .../java/org/spacehq/opennbt/NBTFileIO.java | 128 --------------- src/main/java/org/spacehq/opennbt/NBTIO.java | 149 ++++++++++++++---- .../conversion/ConversionException.java | 26 +++ .../ConverterRegisterException.java | 26 +++ .../opennbt/conversion/ConverterRegistry.java | 144 +++++++++++++++++ .../opennbt/conversion/TagConverter.java | 29 ++++ .../builtin/ByteArrayTagConverter.java | 21 +++ .../conversion/builtin/ByteTagConverter.java | 21 +++ .../builtin/CompoundTagConverter.java | 39 +++++ .../builtin/DoubleTagConverter.java | 21 +++ .../conversion/builtin/FloatTagConverter.java | 21 +++ .../builtin/IntArrayTagConverter.java | 21 +++ .../conversion/builtin/IntTagConverter.java | 21 +++ .../conversion/builtin/ListTagConverter.java | 41 +++++ .../conversion/builtin/LongTagConverter.java | 21 +++ .../conversion/builtin/ShortTagConverter.java | 21 +++ .../builtin/StringTagConverter.java | 21 +++ .../custom/DoubleArrayTagConverter.java | 21 +++ .../custom/FloatArrayTagConverter.java | 21 +++ .../builtin/custom/LongArrayTagConverter.java | 21 +++ .../custom/SerializableArrayTagConverter.java | 23 +++ .../custom/SerializableTagConverter.java | 23 +++ .../custom/ShortArrayTagConverter.java | 21 +++ .../custom/StringArrayTagConverter.java | 21 +++ .../opennbt/{ => tag}/TagCreateException.java | 2 +- .../{ => tag}/TagRegisterException.java | 6 +- .../opennbt/{ => tag}/TagRegistry.java | 46 +++--- .../tag/{ => builtin}/ByteArrayTag.java | 2 +- .../opennbt/tag/{ => builtin}/ByteTag.java | 2 +- .../tag/{ => builtin}/CompoundTag.java | 42 ++--- .../opennbt/tag/{ => builtin}/DoubleTag.java | 2 +- .../opennbt/tag/{ => builtin}/FloatTag.java | 2 +- .../tag/{ => builtin}/IntArrayTag.java | 2 +- .../opennbt/tag/{ => builtin}/IntTag.java | 2 +- .../opennbt/tag/{ => builtin}/ListTag.java | 111 ++++++------- .../opennbt/tag/{ => builtin}/LongTag.java | 2 +- .../opennbt/tag/{ => builtin}/ShortTag.java | 2 +- .../opennbt/tag/{ => builtin}/StringTag.java | 2 +- .../opennbt/tag/{ => builtin}/Tag.java | 2 +- .../{ => builtin}/custom/DoubleArrayTag.java | 4 +- .../{ => builtin}/custom/FloatArrayTag.java | 4 +- .../{ => builtin}/custom/LongArrayTag.java | 4 +- .../custom/SerializableArrayTag.java | 4 +- .../{ => builtin}/custom/SerializableTag.java | 4 +- .../{ => builtin}/custom/ShortArrayTag.java | 4 +- .../{ => builtin}/custom/StringArrayTag.java | 4 +- 47 files changed, 883 insertions(+), 296 deletions(-) delete mode 100644 src/main/java/org/spacehq/opennbt/NBTFileIO.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/ConversionException.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/ConverterRegisterException.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/ConverterRegistry.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/TagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/ByteArrayTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/ByteTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/CompoundTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/DoubleTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/FloatTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/IntArrayTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/IntTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/ListTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/LongTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/ShortTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/StringTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/custom/DoubleArrayTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/custom/FloatArrayTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/custom/LongArrayTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/custom/SerializableArrayTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/custom/SerializableTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/custom/ShortArrayTagConverter.java create mode 100644 src/main/java/org/spacehq/opennbt/conversion/builtin/custom/StringArrayTagConverter.java rename src/main/java/org/spacehq/opennbt/{ => tag}/TagCreateException.java (93%) rename src/main/java/org/spacehq/opennbt/{ => tag}/TagRegisterException.java (81%) rename src/main/java/org/spacehq/opennbt/{ => tag}/TagRegistry.java (73%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/ByteArrayTag.java (97%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/ByteTag.java (96%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/CompoundTag.java (83%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/DoubleTag.java (96%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/FloatTag.java (96%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/IntArrayTag.java (97%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/IntTag.java (96%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/ListTag.java (54%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/LongTag.java (96%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/ShortTag.java (96%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/StringTag.java (96%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/Tag.java (98%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/custom/DoubleArrayTag.java (95%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/custom/FloatArrayTag.java (95%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/custom/LongArrayTag.java (95%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/custom/SerializableArrayTag.java (96%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/custom/SerializableTag.java (93%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/custom/ShortArrayTag.java (95%) rename src/main/java/org/spacehq/opennbt/tag/{ => builtin}/custom/StringArrayTag.java (95%) diff --git a/pom.xml b/pom.xml index b510ac3..e9ac7bd 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.spacehq opennbt - 1.3 + 1.4 jar OpenNBT diff --git a/src/main/java/org/spacehq/opennbt/NBTFileIO.java b/src/main/java/org/spacehq/opennbt/NBTFileIO.java deleted file mode 100644 index 662ef20..0000000 --- a/src/main/java/org/spacehq/opennbt/NBTFileIO.java +++ /dev/null @@ -1,128 +0,0 @@ -package org.spacehq.opennbt; - -import org.spacehq.opennbt.tag.CompoundTag; -import org.spacehq.opennbt.tag.Tag; - -import java.io.*; -import java.util.zip.GZIPInputStream; -import java.util.zip.GZIPOutputStream; - -public class NBTFileIO { - - /** - * Reads the root CompoundTag from the given file. - * - * @param path Path of the file. - * @return The read compound tag. - * @throws java.io.IOException If an I/O error occurs. - */ - public static CompoundTag readFile(String path) throws IOException { - return readFile(new File(path)); - } - - /** - * Reads the root CompoundTag from the given file. - * - * @param file File to read from. - * @return The read compound tag. - * @throws java.io.IOException If an I/O error occurs. - */ - public static CompoundTag readFile(File file) throws IOException { - return readFile(file, true); - } - - /** - * Reads the root CompoundTag from the given file. - * - * @param path Path of the file. - * @param compressed Whether the NBT file is compressed. - * @return The read compound tag. - * @throws java.io.IOException If an I/O error occurs. - */ - public static CompoundTag readFile(String path, boolean compressed) throws IOException { - return readFile(new File(path), compressed); - } - - /** - * Reads the root CompoundTag from the given file. - * - * @param file File to read from. - * @param compressed Whether the NBT file is compressed. - * @return The read compound tag. - * @throws java.io.IOException If an I/O error occurs. - */ - public static CompoundTag readFile(File file, boolean compressed) throws IOException { - InputStream in = new FileInputStream(file); - if(compressed) { - in = new GZIPInputStream(in); - } - - Tag tag = NBTIO.readTag(new DataInputStream(in)); - if(!(tag instanceof CompoundTag)) { - throw new IOException("Root tag is not a CompoundTag!"); - } - - return (CompoundTag) tag; - } - - /** - * Writes the given root CompoundTag to the given file. - * - * @param tag Tag to write. - * @param path Path to write to. - * @throws java.io.IOException If an I/O error occurs. - */ - public static void writeFile(CompoundTag tag, String path) throws IOException { - writeFile(tag, new File(path)); - } - - /** - * Writes the given root CompoundTag to the given file. - * - * @param tag Tag to write. - * @param file File to write to. - * @throws java.io.IOException If an I/O error occurs. - */ - public static void writeFile(CompoundTag tag, File file) throws IOException { - writeFile(tag, file, true); - } - - /** - * Writes the given root CompoundTag to the given file. - * - * @param tag Tag to write. - * @param path Path to write to. - * @param compressed Whether the NBT file should be compressed. - * @throws java.io.IOException If an I/O error occurs. - */ - public static void writeFile(CompoundTag tag, String path, boolean compressed) throws IOException { - writeFile(tag, new File(path), compressed); - } - - /** - * Writes the given root CompoundTag to the given file. - * - * @param tag Tag to write. - * @param file File to write to. - * @param compressed Whether the NBT file should be compressed. - * @throws java.io.IOException If an I/O error occurs. - */ - public static void writeFile(CompoundTag tag, File file, boolean compressed) throws IOException { - if(!file.exists()) { - if(file.getParentFile() != null && !file.getParentFile().exists()) { - file.getParentFile().mkdirs(); - } - - file.createNewFile(); - } - - OutputStream out = new FileOutputStream(file); - if(compressed) { - out = new GZIPOutputStream(out); - } - - NBTIO.writeTag(new DataOutputStream(out), tag); - out.close(); - } - -} diff --git a/src/main/java/org/spacehq/opennbt/NBTIO.java b/src/main/java/org/spacehq/opennbt/NBTIO.java index 8e47a3a..c5ba869 100644 --- a/src/main/java/org/spacehq/opennbt/NBTIO.java +++ b/src/main/java/org/spacehq/opennbt/NBTIO.java @@ -1,15 +1,14 @@ package org.spacehq.opennbt; -import org.spacehq.opennbt.tag.Tag; +import org.spacehq.opennbt.tag.TagCreateException; +import org.spacehq.opennbt.tag.TagRegistry; +import org.spacehq.opennbt.tag.builtin.CompoundTag; +import org.spacehq.opennbt.tag.builtin.Tag; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.EOFException; -import java.io.IOException; +import java.io.*; import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; /** * A class containing methods for reading/writing NBT tags. @@ -19,24 +18,119 @@ public class NBTIO { public static final Charset CHARSET = Charset.forName("UTF-8"); /** - * Reads NBT tags until an end tag is reached. + * Reads the root CompoundTag from the given file. * - * @param in Input stream to read from. - * @return The read tags. + * @param path Path of the file. + * @return The read compound tag. * @throws java.io.IOException If an I/O error occurs. */ - public static List readUntilEndTag(DataInputStream in) throws IOException { - List ret = new ArrayList(); - try { - Tag tag; - while((tag = readTag(in)) != null) { - ret.add(tag); - } - } catch(EOFException e) { - throw new IOException("Closing EndTag was not found!"); + public static CompoundTag readFile(String path) throws IOException { + return readFile(new File(path)); + } + + /** + * Reads the root CompoundTag from the given file. + * + * @param file File to read from. + * @return The read compound tag. + * @throws java.io.IOException If an I/O error occurs. + */ + public static CompoundTag readFile(File file) throws IOException { + return readFile(file, true); + } + + /** + * Reads the root CompoundTag from the given file. + * + * @param path Path of the file. + * @param compressed Whether the NBT file is compressed. + * @return The read compound tag. + * @throws java.io.IOException If an I/O error occurs. + */ + public static CompoundTag readFile(String path, boolean compressed) throws IOException { + return readFile(new File(path), compressed); + } + + /** + * Reads the root CompoundTag from the given file. + * + * @param file File to read from. + * @param compressed Whether the NBT file is compressed. + * @return The read compound tag. + * @throws java.io.IOException If an I/O error occurs. + */ + public static CompoundTag readFile(File file, boolean compressed) throws IOException { + InputStream in = new FileInputStream(file); + if(compressed) { + in = new GZIPInputStream(in); } - return ret; + Tag tag = readTag(new DataInputStream(in)); + if(!(tag instanceof CompoundTag)) { + throw new IOException("Root tag is not a CompoundTag!"); + } + + return (CompoundTag) tag; + } + + /** + * Writes the given root CompoundTag to the given file. + * + * @param tag Tag to write. + * @param path Path to write to. + * @throws java.io.IOException If an I/O error occurs. + */ + public static void writeFile(CompoundTag tag, String path) throws IOException { + writeFile(tag, new File(path)); + } + + /** + * Writes the given root CompoundTag to the given file. + * + * @param tag Tag to write. + * @param file File to write to. + * @throws java.io.IOException If an I/O error occurs. + */ + public static void writeFile(CompoundTag tag, File file) throws IOException { + writeFile(tag, file, true); + } + + /** + * Writes the given root CompoundTag to the given file. + * + * @param tag Tag to write. + * @param path Path to write to. + * @param compressed Whether the NBT file should be compressed. + * @throws java.io.IOException If an I/O error occurs. + */ + public static void writeFile(CompoundTag tag, String path, boolean compressed) throws IOException { + writeFile(tag, new File(path), compressed); + } + + /** + * Writes the given root CompoundTag to the given file. + * + * @param tag Tag to write. + * @param file File to write to. + * @param compressed Whether the NBT file should be compressed. + * @throws java.io.IOException If an I/O error occurs. + */ + public static void writeFile(CompoundTag tag, File file, boolean compressed) throws IOException { + if(!file.exists()) { + if(file.getParentFile() != null && !file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + + file.createNewFile(); + } + + OutputStream out = new FileOutputStream(file); + if(compressed) { + out = new GZIPOutputStream(out); + } + + writeTag(new DataOutputStream(out), tag); + out.close(); } /** @@ -66,19 +160,6 @@ public class NBTIO { return tag; } - /** - * Writes a collection of tags to an output stream. - * - * @param out Output stream to write to. - * @param tags Tags to write. - * @throws java.io.IOException If an I/O error occurs. - */ - public static void writeTags(DataOutputStream out, Collection tags) throws IOException { - for(Tag tag : tags) { - writeTag(out, tag); - } - } - /** * Writes a tag to an output stream. * diff --git a/src/main/java/org/spacehq/opennbt/conversion/ConversionException.java b/src/main/java/org/spacehq/opennbt/conversion/ConversionException.java new file mode 100644 index 0000000..06161de --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/ConversionException.java @@ -0,0 +1,26 @@ +package org.spacehq.opennbt.conversion; + +/** + * An exception thrown when an error occurs while converting something. + */ +public class ConversionException extends RuntimeException { + + private static final long serialVersionUID = -2022049594558041160L; + + public ConversionException() { + super(); + } + + public ConversionException(String message) { + super(message); + } + + public ConversionException(Throwable cause) { + super(cause); + } + + public ConversionException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/ConverterRegisterException.java b/src/main/java/org/spacehq/opennbt/conversion/ConverterRegisterException.java new file mode 100644 index 0000000..ec69539 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/ConverterRegisterException.java @@ -0,0 +1,26 @@ +package org.spacehq.opennbt.conversion; + +/** + * An exception thrown when an error occurs while registering a converter. + */ +public class ConverterRegisterException extends RuntimeException { + + private static final long serialVersionUID = -2022049594558041160L; + + public ConverterRegisterException() { + super(); + } + + public ConverterRegisterException(String message) { + super(message); + } + + public ConverterRegisterException(Throwable cause) { + super(cause); + } + + public ConverterRegisterException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/ConverterRegistry.java b/src/main/java/org/spacehq/opennbt/conversion/ConverterRegistry.java new file mode 100644 index 0000000..96472d3 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/ConverterRegistry.java @@ -0,0 +1,144 @@ +package org.spacehq.opennbt.conversion; + +import org.spacehq.opennbt.conversion.builtin.*; +import org.spacehq.opennbt.conversion.builtin.custom.*; +import org.spacehq.opennbt.tag.TagRegisterException; +import org.spacehq.opennbt.tag.builtin.*; +import org.spacehq.opennbt.tag.builtin.custom.*; + +import java.io.Serializable; +import java.util.*; + +/** + * A registry mapping tags and value types to converters. + */ +public class ConverterRegistry { + + private static final Map, TagConverter> tagToConverter = new HashMap, TagConverter>(); + private static final Map, TagConverter> typeToConverter = new HashMap, TagConverter>(); + + static { + register(ByteTag.class, Byte.class, new ByteTagConverter()); + register(ShortTag.class, Short.class, new ShortTagConverter()); + register(IntTag.class, Integer.class, new IntTagConverter()); + register(LongTag.class, Long.class, new LongTagConverter()); + register(FloatTag.class, Float.class, new FloatTagConverter()); + register(DoubleTag.class, Double.class, new DoubleTagConverter()); + register(ByteArrayTag.class, byte[].class, new ByteArrayTagConverter()); + register(StringTag.class, String.class, new StringTagConverter()); + register(ListTag.class, List.class, new ListTagConverter()); + register(CompoundTag.class, Map.class, new CompoundTagConverter()); + register(IntArrayTag.class, int[].class, new IntArrayTagConverter()); + + register(DoubleArrayTag.class, double[].class, new DoubleArrayTagConverter()); + register(FloatArrayTag.class, float[].class, new FloatArrayTagConverter()); + register(LongArrayTag.class, long[].class, new LongArrayTagConverter()); + register(SerializableArrayTag.class, Serializable[].class, new SerializableArrayTagConverter()); + register(SerializableTag.class, Serializable.class, new SerializableTagConverter()); + register(ShortArrayTag.class, short[].class, new ShortArrayTagConverter()); + register(StringArrayTag.class, String[].class, new StringArrayTagConverter()); + } + + /** + * Registers a converter. + * + * @param tag Tag type class to register the converter to. + * @param type Value type class to register the converter to. + * @param converter Converter to register. + * @throws ConverterRegisterException If an error occurs while registering the converter. + */ + public static void register(Class tag, Class type, TagConverter converter) throws ConverterRegisterException { + if(tagToConverter.containsKey(tag)) { + throw new TagRegisterException("Type conversion to tag " + tag.getName() + " is already registered."); + } + + if(typeToConverter.containsKey(type)) { + throw new TagRegisterException("Tag conversion to type " + type.getName() + " is already registered."); + } + + tagToConverter.put(tag, converter); + typeToConverter.put(type, converter); + } + + /** + * Converts the given tag to a value. + * + * @param tag Tag to convert. + * @return The converted value. + * @throw ConversionException If a suitable converter could not be found. + */ + public static V convertToValue(T tag) throws ConversionException { + if(tag == null || tag.getValue() == null) { + return null; + } + + if(!tagToConverter.containsKey(tag.getClass())) { + throw new ConversionException("Tag type " + tag.getClass().getName() + " has no converter."); + } + + TagConverter converter = (TagConverter) tagToConverter.get(tag.getClass()); + return (V) converter.convert(tag); + } + + /** + * Converts the given value to a tag. + * + * @param name Name of the resulting tag. + * @param value Value to convert. + * @return The converted tag. + * @throw ConversionException If a suitable converter could not be found. + */ + public static T convertToTag(String name, V value) throws ConversionException { + if(value == null) { + return null; + } + + TagConverter converter = (TagConverter) typeToConverter.get(value.getClass()); + if(converter == null) { + for(Class clazz : getAllClasses(value.getClass())) { + if(typeToConverter.containsKey(clazz)) { + try { + converter = (TagConverter) typeToConverter.get(clazz); + break; + } catch(ClassCastException e) { + } + } + } + } + + if(converter == null) { + throw new ConversionException("Value type " + value.getClass().getName() + " has no converter."); + } + + return converter.convert(name, value); + } + + private static Set> getAllClasses(Class clazz) { + Set> ret = new LinkedHashSet>(); + Class c = clazz; + while(c != null) { + ret.add(c); + ret.addAll(getAllSuperInterfaces(c)); + c = c.getSuperclass(); + } + + // Make sure Serializable is at the end to avoid mix-ups. + if(ret.contains(Serializable.class)) { + ret.remove(Serializable.class); + ret.add(Serializable.class); + } + + return ret; + } + + private static Set> getAllSuperInterfaces(Class clazz) { + Set> ret = new HashSet>(); + for(Class c : clazz.getInterfaces()) { + ret.add(c); + ret.addAll(getAllSuperInterfaces(c)); + } + + return ret; + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/TagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/TagConverter.java new file mode 100644 index 0000000..255a5e1 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/TagConverter.java @@ -0,0 +1,29 @@ +package org.spacehq.opennbt.conversion; + +import org.spacehq.opennbt.tag.builtin.Tag; + +/** + * A converter that converts between a tag type and a value type. A converted tag will have its value and all children converted to raw types and vice versa. + * + * @param Tag type. + * @param Value type. + */ +public interface TagConverter { + + /** + * Converts a tag to a value. + * + * @param tag Tag to convert. + * @return The converted value. + */ + public V convert(T tag); + + /** + * Converts a value to a tag. + * + * @param value Value to convert. + * @return The converted tag. + */ + public T convert(String name, V value); + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/ByteArrayTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/ByteArrayTagConverter.java new file mode 100644 index 0000000..976b253 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/ByteArrayTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.ByteArrayTag; + +/** + * A converter that converts between ByteArrayTag and byte[]. + */ +public class ByteArrayTagConverter implements TagConverter { + + @Override + public byte[] convert(ByteArrayTag tag) { + return tag.getValue(); + } + + @Override + public ByteArrayTag convert(String name, byte[] value) { + return new ByteArrayTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/ByteTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/ByteTagConverter.java new file mode 100644 index 0000000..405dd1c --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/ByteTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.ByteTag; + +/** + * A converter that converts between ByteTag and byte. + */ +public class ByteTagConverter implements TagConverter { + + @Override + public Byte convert(ByteTag tag) { + return tag.getValue(); + } + + @Override + public ByteTag convert(String name, Byte value) { + return new ByteTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/CompoundTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/CompoundTagConverter.java new file mode 100644 index 0000000..ad73551 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/CompoundTagConverter.java @@ -0,0 +1,39 @@ +package org.spacehq.opennbt.conversion.builtin; + +import org.spacehq.opennbt.conversion.ConverterRegistry; +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.CompoundTag; +import org.spacehq.opennbt.tag.builtin.Tag; + +import java.util.HashMap; +import java.util.Map; + +/** + * A converter that converts between CompoundTag and Map. + */ +public class CompoundTagConverter implements TagConverter { + + @Override + public Map convert(CompoundTag tag) { + Map ret = new HashMap(); + Map tags = tag.getValue(); + for(String name : tags.keySet()) { + Tag t = tags.get(name); + ret.put(t.getName(), ConverterRegistry.convertToValue(t)); + } + + return ret; + } + + @Override + public CompoundTag convert(String name, Map value) { + Map tags = new HashMap(); + for(Object na : value.keySet()) { + String n = (String) na; + tags.put(n, ConverterRegistry.convertToTag(n, value.get(n))); + } + + return new CompoundTag(name, tags); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/DoubleTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/DoubleTagConverter.java new file mode 100644 index 0000000..d38ff30 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/DoubleTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.DoubleTag; + +/** + * A converter that converts between DoubleTag and double. + */ +public class DoubleTagConverter implements TagConverter { + + @Override + public Double convert(DoubleTag tag) { + return tag.getValue(); + } + + @Override + public DoubleTag convert(String name, Double value) { + return new DoubleTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/FloatTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/FloatTagConverter.java new file mode 100644 index 0000000..d4f7e90 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/FloatTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.FloatTag; + +/** + * A converter that converts between FloatTag and float. + */ +public class FloatTagConverter implements TagConverter { + + @Override + public Float convert(FloatTag tag) { + return tag.getValue(); + } + + @Override + public FloatTag convert(String name, Float value) { + return new FloatTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/IntArrayTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/IntArrayTagConverter.java new file mode 100644 index 0000000..bf6e234 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/IntArrayTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.IntArrayTag; + +/** + * A converter that converts between IntArrayTag and int[]. + */ +public class IntArrayTagConverter implements TagConverter { + + @Override + public int[] convert(IntArrayTag tag) { + return tag.getValue(); + } + + @Override + public IntArrayTag convert(String name, int[] value) { + return new IntArrayTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/IntTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/IntTagConverter.java new file mode 100644 index 0000000..ad1d9d5 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/IntTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.IntTag; + +/** + * A converter that converts between IntTag and int. + */ +public class IntTagConverter implements TagConverter { + + @Override + public Integer convert(IntTag tag) { + return tag.getValue(); + } + + @Override + public IntTag convert(String name, Integer value) { + return new IntTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/ListTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/ListTagConverter.java new file mode 100644 index 0000000..beb32d5 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/ListTagConverter.java @@ -0,0 +1,41 @@ +package org.spacehq.opennbt.conversion.builtin; + +import org.spacehq.opennbt.conversion.ConverterRegistry; +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.ListTag; +import org.spacehq.opennbt.tag.builtin.Tag; + +import java.util.ArrayList; +import java.util.List; + +/** + * A converter that converts between CompoundTag and Map. + */ +public class ListTagConverter implements TagConverter { + + @Override + public List convert(ListTag tag) { + List ret = new ArrayList(); + List tags = tag.getValue(); + for(Tag t : tags) { + ret.add(ConverterRegistry.convertToValue(t)); + } + + return ret; + } + + @Override + public ListTag convert(String name, List value) { + if(value.isEmpty()) { + throw new IllegalArgumentException("Cannot convert ListTag with size of 0."); + } + + List tags = new ArrayList(); + for(Object o : value) { + tags.add(ConverterRegistry.convertToTag("", o)); + } + + return new ListTag(name, tags); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/LongTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/LongTagConverter.java new file mode 100644 index 0000000..bd9e76b --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/LongTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.LongTag; + +/** + * A converter that converts between LongTag and long. + */ +public class LongTagConverter implements TagConverter { + + @Override + public Long convert(LongTag tag) { + return tag.getValue(); + } + + @Override + public LongTag convert(String name, Long value) { + return new LongTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/ShortTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/ShortTagConverter.java new file mode 100644 index 0000000..60d45cb --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/ShortTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.ShortTag; + +/** + * A converter that converts between ShortTag and short. + */ +public class ShortTagConverter implements TagConverter { + + @Override + public Short convert(ShortTag tag) { + return tag.getValue(); + } + + @Override + public ShortTag convert(String name, Short value) { + return new ShortTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/StringTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/StringTagConverter.java new file mode 100644 index 0000000..2bf131d --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/StringTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.StringTag; + +/** + * A converter that converts between StringTag and String. + */ +public class StringTagConverter implements TagConverter { + + @Override + public String convert(StringTag tag) { + return tag.getValue(); + } + + @Override + public StringTag convert(String name, String value) { + return new StringTag(name, value); + } + +} \ No newline at end of file diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/DoubleArrayTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/DoubleArrayTagConverter.java new file mode 100644 index 0000000..30cb53d --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/DoubleArrayTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin.custom; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.custom.DoubleArrayTag; + +/** + * A converter that converts between DoubleArrayTag and double[]. + */ +public class DoubleArrayTagConverter implements TagConverter { + + @Override + public double[] convert(DoubleArrayTag tag) { + return tag.getValue(); + } + + @Override + public DoubleArrayTag convert(String name, double[] value) { + return new DoubleArrayTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/FloatArrayTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/FloatArrayTagConverter.java new file mode 100644 index 0000000..6c1207a --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/FloatArrayTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin.custom; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.custom.FloatArrayTag; + +/** + * A converter that converts between FloatArrayTag and float[]. + */ +public class FloatArrayTagConverter implements TagConverter { + + @Override + public float[] convert(FloatArrayTag tag) { + return tag.getValue(); + } + + @Override + public FloatArrayTag convert(String name, float[] value) { + return new FloatArrayTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/LongArrayTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/LongArrayTagConverter.java new file mode 100644 index 0000000..b07657d --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/LongArrayTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin.custom; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.custom.LongArrayTag; + +/** + * A converter that converts between LongArrayTag and long[]. + */ +public class LongArrayTagConverter implements TagConverter { + + @Override + public long[] convert(LongArrayTag tag) { + return tag.getValue(); + } + + @Override + public LongArrayTag convert(String name, long[] value) { + return new LongArrayTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/SerializableArrayTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/SerializableArrayTagConverter.java new file mode 100644 index 0000000..e92f365 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/SerializableArrayTagConverter.java @@ -0,0 +1,23 @@ +package org.spacehq.opennbt.conversion.builtin.custom; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.custom.SerializableArrayTag; + +import java.io.Serializable; + +/** + * A converter that converts between SerializableArrayTag and Serializable[]. + */ +public class SerializableArrayTagConverter implements TagConverter { + + @Override + public Serializable[] convert(SerializableArrayTag tag) { + return tag.getValue(); + } + + @Override + public SerializableArrayTag convert(String name, Serializable[] value) { + return new SerializableArrayTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/SerializableTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/SerializableTagConverter.java new file mode 100644 index 0000000..8bff6ed --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/SerializableTagConverter.java @@ -0,0 +1,23 @@ +package org.spacehq.opennbt.conversion.builtin.custom; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.custom.SerializableTag; + +import java.io.Serializable; + +/** + * A converter that converts between SerializableTag and Serializable. + */ +public class SerializableTagConverter implements TagConverter { + + @Override + public Serializable convert(SerializableTag tag) { + return tag.getValue(); + } + + @Override + public SerializableTag convert(String name, Serializable value) { + return new SerializableTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/ShortArrayTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/ShortArrayTagConverter.java new file mode 100644 index 0000000..4e0a4d7 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/ShortArrayTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin.custom; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.custom.ShortArrayTag; + +/** + * A converter that converts between ShortArrayTag and short[]. + */ +public class ShortArrayTagConverter implements TagConverter { + + @Override + public short[] convert(ShortArrayTag tag) { + return tag.getValue(); + } + + @Override + public ShortArrayTag convert(String name, short[] value) { + return new ShortArrayTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/StringArrayTagConverter.java b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/StringArrayTagConverter.java new file mode 100644 index 0000000..2705638 --- /dev/null +++ b/src/main/java/org/spacehq/opennbt/conversion/builtin/custom/StringArrayTagConverter.java @@ -0,0 +1,21 @@ +package org.spacehq.opennbt.conversion.builtin.custom; + +import org.spacehq.opennbt.conversion.TagConverter; +import org.spacehq.opennbt.tag.builtin.custom.StringArrayTag; + +/** + * A converter that converts between StringArrayTag and String[]. + */ +public class StringArrayTagConverter implements TagConverter { + + @Override + public String[] convert(StringArrayTag tag) { + return tag.getValue(); + } + + @Override + public StringArrayTag convert(String name, String[] value) { + return new StringArrayTag(name, value); + } + +} diff --git a/src/main/java/org/spacehq/opennbt/TagCreateException.java b/src/main/java/org/spacehq/opennbt/tag/TagCreateException.java similarity index 93% rename from src/main/java/org/spacehq/opennbt/TagCreateException.java rename to src/main/java/org/spacehq/opennbt/tag/TagCreateException.java index 47bd5ac..9b11bec 100644 --- a/src/main/java/org/spacehq/opennbt/TagCreateException.java +++ b/src/main/java/org/spacehq/opennbt/tag/TagCreateException.java @@ -1,4 +1,4 @@ -package org.spacehq.opennbt; +package org.spacehq.opennbt.tag; /** * An exception thrown when an error occurs while created a tag instance. diff --git a/src/main/java/org/spacehq/opennbt/TagRegisterException.java b/src/main/java/org/spacehq/opennbt/tag/TagRegisterException.java similarity index 81% rename from src/main/java/org/spacehq/opennbt/TagRegisterException.java rename to src/main/java/org/spacehq/opennbt/tag/TagRegisterException.java index ab694aa..88dc671 100644 --- a/src/main/java/org/spacehq/opennbt/TagRegisterException.java +++ b/src/main/java/org/spacehq/opennbt/tag/TagRegisterException.java @@ -1,9 +1,9 @@ -package org.spacehq.opennbt; +package org.spacehq.opennbt.tag; /** - * An exception thrown when an error occurs while registering a tag class. + * An exception thrown when an error occurs while registering a tag. */ -public class TagRegisterException extends Exception { +public class TagRegisterException extends RuntimeException { private static final long serialVersionUID = -2022049594558041160L; diff --git a/src/main/java/org/spacehq/opennbt/TagRegistry.java b/src/main/java/org/spacehq/opennbt/tag/TagRegistry.java similarity index 73% rename from src/main/java/org/spacehq/opennbt/TagRegistry.java rename to src/main/java/org/spacehq/opennbt/tag/TagRegistry.java index abce346..6a35b68 100644 --- a/src/main/java/org/spacehq/opennbt/TagRegistry.java +++ b/src/main/java/org/spacehq/opennbt/tag/TagRegistry.java @@ -1,7 +1,7 @@ -package org.spacehq.opennbt; +package org.spacehq.opennbt.tag; -import org.spacehq.opennbt.tag.*; -import org.spacehq.opennbt.tag.custom.*; +import org.spacehq.opennbt.tag.builtin.*; +import org.spacehq.opennbt.tag.builtin.custom.*; import java.lang.reflect.Constructor; import java.util.HashMap; @@ -16,29 +16,25 @@ public class TagRegistry { private static final Map, Integer> tagToId = new HashMap, Integer>(); static { - try { - register(1, ByteTag.class); - register(2, ShortTag.class); - register(3, IntTag.class); - register(4, LongTag.class); - register(5, FloatTag.class); - register(6, DoubleTag.class); - register(7, ByteArrayTag.class); - register(8, StringTag.class); - register(9, ListTag.class); - register(10, CompoundTag.class); - register(11, IntArrayTag.class); + register(1, ByteTag.class); + register(2, ShortTag.class); + register(3, IntTag.class); + register(4, LongTag.class); + register(5, FloatTag.class); + register(6, DoubleTag.class); + register(7, ByteArrayTag.class); + register(8, StringTag.class); + register(9, ListTag.class); + register(10, CompoundTag.class); + register(11, IntArrayTag.class); - register(60, DoubleArrayTag.class); - register(61, FloatArrayTag.class); - register(62, LongArrayTag.class); - register(63, SerializableArrayTag.class); - register(64, SerializableTag.class); - register(65, ShortArrayTag.class); - register(66, StringArrayTag.class); - } catch(TagRegisterException e) { - throw new RuntimeException("Failed to register default tags.", e); - } + register(60, DoubleArrayTag.class); + register(61, FloatArrayTag.class); + register(62, LongArrayTag.class); + register(63, SerializableArrayTag.class); + register(64, SerializableTag.class); + register(65, ShortArrayTag.class); + register(66, StringArrayTag.class); } /** diff --git a/src/main/java/org/spacehq/opennbt/tag/ByteArrayTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/ByteArrayTag.java similarity index 97% rename from src/main/java/org/spacehq/opennbt/tag/ByteArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/ByteArrayTag.java index f31bb46..71eeebe 100644 --- a/src/main/java/org/spacehq/opennbt/tag/ByteArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/ByteArrayTag.java @@ -1,4 +1,4 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/ByteTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/ByteTag.java similarity index 96% rename from src/main/java/org/spacehq/opennbt/tag/ByteTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/ByteTag.java index c28ad1f..e0e9dfc 100644 --- a/src/main/java/org/spacehq/opennbt/tag/ByteTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/ByteTag.java @@ -1,4 +1,4 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/CompoundTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/CompoundTag.java similarity index 83% rename from src/main/java/org/spacehq/opennbt/tag/CompoundTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/CompoundTag.java index f1feafc..980cd58 100644 --- a/src/main/java/org/spacehq/opennbt/tag/CompoundTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/CompoundTag.java @@ -1,9 +1,10 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; import org.spacehq.opennbt.NBTIO; import java.io.DataInputStream; import java.io.DataOutputStream; +import java.io.EOFException; import java.io.IOException; import java.util.*; import java.util.Map.Entry; @@ -132,29 +133,6 @@ public class CompoundTag extends Tag implements Iterable { this.value.clear(); } - /** - * Converts this CompoundTag to a Map with non-tag values. - * @return A Map with non-tag values. - */ - public Map toMap() { - Map ret = new HashMap(); - for(String name : this.value.keySet()) { - Tag tag = this.value.get(name); - Object o = null; - if(tag instanceof CompoundTag) { - o = ((CompoundTag) tag).toMap(); - } else if(tag instanceof ListTag) { - o = ((ListTag) tag).toList(); - } else { - o = tag.getValue(); - } - - ret.put(name, o); - } - - return ret; - } - @Override public Iterator iterator() { return this.values().iterator(); @@ -162,7 +140,16 @@ public class CompoundTag extends Tag implements Iterable { @Override public void read(DataInputStream in) throws IOException { - List tags = NBTIO.readUntilEndTag(in); + List tags = new ArrayList(); + try { + Tag tag; + while((tag = NBTIO.readTag(in)) != null) { + tags.add(tag); + } + } catch(EOFException e) { + throw new IOException("Closing EndTag was not found!"); + } + for(Tag tag : tags) { this.put(tag); } @@ -170,7 +157,10 @@ public class CompoundTag extends Tag implements Iterable { @Override public void write(DataOutputStream out) throws IOException { - NBTIO.writeTags(out, this.value.values()); + for(Tag tag : this.value.values()) { + NBTIO.writeTag(out, tag); + } + out.writeByte(0); } diff --git a/src/main/java/org/spacehq/opennbt/tag/DoubleTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/DoubleTag.java similarity index 96% rename from src/main/java/org/spacehq/opennbt/tag/DoubleTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/DoubleTag.java index 24164c2..2b8a589 100644 --- a/src/main/java/org/spacehq/opennbt/tag/DoubleTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/DoubleTag.java @@ -1,4 +1,4 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/FloatTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/FloatTag.java similarity index 96% rename from src/main/java/org/spacehq/opennbt/tag/FloatTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/FloatTag.java index aa3bfa6..bd4aa80 100644 --- a/src/main/java/org/spacehq/opennbt/tag/FloatTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/FloatTag.java @@ -1,4 +1,4 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/IntArrayTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/IntArrayTag.java similarity index 97% rename from src/main/java/org/spacehq/opennbt/tag/IntArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/IntArrayTag.java index 5e26f37..8958366 100644 --- a/src/main/java/org/spacehq/opennbt/tag/IntArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/IntArrayTag.java @@ -1,4 +1,4 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/IntTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/IntTag.java similarity index 96% rename from src/main/java/org/spacehq/opennbt/tag/IntTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/IntTag.java index f818448..b16ea61 100644 --- a/src/main/java/org/spacehq/opennbt/tag/IntTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/IntTag.java @@ -1,4 +1,4 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/ListTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/ListTag.java similarity index 54% rename from src/main/java/org/spacehq/opennbt/tag/ListTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/ListTag.java index 82bd280..2613879 100644 --- a/src/main/java/org/spacehq/opennbt/tag/ListTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/ListTag.java @@ -1,20 +1,22 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; -import org.spacehq.opennbt.TagCreateException; -import org.spacehq.opennbt.TagRegistry; +import org.spacehq.opennbt.tag.TagCreateException; +import org.spacehq.opennbt.tag.TagRegistry; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; /** * A tag containing a list of tags. */ -public class ListTag extends Tag implements Iterable { +public class ListTag extends Tag implements Iterable { - private Class type; - private List value; + private Class type; + private List value; /** * Creates a tag with the specified name. @@ -31,26 +33,41 @@ public class ListTag extends Tag implements Iterable { * @param name The name of the tag. * @param type Tag type of the list. */ - public ListTag(String name, Class type) { - this(name, type, new ArrayList()); + public ListTag(String name, Class type) { + super(name); + this.type = type; + this.value = new ArrayList(); } /** * Creates a tag with the specified name. * * @param name The name of the tag. - * @param type Tag type of the list. * @param value The value of the tag. + * @throws IllegalArgumentException If all tags in the list are not of the same type. */ - public ListTag(String name, Class type, List value) { + public ListTag(String name, List value) throws IllegalArgumentException { super(name); + Class type = null; + for(Tag tag : value) { + if(tag == null) { + throw new IllegalArgumentException("List cannot contain null tags."); + } + + if(type == null) { + type = tag.getClass(); + } else if(tag.getClass() != type) { + throw new IllegalArgumentException("All tags must be of the same type."); + } + } + this.type = type; - this.value = new ArrayList(value); + this.value = new ArrayList(value); } @Override - public List getValue() { - return new ArrayList(this.value); + public List getValue() { + return new ArrayList(this.value); } /** @@ -58,8 +75,14 @@ public class ListTag extends Tag implements Iterable { * * @param value New value of this tag. */ - public void setValue(List value) { - this.value = new ArrayList(value); + public void setValue(List value) { + for(Tag tag : value) { + if(tag.getClass() != this.type) { + throw new IllegalArgumentException("Tag type cannot differ from ListTag type."); + } + } + + this.value = new ArrayList(value); } /** @@ -67,7 +90,7 @@ public class ListTag extends Tag implements Iterable { * * @return The ListTag's element type. */ - public Class getElementType() { + public Class getElementType() { return this.type; } @@ -77,7 +100,11 @@ public class ListTag extends Tag implements Iterable { * @param tag Tag to add. * @return If the list was changed as a result. */ - public boolean add(T tag) { + public boolean add(Tag tag) { + if(tag.getClass() != this.type) { + throw new IllegalArgumentException("Tag type cannot differ from ListTag type."); + } + return this.value.add(tag); } @@ -87,7 +114,7 @@ public class ListTag extends Tag implements Iterable { * @param tag Tag to remove. * @return If the list contained the tag. */ - public boolean remove(T tag) { + public boolean remove(Tag tag) { return this.value.remove(tag); } @@ -97,8 +124,8 @@ public class ListTag extends Tag implements Iterable { * @param index Index of the tag. * @return The tag at the given index. */ - public T get(int index) { - return this.value.get(index); + public T get(int index) { + return (T) this.value.get(index); } /** @@ -110,39 +137,16 @@ public class ListTag extends Tag implements Iterable { return this.value.size(); } - /** - * Converts this CompoundTag to a List with non-tag values. - * @return A List with non-tag values. - */ - public List toList() { - List ret = new ArrayList(); - for(Tag tag : this.value) { - Object o = null; - if(tag instanceof CompoundTag) { - o = ((CompoundTag) tag).toMap(); - } else if(tag instanceof ListTag) { - o = ((ListTag) tag).toList(); - } else { - o = tag.getValue(); - } - - ret.add(o); - } - - return ret; - } - @Override - public Iterator iterator() { + public Iterator iterator() { return this.value.iterator(); } - @SuppressWarnings("unchecked") @Override public void read(DataInputStream in) throws IOException { int id = in.readUnsignedByte(); - this.type = (Class) TagRegistry.getClassFor(id); - this.value = new ArrayList(); + this.type = TagRegistry.getClassFor(id); + this.value = new ArrayList(); if(this.type == null) { throw new IOException("Unknown tag ID in ListTag: " + id); } @@ -157,7 +161,7 @@ public class ListTag extends Tag implements Iterable { } tag.read(in); - this.add((T) tag); + this.add(tag); } } @@ -175,15 +179,14 @@ public class ListTag extends Tag implements Iterable { } } - @SuppressWarnings("unchecked") @Override - public ListTag clone() { - List newList = new ArrayList(); - for(T value : this.value) { - newList.add((T) value.clone()); + public ListTag clone() { + List newList = new ArrayList(); + for(Tag value : this.value) { + newList.add(value.clone()); } - return new ListTag(this.getName(), this.type, newList); + return new ListTag(this.getName(), newList); } } diff --git a/src/main/java/org/spacehq/opennbt/tag/LongTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/LongTag.java similarity index 96% rename from src/main/java/org/spacehq/opennbt/tag/LongTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/LongTag.java index e04e167..fd920e4 100644 --- a/src/main/java/org/spacehq/opennbt/tag/LongTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/LongTag.java @@ -1,4 +1,4 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/ShortTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/ShortTag.java similarity index 96% rename from src/main/java/org/spacehq/opennbt/tag/ShortTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/ShortTag.java index 0abb01d..ebbeac8 100644 --- a/src/main/java/org/spacehq/opennbt/tag/ShortTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/ShortTag.java @@ -1,4 +1,4 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/StringTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/StringTag.java similarity index 96% rename from src/main/java/org/spacehq/opennbt/tag/StringTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/StringTag.java index 63ee2cc..3f5b8bf 100644 --- a/src/main/java/org/spacehq/opennbt/tag/StringTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/StringTag.java @@ -1,4 +1,4 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; import org.spacehq.opennbt.NBTIO; diff --git a/src/main/java/org/spacehq/opennbt/tag/Tag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/Tag.java similarity index 98% rename from src/main/java/org/spacehq/opennbt/tag/Tag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/Tag.java index 320430c..e7b8867 100644 --- a/src/main/java/org/spacehq/opennbt/tag/Tag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/Tag.java @@ -1,4 +1,4 @@ -package org.spacehq.opennbt.tag; +package org.spacehq.opennbt.tag.builtin; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/custom/DoubleArrayTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/DoubleArrayTag.java similarity index 95% rename from src/main/java/org/spacehq/opennbt/tag/custom/DoubleArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/custom/DoubleArrayTag.java index 3a83226..23d1c2b 100644 --- a/src/main/java/org/spacehq/opennbt/tag/custom/DoubleArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/DoubleArrayTag.java @@ -1,6 +1,6 @@ -package org.spacehq.opennbt.tag.custom; +package org.spacehq.opennbt.tag.builtin.custom; -import org.spacehq.opennbt.tag.Tag; +import org.spacehq.opennbt.tag.builtin.Tag; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/custom/FloatArrayTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/FloatArrayTag.java similarity index 95% rename from src/main/java/org/spacehq/opennbt/tag/custom/FloatArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/custom/FloatArrayTag.java index 23c5283..163fef0 100644 --- a/src/main/java/org/spacehq/opennbt/tag/custom/FloatArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/FloatArrayTag.java @@ -1,6 +1,6 @@ -package org.spacehq.opennbt.tag.custom; +package org.spacehq.opennbt.tag.builtin.custom; -import org.spacehq.opennbt.tag.Tag; +import org.spacehq.opennbt.tag.builtin.Tag; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/custom/LongArrayTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/LongArrayTag.java similarity index 95% rename from src/main/java/org/spacehq/opennbt/tag/custom/LongArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/custom/LongArrayTag.java index 642c43e..f6211c7 100644 --- a/src/main/java/org/spacehq/opennbt/tag/custom/LongArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/LongArrayTag.java @@ -1,6 +1,6 @@ -package org.spacehq.opennbt.tag.custom; +package org.spacehq.opennbt.tag.builtin.custom; -import org.spacehq.opennbt.tag.Tag; +import org.spacehq.opennbt.tag.builtin.Tag; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/custom/SerializableArrayTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/SerializableArrayTag.java similarity index 96% rename from src/main/java/org/spacehq/opennbt/tag/custom/SerializableArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/custom/SerializableArrayTag.java index 046be57..eff6e89 100644 --- a/src/main/java/org/spacehq/opennbt/tag/custom/SerializableArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/SerializableArrayTag.java @@ -1,6 +1,6 @@ -package org.spacehq.opennbt.tag.custom; +package org.spacehq.opennbt.tag.builtin.custom; -import org.spacehq.opennbt.tag.Tag; +import org.spacehq.opennbt.tag.builtin.Tag; import java.io.*; diff --git a/src/main/java/org/spacehq/opennbt/tag/custom/SerializableTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/SerializableTag.java similarity index 93% rename from src/main/java/org/spacehq/opennbt/tag/custom/SerializableTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/custom/SerializableTag.java index 0783e46..acc516f 100644 --- a/src/main/java/org/spacehq/opennbt/tag/custom/SerializableTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/SerializableTag.java @@ -1,6 +1,6 @@ -package org.spacehq.opennbt.tag.custom; +package org.spacehq.opennbt.tag.builtin.custom; -import org.spacehq.opennbt.tag.Tag; +import org.spacehq.opennbt.tag.builtin.Tag; import java.io.*; diff --git a/src/main/java/org/spacehq/opennbt/tag/custom/ShortArrayTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/ShortArrayTag.java similarity index 95% rename from src/main/java/org/spacehq/opennbt/tag/custom/ShortArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/custom/ShortArrayTag.java index 4e8409d..c4201db 100644 --- a/src/main/java/org/spacehq/opennbt/tag/custom/ShortArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/ShortArrayTag.java @@ -1,6 +1,6 @@ -package org.spacehq.opennbt.tag.custom; +package org.spacehq.opennbt.tag.builtin.custom; -import org.spacehq.opennbt.tag.Tag; +import org.spacehq.opennbt.tag.builtin.Tag; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/org/spacehq/opennbt/tag/custom/StringArrayTag.java b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/StringArrayTag.java similarity index 95% rename from src/main/java/org/spacehq/opennbt/tag/custom/StringArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/builtin/custom/StringArrayTag.java index 0b9d1f4..c543fb5 100644 --- a/src/main/java/org/spacehq/opennbt/tag/custom/StringArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/builtin/custom/StringArrayTag.java @@ -1,7 +1,7 @@ -package org.spacehq.opennbt.tag.custom; +package org.spacehq.opennbt.tag.builtin.custom; import org.spacehq.opennbt.NBTIO; -import org.spacehq.opennbt.tag.Tag; +import org.spacehq.opennbt.tag.builtin.Tag; import java.io.DataInputStream; import java.io.DataOutputStream;