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; 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.TagCreateException;
import com.github.steveice10.opennbt.tag.TagRegistry; import com.github.steveice10.opennbt.tag.TagRegistry;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.Tag; 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.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
@ -13,125 +25,125 @@ import java.util.zip.GZIPOutputStream;
* A class containing methods for reading/writing NBT tags. * A class containing methods for reading/writing NBT tags.
*/ */
public class NBTIO { public class NBTIO {
/** /**
* Reads the compressed, big endian root CompoundTag from the given file. * Reads the compressed, big endian root CompoundTag from the given file.
* *
* @param path Path of the file. * @param path Path of the file.
* @return The read compound tag. * @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static CompoundTag readFile(String path) throws IOException { public static CompoundTag readFile(String path) throws IOException {
return readFile(new File(path)); return readFile(new File(path));
} }
/** /**
* Reads the compressed, big endian root CompoundTag from the given file. * Reads the compressed, big endian root CompoundTag from the given file.
* *
* @param file File to read from. * @param file File to read from.
* @return The read compound tag. * @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static CompoundTag readFile(File file) throws IOException { public static CompoundTag readFile(File file) throws IOException {
return readFile(file, true, false); return readFile(file, true, false);
} }
/** /**
* Reads the root CompoundTag from the given file. * Reads the root CompoundTag from the given file.
* *
* @param path Path of the file. * @param path Path of the file.
* @param compressed Whether the NBT file is compressed. * @param compressed Whether the NBT file is compressed.
* @param littleEndian Whether the NBT file is little endian. * @param littleEndian Whether the NBT file is little endian.
* @return The read compound tag. * @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static CompoundTag readFile(String path, boolean compressed, boolean littleEndian) throws IOException { public static CompoundTag readFile(String path, boolean compressed, boolean littleEndian) throws IOException {
return readFile(new File(path), compressed, littleEndian); return readFile(new File(path), compressed, littleEndian);
} }
/** /**
* Reads the root CompoundTag from the given file. * Reads the root CompoundTag from the given file.
* *
* @param file File to read from. * @param file File to read from.
* @param compressed Whether the NBT file is compressed. * @param compressed Whether the NBT file is compressed.
* @param littleEndian Whether the NBT file is little endian. * @param littleEndian Whether the NBT file is little endian.
* @return The read compound tag. * @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static CompoundTag readFile(File file, boolean compressed, boolean littleEndian) throws IOException { public static CompoundTag readFile(File file, boolean compressed, boolean littleEndian) throws IOException {
InputStream in = new FileInputStream(file); InputStream in = new FileInputStream(file);
if(compressed) { if(compressed) {
in = new GZIPInputStream(in); in = new GZIPInputStream(in);
} }
Tag tag = readTag(in, littleEndian); Tag tag = readTag(in, littleEndian);
if(!(tag instanceof CompoundTag)) { if(!(tag instanceof CompoundTag)) {
throw new IOException("Root tag is not a 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. * Writes the given root CompoundTag to the given file, compressed and in big endian.
* *
* @param tag Tag to write. * @param tag Tag to write.
* @param path Path to write to. * @param path Path to write to.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static void writeFile(CompoundTag tag, String path) throws IOException { public static void writeFile(CompoundTag tag, String path) throws IOException {
writeFile(tag, new File(path)); writeFile(tag, new File(path));
} }
/** /**
* Writes the given root CompoundTag to the given file, compressed and in big endian. * Writes the given root CompoundTag to the given file, compressed and in big endian.
* *
* @param tag Tag to write. * @param tag Tag to write.
* @param file File to write to. * @param file File to write to.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static void writeFile(CompoundTag tag, File file) throws IOException { public static void writeFile(CompoundTag tag, File file) throws IOException {
writeFile(tag, file, true, false); writeFile(tag, file, true, false);
} }
/** /**
* Writes the given root CompoundTag to the given file. * Writes the given root CompoundTag to the given file.
* *
* @param tag Tag to write. * @param tag Tag to write.
* @param path Path to write to. * @param path Path to write to.
* @param compressed Whether the NBT file should be compressed. * @param compressed Whether the NBT file should be compressed.
* @param littleEndian Whether to write little endian NBT. * @param littleEndian Whether to write little endian NBT.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static void writeFile(CompoundTag tag, String path, boolean compressed, boolean littleEndian) throws IOException { public static void writeFile(CompoundTag tag, String path, boolean compressed, boolean littleEndian) throws IOException {
writeFile(tag, new File(path), compressed, littleEndian); writeFile(tag, new File(path), compressed, littleEndian);
} }
/** /**
* Writes the given root CompoundTag to the given file. * Writes the given root CompoundTag to the given file.
* *
* @param tag Tag to write. * @param tag Tag to write.
* @param file File to write to. * @param file File to write to.
* @param compressed Whether the NBT file should be compressed. * @param compressed Whether the NBT file should be compressed.
* @param littleEndian Whether to write little endian NBT. * @param littleEndian Whether to write little endian NBT.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static void writeFile(CompoundTag tag, File file, boolean compressed, boolean littleEndian) throws IOException { public static void writeFile(CompoundTag tag, File file, boolean compressed, boolean littleEndian) throws IOException {
if(!file.exists()) { if(!file.exists()) {
if(file.getParentFile() != null && !file.getParentFile().exists()) { if(file.getParentFile() != null && !file.getParentFile().exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
file.createNewFile(); file.createNewFile();
} }
OutputStream out = new FileOutputStream(file); OutputStream out = new FileOutputStream(file);
if(compressed) { if(compressed) {
out = new GZIPOutputStream(out); out = new GZIPOutputStream(out);
} }
writeTag(out, tag, littleEndian); writeTag(out, tag, littleEndian);
out.close(); out.close();
} }
/** /**
* Reads a big endian NBT tag. * Reads a big endian NBT tag.
@ -144,17 +156,17 @@ public class NBTIO {
return readTag(in, false); return readTag(in, false);
} }
/** /**
* Reads an NBT tag. * Reads an NBT tag.
* *
* @param in Input stream to read from. * @param in Input stream to read from.
* @param littleEndian Whether to read little endian NBT. * @param littleEndian Whether to read little endian NBT.
* @return The read tag, or null if the tag is an end tag. * @return The read tag, or null if the tag is an end tag.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static Tag readTag(InputStream in, boolean littleEndian) throws IOException { public static Tag readTag(InputStream in, boolean littleEndian) throws IOException {
return readTag((DataInput) (littleEndian ? new LittleEndianDataInputStream(in) : new DataInputStream(in))); return readTag((DataInput) (littleEndian ? new LittleEndianDataInputStream(in) : new DataInputStream(in)));
} }
/** /**
* Reads an NBT tag. * Reads an NBT tag.
@ -201,9 +213,9 @@ public class NBTIO {
* @param littleEndian Whether to write little endian NBT. * @param littleEndian Whether to write little endian NBT.
* @throws java.io.IOException If an I/O error occurs. * @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); writeTag((DataOutput) (littleEndian ? new LittleEndianDataOutputStream(out) : new DataOutputStream(out)), tag);
} }
/** /**
* Writes an NBT tag. * Writes an NBT tag.
@ -218,222 +230,222 @@ public class NBTIO {
tag.write(out); tag.write(out);
} }
private static class LittleEndianDataInputStream extends FilterInputStream implements DataInput { private static class LittleEndianDataInputStream extends FilterInputStream implements DataInput {
public LittleEndianDataInputStream(InputStream in) { public LittleEndianDataInputStream(InputStream in) {
super(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);
}
@Override @Override
public synchronized void write(byte[] b, int off, int len) throws IOException { public int read(byte[] b) throws IOException {
this.out.write(b, off, len); return this.in.read(b, 0, b.length);
}
}
@Override @Override
public void flush() throws IOException { public int read(byte[] b, int off, int len) throws IOException {
this.out.flush(); return this.in.read(b, off, len);
} }
@Override @Override
public void writeBoolean(boolean b) throws IOException { public void readFully(byte[] b) throws IOException {
this.out.write(b ? 1 : 0); this.readFully(b, 0, b.length);
} }
@Override @Override
public void writeByte(int b) throws IOException { public void readFully(byte[] b, int off, int len) throws IOException {
this.out.write(b); 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 @Override
public void writeShort(int s) throws IOException { public int skipBytes(int n) throws IOException {
this.out.write(s & 0xFF); int total = 0;
this.out.write((s >>> 8) & 0xFF); int skipped = 0;
} while(total < n && (skipped = (int) this.in.skip(n - total)) > 0) {
total += skipped;
}
return total;
}
@Override @Override
public void writeChar(int c) throws IOException { public boolean readBoolean() throws IOException {
this.out.write(c & 0xFF); int val = this.in.read();
this.out.write((c >>> 8) & 0xFF); if(val < 0) {
} throw new EOFException();
}
return val != 0;
}
@Override @Override
public void writeInt(int i) throws IOException { public byte readByte() throws IOException {
this.out.write(i & 0xFF); int val = this.in.read();
this.out.write((i >>> 8) & 0xFF); if(val < 0) {
this.out.write((i >>> 16) & 0xFF); throw new EOFException();
this.out.write((i >>> 24) & 0xFF); }
}
return (byte) val;
}
@Override @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 & 0xFF));
this.out.write((int) ((l >>> 8) & 0xFF)); this.out.write((int) ((l >>> 8) & 0xFF));
this.out.write((int) ((l >>> 16) & 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 >>> 40) & 0xFF));
this.out.write((int) ((l >>> 48) & 0xFF)); this.out.write((int) ((l >>> 48) & 0xFF));
this.out.write((int) ((l >>> 56) & 0xFF)); this.out.write((int) ((l >>> 56) & 0xFF));
} }
@Override @Override
public void writeFloat(float f) throws IOException { public void writeFloat(float f) throws IOException {
this.writeInt(Float.floatToIntBits(f)); this.writeInt(Float.floatToIntBits(f));
} }
@Override @Override
public void writeDouble(double d) throws IOException { public void writeDouble(double d) throws IOException {
this.writeLong(Double.doubleToLongBits(d)); this.writeLong(Double.doubleToLongBits(d));
} }
@Override @Override
public void writeBytes(String s) throws IOException { public void writeBytes(String s) throws IOException {
int len = s.length(); int len = s.length();
for(int index = 0; index < len; index++) { for(int index = 0; index < len; index++) {
this.out.write((byte) s.charAt(index)); this.out.write((byte) s.charAt(index));
} }
} }
@Override @Override
public void writeChars(String s) throws IOException { public void writeChars(String s) throws IOException {
int len = s.length(); int len = s.length();
for(int index = 0; index < len; index++) { for(int index = 0; index < len; index++) {
char c = s.charAt(index); char c = s.charAt(index);
this.out.write(c & 0xFF); this.out.write(c & 0xFF);
this.out.write((c >>> 8) & 0xFF); this.out.write((c >>> 8) & 0xFF);
} }
} }
@Override @Override
public void writeUTF(String s) throws IOException { public void writeUTF(String s) throws IOException {
byte[] bytes = s.getBytes("UTF-8"); byte[] bytes = s.getBytes("UTF-8");
this.writeShort(bytes.length); this.writeShort(bytes.length);
this.write(bytes); 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. * An exception thrown when an error occurs while converting something.
*/ */
public class ConversionException extends RuntimeException { public class ConversionException extends RuntimeException {
private static final long serialVersionUID = -2022049594558041160L; private static final long serialVersionUID = -2022049594558041160L;
public ConversionException() { public ConversionException() {
super(); super();
} }
public ConversionException(String message) { public ConversionException(String message) {
super(message); super(message);
} }
public ConversionException(Throwable cause) { public ConversionException(Throwable cause) {
super(cause); super(cause);
} }
public ConversionException(String message, Throwable cause) { public ConversionException(String message, Throwable cause) {
super(message, 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. * An exception thrown when an error occurs while registering a converter.
*/ */
public class ConverterRegisterException extends RuntimeException { public class ConverterRegisterException extends RuntimeException {
private static final long serialVersionUID = -2022049594558041160L; private static final long serialVersionUID = -2022049594558041160L;
public ConverterRegisterException() { public ConverterRegisterException() {
super(); super();
} }
public ConverterRegisterException(String message) { public ConverterRegisterException(String message) {
super(message); super(message);
} }
public ConverterRegisterException(Throwable cause) { public ConverterRegisterException(Throwable cause) {
super(cause); super(cause);
} }
public ConverterRegisterException(String message, Throwable cause) { public ConverterRegisterException(String message, Throwable cause) {
super(message, 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.SerializableTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.ShortArrayTagConverter; import com.github.steveice10.opennbt.conversion.builtin.custom.ShortArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.StringArrayTagConverter; 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.ByteArrayTag;
import com.github.steveice10.opennbt.tag.builtin.ByteTag; import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; 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.SerializableTag;
import com.github.steveice10.opennbt.tag.builtin.custom.ShortArrayTag; import com.github.steveice10.opennbt.tag.builtin.custom.ShortArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.StringArrayTag; import com.github.steveice10.opennbt.tag.builtin.custom.StringArrayTag;
import com.github.steveice10.opennbt.tag.TagRegisterException;
import java.io.Serializable; 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. * A registry mapping tags and value types to converters.
*/ */
public class ConverterRegistry { 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<? 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<?>, TagConverter<? extends Tag, ?>> typeToConverter = new HashMap<Class<?>, TagConverter<? extends Tag, ?>>();
static { static {
register(ByteTag.class, Byte.class, new ByteTagConverter()); register(ByteTag.class, Byte.class, new ByteTagConverter());
register(ShortTag.class, Short.class, new ShortTagConverter()); register(ShortTag.class, Short.class, new ShortTagConverter());
register(IntTag.class, Integer.class, new IntTagConverter()); register(IntTag.class, Integer.class, new IntTagConverter());
register(LongTag.class, Long.class, new LongTagConverter()); register(LongTag.class, Long.class, new LongTagConverter());
register(FloatTag.class, Float.class, new FloatTagConverter()); register(FloatTag.class, Float.class, new FloatTagConverter());
register(DoubleTag.class, Double.class, new DoubleTagConverter()); register(DoubleTag.class, Double.class, new DoubleTagConverter());
register(ByteArrayTag.class, byte[].class, new ByteArrayTagConverter()); register(ByteArrayTag.class, byte[].class, new ByteArrayTagConverter());
register(StringTag.class, String.class, new StringTagConverter()); register(StringTag.class, String.class, new StringTagConverter());
register(ListTag.class, List.class, new ListTagConverter()); register(ListTag.class, List.class, new ListTagConverter());
register(CompoundTag.class, Map.class, new CompoundTagConverter()); register(CompoundTag.class, Map.class, new CompoundTagConverter());
register(IntArrayTag.class, int[].class, new IntArrayTagConverter()); register(IntArrayTag.class, int[].class, new IntArrayTagConverter());
register(DoubleArrayTag.class, double[].class, new DoubleArrayTagConverter()); register(DoubleArrayTag.class, double[].class, new DoubleArrayTagConverter());
register(FloatArrayTag.class, float[].class, new FloatArrayTagConverter()); register(FloatArrayTag.class, float[].class, new FloatArrayTagConverter());
register(LongArrayTag.class, long[].class, new LongArrayTagConverter()); register(LongArrayTag.class, long[].class, new LongArrayTagConverter());
register(SerializableArrayTag.class, Serializable[].class, new SerializableArrayTagConverter()); register(SerializableArrayTag.class, Serializable[].class, new SerializableArrayTagConverter());
register(SerializableTag.class, Serializable.class, new SerializableTagConverter()); register(SerializableTag.class, Serializable.class, new SerializableTagConverter());
register(ShortArrayTag.class, short[].class, new ShortArrayTagConverter()); register(ShortArrayTag.class, short[].class, new ShortArrayTagConverter());
register(StringArrayTag.class, String[].class, new StringArrayTagConverter()); register(StringArrayTag.class, String[].class, new StringArrayTagConverter());
} }
/** /**
* Registers a converter. * Registers a converter.
* *
* @param <T> Tag type to convert from. * @param <T> Tag type to convert from.
* @param <V> Value type to convert to. * @param <V> Value type to convert to.
* @param tag Tag type class to register the converter to. * @param tag Tag type class to register the converter to.
* @param type Value type class to register the converter to. * @param type Value type class to register the converter to.
* @param converter Converter to register. * @param converter Converter to register.
* @throws ConverterRegisterException If an error occurs while registering the converter. * @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 { public static <T extends Tag, V> void register(Class<T> tag, Class<V> type, TagConverter<T, V> converter) throws ConverterRegisterException {
if(tagToConverter.containsKey(tag)) { if(tagToConverter.containsKey(tag)) {
throw new TagRegisterException("Type conversion to tag " + tag.getName() + " is already registered."); throw new TagRegisterException("Type conversion to tag " + tag.getName() + " is already registered.");
} }
if(typeToConverter.containsKey(type)) { if(typeToConverter.containsKey(type)) {
throw new TagRegisterException("Tag conversion to type " + type.getName() + " is already registered."); throw new TagRegisterException("Tag conversion to type " + type.getName() + " is already registered.");
} }
tagToConverter.put(tag, converter); tagToConverter.put(tag, converter);
typeToConverter.put(type, converter); typeToConverter.put(type, converter);
} }
/** /**
* Converts the given tag to a value. * Converts the given tag to a value.
* *
* @param <T> Tag type to convert from. * @param <T> Tag type to convert from.
* @param <V> Value type to convert to. * @param <V> Value type to convert to.
* @param tag Tag to convert. * @param tag Tag to convert.
* @return The converted value. * @return The converted value.
* @throws ConversionException If a suitable converter could not be found. * @throws ConversionException If a suitable converter could not be found.
*/ */
public static <T extends Tag, V> V convertToValue(T tag) throws ConversionException { public static <T extends Tag, V> V convertToValue(T tag) throws ConversionException {
if(tag == null || tag.getValue() == null) { if(tag == null || tag.getValue() == null) {
return null; return null;
} }
if(!tagToConverter.containsKey(tag.getClass())) { if(!tagToConverter.containsKey(tag.getClass())) {
throw new ConversionException("Tag type " + tag.getClass().getName() + " has no converter."); throw new ConversionException("Tag type " + tag.getClass().getName() + " has no converter.");
} }
TagConverter<T, ?> converter = (TagConverter<T, ?>) tagToConverter.get(tag.getClass()); TagConverter<T, ?> converter = (TagConverter<T, ?>) tagToConverter.get(tag.getClass());
return (V) converter.convert(tag); return (V) converter.convert(tag);
} }
/** /**
* Converts the given value to a tag. * Converts the given value to a tag.
* *
* @param <V> Value type to convert from. * @param <V> Value type to convert from.
* @param <T> Tag type to convert to. * @param <T> Tag type to convert to.
* @param name Name of the resulting tag. * @param name Name of the resulting tag.
* @param value Value to convert. * @param value Value to convert.
* @return The converted tag. * @return The converted tag.
* @throws ConversionException If a suitable converter could not be found. * @throws ConversionException If a suitable converter could not be found.
*/ */
public static <V, T extends Tag> T convertToTag(String name, V value) throws ConversionException { public static <V, T extends Tag> T convertToTag(String name, V value) throws ConversionException {
if(value == null) { if(value == null) {
return null; return null;
} }
TagConverter<T, V> converter = (TagConverter<T, V>) typeToConverter.get(value.getClass()); TagConverter<T, V> converter = (TagConverter<T, V>) typeToConverter.get(value.getClass());
if(converter == null) { if(converter == null) {
for(Class<?> clazz : getAllClasses(value.getClass())) { for(Class<?> clazz : getAllClasses(value.getClass())) {
if(typeToConverter.containsKey(clazz)) { if(typeToConverter.containsKey(clazz)) {
try { try {
converter = (TagConverter<T, V>) typeToConverter.get(clazz); converter = (TagConverter<T, V>) typeToConverter.get(clazz);
break; break;
} catch(ClassCastException e) { } catch(ClassCastException e) {
} }
} }
} }
} }
if(converter == null) { if(converter == null) {
throw new ConversionException("Value type " + value.getClass().getName() + " has no converter."); 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) { private static Set<Class<?>> getAllClasses(Class<?> clazz) {
Set<Class<?>> ret = new LinkedHashSet<Class<?>>(); Set<Class<?>> ret = new LinkedHashSet<Class<?>>();
Class<?> c = clazz; Class<?> c = clazz;
while(c != null) { while(c != null) {
ret.add(c); ret.add(c);
ret.addAll(getAllSuperInterfaces(c)); ret.addAll(getAllSuperInterfaces(c));
c = c.getSuperclass(); c = c.getSuperclass();
} }
// Make sure Serializable is at the end to avoid mix-ups. // Make sure Serializable is at the end to avoid mix-ups.
if(ret.contains(Serializable.class)) { if(ret.contains(Serializable.class)) {
ret.remove(Serializable.class); ret.remove(Serializable.class);
ret.add(Serializable.class); ret.add(Serializable.class);
} }
return ret; return ret;
} }
private static Set<Class<?>> getAllSuperInterfaces(Class<?> clazz) { private static Set<Class<?>> getAllSuperInterfaces(Class<?> clazz) {
Set<Class<?>> ret = new HashSet<Class<?>>(); Set<Class<?>> ret = new HashSet<Class<?>>();
for(Class<?> c : clazz.getInterfaces()) { for(Class<?> c : clazz.getInterfaces()) {
ret.add(c); ret.add(c);
ret.addAll(getAllSuperInterfaces(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. * @param <V> Value type.
*/ */
public interface TagConverter<T extends Tag, V> { public interface TagConverter<T extends Tag, V> {
/** /**
* Converts a tag to a value. * Converts a tag to a value.
* *
* @param tag Tag to convert. * @param tag Tag to convert.
* @return The converted value. * @return The converted value.
*/ */
public V convert(T tag); public V convert(T tag);
/** /**
* Converts a value to a tag. * Converts a value to a tag.
* *
* @param name Name of the tag. * @param name Name of the tag.
* @param value Value to convert. * @param value Value to convert.
* @return The converted tag. * @return The converted tag.
*/ */
public T convert(String name, V value); 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[]. * A converter that converts between ByteArrayTag and byte[].
*/ */
public class ByteArrayTagConverter implements TagConverter<ByteArrayTag, byte[]> { public class ByteArrayTagConverter implements TagConverter<ByteArrayTag, byte[]> {
@Override @Override
public byte[] convert(ByteArrayTag tag) { public byte[] convert(ByteArrayTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public ByteArrayTag convert(String name, byte[] value) { public ByteArrayTag convert(String name, byte[] value) {
return new ByteArrayTag(name, 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. * A converter that converts between ByteTag and byte.
*/ */
public class ByteTagConverter implements TagConverter<ByteTag, Byte> { public class ByteTagConverter implements TagConverter<ByteTag, Byte> {
@Override @Override
public Byte convert(ByteTag tag) { public Byte convert(ByteTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public ByteTag convert(String name, Byte value) { public ByteTag convert(String name, Byte value) {
return new ByteTag(name, value); return new ByteTag(name, value);
} }
} }

View File

@ -1,7 +1,7 @@
package com.github.steveice10.opennbt.conversion.builtin; 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.ConverterRegistry;
import com.github.steveice10.opennbt.conversion.TagConverter;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.Tag; import com.github.steveice10.opennbt.tag.builtin.Tag;
@ -12,26 +12,26 @@ import java.util.Map;
* A converter that converts between CompoundTag and Map. * A converter that converts between CompoundTag and Map.
*/ */
public class CompoundTagConverter implements TagConverter<CompoundTag, Map> { public class CompoundTagConverter implements TagConverter<CompoundTag, Map> {
@Override @Override
public Map convert(CompoundTag tag) { public Map convert(CompoundTag tag) {
Map<String, Object> ret = new HashMap<String, Object>(); Map<String, Object> ret = new HashMap<String, Object>();
Map<String, Tag> tags = tag.getValue(); Map<String, Tag> tags = tag.getValue();
for(String name : tags.keySet()) { for(String name : tags.keySet()) {
Tag t = tags.get(name); Tag t = tags.get(name);
ret.put(t.getName(), ConverterRegistry.convertToValue(t)); ret.put(t.getName(), ConverterRegistry.convertToValue(t));
} }
return ret; return ret;
} }
@Override @Override
public CompoundTag convert(String name, Map value) { public CompoundTag convert(String name, Map value) {
Map<String, Tag> tags = new HashMap<String, Tag>(); Map<String, Tag> tags = new HashMap<String, Tag>();
for(Object na : value.keySet()) { for(Object na : value.keySet()) {
String n = (String) na; String n = (String) na;
tags.put(n, ConverterRegistry.convertToTag(n, value.get(n))); 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. * A converter that converts between DoubleTag and double.
*/ */
public class DoubleTagConverter implements TagConverter<DoubleTag, Double> { public class DoubleTagConverter implements TagConverter<DoubleTag, Double> {
@Override @Override
public Double convert(DoubleTag tag) { public Double convert(DoubleTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public DoubleTag convert(String name, Double value) { public DoubleTag convert(String name, Double value) {
return new DoubleTag(name, 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. * A converter that converts between FloatTag and float.
*/ */
public class FloatTagConverter implements TagConverter<FloatTag, Float> { public class FloatTagConverter implements TagConverter<FloatTag, Float> {
@Override @Override
public Float convert(FloatTag tag) { public Float convert(FloatTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public FloatTag convert(String name, Float value) { public FloatTag convert(String name, Float value) {
return new FloatTag(name, 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[]. * A converter that converts between IntArrayTag and int[].
*/ */
public class IntArrayTagConverter implements TagConverter<IntArrayTag, int[]> { public class IntArrayTagConverter implements TagConverter<IntArrayTag, int[]> {
@Override @Override
public int[] convert(IntArrayTag tag) { public int[] convert(IntArrayTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public IntArrayTag convert(String name, int[] value) { public IntArrayTag convert(String name, int[] value) {
return new IntArrayTag(name, 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. * A converter that converts between IntTag and int.
*/ */
public class IntTagConverter implements TagConverter<IntTag, Integer> { public class IntTagConverter implements TagConverter<IntTag, Integer> {
@Override @Override
public Integer convert(IntTag tag) { public Integer convert(IntTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public IntTag convert(String name, Integer value) { public IntTag convert(String name, Integer value) {
return new IntTag(name, value); return new IntTag(name, value);
} }
} }

View File

@ -1,7 +1,7 @@
package com.github.steveice10.opennbt.conversion.builtin; 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.ConverterRegistry;
import com.github.steveice10.opennbt.conversion.TagConverter;
import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.Tag; import com.github.steveice10.opennbt.tag.builtin.Tag;
@ -12,28 +12,28 @@ import java.util.List;
* A converter that converts between CompoundTag and Map. * A converter that converts between CompoundTag and Map.
*/ */
public class ListTagConverter implements TagConverter<ListTag, List> { public class ListTagConverter implements TagConverter<ListTag, List> {
@Override @Override
public List convert(ListTag tag) { public List convert(ListTag tag) {
List<Object> ret = new ArrayList<Object>(); List<Object> ret = new ArrayList<Object>();
List<? extends Tag> tags = tag.getValue(); List<? extends Tag> tags = tag.getValue();
for(Tag t : tags) { for(Tag t : tags) {
ret.add(ConverterRegistry.convertToValue(t)); ret.add(ConverterRegistry.convertToValue(t));
} }
return ret; return ret;
} }
@Override @Override
public ListTag convert(String name, List value) { public ListTag convert(String name, List value) {
if(value.isEmpty()) { if(value.isEmpty()) {
throw new IllegalArgumentException("Cannot convert ListTag with size of 0."); throw new IllegalArgumentException("Cannot convert ListTag with size of 0.");
} }
List<Tag> tags = new ArrayList<Tag>(); List<Tag> tags = new ArrayList<Tag>();
for(Object o : value) { for(Object o : value) {
tags.add(ConverterRegistry.convertToTag("", o)); 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. * A converter that converts between LongTag and long.
*/ */
public class LongTagConverter implements TagConverter<LongTag, Long> { public class LongTagConverter implements TagConverter<LongTag, Long> {
@Override @Override
public Long convert(LongTag tag) { public Long convert(LongTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public LongTag convert(String name, Long value) { public LongTag convert(String name, Long value) {
return new LongTag(name, 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. * A converter that converts between ShortTag and short.
*/ */
public class ShortTagConverter implements TagConverter<ShortTag, Short> { public class ShortTagConverter implements TagConverter<ShortTag, Short> {
@Override @Override
public Short convert(ShortTag tag) { public Short convert(ShortTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public ShortTag convert(String name, Short value) { public ShortTag convert(String name, Short value) {
return new ShortTag(name, 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. * A converter that converts between StringTag and String.
*/ */
public class StringTagConverter implements TagConverter<StringTag, String> { public class StringTagConverter implements TagConverter<StringTag, String> {
@Override @Override
public String convert(StringTag tag) { public String convert(StringTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public StringTag convert(String name, String value) { public StringTag convert(String name, String value) {
return new StringTag(name, 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[]. * A converter that converts between DoubleArrayTag and double[].
*/ */
public class DoubleArrayTagConverter implements TagConverter<DoubleArrayTag, double[]> { public class DoubleArrayTagConverter implements TagConverter<DoubleArrayTag, double[]> {
@Override @Override
public double[] convert(DoubleArrayTag tag) { public double[] convert(DoubleArrayTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public DoubleArrayTag convert(String name, double[] value) { public DoubleArrayTag convert(String name, double[] value) {
return new DoubleArrayTag(name, 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[]. * A converter that converts between FloatArrayTag and float[].
*/ */
public class FloatArrayTagConverter implements TagConverter<FloatArrayTag, float[]> { public class FloatArrayTagConverter implements TagConverter<FloatArrayTag, float[]> {
@Override @Override
public float[] convert(FloatArrayTag tag) { public float[] convert(FloatArrayTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public FloatArrayTag convert(String name, float[] value) { public FloatArrayTag convert(String name, float[] value) {
return new FloatArrayTag(name, 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[]. * A converter that converts between LongArrayTag and long[].
*/ */
public class LongArrayTagConverter implements TagConverter<LongArrayTag, long[]> { public class LongArrayTagConverter implements TagConverter<LongArrayTag, long[]> {
@Override @Override
public long[] convert(LongArrayTag tag) { public long[] convert(LongArrayTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public LongArrayTag convert(String name, long[] value) { public LongArrayTag convert(String name, long[] value) {
return new LongArrayTag(name, value); return new LongArrayTag(name, value);
} }
} }

View File

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

View File

@ -9,13 +9,13 @@ import java.io.Serializable;
* A converter that converts between SerializableTag and Serializable. * A converter that converts between SerializableTag and Serializable.
*/ */
public class SerializableTagConverter implements TagConverter<SerializableTag, Serializable> { public class SerializableTagConverter implements TagConverter<SerializableTag, Serializable> {
@Override @Override
public Serializable convert(SerializableTag tag) { public Serializable convert(SerializableTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public SerializableTag convert(String name, Serializable value) { public SerializableTag convert(String name, Serializable value) {
return new SerializableTag(name, 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[]. * A converter that converts between ShortArrayTag and short[].
*/ */
public class ShortArrayTagConverter implements TagConverter<ShortArrayTag, short[]> { public class ShortArrayTagConverter implements TagConverter<ShortArrayTag, short[]> {
@Override @Override
public short[] convert(ShortArrayTag tag) { public short[] convert(ShortArrayTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public ShortArrayTag convert(String name, short[] value) { public ShortArrayTag convert(String name, short[] value) {
return new ShortArrayTag(name, 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[]. * A converter that converts between StringArrayTag and String[].
*/ */
public class StringArrayTagConverter implements TagConverter<StringArrayTag, String[]> { public class StringArrayTagConverter implements TagConverter<StringArrayTag, String[]> {
@Override @Override
public String[] convert(StringArrayTag tag) { public String[] convert(StringArrayTag tag) {
return tag.getValue(); return tag.getValue();
} }
@Override @Override
public StringArrayTag convert(String name, String[] value) { public StringArrayTag convert(String name, String[] value) {
return new StringArrayTag(name, 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. * An exception thrown when an error occurs while created a tag instance.
*/ */
public class TagCreateException extends Exception { public class TagCreateException extends Exception {
private static final long serialVersionUID = -2022049594558041160L; private static final long serialVersionUID = -2022049594558041160L;
public TagCreateException() { public TagCreateException() {
super(); super();
} }
public TagCreateException(String message) { public TagCreateException(String message) {
super(message); super(message);
} }
public TagCreateException(Throwable cause) { public TagCreateException(Throwable cause) {
super(cause); super(cause);
} }
public TagCreateException(String message, Throwable cause) { public TagCreateException(String message, Throwable cause) {
super(message, 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. * An exception thrown when an error occurs while registering a tag.
*/ */
public class TagRegisterException extends RuntimeException { public class TagRegisterException extends RuntimeException {
private static final long serialVersionUID = -2022049594558041160L; private static final long serialVersionUID = -2022049594558041160L;
public TagRegisterException() { public TagRegisterException() {
super(); super();
} }
public TagRegisterException(String message) { public TagRegisterException(String message) {
super(message); super(message);
} }
public TagRegisterException(Throwable cause) { public TagRegisterException(Throwable cause) {
super(cause); super(cause);
} }
public TagRegisterException(String message, Throwable cause) { public TagRegisterException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
} }

View File

@ -28,99 +28,99 @@ import java.util.Map;
* A registry containing different tag classes. * A registry containing different tag classes.
*/ */
public class TagRegistry { public class TagRegistry {
private static final Map<Integer, Class<? extends Tag>> idToTag = new HashMap<Integer, Class<? extends Tag>>(); 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<Class<? extends Tag>, Integer> tagToId = new HashMap<Class<? extends Tag>, Integer>();
static { static {
register(1, ByteTag.class); register(1, ByteTag.class);
register(2, ShortTag.class); register(2, ShortTag.class);
register(3, IntTag.class); register(3, IntTag.class);
register(4, LongTag.class); register(4, LongTag.class);
register(5, FloatTag.class); register(5, FloatTag.class);
register(6, DoubleTag.class); register(6, DoubleTag.class);
register(7, ByteArrayTag.class); register(7, ByteArrayTag.class);
register(8, StringTag.class); register(8, StringTag.class);
register(9, ListTag.class); register(9, ListTag.class);
register(10, CompoundTag.class); register(10, CompoundTag.class);
register(11, IntArrayTag.class); register(11, IntArrayTag.class);
register(60, DoubleArrayTag.class); register(60, DoubleArrayTag.class);
register(61, FloatArrayTag.class); register(61, FloatArrayTag.class);
register(62, LongArrayTag.class); register(62, LongArrayTag.class);
register(63, SerializableArrayTag.class); register(63, SerializableArrayTag.class);
register(64, SerializableTag.class); register(64, SerializableTag.class);
register(65, ShortArrayTag.class); register(65, ShortArrayTag.class);
register(66, StringArrayTag.class); register(66, StringArrayTag.class);
} }
/** /**
* Registers a tag class. * Registers a tag class.
* *
* @param id ID of the tag. * @param id ID of the tag.
* @param tag Tag class to register. * @param tag Tag class to register.
* @throws TagRegisterException If an error occurs while registering the tag. * @throws TagRegisterException If an error occurs while registering the tag.
*/ */
public static void register(int id, Class<? extends Tag> tag) throws TagRegisterException { public static void register(int id, Class<? extends Tag> tag) throws TagRegisterException {
if(idToTag.containsKey(id)) { if(idToTag.containsKey(id)) {
throw new TagRegisterException("Tag ID \"" + id + "\" is already in use."); throw new TagRegisterException("Tag ID \"" + id + "\" is already in use.");
} }
if(tagToId.containsKey(tag)) { if(tagToId.containsKey(tag)) {
throw new TagRegisterException("Tag \"" + tag.getSimpleName() + "\" is already registered."); throw new TagRegisterException("Tag \"" + tag.getSimpleName() + "\" is already registered.");
} }
idToTag.put(id, tag); idToTag.put(id, tag);
tagToId.put(tag, id); tagToId.put(tag, id);
} }
/** /**
* Gets the tag class with the given id. * Gets the tag class with the given id.
* *
* @param id Id of the tag. * @param id Id of the tag.
* @return The tag class with the given id, or null if it cannot be found. * @return The tag class with the given id, or null if it cannot be found.
*/ */
public static Class<? extends Tag> getClassFor(int id) { public static Class<? extends Tag> getClassFor(int id) {
if(!idToTag.containsKey(id)) { if(!idToTag.containsKey(id)) {
return null; return null;
} }
return idToTag.get(id); return idToTag.get(id);
} }
/** /**
* Gets the id of the given tag class. * Gets the id of the given tag class.
* *
* @param clazz The tag class to get the id of. * @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. * @return The id of the given tag class, or -1 if it cannot be found.
*/ */
public static int getIdFor(Class<? extends Tag> clazz) { public static int getIdFor(Class<? extends Tag> clazz) {
if(!tagToId.containsKey(clazz)) { if(!tagToId.containsKey(clazz)) {
return -1; return -1;
} }
return tagToId.get(clazz); return tagToId.get(clazz);
} }
/** /**
* Creates an instance of the tag with the given id, using the String constructor. * Creates an instance of the tag with the given id, using the String constructor.
* *
* @param id Id of the tag. * @param id Id of the tag.
* @param tagName Name to give the tag. * @param tagName Name to give the tag.
* @return The created tag. * @return The created tag.
* @throws TagCreateException If an error occurs while creating the tag. * @throws TagCreateException If an error occurs while creating the tag.
*/ */
public static Tag createInstance(int id, String tagName) throws TagCreateException { public static Tag createInstance(int id, String tagName) throws TagCreateException {
Class<? extends Tag> clazz = idToTag.get(id); Class<? extends Tag> clazz = idToTag.get(id);
if(clazz == null) { if(clazz == null) {
throw new TagCreateException("Could not find tag with ID \"" + id + "\"."); throw new TagCreateException("Could not find tag with ID \"" + id + "\".");
} }
try { try {
Constructor<? extends Tag> constructor = clazz.getDeclaredConstructor(String.class); Constructor<? extends Tag> constructor = clazz.getDeclaredConstructor(String.class);
constructor.setAccessible(true); constructor.setAccessible(true);
return constructor.newInstance(tagName); return constructor.newInstance(tagName);
} catch(Exception e) { } catch(Exception e) {
throw new TagCreateException("Failed to create instance of tag \"" + clazz.getSimpleName() + "\".", 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. * A tag containing a byte array.
*/ */
public class ByteArrayTag extends Tag { public class ByteArrayTag extends Tag {
private byte[] value; private byte[] value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* *
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public ByteArrayTag(String name) { public ByteArrayTag(String name) {
this(name, new byte[0]); this(name, new byte[0]);
} }
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* *
* @param name The name of the tag. * @param name The name of the tag.
* @param value The value of the tag. * @param value The value of the tag.
*/ */
public ByteArrayTag(String name, byte[] value) { public ByteArrayTag(String name, byte[] value) {
super(name); super(name);
this.value = value; this.value = value;
} }
@Override @Override
public byte[] getValue() { public byte[] getValue() {
return this.value.clone(); return this.value.clone();
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
* *
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(byte[] value) { public void setValue(byte[] value) {
if(value == null) { if(value == null) {
return; return;
} }
this.value = value.clone(); this.value = value.clone();
} }
/** /**
* Gets a value in this tag's array. * Gets a value in this tag's array.
* *
* @param index Index of the value. * @param index Index of the value.
* @return The value at the given index. * @return The value at the given index.
*/ */
public byte getValue(int index) { public byte getValue(int index) {
return this.value[index]; return this.value[index];
} }
/** /**
* Sets a value in this tag's array. * Sets a value in this tag's array.
* *
* @param index Index of the value. * @param index Index of the value.
* @param value Value to set. * @param value Value to set.
*/ */
public void setValue(int index, byte value) { public void setValue(int index, byte value) {
this.value[index] = value; this.value[index] = value;
} }
/** /**
* Gets the length of this tag's array. * Gets the length of this tag's array.
* *
* @return This tag's array length. * @return This tag's array length.
*/ */
public int length() { public int length() {
return this.value.length; return this.value.length;
} }
@Override @Override
public void read(DataInput in) throws IOException { public void read(DataInput in) throws IOException {
this.value = new byte[in.readInt()]; this.value = new byte[in.readInt()];
in.readFully(this.value); in.readFully(this.value);
} }
@Override @Override
public void write(DataOutput out) throws IOException { public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length); out.writeInt(this.value.length);
out.write(this.value); out.write(this.value);
} }
@Override @Override
public ByteArrayTag clone() { public ByteArrayTag clone() {
return new ByteArrayTag(this.getName(), this.getValue()); return new ByteArrayTag(this.getName(), this.getValue());
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -10,93 +10,93 @@ import java.io.IOException;
* A tag containing a long array. * A tag containing a long array.
*/ */
public class LongArrayTag extends Tag { public class LongArrayTag extends Tag {
private long[] value; private long[] value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* *
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public LongArrayTag(String name) { public LongArrayTag(String name) {
this(name, new long[0]); this(name, new long[0]);
} }
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* *
* @param name The name of the tag. * @param name The name of the tag.
* @param value The value of the tag. * @param value The value of the tag.
*/ */
public LongArrayTag(String name, long[] value) { public LongArrayTag(String name, long[] value) {
super(name); super(name);
this.value = value; this.value = value;
} }
@Override @Override
public long[] getValue() { public long[] getValue() {
return this.value.clone(); return this.value.clone();
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
* *
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(long[] value) { public void setValue(long[] value) {
if(value == null) { if(value == null) {
return; return;
} }
this.value = value.clone(); this.value = value.clone();
} }
/** /**
* Gets a value in this tag's array. * Gets a value in this tag's array.
* *
* @param index Index of the value. * @param index Index of the value.
* @return The value at the given index. * @return The value at the given index.
*/ */
public long getValue(int index) { public long getValue(int index) {
return this.value[index]; return this.value[index];
} }
/** /**
* Sets a value in this tag's array. * Sets a value in this tag's array.
* *
* @param index Index of the value. * @param index Index of the value.
* @param value Value to set. * @param value Value to set.
*/ */
public void setValue(int index, long value) { public void setValue(int index, long value) {
this.value[index] = value; this.value[index] = value;
} }
/** /**
* Gets the length of this tag's array. * Gets the length of this tag's array.
* *
* @return This tag's array length. * @return This tag's array length.
*/ */
public int length() { public int length() {
return this.value.length; return this.value.length;
} }
@Override @Override
public void read(DataInput in) throws IOException { public void read(DataInput in) throws IOException {
this.value = new long[in.readInt()]; this.value = new long[in.readInt()];
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readLong(); this.value[index] = in.readLong();
} }
} }
@Override @Override
public void write(DataOutput out) throws IOException { public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length); out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
out.writeLong(this.value[index]); out.writeLong(this.value[index]);
} }
} }
@Override @Override
public LongArrayTag clone() { public LongArrayTag clone() {
return new LongArrayTag(this.getName(), this.getValue()); 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 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. * A tag containing an array of serializable objects.
*/ */
public class SerializableArrayTag extends Tag { public class SerializableArrayTag extends Tag {
private Serializable[] value; private Serializable[] value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* *
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public SerializableArrayTag(String name) { public SerializableArrayTag(String name) {
this(name, new Serializable[0]); this(name, new Serializable[0]);
} }
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* *
* @param name The name of the tag. * @param name The name of the tag.
* @param value The value of the tag. * @param value The value of the tag.
*/ */
public SerializableArrayTag(String name, Serializable[] value) { public SerializableArrayTag(String name, Serializable[] value) {
super(name); super(name);
this.value = value; this.value = value;
} }
@Override @Override
public Serializable[] getValue() { public Serializable[] getValue() {
return this.value.clone(); return this.value.clone();
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
* *
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(Serializable[] value) { public void setValue(Serializable[] value) {
if(value == null) { if(value == null) {
return; return;
} }
this.value = value.clone(); this.value = value.clone();
} }
/** /**
* Gets a value in this tag's array. * Gets a value in this tag's array.
* *
* @param index Index of the value. * @param index Index of the value.
* @return The value at the given index. * @return The value at the given index.
*/ */
public Serializable getValue(int index) { public Serializable getValue(int index) {
return this.value[index]; return this.value[index];
} }
/** /**
* Sets a value in this tag's array. * Sets a value in this tag's array.
* *
* @param index Index of the value. * @param index Index of the value.
* @param value Value to set. * @param value Value to set.
*/ */
public void setValue(int index, Serializable value) { public void setValue(int index, Serializable value) {
this.value[index] = value; this.value[index] = value;
} }
/** /**
* Gets the length of this tag's array. * Gets the length of this tag's array.
* *
* @return This tag's array length. * @return This tag's array length.
*/ */
public int length() { public int length() {
return this.value.length; return this.value.length;
} }
@Override @Override
public void read(DataInput in) throws IOException { public void read(DataInput in) throws IOException {
this.value = new Serializable[in.readInt()]; this.value = new Serializable[in.readInt()];
ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in)); ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in));
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
try { try {
this.value[index] = (Serializable) str.readObject(); this.value[index] = (Serializable) str.readObject();
} catch(ClassNotFoundException e) { } catch(ClassNotFoundException e) {
throw new IOException("Class not found while reading SerializableArrayTag!", e); throw new IOException("Class not found while reading SerializableArrayTag!", e);
} }
} }
} }
@Override @Override
public void write(DataOutput out) throws IOException { public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length); out.writeInt(this.value.length);
ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out)); ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out));
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
str.writeObject(this.value[index]); str.writeObject(this.value[index]);
} }
} }
@Override @Override
public SerializableArrayTag clone() { public SerializableArrayTag clone() {
return new SerializableArrayTag(this.getName(), this.getValue()); return new SerializableArrayTag(this.getName(), this.getValue());
} }
private static class DataInputInputStream extends InputStream { private static class DataInputInputStream extends InputStream {
private DataInput in; 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 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. * A tag containing a serializable object.
*/ */
public class SerializableTag extends Tag { public class SerializableTag extends Tag {
private Serializable value; private Serializable value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* *
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public SerializableTag(String name) { public SerializableTag(String name) {
this(name, 0); this(name, 0);
} }
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* *
* @param name The name of the tag. * @param name The name of the tag.
* @param value The value of the tag. * @param value The value of the tag.
*/ */
public SerializableTag(String name, Serializable value) { public SerializableTag(String name, Serializable value) {
super(name); super(name);
this.value = value; this.value = value;
} }
@Override @Override
public Serializable getValue() { public Serializable getValue() {
return this.value; return this.value;
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
* *
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(Serializable value) { public void setValue(Serializable value) {
this.value = value; this.value = value;
} }
@Override @Override
public void read(DataInput in) throws IOException { public void read(DataInput in) throws IOException {
ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in)); ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in));
try { try {
this.value = (Serializable) str.readObject(); this.value = (Serializable) str.readObject();
} catch(ClassNotFoundException e) { } catch(ClassNotFoundException e) {
throw new IOException("Class not found while reading SerializableTag!", e); throw new IOException("Class not found while reading SerializableTag!", e);
} }
} }
@Override @Override
public void write(DataOutput out) throws IOException { public void write(DataOutput out) throws IOException {
ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out)); ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out));
str.writeObject(this.value); str.writeObject(this.value);
} }
@Override @Override
public SerializableTag clone() { public SerializableTag clone() {
return new SerializableTag(this.getName(), this.getValue()); return new SerializableTag(this.getName(), this.getValue());
} }
private static class DataInputInputStream extends InputStream { private static class DataInputInputStream extends InputStream {
private DataInput in; private DataInput in;
public DataInputInputStream(DataInput in) { public DataInputInputStream(DataInput in) {
this.in = in; this.in = in;
} }
@Override @Override
public int read() throws IOException { public int read() throws IOException {
return this.in.readUnsignedByte(); return this.in.readUnsignedByte();
} }
@Override @Override
public int read(byte[] b) throws IOException { public int read(byte[] b) throws IOException {
this.in.readFully(b); this.in.readFully(b);
return b.length; return b.length;
} }
@Override @Override
public int read(byte[] b, int off, int len) throws IOException { public int read(byte[] b, int off, int len) throws IOException {
this.in.readFully(b, off, len); this.in.readFully(b, off, len);
return len; return len;
} }
@Override @Override
public long skip(long l) throws IOException { public long skip(long l) throws IOException {
return this.in.skipBytes((int) l); return this.in.skipBytes((int) l);
} }
@Override @Override
public int available() throws IOException { public int available() throws IOException {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override @Override
public void close() throws IOException { public void close() throws IOException {
} }
@Override @Override
public synchronized void mark(int i) { public synchronized void mark(int i) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override @Override
public synchronized void reset() throws IOException { public synchronized void reset() throws IOException {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override @Override
public boolean markSupported() { public boolean markSupported() {
return false; return false;
} }
} }
private static class DataOutputOutputStream extends OutputStream { private static class DataOutputOutputStream extends OutputStream {
private DataOutput out; private DataOutput out;
public DataOutputOutputStream(DataOutput out) { public DataOutputOutputStream(DataOutput out) {
this.out = out; this.out = out;
} }
@Override @Override
public void write(int b) throws IOException { public void write(int b) throws IOException {
this.out.write(b); this.out.write(b);
} }
@Override @Override
public void write(byte[] b) throws IOException { public void write(byte[] b) throws IOException {
this.out.write(b); this.out.write(b);
} }
@Override @Override
public void write(byte[] b, int off, int len) throws IOException { public void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len); this.out.write(b, off, len);
} }
@Override @Override
public void flush() throws IOException { public void flush() throws IOException {
} }
@Override @Override
public void close() throws IOException { public void close() throws IOException {
} }
} }
} }

View File

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

View File

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