Format code.

This commit is contained in:
Steveice10 2017-09-01 12:52:20 -07:00
parent e1e28c8809
commit 1164dab716
45 changed files with 2250 additions and 2213 deletions

View File

@ -1,11 +1,23 @@
package com.github.steveice10.opennbt;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.TagCreateException;
import com.github.steveice10.opennbt.tag.TagRegistry;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import java.io.*;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@ -13,125 +25,125 @@ import java.util.zip.GZIPOutputStream;
* A class containing methods for reading/writing NBT tags.
*/
public class NBTIO {
/**
* Reads the compressed, big endian 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 compressed, big endian 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 compressed, big endian 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, false);
}
/**
* Reads the compressed, big endian 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, false);
}
/**
* Reads the root CompoundTag from the given file.
*
* @param path Path of the file.
* @param compressed Whether the NBT file is compressed.
/**
* Reads the root CompoundTag from the given file.
*
* @param path Path of the file.
* @param compressed Whether the NBT file is compressed.
* @param littleEndian Whether the NBT file is little endian.
* @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static CompoundTag readFile(String path, boolean compressed, boolean littleEndian) throws IOException {
return readFile(new File(path), compressed, littleEndian);
}
* @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static CompoundTag readFile(String path, boolean compressed, boolean littleEndian) throws IOException {
return readFile(new File(path), compressed, littleEndian);
}
/**
* Reads the root CompoundTag from the given file.
*
* @param file File to read from.
* @param compressed Whether the NBT file is compressed.
/**
* Reads the root CompoundTag from the given file.
*
* @param file File to read from.
* @param compressed Whether the NBT file is compressed.
* @param littleEndian Whether the NBT file is little endian.
* @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static CompoundTag readFile(File file, boolean compressed, boolean littleEndian) throws IOException {
InputStream in = new FileInputStream(file);
if(compressed) {
in = new GZIPInputStream(in);
}
* @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static CompoundTag readFile(File file, boolean compressed, boolean littleEndian) throws IOException {
InputStream in = new FileInputStream(file);
if(compressed) {
in = new GZIPInputStream(in);
}
Tag tag = readTag(in, littleEndian);
if(!(tag instanceof CompoundTag)) {
throw new IOException("Root tag is not a CompoundTag!");
}
Tag tag = readTag(in, littleEndian);
if(!(tag instanceof CompoundTag)) {
throw new IOException("Root tag is not a CompoundTag!");
}
return (CompoundTag) tag;
}
return (CompoundTag) tag;
}
/**
* Writes the given root CompoundTag to the given file, compressed and in big endian.
*
* @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, compressed and in big endian.
*
* @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, compressed and in big endian.
*
* @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, false);
}
/**
* Writes the given root CompoundTag to the given file, compressed and in big endian.
*
* @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, false);
}
/**
* 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.
/**
* 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.
* @param littleEndian Whether to write little endian NBT.
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeFile(CompoundTag tag, String path, boolean compressed, boolean littleEndian) throws IOException {
writeFile(tag, new File(path), compressed, littleEndian);
}
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeFile(CompoundTag tag, String path, boolean compressed, boolean littleEndian) throws IOException {
writeFile(tag, new File(path), compressed, littleEndian);
}
/**
* 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.
/**
* 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.
* @param littleEndian Whether to write little endian NBT.
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeFile(CompoundTag tag, File file, boolean compressed, boolean littleEndian) throws IOException {
if(!file.exists()) {
if(file.getParentFile() != null && !file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeFile(CompoundTag tag, File file, boolean compressed, boolean littleEndian) throws IOException {
if(!file.exists()) {
if(file.getParentFile() != null && !file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
file.createNewFile();
}
OutputStream out = new FileOutputStream(file);
if(compressed) {
out = new GZIPOutputStream(out);
}
OutputStream out = new FileOutputStream(file);
if(compressed) {
out = new GZIPOutputStream(out);
}
writeTag(out, tag, littleEndian);
out.close();
}
writeTag(out, tag, littleEndian);
out.close();
}
/**
* Reads a big endian NBT tag.
@ -144,17 +156,17 @@ public class NBTIO {
return readTag(in, false);
}
/**
* Reads an NBT tag.
*
* @param in Input stream to read from.
/**
* Reads an NBT tag.
*
* @param in Input stream to read from.
* @param littleEndian Whether to read little endian NBT.
* @return The read tag, or null if the tag is an end tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static Tag readTag(InputStream in, boolean littleEndian) throws IOException {
return readTag((DataInput) (littleEndian ? new LittleEndianDataInputStream(in) : new DataInputStream(in)));
}
* @return The read tag, or null if the tag is an end tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static Tag readTag(InputStream in, boolean littleEndian) throws IOException {
return readTag((DataInput) (littleEndian ? new LittleEndianDataInputStream(in) : new DataInputStream(in)));
}
/**
* Reads an NBT tag.
@ -201,9 +213,9 @@ public class NBTIO {
* @param littleEndian Whether to write little endian NBT.
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeTag(OutputStream out, Tag tag, boolean littleEndian) throws IOException {
public static void writeTag(OutputStream out, Tag tag, boolean littleEndian) throws IOException {
writeTag((DataOutput) (littleEndian ? new LittleEndianDataOutputStream(out) : new DataOutputStream(out)), tag);
}
}
/**
* Writes an NBT tag.
@ -218,222 +230,222 @@ public class NBTIO {
tag.write(out);
}
private static class LittleEndianDataInputStream extends FilterInputStream implements DataInput {
public LittleEndianDataInputStream(InputStream in) {
super(in);
}
@Override
public int read(byte[] b) throws IOException {
return this.in.read(b, 0, b.length);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return this.in.read(b, off, len);
}
@Override
public void readFully(byte[] b) throws IOException {
this.readFully(b, 0, b.length);
}
@Override
public void readFully(byte[] b, int off, int len) throws IOException {
if(len < 0) {
throw new IndexOutOfBoundsException();
} else {
int read;
for(int pos = 0; pos < len; pos += read) {
read = this.in.read(b, off + pos, len - pos);
if(read < 0) {
throw new EOFException();
}
}
}
}
@Override
public int skipBytes(int n) throws IOException {
int total = 0;
int skipped = 0;
while(total < n && (skipped = (int) this.in.skip(n - total)) > 0) {
total += skipped;
}
return total;
}
@Override
public boolean readBoolean() throws IOException {
int val = this.in.read();
if(val < 0) {
throw new EOFException();
}
return val != 0;
}
@Override
public byte readByte() throws IOException {
int val = this.in.read();
if(val < 0) {
throw new EOFException();
}
return (byte) val;
}
@Override
public int readUnsignedByte() throws IOException {
int val = this.in.read();
if(val < 0) {
throw new EOFException();
}
return val;
}
@Override
public short readShort() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
if((b1 | b2) < 0) {
throw new EOFException();
}
return (short) (b1 | (b2 << 8));
}
@Override
public int readUnsignedShort() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
if((b1 | b2) < 0) {
throw new EOFException();
}
return b1 | (b2 << 8);
}
@Override
public char readChar() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
if((b1 | b2) < 0) {
throw new EOFException();
}
return (char) (b1 | (b2 << 8));
}
@Override
public int readInt() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
int b3 = this.in.read();
int b4 = this.in.read();
if((b1 | b2 | b3 | b4) < 0) {
throw new EOFException();
}
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
}
@Override
public long readLong() throws IOException {
long b1 = this.in.read();
long b2 = this.in.read();
long b3 = this.in.read();
long b4 = this.in.read();
long b5 = this.in.read();
long b6 = this.in.read();
long b7 = this.in.read();
long b8 = this.in.read();
if((b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8) < 0) {
throw new EOFException();
}
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24) | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
}
@Override
public float readFloat() throws IOException {
return Float.intBitsToFloat(this.readInt());
}
@Override
public double readDouble() throws IOException {
return Double.longBitsToDouble(this.readLong());
}
@Override
public String readLine() throws IOException {
throw new UnsupportedOperationException("Use readUTF.");
}
@Override
public String readUTF() throws IOException {
byte[] bytes = new byte[this.readUnsignedShort()];
this.readFully(bytes);
return new String(bytes, "UTF-8");
}
}
private static class LittleEndianDataOutputStream extends FilterOutputStream implements DataOutput {
public LittleEndianDataOutputStream(OutputStream out) {
super(out);
}
@Override
public synchronized void write(int b) throws IOException {
this.out.write(b);
}
private static class LittleEndianDataInputStream extends FilterInputStream implements DataInput {
public LittleEndianDataInputStream(InputStream in) {
super(in);
}
@Override
public synchronized void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
}
public int read(byte[] b) throws IOException {
return this.in.read(b, 0, b.length);
}
@Override
public void flush() throws IOException {
this.out.flush();
}
public int read(byte[] b, int off, int len) throws IOException {
return this.in.read(b, off, len);
}
@Override
public void writeBoolean(boolean b) throws IOException {
this.out.write(b ? 1 : 0);
}
public void readFully(byte[] b) throws IOException {
this.readFully(b, 0, b.length);
}
@Override
public void writeByte(int b) throws IOException {
this.out.write(b);
}
public void readFully(byte[] b, int off, int len) throws IOException {
if(len < 0) {
throw new IndexOutOfBoundsException();
} else {
int read;
for(int pos = 0; pos < len; pos += read) {
read = this.in.read(b, off + pos, len - pos);
if(read < 0) {
throw new EOFException();
}
}
}
}
@Override
public void writeShort(int s) throws IOException {
this.out.write(s & 0xFF);
this.out.write((s >>> 8) & 0xFF);
}
public int skipBytes(int n) throws IOException {
int total = 0;
int skipped = 0;
while(total < n && (skipped = (int) this.in.skip(n - total)) > 0) {
total += skipped;
}
return total;
}
@Override
public void writeChar(int c) throws IOException {
this.out.write(c & 0xFF);
this.out.write((c >>> 8) & 0xFF);
}
public boolean readBoolean() throws IOException {
int val = this.in.read();
if(val < 0) {
throw new EOFException();
}
return val != 0;
}
@Override
public void writeInt(int i) throws IOException {
this.out.write(i & 0xFF);
this.out.write((i >>> 8) & 0xFF);
this.out.write((i >>> 16) & 0xFF);
this.out.write((i >>> 24) & 0xFF);
}
public byte readByte() throws IOException {
int val = this.in.read();
if(val < 0) {
throw new EOFException();
}
return (byte) val;
}
@Override
public void writeLong(long l) throws IOException {
public int readUnsignedByte() throws IOException {
int val = this.in.read();
if(val < 0) {
throw new EOFException();
}
return val;
}
@Override
public short readShort() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
if((b1 | b2) < 0) {
throw new EOFException();
}
return (short) (b1 | (b2 << 8));
}
@Override
public int readUnsignedShort() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
if((b1 | b2) < 0) {
throw new EOFException();
}
return b1 | (b2 << 8);
}
@Override
public char readChar() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
if((b1 | b2) < 0) {
throw new EOFException();
}
return (char) (b1 | (b2 << 8));
}
@Override
public int readInt() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
int b3 = this.in.read();
int b4 = this.in.read();
if((b1 | b2 | b3 | b4) < 0) {
throw new EOFException();
}
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
}
@Override
public long readLong() throws IOException {
long b1 = this.in.read();
long b2 = this.in.read();
long b3 = this.in.read();
long b4 = this.in.read();
long b5 = this.in.read();
long b6 = this.in.read();
long b7 = this.in.read();
long b8 = this.in.read();
if((b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8) < 0) {
throw new EOFException();
}
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24) | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
}
@Override
public float readFloat() throws IOException {
return Float.intBitsToFloat(this.readInt());
}
@Override
public double readDouble() throws IOException {
return Double.longBitsToDouble(this.readLong());
}
@Override
public String readLine() throws IOException {
throw new UnsupportedOperationException("Use readUTF.");
}
@Override
public String readUTF() throws IOException {
byte[] bytes = new byte[this.readUnsignedShort()];
this.readFully(bytes);
return new String(bytes, "UTF-8");
}
}
private static class LittleEndianDataOutputStream extends FilterOutputStream implements DataOutput {
public LittleEndianDataOutputStream(OutputStream out) {
super(out);
}
@Override
public synchronized void write(int b) throws IOException {
this.out.write(b);
}
@Override
public synchronized void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
}
@Override
public void flush() throws IOException {
this.out.flush();
}
@Override
public void writeBoolean(boolean b) throws IOException {
this.out.write(b ? 1 : 0);
}
@Override
public void writeByte(int b) throws IOException {
this.out.write(b);
}
@Override
public void writeShort(int s) throws IOException {
this.out.write(s & 0xFF);
this.out.write((s >>> 8) & 0xFF);
}
@Override
public void writeChar(int c) throws IOException {
this.out.write(c & 0xFF);
this.out.write((c >>> 8) & 0xFF);
}
@Override
public void writeInt(int i) throws IOException {
this.out.write(i & 0xFF);
this.out.write((i >>> 8) & 0xFF);
this.out.write((i >>> 16) & 0xFF);
this.out.write((i >>> 24) & 0xFF);
}
@Override
public void writeLong(long l) throws IOException {
this.out.write((int) (l & 0xFF));
this.out.write((int) ((l >>> 8) & 0xFF));
this.out.write((int) ((l >>> 16) & 0xFF));
@ -442,42 +454,42 @@ public class NBTIO {
this.out.write((int) ((l >>> 40) & 0xFF));
this.out.write((int) ((l >>> 48) & 0xFF));
this.out.write((int) ((l >>> 56) & 0xFF));
}
}
@Override
public void writeFloat(float f) throws IOException {
this.writeInt(Float.floatToIntBits(f));
}
public void writeFloat(float f) throws IOException {
this.writeInt(Float.floatToIntBits(f));
}
@Override
public void writeDouble(double d) throws IOException {
this.writeLong(Double.doubleToLongBits(d));
}
public void writeDouble(double d) throws IOException {
this.writeLong(Double.doubleToLongBits(d));
}
@Override
public void writeBytes(String s) throws IOException {
int len = s.length();
for(int index = 0; index < len; index++) {
this.out.write((byte) s.charAt(index));
}
}
public void writeBytes(String s) throws IOException {
int len = s.length();
for(int index = 0; index < len; index++) {
this.out.write((byte) s.charAt(index));
}
}
@Override
public void writeChars(String s) throws IOException {
int len = s.length();
for(int index = 0; index < len; index++) {
char c = s.charAt(index);
this.out.write(c & 0xFF);
this.out.write((c >>> 8) & 0xFF);
}
}
public void writeChars(String s) throws IOException {
int len = s.length();
for(int index = 0; index < len; index++) {
char c = s.charAt(index);
this.out.write(c & 0xFF);
this.out.write((c >>> 8) & 0xFF);
}
}
@Override
public void writeUTF(String s) throws IOException {
public void writeUTF(String s) throws IOException {
byte[] bytes = s.getBytes("UTF-8");
this.writeShort(bytes.length);
this.write(bytes);
}
}
}
}
}

View File

@ -4,21 +4,21 @@ package com.github.steveice10.opennbt.conversion;
* An exception thrown when an error occurs while converting something.
*/
public class ConversionException extends RuntimeException {
private static final long serialVersionUID = -2022049594558041160L;
private static final long serialVersionUID = -2022049594558041160L;
public ConversionException() {
super();
}
public ConversionException() {
super();
}
public ConversionException(String message) {
super(message);
}
public ConversionException(String message) {
super(message);
}
public ConversionException(Throwable cause) {
super(cause);
}
public ConversionException(Throwable cause) {
super(cause);
}
public ConversionException(String message, Throwable cause) {
super(message, cause);
}
public ConversionException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -4,21 +4,21 @@ package com.github.steveice10.opennbt.conversion;
* An exception thrown when an error occurs while registering a converter.
*/
public class ConverterRegisterException extends RuntimeException {
private static final long serialVersionUID = -2022049594558041160L;
private static final long serialVersionUID = -2022049594558041160L;
public ConverterRegisterException() {
super();
}
public ConverterRegisterException() {
super();
}
public ConverterRegisterException(String message) {
super(message);
}
public ConverterRegisterException(String message) {
super(message);
}
public ConverterRegisterException(Throwable cause) {
super(cause);
}
public ConverterRegisterException(Throwable cause) {
super(cause);
}
public ConverterRegisterException(String message, Throwable cause) {
super(message, cause);
}
public ConverterRegisterException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -18,6 +18,7 @@ import com.github.steveice10.opennbt.conversion.builtin.custom.SerializableArray
import com.github.steveice10.opennbt.conversion.builtin.custom.SerializableTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.ShortArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.StringArrayTagConverter;
import com.github.steveice10.opennbt.tag.TagRegisterException;
import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
@ -37,145 +38,149 @@ import com.github.steveice10.opennbt.tag.builtin.custom.SerializableArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.SerializableTag;
import com.github.steveice10.opennbt.tag.builtin.custom.ShortArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.StringArrayTag;
import com.github.steveice10.opennbt.tag.TagRegisterException;
import java.io.Serializable;
import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* A registry mapping tags and value types to converters.
*/
public class ConverterRegistry {
private static final Map<Class<? extends Tag>, TagConverter<? extends Tag, ?>> tagToConverter = new HashMap<Class<? extends Tag>, TagConverter<? extends Tag, ?>>();
private static final Map<Class<?>, TagConverter<? extends Tag, ?>> typeToConverter = new HashMap<Class<?>, TagConverter<? extends Tag, ?>>();
private static final Map<Class<? extends Tag>, TagConverter<? extends Tag, ?>> tagToConverter = new HashMap<Class<? extends Tag>, TagConverter<? extends Tag, ?>>();
private static final Map<Class<?>, TagConverter<? extends Tag, ?>> typeToConverter = new HashMap<Class<?>, TagConverter<? extends Tag, ?>>();
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());
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());
}
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 <T> Tag type to convert from.
* @param <V> Value type to convert to.
* @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 <T extends Tag, V> void register(Class<T> tag, Class<V> type, TagConverter<T, V> converter) throws ConverterRegisterException {
if(tagToConverter.containsKey(tag)) {
throw new TagRegisterException("Type conversion to tag " + tag.getName() + " is already registered.");
}
/**
* Registers a converter.
*
* @param <T> Tag type to convert from.
* @param <V> Value type to convert to.
* @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 <T extends Tag, V> void register(Class<T> tag, Class<V> type, TagConverter<T, V> 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.");
}
if(typeToConverter.containsKey(type)) {
throw new TagRegisterException("Tag conversion to type " + type.getName() + " is already registered.");
}
tagToConverter.put(tag, converter);
typeToConverter.put(type, converter);
}
tagToConverter.put(tag, converter);
typeToConverter.put(type, converter);
}
/**
* Converts the given tag to a value.
*
* @param <T> Tag type to convert from.
* @param <V> Value type to convert to.
* @param tag Tag to convert.
* @return The converted value.
* @throws ConversionException If a suitable converter could not be found.
*/
public static <T extends Tag, V> V convertToValue(T tag) throws ConversionException {
if(tag == null || tag.getValue() == null) {
return null;
}
/**
* Converts the given tag to a value.
*
* @param <T> Tag type to convert from.
* @param <V> Value type to convert to.
* @param tag Tag to convert.
* @return The converted value.
* @throws ConversionException If a suitable converter could not be found.
*/
public static <T extends Tag, V> 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.");
}
if(!tagToConverter.containsKey(tag.getClass())) {
throw new ConversionException("Tag type " + tag.getClass().getName() + " has no converter.");
}
TagConverter<T, ?> converter = (TagConverter<T, ?>) tagToConverter.get(tag.getClass());
return (V) converter.convert(tag);
}
TagConverter<T, ?> converter = (TagConverter<T, ?>) tagToConverter.get(tag.getClass());
return (V) converter.convert(tag);
}
/**
* Converts the given value to a tag.
*
* @param <V> Value type to convert from.
* @param <T> Tag type to convert to.
* @param name Name of the resulting tag.
* @param value Value to convert.
* @return The converted tag.
* @throws ConversionException If a suitable converter could not be found.
*/
public static <V, T extends Tag> T convertToTag(String name, V value) throws ConversionException {
if(value == null) {
return null;
}
/**
* Converts the given value to a tag.
*
* @param <V> Value type to convert from.
* @param <T> Tag type to convert to.
* @param name Name of the resulting tag.
* @param value Value to convert.
* @return The converted tag.
* @throws ConversionException If a suitable converter could not be found.
*/
public static <V, T extends Tag> T convertToTag(String name, V value) throws ConversionException {
if(value == null) {
return null;
}
TagConverter<T, V> converter = (TagConverter<T, V>) typeToConverter.get(value.getClass());
if(converter == null) {
for(Class<?> clazz : getAllClasses(value.getClass())) {
if(typeToConverter.containsKey(clazz)) {
try {
converter = (TagConverter<T, V>) typeToConverter.get(clazz);
break;
} catch(ClassCastException e) {
}
}
}
}
TagConverter<T, V> converter = (TagConverter<T, V>) typeToConverter.get(value.getClass());
if(converter == null) {
for(Class<?> clazz : getAllClasses(value.getClass())) {
if(typeToConverter.containsKey(clazz)) {
try {
converter = (TagConverter<T, V>) typeToConverter.get(clazz);
break;
} catch(ClassCastException e) {
}
}
}
}
if(converter == null) {
throw new ConversionException("Value type " + value.getClass().getName() + " has no converter.");
}
if(converter == null) {
throw new ConversionException("Value type " + value.getClass().getName() + " has no converter.");
}
return converter.convert(name, value);
}
return converter.convert(name, value);
}
private static Set<Class<?>> getAllClasses(Class<?> clazz) {
Set<Class<?>> ret = new LinkedHashSet<Class<?>>();
Class<?> c = clazz;
while(c != null) {
ret.add(c);
ret.addAll(getAllSuperInterfaces(c));
c = c.getSuperclass();
}
private static Set<Class<?>> getAllClasses(Class<?> clazz) {
Set<Class<?>> ret = new LinkedHashSet<Class<?>>();
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);
}
// 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;
}
return ret;
}
private static Set<Class<?>> getAllSuperInterfaces(Class<?> clazz) {
Set<Class<?>> ret = new HashSet<Class<?>>();
for(Class<?> c : clazz.getInterfaces()) {
ret.add(c);
ret.addAll(getAllSuperInterfaces(c));
}
private static Set<Class<?>> getAllSuperInterfaces(Class<?> clazz) {
Set<Class<?>> ret = new HashSet<Class<?>>();
for(Class<?> c : clazz.getInterfaces()) {
ret.add(c);
ret.addAll(getAllSuperInterfaces(c));
}
return ret;
}
return ret;
}
}

View File

@ -9,20 +9,20 @@ import com.github.steveice10.opennbt.tag.builtin.Tag;
* @param <V> Value type.
*/
public interface TagConverter<T extends Tag, V> {
/**
* Converts a tag to a value.
*
* @param tag Tag to convert.
* @return The converted value.
*/
public V convert(T tag);
/**
* 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 name Name of the tag.
* @param value Value to convert.
* @return The converted tag.
*/
public T convert(String name, V value);
/**
* Converts a value to a tag.
*
* @param name Name of the tag.
* @param value Value to convert.
* @return The converted tag.
*/
public T convert(String name, V value);
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag;
* A converter that converts between ByteArrayTag and byte[].
*/
public class ByteArrayTagConverter implements TagConverter<ByteArrayTag, byte[]> {
@Override
public byte[] convert(ByteArrayTag tag) {
return tag.getValue();
}
@Override
public byte[] convert(ByteArrayTag tag) {
return tag.getValue();
}
@Override
public ByteArrayTag convert(String name, byte[] value) {
return new ByteArrayTag(name, value);
}
@Override
public ByteArrayTag convert(String name, byte[] value) {
return new ByteArrayTag(name, value);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.ByteTag;
* A converter that converts between ByteTag and byte.
*/
public class ByteTagConverter implements TagConverter<ByteTag, Byte> {
@Override
public Byte convert(ByteTag tag) {
return tag.getValue();
}
@Override
public Byte convert(ByteTag tag) {
return tag.getValue();
}
@Override
public ByteTag convert(String name, Byte value) {
return new ByteTag(name, value);
}
@Override
public ByteTag convert(String name, Byte value) {
return new ByteTag(name, value);
}
}

View File

@ -1,7 +1,7 @@
package com.github.steveice10.opennbt.conversion.builtin;
import com.github.steveice10.opennbt.conversion.TagConverter;
import com.github.steveice10.opennbt.conversion.ConverterRegistry;
import com.github.steveice10.opennbt.conversion.TagConverter;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
@ -12,26 +12,26 @@ import java.util.Map;
* A converter that converts between CompoundTag and Map.
*/
public class CompoundTagConverter implements TagConverter<CompoundTag, Map> {
@Override
public Map convert(CompoundTag tag) {
Map<String, Object> ret = new HashMap<String, Object>();
Map<String, Tag> tags = tag.getValue();
for(String name : tags.keySet()) {
Tag t = tags.get(name);
ret.put(t.getName(), ConverterRegistry.convertToValue(t));
}
@Override
public Map convert(CompoundTag tag) {
Map<String, Object> ret = new HashMap<String, Object>();
Map<String, Tag> tags = tag.getValue();
for(String name : tags.keySet()) {
Tag t = tags.get(name);
ret.put(t.getName(), ConverterRegistry.convertToValue(t));
}
return ret;
}
return ret;
}
@Override
public CompoundTag convert(String name, Map value) {
Map<String, Tag> tags = new HashMap<String, Tag>();
for(Object na : value.keySet()) {
String n = (String) na;
tags.put(n, ConverterRegistry.convertToTag(n, value.get(n)));
}
@Override
public CompoundTag convert(String name, Map value) {
Map<String, Tag> tags = new HashMap<String, Tag>();
for(Object na : value.keySet()) {
String n = (String) na;
tags.put(n, ConverterRegistry.convertToTag(n, value.get(n)));
}
return new CompoundTag(name, tags);
}
return new CompoundTag(name, tags);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.DoubleTag;
* A converter that converts between DoubleTag and double.
*/
public class DoubleTagConverter implements TagConverter<DoubleTag, Double> {
@Override
public Double convert(DoubleTag tag) {
return tag.getValue();
}
@Override
public Double convert(DoubleTag tag) {
return tag.getValue();
}
@Override
public DoubleTag convert(String name, Double value) {
return new DoubleTag(name, value);
}
@Override
public DoubleTag convert(String name, Double value) {
return new DoubleTag(name, value);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.FloatTag;
* A converter that converts between FloatTag and float.
*/
public class FloatTagConverter implements TagConverter<FloatTag, Float> {
@Override
public Float convert(FloatTag tag) {
return tag.getValue();
}
@Override
public Float convert(FloatTag tag) {
return tag.getValue();
}
@Override
public FloatTag convert(String name, Float value) {
return new FloatTag(name, value);
}
@Override
public FloatTag convert(String name, Float value) {
return new FloatTag(name, value);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
* A converter that converts between IntArrayTag and int[].
*/
public class IntArrayTagConverter implements TagConverter<IntArrayTag, int[]> {
@Override
public int[] convert(IntArrayTag tag) {
return tag.getValue();
}
@Override
public int[] convert(IntArrayTag tag) {
return tag.getValue();
}
@Override
public IntArrayTag convert(String name, int[] value) {
return new IntArrayTag(name, value);
}
@Override
public IntArrayTag convert(String name, int[] value) {
return new IntArrayTag(name, value);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.IntTag;
* A converter that converts between IntTag and int.
*/
public class IntTagConverter implements TagConverter<IntTag, Integer> {
@Override
public Integer convert(IntTag tag) {
return tag.getValue();
}
@Override
public Integer convert(IntTag tag) {
return tag.getValue();
}
@Override
public IntTag convert(String name, Integer value) {
return new IntTag(name, value);
}
@Override
public IntTag convert(String name, Integer value) {
return new IntTag(name, value);
}
}

View File

@ -1,7 +1,7 @@
package com.github.steveice10.opennbt.conversion.builtin;
import com.github.steveice10.opennbt.conversion.TagConverter;
import com.github.steveice10.opennbt.conversion.ConverterRegistry;
import com.github.steveice10.opennbt.conversion.TagConverter;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
@ -12,28 +12,28 @@ import java.util.List;
* A converter that converts between CompoundTag and Map.
*/
public class ListTagConverter implements TagConverter<ListTag, List> {
@Override
public List convert(ListTag tag) {
List<Object> ret = new ArrayList<Object>();
List<? extends Tag> tags = tag.getValue();
for(Tag t : tags) {
ret.add(ConverterRegistry.convertToValue(t));
}
@Override
public List convert(ListTag tag) {
List<Object> ret = new ArrayList<Object>();
List<? extends Tag> tags = tag.getValue();
for(Tag t : tags) {
ret.add(ConverterRegistry.convertToValue(t));
}
return ret;
}
return ret;
}
@Override
public ListTag convert(String name, List value) {
if(value.isEmpty()) {
throw new IllegalArgumentException("Cannot convert ListTag with size of 0.");
}
@Override
public ListTag convert(String name, List value) {
if(value.isEmpty()) {
throw new IllegalArgumentException("Cannot convert ListTag with size of 0.");
}
List<Tag> tags = new ArrayList<Tag>();
for(Object o : value) {
tags.add(ConverterRegistry.convertToTag("", o));
}
List<Tag> tags = new ArrayList<Tag>();
for(Object o : value) {
tags.add(ConverterRegistry.convertToTag("", o));
}
return new ListTag(name, tags);
}
return new ListTag(name, tags);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.LongTag;
* A converter that converts between LongTag and long.
*/
public class LongTagConverter implements TagConverter<LongTag, Long> {
@Override
public Long convert(LongTag tag) {
return tag.getValue();
}
@Override
public Long convert(LongTag tag) {
return tag.getValue();
}
@Override
public LongTag convert(String name, Long value) {
return new LongTag(name, value);
}
@Override
public LongTag convert(String name, Long value) {
return new LongTag(name, value);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.ShortTag;
* A converter that converts between ShortTag and short.
*/
public class ShortTagConverter implements TagConverter<ShortTag, Short> {
@Override
public Short convert(ShortTag tag) {
return tag.getValue();
}
@Override
public Short convert(ShortTag tag) {
return tag.getValue();
}
@Override
public ShortTag convert(String name, Short value) {
return new ShortTag(name, value);
}
@Override
public ShortTag convert(String name, Short value) {
return new ShortTag(name, value);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.StringTag;
* A converter that converts between StringTag and String.
*/
public class StringTagConverter implements TagConverter<StringTag, String> {
@Override
public String convert(StringTag tag) {
return tag.getValue();
}
@Override
public String convert(StringTag tag) {
return tag.getValue();
}
@Override
public StringTag convert(String name, String value) {
return new StringTag(name, value);
}
@Override
public StringTag convert(String name, String value) {
return new StringTag(name, value);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.custom.DoubleArrayTag;
* A converter that converts between DoubleArrayTag and double[].
*/
public class DoubleArrayTagConverter implements TagConverter<DoubleArrayTag, double[]> {
@Override
public double[] convert(DoubleArrayTag tag) {
return tag.getValue();
}
@Override
public double[] convert(DoubleArrayTag tag) {
return tag.getValue();
}
@Override
public DoubleArrayTag convert(String name, double[] value) {
return new DoubleArrayTag(name, value);
}
@Override
public DoubleArrayTag convert(String name, double[] value) {
return new DoubleArrayTag(name, value);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.custom.FloatArrayTag;
* A converter that converts between FloatArrayTag and float[].
*/
public class FloatArrayTagConverter implements TagConverter<FloatArrayTag, float[]> {
@Override
public float[] convert(FloatArrayTag tag) {
return tag.getValue();
}
@Override
public float[] convert(FloatArrayTag tag) {
return tag.getValue();
}
@Override
public FloatArrayTag convert(String name, float[] value) {
return new FloatArrayTag(name, value);
}
@Override
public FloatArrayTag convert(String name, float[] value) {
return new FloatArrayTag(name, value);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.custom.LongArrayTag;
* A converter that converts between LongArrayTag and long[].
*/
public class LongArrayTagConverter implements TagConverter<LongArrayTag, long[]> {
@Override
public long[] convert(LongArrayTag tag) {
return tag.getValue();
}
@Override
public long[] convert(LongArrayTag tag) {
return tag.getValue();
}
@Override
public LongArrayTag convert(String name, long[] value) {
return new LongArrayTag(name, value);
}
@Override
public LongArrayTag convert(String name, long[] value) {
return new LongArrayTag(name, value);
}
}

View File

@ -9,13 +9,13 @@ import java.io.Serializable;
* A converter that converts between SerializableArrayTag and Serializable[].
*/
public class SerializableArrayTagConverter implements TagConverter<SerializableArrayTag, Serializable[]> {
@Override
public Serializable[] convert(SerializableArrayTag tag) {
return tag.getValue();
}
@Override
public Serializable[] convert(SerializableArrayTag tag) {
return tag.getValue();
}
@Override
public SerializableArrayTag convert(String name, Serializable[] value) {
return new SerializableArrayTag(name, value);
}
@Override
public SerializableArrayTag convert(String name, Serializable[] value) {
return new SerializableArrayTag(name, value);
}
}

View File

@ -9,13 +9,13 @@ import java.io.Serializable;
* A converter that converts between SerializableTag and Serializable.
*/
public class SerializableTagConverter implements TagConverter<SerializableTag, Serializable> {
@Override
public Serializable convert(SerializableTag tag) {
return tag.getValue();
}
@Override
public Serializable convert(SerializableTag tag) {
return tag.getValue();
}
@Override
public SerializableTag convert(String name, Serializable value) {
return new SerializableTag(name, value);
}
@Override
public SerializableTag convert(String name, Serializable value) {
return new SerializableTag(name, value);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.custom.ShortArrayTag;
* A converter that converts between ShortArrayTag and short[].
*/
public class ShortArrayTagConverter implements TagConverter<ShortArrayTag, short[]> {
@Override
public short[] convert(ShortArrayTag tag) {
return tag.getValue();
}
@Override
public short[] convert(ShortArrayTag tag) {
return tag.getValue();
}
@Override
public ShortArrayTag convert(String name, short[] value) {
return new ShortArrayTag(name, value);
}
@Override
public ShortArrayTag convert(String name, short[] value) {
return new ShortArrayTag(name, value);
}
}

View File

@ -7,13 +7,13 @@ import com.github.steveice10.opennbt.tag.builtin.custom.StringArrayTag;
* A converter that converts between StringArrayTag and String[].
*/
public class StringArrayTagConverter implements TagConverter<StringArrayTag, String[]> {
@Override
public String[] convert(StringArrayTag tag) {
return tag.getValue();
}
@Override
public String[] convert(StringArrayTag tag) {
return tag.getValue();
}
@Override
public StringArrayTag convert(String name, String[] value) {
return new StringArrayTag(name, value);
}
@Override
public StringArrayTag convert(String name, String[] value) {
return new StringArrayTag(name, value);
}
}

View File

@ -4,21 +4,21 @@ package com.github.steveice10.opennbt.tag;
* An exception thrown when an error occurs while created a tag instance.
*/
public class TagCreateException extends Exception {
private static final long serialVersionUID = -2022049594558041160L;
private static final long serialVersionUID = -2022049594558041160L;
public TagCreateException() {
super();
}
public TagCreateException() {
super();
}
public TagCreateException(String message) {
super(message);
}
public TagCreateException(String message) {
super(message);
}
public TagCreateException(Throwable cause) {
super(cause);
}
public TagCreateException(Throwable cause) {
super(cause);
}
public TagCreateException(String message, Throwable cause) {
super(message, cause);
}
public TagCreateException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -4,21 +4,21 @@ package com.github.steveice10.opennbt.tag;
* An exception thrown when an error occurs while registering a tag.
*/
public class TagRegisterException extends RuntimeException {
private static final long serialVersionUID = -2022049594558041160L;
private static final long serialVersionUID = -2022049594558041160L;
public TagRegisterException() {
super();
}
public TagRegisterException() {
super();
}
public TagRegisterException(String message) {
super(message);
}
public TagRegisterException(String message) {
super(message);
}
public TagRegisterException(Throwable cause) {
super(cause);
}
public TagRegisterException(Throwable cause) {
super(cause);
}
public TagRegisterException(String message, Throwable cause) {
super(message, cause);
}
public TagRegisterException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -28,99 +28,99 @@ import java.util.Map;
* A registry containing different tag classes.
*/
public class TagRegistry {
private static final Map<Integer, Class<? extends Tag>> idToTag = new HashMap<Integer, Class<? extends Tag>>();
private static final Map<Class<? extends Tag>, Integer> tagToId = new HashMap<Class<? extends Tag>, Integer>();
private static final Map<Integer, Class<? extends Tag>> idToTag = new HashMap<Integer, Class<? extends Tag>>();
private static final Map<Class<? extends Tag>, Integer> tagToId = new HashMap<Class<? extends Tag>, Integer>();
static {
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);
static {
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);
}
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);
}
/**
* Registers a tag class.
*
* @param id ID of the tag.
* @param tag Tag class to register.
* @throws TagRegisterException If an error occurs while registering the tag.
*/
public static void register(int id, Class<? extends Tag> tag) throws TagRegisterException {
if(idToTag.containsKey(id)) {
throw new TagRegisterException("Tag ID \"" + id + "\" is already in use.");
}
/**
* Registers a tag class.
*
* @param id ID of the tag.
* @param tag Tag class to register.
* @throws TagRegisterException If an error occurs while registering the tag.
*/
public static void register(int id, Class<? extends Tag> tag) throws TagRegisterException {
if(idToTag.containsKey(id)) {
throw new TagRegisterException("Tag ID \"" + id + "\" is already in use.");
}
if(tagToId.containsKey(tag)) {
throw new TagRegisterException("Tag \"" + tag.getSimpleName() + "\" is already registered.");
}
if(tagToId.containsKey(tag)) {
throw new TagRegisterException("Tag \"" + tag.getSimpleName() + "\" is already registered.");
}
idToTag.put(id, tag);
tagToId.put(tag, id);
}
idToTag.put(id, tag);
tagToId.put(tag, id);
}
/**
* Gets the tag class with the given id.
*
* @param id Id of the tag.
* @return The tag class with the given id, or null if it cannot be found.
*/
public static Class<? extends Tag> getClassFor(int id) {
if(!idToTag.containsKey(id)) {
return null;
}
/**
* Gets the tag class with the given id.
*
* @param id Id of the tag.
* @return The tag class with the given id, or null if it cannot be found.
*/
public static Class<? extends Tag> getClassFor(int id) {
if(!idToTag.containsKey(id)) {
return null;
}
return idToTag.get(id);
}
return idToTag.get(id);
}
/**
* Gets the id of the given tag class.
*
* @param clazz The tag class to get the id of.
* @return The id of the given tag class, or -1 if it cannot be found.
*/
public static int getIdFor(Class<? extends Tag> clazz) {
if(!tagToId.containsKey(clazz)) {
return -1;
}
/**
* Gets the id of the given tag class.
*
* @param clazz The tag class to get the id of.
* @return The id of the given tag class, or -1 if it cannot be found.
*/
public static int getIdFor(Class<? extends Tag> clazz) {
if(!tagToId.containsKey(clazz)) {
return -1;
}
return tagToId.get(clazz);
}
return tagToId.get(clazz);
}
/**
* Creates an instance of the tag with the given id, using the String constructor.
*
* @param id Id of the tag.
* @param tagName Name to give the tag.
* @return The created tag.
* @throws TagCreateException If an error occurs while creating the tag.
*/
public static Tag createInstance(int id, String tagName) throws TagCreateException {
Class<? extends Tag> clazz = idToTag.get(id);
if(clazz == null) {
throw new TagCreateException("Could not find tag with ID \"" + id + "\".");
}
/**
* Creates an instance of the tag with the given id, using the String constructor.
*
* @param id Id of the tag.
* @param tagName Name to give the tag.
* @return The created tag.
* @throws TagCreateException If an error occurs while creating the tag.
*/
public static Tag createInstance(int id, String tagName) throws TagCreateException {
Class<? extends Tag> clazz = idToTag.get(id);
if(clazz == null) {
throw new TagCreateException("Could not find tag with ID \"" + id + "\".");
}
try {
Constructor<? extends Tag> constructor = clazz.getDeclaredConstructor(String.class);
constructor.setAccessible(true);
return constructor.newInstance(tagName);
} catch(Exception e) {
throw new TagCreateException("Failed to create instance of tag \"" + clazz.getSimpleName() + "\".", e);
}
}
try {
Constructor<? extends Tag> constructor = clazz.getDeclaredConstructor(String.class);
constructor.setAccessible(true);
return constructor.newInstance(tagName);
} catch(Exception e) {
throw new TagCreateException("Failed to create instance of tag \"" + clazz.getSimpleName() + "\".", e);
}
}
}

View File

@ -8,89 +8,89 @@ import java.io.IOException;
* A tag containing a byte array.
*/
public class ByteArrayTag extends Tag {
private byte[] value;
private byte[] value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public ByteArrayTag(String name) {
this(name, new byte[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public ByteArrayTag(String name) {
this(name, new byte[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public ByteArrayTag(String name, byte[] value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public ByteArrayTag(String name, byte[] value) {
super(name);
this.value = value;
}
@Override
public byte[] getValue() {
return this.value.clone();
}
@Override
public byte[] getValue() {
return this.value.clone();
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(byte[] value) {
if(value == null) {
return;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(byte[] value) {
if(value == null) {
return;
}
this.value = value.clone();
}
this.value = value.clone();
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public byte getValue(int index) {
return this.value[index];
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public byte getValue(int index) {
return this.value[index];
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, byte value) {
this.value[index] = value;
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, byte value) {
this.value[index] = value;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
@Override
public void read(DataInput in) throws IOException {
this.value = new byte[in.readInt()];
in.readFully(this.value);
}
@Override
public void read(DataInput in) throws IOException {
this.value = new byte[in.readInt()];
in.readFully(this.value);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
out.write(this.value);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
out.write(this.value);
}
@Override
public ByteArrayTag clone() {
return new ByteArrayTag(this.getName(), this.getValue());
}
@Override
public ByteArrayTag clone() {
return new ByteArrayTag(this.getName(), this.getValue());
}
}

View File

@ -8,54 +8,54 @@ import java.io.IOException;
* A tag containing a byte.
*/
public class ByteTag extends Tag {
private byte value;
private byte value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public ByteTag(String name) {
this(name, (byte) 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public ByteTag(String name) {
this(name, (byte) 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public ByteTag(String name, byte value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public ByteTag(String name, byte value) {
super(name);
this.value = value;
}
@Override
public Byte getValue() {
return this.value;
}
@Override
public Byte getValue() {
return this.value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(byte value) {
this.value = value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(byte value) {
this.value = value;
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readByte();
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readByte();
}
@Override
public void write(DataOutput out) throws IOException {
out.writeByte(this.value);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeByte(this.value);
}
@Override
public ByteTag clone() {
return new ByteTag(this.getName(), this.getValue());
}
@Override
public ByteTag clone() {
return new ByteTag(this.getName(), this.getValue());
}
}

View File

@ -6,173 +6,179 @@ import java.io.DataInput;
import java.io.DataOutput;
import java.io.EOFException;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* A compound tag containing other tags.
*/
public class CompoundTag extends Tag implements Iterable<Tag> {
private Map<String, Tag> value;
private Map<String, Tag> value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public CompoundTag(String name) {
this(name, new LinkedHashMap<String, Tag>());
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public CompoundTag(String name) {
this(name, new LinkedHashMap<String, Tag>());
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public CompoundTag(String name, Map<String, Tag> value) {
super(name);
this.value = new LinkedHashMap<String, Tag>(value);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public CompoundTag(String name, Map<String, Tag> value) {
super(name);
this.value = new LinkedHashMap<String, Tag>(value);
}
@Override
public Map<String, Tag> getValue() {
return new LinkedHashMap<String, Tag>(this.value);
}
@Override
public Map<String, Tag> getValue() {
return new LinkedHashMap<String, Tag>(this.value);
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(Map<String, Tag> value) {
this.value = new LinkedHashMap<String, Tag>(value);
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(Map<String, Tag> value) {
this.value = new LinkedHashMap<String, Tag>(value);
}
/**
* Checks whether the compound tag is empty.
*
* @return Whether the compound tag is empty.
*/
public boolean isEmpty() {
return this.value.isEmpty();
}
/**
* Checks whether the compound tag is empty.
*
* @return Whether the compound tag is empty.
*/
public boolean isEmpty() {
return this.value.isEmpty();
}
/**
* Checks whether the compound tag contains a tag with the specified name.
*
* @param tagName Name of the tag to check for.
* @return Whether the compound tag contains a tag with the specified name.
*/
public boolean contains(String tagName) {
return this.value.containsKey(tagName);
}
/**
* Checks whether the compound tag contains a tag with the specified name.
*
* @param tagName Name of the tag to check for.
* @return Whether the compound tag contains a tag with the specified name.
*/
public boolean contains(String tagName) {
return this.value.containsKey(tagName);
}
/**
* Gets the tag with the specified name.
*
* @param <T> Type of tag to get.
* @param tagName Name of the tag.
* @return The tag with the specified name.
*/
public <T extends Tag> T get(String tagName) {
return (T) this.value.get(tagName);
}
/**
* Gets the tag with the specified name.
*
* @param <T> Type of tag to get.
* @param tagName Name of the tag.
* @return The tag with the specified name.
*/
public <T extends Tag> T get(String tagName) {
return (T) this.value.get(tagName);
}
/**
* Puts the tag into this compound tag.
*
* @param <T> Type of tag to put.
* @param tag Tag to put into this compound tag.
* @return The previous tag associated with its name, or null if there wasn't one.
*/
public <T extends Tag> T put(T tag) {
return (T) this.value.put(tag.getName(), tag);
}
/**
* Puts the tag into this compound tag.
*
* @param <T> Type of tag to put.
* @param tag Tag to put into this compound tag.
* @return The previous tag associated with its name, or null if there wasn't one.
*/
public <T extends Tag> T put(T tag) {
return (T) this.value.put(tag.getName(), tag);
}
/**
* Removes a tag from this compound tag.
*
* @param <T> Type of tag to remove.
* @param tagName Name of the tag to remove.
* @return The removed tag.
*/
public <T extends Tag> T remove(String tagName) {
return (T) this.value.remove(tagName);
}
/**
* Removes a tag from this compound tag.
*
* @param <T> Type of tag to remove.
* @param tagName Name of the tag to remove.
* @return The removed tag.
*/
public <T extends Tag> T remove(String tagName) {
return (T) this.value.remove(tagName);
}
/**
* Gets a set of keys in this compound tag.
*
* @return The compound tag's key set.
*/
public Set<String> keySet() {
return this.value.keySet();
}
/**
* Gets a set of keys in this compound tag.
*
* @return The compound tag's key set.
*/
public Set<String> keySet() {
return this.value.keySet();
}
/**
* Gets a collection of tags in this compound tag.
*
* @return This compound tag's tags.
*/
public Collection<Tag> values() {
return this.value.values();
}
/**
* Gets a collection of tags in this compound tag.
*
* @return This compound tag's tags.
*/
public Collection<Tag> values() {
return this.value.values();
}
/**
* Gets the number of tags in this compound tag.
*
* @return This compound tag's size.
*/
public int size() {
return this.value.size();
}
/**
* Gets the number of tags in this compound tag.
*
* @return This compound tag's size.
*/
public int size() {
return this.value.size();
}
/**
* Clears all tags from this compound tag.
*/
public void clear() {
this.value.clear();
}
/**
* Clears all tags from this compound tag.
*/
public void clear() {
this.value.clear();
}
@Override
public Iterator<Tag> iterator() {
return this.values().iterator();
}
@Override
public Iterator<Tag> iterator() {
return this.values().iterator();
}
@Override
public void read(DataInput in) throws IOException {
List<Tag> tags = new ArrayList<Tag>();
try {
Tag tag;
while((tag = NBTIO.readTag(in)) != null) {
tags.add(tag);
}
} catch(EOFException e) {
throw new IOException("Closing EndTag was not found!");
}
@Override
public void read(DataInput in) throws IOException {
List<Tag> tags = new ArrayList<Tag>();
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);
}
}
for(Tag tag : tags) {
this.put(tag);
}
}
@Override
public void write(DataOutput out) throws IOException {
for(Tag tag : this.value.values()) {
NBTIO.writeTag(out, tag);
}
@Override
public void write(DataOutput out) throws IOException {
for(Tag tag : this.value.values()) {
NBTIO.writeTag(out, tag);
}
out.writeByte(0);
}
out.writeByte(0);
}
@Override
public CompoundTag clone() {
Map<String, Tag> newMap = new LinkedHashMap<String, Tag>();
for(Entry<String, Tag> entry : this.value.entrySet()) {
newMap.put(entry.getKey(), entry.getValue().clone());
}
@Override
public CompoundTag clone() {
Map<String, Tag> newMap = new LinkedHashMap<String, Tag>();
for(Entry<String, Tag> entry : this.value.entrySet()) {
newMap.put(entry.getKey(), entry.getValue().clone());
}
return new CompoundTag(this.getName(), newMap);
}
return new CompoundTag(this.getName(), newMap);
}
}

View File

@ -8,54 +8,54 @@ import java.io.IOException;
* A tag containing a double.
*/
public class DoubleTag extends Tag {
private double value;
private double value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public DoubleTag(String name) {
this(name, 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public DoubleTag(String name) {
this(name, 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public DoubleTag(String name, double value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public DoubleTag(String name, double value) {
super(name);
this.value = value;
}
@Override
public Double getValue() {
return this.value;
}
@Override
public Double getValue() {
return this.value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(double value) {
this.value = value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(double value) {
this.value = value;
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readDouble();
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readDouble();
}
@Override
public void write(DataOutput out) throws IOException {
out.writeDouble(this.value);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeDouble(this.value);
}
@Override
public DoubleTag clone() {
return new DoubleTag(this.getName(), this.getValue());
}
@Override
public DoubleTag clone() {
return new DoubleTag(this.getName(), this.getValue());
}
}

View File

@ -8,54 +8,54 @@ import java.io.IOException;
* A tag containing a float.
*/
public class FloatTag extends Tag {
private float value;
private float value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public FloatTag(String name) {
this(name, 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public FloatTag(String name) {
this(name, 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public FloatTag(String name, float value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public FloatTag(String name, float value) {
super(name);
this.value = value;
}
@Override
public Float getValue() {
return this.value;
}
@Override
public Float getValue() {
return this.value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(float value) {
this.value = value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(float value) {
this.value = value;
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readFloat();
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readFloat();
}
@Override
public void write(DataOutput out) throws IOException {
out.writeFloat(this.value);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeFloat(this.value);
}
@Override
public FloatTag clone() {
return new FloatTag(this.getName(), this.getValue());
}
@Override
public FloatTag clone() {
return new FloatTag(this.getName(), this.getValue());
}
}

View File

@ -8,93 +8,93 @@ import java.io.IOException;
* A tag containing an integer array.
*/
public class IntArrayTag extends Tag {
private int[] value;
private int[] value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public IntArrayTag(String name) {
this(name, new int[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public IntArrayTag(String name) {
this(name, new int[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public IntArrayTag(String name, int[] value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public IntArrayTag(String name, int[] value) {
super(name);
this.value = value;
}
@Override
public int[] getValue() {
return this.value.clone();
}
@Override
public int[] getValue() {
return this.value.clone();
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(int[] value) {
if(value == null) {
return;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(int[] value) {
if(value == null) {
return;
}
this.value = value.clone();
}
this.value = value.clone();
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public int getValue(int index) {
return this.value[index];
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public int getValue(int index) {
return this.value[index];
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, int value) {
this.value[index] = value;
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, int value) {
this.value[index] = value;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
@Override
public void read(DataInput in) throws IOException {
this.value = new int[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readInt();
}
}
@Override
public void read(DataInput in) throws IOException {
this.value = new int[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readInt();
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeInt(this.value[index]);
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeInt(this.value[index]);
}
}
@Override
public IntArrayTag clone() {
return new IntArrayTag(this.getName(), this.getValue());
}
@Override
public IntArrayTag clone() {
return new IntArrayTag(this.getName(), this.getValue());
}
}

View File

@ -8,54 +8,54 @@ import java.io.IOException;
* A tag containing an integer.
*/
public class IntTag extends Tag {
private int value;
private int value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public IntTag(String name) {
this(name, 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public IntTag(String name) {
this(name, 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public IntTag(String name, int value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public IntTag(String name, int value) {
super(name);
this.value = value;
}
@Override
public Integer getValue() {
return this.value;
}
@Override
public Integer getValue() {
return this.value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(int value) {
this.value = value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(int value) {
this.value = value;
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readInt();
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readInt();
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value);
}
@Override
public IntTag clone() {
return new IntTag(this.getName(), this.getValue());
}
@Override
public IntTag clone() {
return new IntTag(this.getName(), this.getValue());
}
}

View File

@ -14,183 +14,183 @@ import java.util.List;
* A tag containing a list of tags.
*/
public class ListTag extends Tag implements Iterable<Tag> {
private Class<? extends Tag> type;
private List<Tag> value;
private Class<? extends Tag> type;
private List<Tag> value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
private ListTag(String name) {
super(name);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
private ListTag(String name) {
super(name);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param type Tag type of the list.
*/
public ListTag(String name, Class<? extends Tag> type) {
super(name);
this.type = type;
this.value = new ArrayList<Tag>();
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param type Tag type of the list.
*/
public ListTag(String name, Class<? extends Tag> type) {
super(name);
this.type = type;
this.value = new ArrayList<Tag>();
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @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, List<Tag> value) throws IllegalArgumentException {
super(name);
Class<? extends Tag> type = null;
for(Tag tag : value) {
if(tag == null) {
throw new IllegalArgumentException("List cannot contain null tags.");
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @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, List<Tag> value) throws IllegalArgumentException {
super(name);
Class<? extends Tag> 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.");
}
}
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<Tag>(value);
}
this.type = type;
this.value = new ArrayList<Tag>(value);
}
@Override
public List<Tag> getValue() {
return new ArrayList<Tag>(this.value);
}
@Override
public List<Tag> getValue() {
return new ArrayList<Tag>(this.value);
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(List<Tag> value) {
for(Tag tag : value) {
if(tag.getClass() != this.type) {
throw new IllegalArgumentException("Tag type cannot differ from ListTag type.");
}
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(List<Tag> value) {
for(Tag tag : value) {
if(tag.getClass() != this.type) {
throw new IllegalArgumentException("Tag type cannot differ from ListTag type.");
}
}
this.value = new ArrayList<Tag>(value);
}
this.value = new ArrayList<Tag>(value);
}
/**
* Gets the element type of the ListTag.
*
* @return The ListTag's element type.
*/
public Class<? extends Tag> getElementType() {
return this.type;
}
/**
* Gets the element type of the ListTag.
*
* @return The ListTag's element type.
*/
public Class<? extends Tag> getElementType() {
return this.type;
}
/**
* Adds a tag to this list tag.
*
* @param tag Tag to add.
* @return If the list was changed as a result.
*/
public boolean add(Tag tag) {
if(tag.getClass() != this.type) {
throw new IllegalArgumentException("Tag type cannot differ from ListTag type.");
}
/**
* Adds a tag to this list tag.
*
* @param tag Tag to add.
* @return If the list was changed as a result.
*/
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);
}
return this.value.add(tag);
}
/**
* Removes a tag from this list tag.
*
* @param tag Tag to remove.
* @return If the list contained the tag.
*/
public boolean remove(Tag tag) {
return this.value.remove(tag);
}
/**
* Removes a tag from this list tag.
*
* @param tag Tag to remove.
* @return If the list contained the tag.
*/
public boolean remove(Tag tag) {
return this.value.remove(tag);
}
/**
* Gets the tag at the given index of this list tag.
*
* @param <T> Type of tag to get
* @param index Index of the tag.
* @return The tag at the given index.
*/
public <T extends Tag> T get(int index) {
return (T) this.value.get(index);
}
/**
* Gets the tag at the given index of this list tag.
*
* @param <T> Type of tag to get
* @param index Index of the tag.
* @return The tag at the given index.
*/
public <T extends Tag> T get(int index) {
return (T) this.value.get(index);
}
/**
* Gets the number of tags in this list tag.
*
* @return The size of this list tag.
*/
public int size() {
return this.value.size();
}
/**
* Gets the number of tags in this list tag.
*
* @return The size of this list tag.
*/
public int size() {
return this.value.size();
}
@Override
public Iterator<Tag> iterator() {
return this.value.iterator();
}
@Override
public Iterator<Tag> iterator() {
return this.value.iterator();
}
@Override
public void read(DataInput in) throws IOException {
int id = in.readUnsignedByte();
this.type = TagRegistry.getClassFor(id);
this.value = new ArrayList<Tag>();
if(id != 0 && this.type == null) {
throw new IOException("Unknown tag ID in ListTag: " + id);
}
@Override
public void read(DataInput in) throws IOException {
int id = in.readUnsignedByte();
this.type = TagRegistry.getClassFor(id);
this.value = new ArrayList<Tag>();
if(id != 0 && this.type == null) {
throw new IOException("Unknown tag ID in ListTag: " + id);
}
int count = in.readInt();
for(int index = 0; index < count; index++) {
Tag tag = null;
try {
tag = TagRegistry.createInstance(id, "");
} catch(TagCreateException e) {
throw new IOException("Failed to create tag.", e);
}
int count = in.readInt();
for(int index = 0; index < count; index++) {
Tag tag = null;
try {
tag = TagRegistry.createInstance(id, "");
} catch(TagCreateException e) {
throw new IOException("Failed to create tag.", e);
}
tag.read(in);
this.add(tag);
}
}
tag.read(in);
this.add(tag);
}
}
@Override
public void write(DataOutput out) throws IOException {
if(this.value.isEmpty()) {
out.writeByte(0);
} else {
int id = TagRegistry.getIdFor(this.type);
if(id == -1) {
throw new IOException("ListTag contains unregistered tag class.");
}
@Override
public void write(DataOutput out) throws IOException {
if(this.value.isEmpty()) {
out.writeByte(0);
} else {
int id = TagRegistry.getIdFor(this.type);
if(id == -1) {
throw new IOException("ListTag contains unregistered tag class.");
}
out.writeByte(id);
}
out.writeByte(id);
}
out.writeInt(this.value.size());
for(Tag tag : this.value) {
tag.write(out);
}
}
out.writeInt(this.value.size());
for(Tag tag : this.value) {
tag.write(out);
}
}
@Override
public ListTag clone() {
List<Tag> newList = new ArrayList<Tag>();
for(Tag value : this.value) {
newList.add(value.clone());
}
@Override
public ListTag clone() {
List<Tag> newList = new ArrayList<Tag>();
for(Tag value : this.value) {
newList.add(value.clone());
}
return new ListTag(this.getName(), newList);
}
return new ListTag(this.getName(), newList);
}
}

View File

@ -8,54 +8,54 @@ import java.io.IOException;
* A tag containing a long.
*/
public class LongTag extends Tag {
private long value;
private long value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public LongTag(String name) {
this(name, 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public LongTag(String name) {
this(name, 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public LongTag(String name, long value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public LongTag(String name, long value) {
super(name);
this.value = value;
}
@Override
public Long getValue() {
return this.value;
}
@Override
public Long getValue() {
return this.value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(long value) {
this.value = value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(long value) {
this.value = value;
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readLong();
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readLong();
}
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(this.value);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(this.value);
}
@Override
public LongTag clone() {
return new LongTag(this.getName(), this.getValue());
}
@Override
public LongTag clone() {
return new LongTag(this.getName(), this.getValue());
}
}

View File

@ -8,54 +8,54 @@ import java.io.IOException;
* A tag containing a short.
*/
public class ShortTag extends Tag {
private short value;
private short value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public ShortTag(String name) {
this(name, (short) 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public ShortTag(String name) {
this(name, (short) 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public ShortTag(String name, short value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public ShortTag(String name, short value) {
super(name);
this.value = value;
}
@Override
public Short getValue() {
return this.value;
}
@Override
public Short getValue() {
return this.value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(short value) {
this.value = value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(short value) {
this.value = value;
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readShort();
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readShort();
}
@Override
public void write(DataOutput out) throws IOException {
out.writeShort(this.value);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeShort(this.value);
}
@Override
public ShortTag clone() {
return new ShortTag(this.getName(), this.getValue());
}
@Override
public ShortTag clone() {
return new ShortTag(this.getName(), this.getValue());
}
}

View File

@ -8,54 +8,54 @@ import java.io.IOException;
* A tag containing a string.
*/
public class StringTag extends Tag {
private String value;
private String value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public StringTag(String name) {
this(name, "");
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public StringTag(String name) {
this(name, "");
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public StringTag(String name, String value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public StringTag(String name, String value) {
super(name);
this.value = value;
}
@Override
public String getValue() {
return this.value;
}
@Override
public String getValue() {
return this.value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(String value) {
this.value = value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(String value) {
this.value = value;
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readUTF();
}
@Override
public void read(DataInput in) throws IOException {
this.value = in.readUTF();
}
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(this.value);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(this.value);
}
@Override
public StringTag clone() {
return new StringTag(this.getName(), this.getValue());
}
@Override
public StringTag clone() {
return new StringTag(this.getName(), this.getValue());
}
}

View File

@ -7,116 +7,116 @@ import java.lang.reflect.Array;
/**
* Represents an NBT tag.
*
* <p>
* All tags must have a constructor with a single string parameter for reading tags (can be any visibility).
* Tags should also have setter methods specific to their value types.
*/
public abstract class Tag implements Cloneable {
private String name;
private String name;
/**
* Creates a tag with the specified name.
*
* @param name The name.
*/
public Tag(String name) {
this.name = name;
}
/**
* Creates a tag with the specified name.
*
* @param name The name.
*/
public Tag(String name) {
this.name = name;
}
/**
* Gets the name of this tag.
*
* @return The name of this tag.
*/
public final String getName() {
return this.name;
}
/**
* Gets the name of this tag.
*
* @return The name of this tag.
*/
public final String getName() {
return this.name;
}
/**
* Gets the value of this tag.
*
* @return The value of this tag.
*/
public abstract Object getValue();
/**
* Gets the value of this tag.
*
* @return The value of this tag.
*/
public abstract Object getValue();
/**
* Reads this tag from an input stream.
*
* @param in Stream to write to.
* @throws java.io.IOException If an I/O error occurs.
*/
public abstract void read(DataInput in) throws IOException;
/**
* Reads this tag from an input stream.
*
* @param in Stream to write to.
* @throws java.io.IOException If an I/O error occurs.
*/
public abstract void read(DataInput in) throws IOException;
/**
* Writes this tag to an output stream.
*
* @param out Stream to write to.
* @throws java.io.IOException If an I/O error occurs.
*/
public abstract void write(DataOutput out) throws IOException;
/**
* Writes this tag to an output stream.
*
* @param out Stream to write to.
* @throws java.io.IOException If an I/O error occurs.
*/
public abstract void write(DataOutput out) throws IOException;
@Override
public abstract Tag clone();
@Override
public abstract Tag clone();
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Tag)) {
return false;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Tag)) {
return false;
}
Tag tag = (Tag) obj;
if(!this.getName().equals(tag.getName())) {
return false;
}
Tag tag = (Tag) obj;
if(!this.getName().equals(tag.getName())) {
return false;
}
if(this.getValue() == null) {
return tag.getValue() == null;
} else if(tag.getValue() == null) {
return false;
}
if(this.getValue() == null) {
return tag.getValue() == null;
} else if(tag.getValue() == null) {
return false;
}
if(this.getValue().getClass().isArray() && tag.getValue().getClass().isArray()) {
int length = Array.getLength(this.getValue());
if(Array.getLength(tag.getValue()) != length) {
return false;
}
if(this.getValue().getClass().isArray() && tag.getValue().getClass().isArray()) {
int length = Array.getLength(this.getValue());
if(Array.getLength(tag.getValue()) != length) {
return false;
}
for(int index = 0; index < length; index++) {
Object o = Array.get(this.getValue(), index);
Object other = Array.get(tag.getValue(), index);
if(o == null && other != null || o != null && !o.equals(other)) {
return false;
}
}
for(int index = 0; index < length; index++) {
Object o = Array.get(this.getValue(), index);
Object other = Array.get(tag.getValue(), index);
if(o == null && other != null || o != null && !o.equals(other)) {
return false;
}
}
return true;
}
return true;
}
return this.getValue().equals(tag.getValue());
}
return this.getValue().equals(tag.getValue());
}
@Override
public String toString() {
String name = this.getName() != null && !this.getName().equals("") ? "(" + this.getName() + ")" : "";
String value = "";
if(this.getValue() != null) {
value = this.getValue().toString();
if(this.getValue().getClass().isArray()) {
StringBuilder build = new StringBuilder();
build.append("[");
for(int index = 0; index < Array.getLength(this.getValue()); index++) {
if(index > 0) {
build.append(", ");
}
@Override
public String toString() {
String name = this.getName() != null && !this.getName().equals("") ? "(" + this.getName() + ")" : "";
String value = "";
if(this.getValue() != null) {
value = this.getValue().toString();
if(this.getValue().getClass().isArray()) {
StringBuilder build = new StringBuilder();
build.append("[");
for(int index = 0; index < Array.getLength(this.getValue()); index++) {
if(index > 0) {
build.append(", ");
}
build.append(Array.get(this.getValue(), index));
}
build.append(Array.get(this.getValue(), index));
}
build.append("]");
value = build.toString();
}
}
build.append("]");
value = build.toString();
}
}
return this.getClass().getSimpleName() + name + " { " + value + " }";
}
return this.getClass().getSimpleName() + name + " { " + value + " }";
}
}

View File

@ -10,93 +10,93 @@ import java.io.IOException;
* A tag containing a double array.
*/
public class DoubleArrayTag extends Tag {
private double[] value;
private double[] value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public DoubleArrayTag(String name) {
this(name, new double[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public DoubleArrayTag(String name) {
this(name, new double[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public DoubleArrayTag(String name, double[] value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public DoubleArrayTag(String name, double[] value) {
super(name);
this.value = value;
}
@Override
public double[] getValue() {
return this.value.clone();
}
@Override
public double[] getValue() {
return this.value.clone();
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(double[] value) {
if(value == null) {
return;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(double[] value) {
if(value == null) {
return;
}
this.value = value.clone();
}
this.value = value.clone();
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public double getValue(int index) {
return this.value[index];
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public double getValue(int index) {
return this.value[index];
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, double value) {
this.value[index] = value;
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, double value) {
this.value[index] = value;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
@Override
public void read(DataInput in) throws IOException {
this.value = new double[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readDouble();
}
}
@Override
public void read(DataInput in) throws IOException {
this.value = new double[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readDouble();
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeDouble(this.value[index]);
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeDouble(this.value[index]);
}
}
@Override
public DoubleArrayTag clone() {
return new DoubleArrayTag(this.getName(), this.getValue());
}
@Override
public DoubleArrayTag clone() {
return new DoubleArrayTag(this.getName(), this.getValue());
}
}

View File

@ -10,93 +10,93 @@ import java.io.IOException;
* A tag containing a float array.
*/
public class FloatArrayTag extends Tag {
private float[] value;
private float[] value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public FloatArrayTag(String name) {
this(name, new float[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public FloatArrayTag(String name) {
this(name, new float[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public FloatArrayTag(String name, float[] value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public FloatArrayTag(String name, float[] value) {
super(name);
this.value = value;
}
@Override
public float[] getValue() {
return this.value.clone();
}
@Override
public float[] getValue() {
return this.value.clone();
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(float[] value) {
if(value == null) {
return;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(float[] value) {
if(value == null) {
return;
}
this.value = value.clone();
}
this.value = value.clone();
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public float getValue(int index) {
return this.value[index];
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public float getValue(int index) {
return this.value[index];
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, float value) {
this.value[index] = value;
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, float value) {
this.value[index] = value;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
@Override
public void read(DataInput in) throws IOException {
this.value = new float[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readFloat();
}
}
@Override
public void read(DataInput in) throws IOException {
this.value = new float[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readFloat();
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeFloat(this.value[index]);
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeFloat(this.value[index]);
}
}
@Override
public FloatArrayTag clone() {
return new FloatArrayTag(this.getName(), this.getValue());
}
@Override
public FloatArrayTag clone() {
return new FloatArrayTag(this.getName(), this.getValue());
}
}

View File

@ -10,93 +10,93 @@ import java.io.IOException;
* A tag containing a long array.
*/
public class LongArrayTag extends Tag {
private long[] value;
private long[] value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public LongArrayTag(String name) {
this(name, new long[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public LongArrayTag(String name) {
this(name, new long[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public LongArrayTag(String name, long[] value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public LongArrayTag(String name, long[] value) {
super(name);
this.value = value;
}
@Override
public long[] getValue() {
return this.value.clone();
}
@Override
public long[] getValue() {
return this.value.clone();
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(long[] value) {
if(value == null) {
return;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(long[] value) {
if(value == null) {
return;
}
this.value = value.clone();
}
this.value = value.clone();
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public long getValue(int index) {
return this.value[index];
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public long getValue(int index) {
return this.value[index];
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, long value) {
this.value[index] = value;
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, long value) {
this.value[index] = value;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
@Override
public void read(DataInput in) throws IOException {
this.value = new long[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readLong();
}
}
@Override
public void read(DataInput in) throws IOException {
this.value = new long[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readLong();
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeLong(this.value[index]);
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeLong(this.value[index]);
}
}
@Override
public LongArrayTag clone() {
return new LongArrayTag(this.getName(), this.getValue());
}
@Override
public LongArrayTag clone() {
return new LongArrayTag(this.getName(), this.getValue());
}
}

View File

@ -2,107 +2,114 @@ package com.github.steveice10.opennbt.tag.builtin.custom;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import java.io.*;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
/**
* A tag containing an array of serializable objects.
*/
public class SerializableArrayTag extends Tag {
private Serializable[] value;
private Serializable[] value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public SerializableArrayTag(String name) {
this(name, new Serializable[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public SerializableArrayTag(String name) {
this(name, new Serializable[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public SerializableArrayTag(String name, Serializable[] value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public SerializableArrayTag(String name, Serializable[] value) {
super(name);
this.value = value;
}
@Override
public Serializable[] getValue() {
return this.value.clone();
}
@Override
public Serializable[] getValue() {
return this.value.clone();
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(Serializable[] value) {
if(value == null) {
return;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(Serializable[] value) {
if(value == null) {
return;
}
this.value = value.clone();
}
this.value = value.clone();
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public Serializable getValue(int index) {
return this.value[index];
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public Serializable getValue(int index) {
return this.value[index];
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, Serializable value) {
this.value[index] = value;
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, Serializable value) {
this.value[index] = value;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
@Override
public void read(DataInput in) throws IOException {
this.value = new Serializable[in.readInt()];
ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in));
for(int index = 0; index < this.value.length; index++) {
try {
this.value[index] = (Serializable) str.readObject();
} catch(ClassNotFoundException e) {
throw new IOException("Class not found while reading SerializableArrayTag!", e);
}
}
}
@Override
public void read(DataInput in) throws IOException {
this.value = new Serializable[in.readInt()];
ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in));
for(int index = 0; index < this.value.length; index++) {
try {
this.value[index] = (Serializable) str.readObject();
} catch(ClassNotFoundException e) {
throw new IOException("Class not found while reading SerializableArrayTag!", e);
}
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out));
for(int index = 0; index < this.value.length; index++) {
str.writeObject(this.value[index]);
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out));
for(int index = 0; index < this.value.length; index++) {
str.writeObject(this.value[index]);
}
}
@Override
public SerializableArrayTag clone() {
return new SerializableArrayTag(this.getName(), this.getValue());
}
@Override
public SerializableArrayTag clone() {
return new SerializableArrayTag(this.getName(), this.getValue());
}
private static class DataInputInputStream extends InputStream {
private DataInput in;

View File

@ -2,151 +2,158 @@ package com.github.steveice10.opennbt.tag.builtin.custom;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import java.io.*;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
/**
* A tag containing a serializable object.
*/
public class SerializableTag extends Tag {
private Serializable value;
private Serializable value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public SerializableTag(String name) {
this(name, 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public SerializableTag(String name) {
this(name, 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public SerializableTag(String name, Serializable value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public SerializableTag(String name, Serializable value) {
super(name);
this.value = value;
}
@Override
public Serializable getValue() {
return this.value;
}
@Override
public Serializable getValue() {
return this.value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(Serializable value) {
this.value = value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(Serializable value) {
this.value = value;
}
@Override
public void read(DataInput in) throws IOException {
ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in));
try {
this.value = (Serializable) str.readObject();
} catch(ClassNotFoundException e) {
throw new IOException("Class not found while reading SerializableTag!", e);
}
}
@Override
public void read(DataInput in) throws IOException {
ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in));
try {
this.value = (Serializable) str.readObject();
} catch(ClassNotFoundException e) {
throw new IOException("Class not found while reading SerializableTag!", e);
}
}
@Override
public void write(DataOutput out) throws IOException {
ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out));
str.writeObject(this.value);
}
@Override
public void write(DataOutput out) throws IOException {
ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out));
str.writeObject(this.value);
}
@Override
public SerializableTag clone() {
return new SerializableTag(this.getName(), this.getValue());
}
@Override
public SerializableTag clone() {
return new SerializableTag(this.getName(), this.getValue());
}
private static class DataInputInputStream extends InputStream {
private DataInput in;
private static class DataInputInputStream extends InputStream {
private DataInput in;
public DataInputInputStream(DataInput in) {
this.in = in;
}
public DataInputInputStream(DataInput in) {
this.in = in;
}
@Override
public int read() throws IOException {
return this.in.readUnsignedByte();
}
@Override
public int read() throws IOException {
return this.in.readUnsignedByte();
}
@Override
public int read(byte[] b) throws IOException {
this.in.readFully(b);
return b.length;
}
@Override
public int read(byte[] b) throws IOException {
this.in.readFully(b);
return b.length;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
this.in.readFully(b, off, len);
return len;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
this.in.readFully(b, off, len);
return len;
}
@Override
public long skip(long l) throws IOException {
return this.in.skipBytes((int) l);
}
@Override
public long skip(long l) throws IOException {
return this.in.skipBytes((int) l);
}
@Override
public int available() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public int available() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException {
}
@Override
public void close() throws IOException {
}
@Override
public synchronized void mark(int i) {
throw new UnsupportedOperationException();
}
@Override
public synchronized void mark(int i) {
throw new UnsupportedOperationException();
}
@Override
public synchronized void reset() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public synchronized void reset() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public boolean markSupported() {
return false;
}
}
@Override
public boolean markSupported() {
return false;
}
}
private static class DataOutputOutputStream extends OutputStream {
private DataOutput out;
private static class DataOutputOutputStream extends OutputStream {
private DataOutput out;
public DataOutputOutputStream(DataOutput out) {
this.out = out;
}
public DataOutputOutputStream(DataOutput out) {
this.out = out;
}
@Override
public void write(int b) throws IOException {
this.out.write(b);
}
@Override
public void write(int b) throws IOException {
this.out.write(b);
}
@Override
public void write(byte[] b) throws IOException {
this.out.write(b);
}
@Override
public void write(byte[] b) throws IOException {
this.out.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
}
@Override
public void flush() throws IOException {
}
@Override
public void flush() throws IOException {
}
@Override
public void close() throws IOException {
}
}
@Override
public void close() throws IOException {
}
}
}

View File

@ -10,93 +10,93 @@ import java.io.IOException;
* A tag containing a short array.
*/
public class ShortArrayTag extends Tag {
private short[] value;
private short[] value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public ShortArrayTag(String name) {
this(name, new short[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public ShortArrayTag(String name) {
this(name, new short[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public ShortArrayTag(String name, short[] value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public ShortArrayTag(String name, short[] value) {
super(name);
this.value = value;
}
@Override
public short[] getValue() {
return this.value.clone();
}
@Override
public short[] getValue() {
return this.value.clone();
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(short[] value) {
if(value == null) {
return;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(short[] value) {
if(value == null) {
return;
}
this.value = value.clone();
}
this.value = value.clone();
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public short getValue(int index) {
return this.value[index];
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public short getValue(int index) {
return this.value[index];
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, short value) {
this.value[index] = value;
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, short value) {
this.value[index] = value;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
@Override
public void read(DataInput in) throws IOException {
this.value = new short[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readShort();
}
}
@Override
public void read(DataInput in) throws IOException {
this.value = new short[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readShort();
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeShort(this.value[index]);
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeShort(this.value[index]);
}
}
@Override
public ShortArrayTag clone() {
return new ShortArrayTag(this.getName(), this.getValue());
}
@Override
public ShortArrayTag clone() {
return new ShortArrayTag(this.getName(), this.getValue());
}
}

View File

@ -10,93 +10,93 @@ import java.io.IOException;
* A tag containing a string array.
*/
public class StringArrayTag extends Tag {
private String[] value;
private String[] value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public StringArrayTag(String name) {
this(name, new String[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public StringArrayTag(String name) {
this(name, new String[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public StringArrayTag(String name, String[] value) {
super(name);
this.value = value;
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public StringArrayTag(String name, String[] value) {
super(name);
this.value = value;
}
@Override
public String[] getValue() {
return this.value.clone();
}
@Override
public String[] getValue() {
return this.value.clone();
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(String[] value) {
if(value == null) {
return;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(String[] value) {
if(value == null) {
return;
}
this.value = value.clone();
}
this.value = value.clone();
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public String getValue(int index) {
return this.value[index];
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public String getValue(int index) {
return this.value[index];
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, String value) {
this.value[index] = value;
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, String value) {
this.value[index] = value;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
@Override
public void read(DataInput in) throws IOException {
this.value = new String[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readUTF();
}
}
@Override
public void read(DataInput in) throws IOException {
this.value = new String[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readUTF();
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeUTF(this.value[index]);
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeUTF(this.value[index]);
}
}
@Override
public StringArrayTag clone() {
return new StringArrayTag(this.getName(), this.getValue());
}
@Override
public StringArrayTag clone() {
return new StringArrayTag(this.getName(), this.getValue());
}
}