mirror of
https://github.com/ViaVersion/ViaNBT.git
synced 2025-02-18 01:31:30 +01:00
Rewrote library to be a bit more flexible
This commit is contained in:
parent
2e7ecec145
commit
0353d28c60
2
pom.xml
2
pom.xml
@ -2,7 +2,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>ch.spacebase</groupId>
|
<groupId>ch.spacebase</groupId>
|
||||||
<artifactId>opennbt</artifactId>
|
<artifactId>opennbt</artifactId>
|
||||||
<version>1.1-SNAPSHOT</version>
|
<version>1.1</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>OpenNBT</name>
|
<name>OpenNBT</name>
|
||||||
|
@ -1,80 +0,0 @@
|
|||||||
package ch.spacebase.opennbt;
|
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the OpenNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class which holds constant values.
|
|
||||||
*/
|
|
||||||
public final class NBTConstants {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The character set used by NBT (UTF-8).
|
|
||||||
*/
|
|
||||||
public static final Charset CHARSET = Charset.forName("UTF-8");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tag type constants.
|
|
||||||
*/
|
|
||||||
public static final int TYPE_END = 0,
|
|
||||||
TYPE_BYTE = 1,
|
|
||||||
TYPE_SHORT = 2,
|
|
||||||
TYPE_INT = 3,
|
|
||||||
TYPE_LONG = 4,
|
|
||||||
TYPE_FLOAT = 5,
|
|
||||||
TYPE_DOUBLE = 6,
|
|
||||||
TYPE_BYTE_ARRAY = 7,
|
|
||||||
TYPE_STRING = 8,
|
|
||||||
TYPE_LIST = 9,
|
|
||||||
TYPE_COMPOUND = 10,
|
|
||||||
TYPE_INT_ARRAY = 11,
|
|
||||||
TYPE_DOUBLE_ARRAY = 60,
|
|
||||||
TYPE_FLOAT_ARRAY = 61,
|
|
||||||
TYPE_LONG_ARRAY = 62,
|
|
||||||
TYPE_OBJECT_ARRAY = 63,
|
|
||||||
TYPE_OBJECT = 64,
|
|
||||||
TYPE_SHORT_ARRAY = 65,
|
|
||||||
TYPE_STRING_ARRAY = 66,
|
|
||||||
TYPE_UNKNOWN = 67;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default private constructor.
|
|
||||||
*/
|
|
||||||
private NBTConstants() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
139
src/main/java/ch/spacebase/opennbt/NBTFileIO.java
Normal file
139
src/main/java/ch/spacebase/opennbt/NBTFileIO.java
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
package ch.spacebase.opennbt;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.zip.GZIPInputStream;
|
||||||
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
|
import ch.spacebase.opennbt.tag.CompoundTag;
|
||||||
|
import ch.spacebase.opennbt.tag.Tag;
|
||||||
|
|
||||||
|
public class NBTFileIO {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the root CompoundTag from the given file.
|
||||||
|
* @param path Path of the file.
|
||||||
|
* @return The read compound tag.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static CompoundTag readFile(String path) throws IOException {
|
||||||
|
return readFile(new File(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the root CompoundTag from the given file.
|
||||||
|
* @param file File to read from.
|
||||||
|
* @return The read compound tag.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static CompoundTag readFile(File file) throws IOException {
|
||||||
|
return readFile(file, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the root CompoundTag from the given file.
|
||||||
|
* @param path Path of the file.
|
||||||
|
* @param compressed Whether the NBT file is compressed.
|
||||||
|
* @return The read compound tag.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static CompoundTag readFile(String path, boolean compressed) throws IOException {
|
||||||
|
return readFile(new File(path), compressed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the root CompoundTag from the given file.
|
||||||
|
* @param file File to read from.
|
||||||
|
* @param compressed Whether the NBT file is compressed.
|
||||||
|
* @return The read compound tag.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static CompoundTag readFile(File file, boolean compressed) throws IOException {
|
||||||
|
try {
|
||||||
|
TagRegistry.registerDefaultTags();
|
||||||
|
} catch(TagRegisterException e) {
|
||||||
|
throw new IOException("Failed to register default tags.", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream in = new FileInputStream(file);
|
||||||
|
if(compressed) {
|
||||||
|
in = new GZIPInputStream(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
Tag tag = NBTIO.readTag(new DataInputStream(in));
|
||||||
|
if(!(tag instanceof CompoundTag)) {
|
||||||
|
throw new IOException("Root tag is not a CompoundTag!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return (CompoundTag) tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the given root CompoundTag to the given file.
|
||||||
|
* @param tag Tag to write.
|
||||||
|
* @param path Path to write to.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static void writeFile(CompoundTag tag, String path) throws IOException {
|
||||||
|
writeFile(tag, new File(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the given root CompoundTag to the given file.
|
||||||
|
* @param tag Tag to write.
|
||||||
|
* @param file File to write to.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static void writeFile(CompoundTag tag, File file) throws IOException {
|
||||||
|
writeFile(tag, file, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the given root CompoundTag to the given file.
|
||||||
|
* @param tag Tag to write.
|
||||||
|
* @param path Path to write to.
|
||||||
|
* @param compressed Whether the NBT file should be compressed.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static void writeFile(CompoundTag tag, String path, boolean compressed) throws IOException {
|
||||||
|
writeFile(tag, new File(path), compressed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the given root CompoundTag to the given file.
|
||||||
|
* @param tag Tag to write.
|
||||||
|
* @param file File to write to.
|
||||||
|
* @param compressed Whether the NBT file should be compressed.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static void writeFile(CompoundTag tag, File file, boolean compressed) throws IOException {
|
||||||
|
try {
|
||||||
|
TagRegistry.registerDefaultTags();
|
||||||
|
} catch(TagRegisterException e) {
|
||||||
|
throw new IOException("Failed to register default tags.", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!file.exists()) {
|
||||||
|
if(file.getParentFile() != null && !file.getParentFile().exists()) {
|
||||||
|
file.getParentFile().mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
file.createNewFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
OutputStream out = new FileOutputStream(file);
|
||||||
|
if(compressed) {
|
||||||
|
out = new GZIPOutputStream(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
NBTIO.writeTag(new DataOutputStream(out), tag);
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
91
src/main/java/ch/spacebase/opennbt/NBTIO.java
Normal file
91
src/main/java/ch/spacebase/opennbt/NBTIO.java
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
package ch.spacebase.opennbt;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.EOFException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import ch.spacebase.opennbt.tag.Tag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class containing methods for reading/writing NBT tags.
|
||||||
|
*/
|
||||||
|
public class NBTIO {
|
||||||
|
|
||||||
|
public static final Charset CHARSET = Charset.forName("UTF-8");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads NBT tags until an end tag is reached.
|
||||||
|
* @param in Input stream to read from.
|
||||||
|
* @return The read tags.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static List<Tag> readUntilEndTag(DataInputStream in) throws IOException {
|
||||||
|
List<Tag> ret = new ArrayList<Tag>();
|
||||||
|
try {
|
||||||
|
Tag tag;
|
||||||
|
while((tag = readTag(in)) != null) {
|
||||||
|
ret.add(tag);
|
||||||
|
}
|
||||||
|
} catch(EOFException e) {
|
||||||
|
throw new IOException("Closing EndTag was not found!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads an NBT tag.
|
||||||
|
* @param in Input stream to read from.
|
||||||
|
* @return The read tag, or null if the tag is an end tag.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static Tag readTag(DataInputStream in) throws IOException {
|
||||||
|
int id = in.readByte() & 0xFF;
|
||||||
|
if(id == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] nameBytes = new byte[in.readShort() & 0xFFFF];
|
||||||
|
in.readFully(nameBytes);
|
||||||
|
String name = new String(nameBytes, CHARSET);
|
||||||
|
Tag tag = TagRegistry.createInstance(id, name);
|
||||||
|
if(tag == null) {
|
||||||
|
throw new IOException("Invalid tag: " + id);
|
||||||
|
}
|
||||||
|
|
||||||
|
tag.read(in);
|
||||||
|
return tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a collection of tags to an output stream.
|
||||||
|
* @param out Output stream to write to.
|
||||||
|
* @param tags Tags to write.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static void writeTags(DataOutputStream out, Collection<Tag> tags) throws IOException {
|
||||||
|
for(Tag tag : tags) {
|
||||||
|
writeTag(out, tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a tag to an output stream.
|
||||||
|
* @param out Output stream to write to.
|
||||||
|
* @param tag Tag to write.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public static void writeTag(DataOutputStream out, Tag tag) throws IOException {
|
||||||
|
byte[] nameBytes = tag.getName().getBytes(CHARSET);
|
||||||
|
out.writeByte(tag.getId());
|
||||||
|
out.writeShort(nameBytes.length);
|
||||||
|
out.write(nameBytes);
|
||||||
|
tag.write(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,122 +0,0 @@
|
|||||||
package ch.spacebase.opennbt;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import ch.spacebase.opennbt.stream.NBTInputStream;
|
|
||||||
import ch.spacebase.opennbt.stream.NBTOutputStream;
|
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A utility for reading and writing NBTs to/from files.
|
|
||||||
*/
|
|
||||||
public class NBTIOUtils {
|
|
||||||
|
|
||||||
public static List<Tag> loadNBT(File file) {
|
|
||||||
List<Tag> result = new ArrayList<Tag>();
|
|
||||||
|
|
||||||
NBTInputStream input = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
input = new NBTInputStream(new FileInputStream(file));
|
|
||||||
|
|
||||||
Tag next = null;
|
|
||||||
|
|
||||||
while((next = input.readTag()) != null) {
|
|
||||||
result.add(next);
|
|
||||||
}
|
|
||||||
} catch(IOException ioe) {
|
|
||||||
System.out.println("Failed to create NBTInputStream from file " + file.getName() + ".");
|
|
||||||
return null;
|
|
||||||
} finally {
|
|
||||||
if(input != null) {
|
|
||||||
try {
|
|
||||||
input.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("Failed to close NBTInputStream.");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void writeNBT(File file, List<Tag> tags) {
|
|
||||||
NBTOutputStream output = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
output = new NBTOutputStream(new FileOutputStream(file));
|
|
||||||
|
|
||||||
for(Tag tag : tags) {
|
|
||||||
output.writeTag(tag);
|
|
||||||
}
|
|
||||||
} catch(IOException ioe) {
|
|
||||||
System.out.println("Failed to create NBTOutputStream from file " + file.getName() + ".");
|
|
||||||
return;
|
|
||||||
} finally {
|
|
||||||
if(output != null) {
|
|
||||||
try {
|
|
||||||
output.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("Failed to close NBTOutputStream.");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Tag[] loadNBTArray(File file) {
|
|
||||||
List<Tag> tags = loadNBT(file);
|
|
||||||
|
|
||||||
if(tags != null) {
|
|
||||||
return tags.toArray(new Tag[tags.size()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void writeNBTArray(File file, Tag[] tags) {
|
|
||||||
writeNBT(file, Arrays.asList(tags));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,275 +0,0 @@
|
|||||||
package ch.spacebase.opennbt;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import ch.spacebase.opennbt.exception.InvalidNBTException;
|
|
||||||
import ch.spacebase.opennbt.tag.ByteArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.ByteTag;
|
|
||||||
import ch.spacebase.opennbt.tag.CompoundTag;
|
|
||||||
import ch.spacebase.opennbt.tag.DoubleTag;
|
|
||||||
import ch.spacebase.opennbt.tag.EndTag;
|
|
||||||
import ch.spacebase.opennbt.tag.FloatTag;
|
|
||||||
import ch.spacebase.opennbt.tag.IntArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.IntTag;
|
|
||||||
import ch.spacebase.opennbt.tag.ListTag;
|
|
||||||
import ch.spacebase.opennbt.tag.LongTag;
|
|
||||||
import ch.spacebase.opennbt.tag.ShortTag;
|
|
||||||
import ch.spacebase.opennbt.tag.StringTag;
|
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.DoubleArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.FloatArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.LongArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.ObjectArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.ObjectTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.ShortArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.StringArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.UnknownTag;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the OpenNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class which contains NBT-related utility methods.
|
|
||||||
*/
|
|
||||||
public final class NBTUtils {
|
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger("NBTUtils");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the type name of a tag.
|
|
||||||
* @param clazz The tag class.
|
|
||||||
* @return The type name.
|
|
||||||
*/
|
|
||||||
public static String getTypeName(Class<? extends Tag> clazz) {
|
|
||||||
if(clazz.equals(ByteArrayTag.class)) {
|
|
||||||
return "TAG_Byte_Array";
|
|
||||||
} else if(clazz.equals(ByteTag.class)) {
|
|
||||||
return "TAG_Byte";
|
|
||||||
} else if(clazz.equals(CompoundTag.class)) {
|
|
||||||
return "TAG_Compound";
|
|
||||||
} else if(clazz.equals(DoubleTag.class)) {
|
|
||||||
return "TAG_Double";
|
|
||||||
} else if(clazz.equals(EndTag.class)) {
|
|
||||||
return "TAG_End";
|
|
||||||
} else if(clazz.equals(FloatTag.class)) {
|
|
||||||
return "TAG_Float";
|
|
||||||
} else if(clazz.equals(IntTag.class)) {
|
|
||||||
return "TAG_Int";
|
|
||||||
} else if(clazz.equals(ListTag.class)) {
|
|
||||||
return "TAG_List";
|
|
||||||
} else if(clazz.equals(LongTag.class)) {
|
|
||||||
return "TAG_Long";
|
|
||||||
} else if(clazz.equals(ShortTag.class)) {
|
|
||||||
return "TAG_Short";
|
|
||||||
} else if(clazz.equals(StringTag.class)) {
|
|
||||||
return "TAG_String";
|
|
||||||
} else if (clazz.equals(IntArrayTag.class)) {
|
|
||||||
return "TAG_Int_Array";
|
|
||||||
} else if (clazz.equals(DoubleArrayTag.class)) {
|
|
||||||
return "TAG_Double_Array";
|
|
||||||
} else if (clazz.equals(FloatArrayTag.class)) {
|
|
||||||
return "TAG_Float_Array";
|
|
||||||
} else if (clazz.equals(LongArrayTag.class)) {
|
|
||||||
return "TAG_Long_Array";
|
|
||||||
} else if (clazz.equals(ObjectArrayTag.class)) {
|
|
||||||
return "TAG_Object_Array";
|
|
||||||
} else if (clazz.equals(ObjectTag.class)) {
|
|
||||||
return "TAG_Object";
|
|
||||||
} else if (clazz.equals(ShortArrayTag.class)) {
|
|
||||||
return "TAG_Short_Array";
|
|
||||||
} else if (clazz.equals(StringArrayTag.class)) {
|
|
||||||
return "TAG_String_Array";
|
|
||||||
} else if (clazz.equals(UnknownTag.class)) {
|
|
||||||
return "TAG_Unknown";
|
|
||||||
} else {
|
|
||||||
logger.warning("Unknown tag class (" + clazz.getName() + ") found.");
|
|
||||||
return "TAG_Unknown";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the type code of a tag class.
|
|
||||||
* @param clazz The tag class.
|
|
||||||
* @return The type code.
|
|
||||||
* @throws IllegalArgumentException if the tag class is invalid.
|
|
||||||
*/
|
|
||||||
public static int getTypeCode(Class<? extends Tag> clazz) {
|
|
||||||
if(clazz.equals(ByteArrayTag.class)) {
|
|
||||||
return NBTConstants.TYPE_BYTE_ARRAY;
|
|
||||||
} else if(clazz.equals(ByteTag.class)) {
|
|
||||||
return NBTConstants.TYPE_BYTE;
|
|
||||||
} else if(clazz.equals(CompoundTag.class)) {
|
|
||||||
return NBTConstants.TYPE_COMPOUND;
|
|
||||||
} else if(clazz.equals(DoubleTag.class)) {
|
|
||||||
return NBTConstants.TYPE_DOUBLE;
|
|
||||||
} else if(clazz.equals(EndTag.class)) {
|
|
||||||
return NBTConstants.TYPE_END;
|
|
||||||
} else if(clazz.equals(FloatTag.class)) {
|
|
||||||
return NBTConstants.TYPE_FLOAT;
|
|
||||||
} else if(clazz.equals(IntTag.class)) {
|
|
||||||
return NBTConstants.TYPE_INT;
|
|
||||||
} else if(clazz.equals(ListTag.class)) {
|
|
||||||
return NBTConstants.TYPE_LIST;
|
|
||||||
} else if(clazz.equals(LongTag.class)) {
|
|
||||||
return NBTConstants.TYPE_LONG;
|
|
||||||
} else if(clazz.equals(ShortTag.class)) {
|
|
||||||
return NBTConstants.TYPE_SHORT;
|
|
||||||
} else if(clazz.equals(StringTag.class)) {
|
|
||||||
return NBTConstants.TYPE_STRING;
|
|
||||||
} else if(clazz.equals(IntArrayTag.class)) {
|
|
||||||
return NBTConstants.TYPE_INT_ARRAY;
|
|
||||||
} else if(clazz.equals(DoubleArrayTag.class)) {
|
|
||||||
return NBTConstants.TYPE_DOUBLE_ARRAY;
|
|
||||||
} else if(clazz.equals(FloatArrayTag.class)) {
|
|
||||||
return NBTConstants.TYPE_FLOAT_ARRAY;
|
|
||||||
} else if(clazz.equals(LongArrayTag.class)) {
|
|
||||||
return NBTConstants.TYPE_LONG_ARRAY;
|
|
||||||
} else if(clazz.equals(ObjectArrayTag.class)) {
|
|
||||||
return NBTConstants.TYPE_OBJECT_ARRAY;
|
|
||||||
} else if(clazz.equals(ObjectTag.class)) {
|
|
||||||
return NBTConstants.TYPE_OBJECT;
|
|
||||||
} else if(clazz.equals(ShortArrayTag.class)) {
|
|
||||||
return NBTConstants.TYPE_SHORT_ARRAY;
|
|
||||||
} else if(clazz.equals(StringArrayTag.class)) {
|
|
||||||
return NBTConstants.TYPE_STRING_ARRAY;
|
|
||||||
} else if(clazz.equals(UnknownTag.class)) {
|
|
||||||
return NBTConstants.TYPE_UNKNOWN;
|
|
||||||
} else {
|
|
||||||
logger.warning("Unknown tag class (" + clazz.getName() + ") found.");
|
|
||||||
return NBTConstants.TYPE_UNKNOWN;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the class of a type of tag.
|
|
||||||
* @param type The type.
|
|
||||||
* @return The class.
|
|
||||||
* @throws IllegalArgumentException if the tag type is invalid.
|
|
||||||
*/
|
|
||||||
public static Class<? extends Tag> getTypeClass(int type) {
|
|
||||||
switch(type) {
|
|
||||||
case NBTConstants.TYPE_END:
|
|
||||||
return EndTag.class;
|
|
||||||
case NBTConstants.TYPE_BYTE:
|
|
||||||
return ByteTag.class;
|
|
||||||
case NBTConstants.TYPE_SHORT:
|
|
||||||
return ShortTag.class;
|
|
||||||
case NBTConstants.TYPE_INT:
|
|
||||||
return IntTag.class;
|
|
||||||
case NBTConstants.TYPE_LONG:
|
|
||||||
return LongTag.class;
|
|
||||||
case NBTConstants.TYPE_FLOAT:
|
|
||||||
return FloatTag.class;
|
|
||||||
case NBTConstants.TYPE_DOUBLE:
|
|
||||||
return DoubleTag.class;
|
|
||||||
case NBTConstants.TYPE_BYTE_ARRAY:
|
|
||||||
return ByteArrayTag.class;
|
|
||||||
case NBTConstants.TYPE_STRING:
|
|
||||||
return StringTag.class;
|
|
||||||
case NBTConstants.TYPE_LIST:
|
|
||||||
return ListTag.class;
|
|
||||||
case NBTConstants.TYPE_COMPOUND:
|
|
||||||
return CompoundTag.class;
|
|
||||||
case NBTConstants.TYPE_INT_ARRAY:
|
|
||||||
return IntArrayTag.class;
|
|
||||||
case NBTConstants.TYPE_UNKNOWN:
|
|
||||||
return UnknownTag.class;
|
|
||||||
default:
|
|
||||||
logger.warning("Unknown tag type (" + type + ") found.");
|
|
||||||
return UnknownTag.class;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clones a <String, Tag> Map.
|
|
||||||
* @param map to clone
|
|
||||||
* @return clone of map
|
|
||||||
*/
|
|
||||||
public static Map<String, Tag> cloneMap(Map<String, Tag> map) {
|
|
||||||
Map<String, Tag> newMap = new HashMap<String, Tag>();
|
|
||||||
|
|
||||||
for(Entry<String, Tag> entry : map.entrySet()) {
|
|
||||||
newMap.put(entry.getKey(), entry.getValue().clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
return newMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get child tag of a NBT structure.
|
|
||||||
*
|
|
||||||
* @param items
|
|
||||||
* @param key
|
|
||||||
* @param expected
|
|
||||||
* @return child tag
|
|
||||||
* @throws InvalidNBTException
|
|
||||||
*/
|
|
||||||
public static <T extends Tag> T getChildTag(CompoundTag items, String key, Class<T> expected) throws InvalidNBTException {
|
|
||||||
return getChildTag(items.getValue(), key, expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get child tag of a NBT structure.
|
|
||||||
*
|
|
||||||
* @param items
|
|
||||||
* @param key
|
|
||||||
* @param expected
|
|
||||||
* @return child tag
|
|
||||||
* @throws InvalidNBTException
|
|
||||||
*/
|
|
||||||
public static <T extends Tag> T getChildTag(Map<String,Tag> items, String key, Class<T> expected) throws InvalidNBTException {
|
|
||||||
if (!items.containsKey(key)) {
|
|
||||||
throw new InvalidNBTException("Missing a \"" + key + "\" tag");
|
|
||||||
}
|
|
||||||
|
|
||||||
Tag tag = items.get(key);
|
|
||||||
|
|
||||||
if (!expected.isInstance(tag)) {
|
|
||||||
throw new InvalidNBTException(key + " tag is not of tag type " + expected.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
return expected.cast(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default private constructor.
|
|
||||||
*/
|
|
||||||
private NBTUtils() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,164 +0,0 @@
|
|||||||
package ch.spacebase.opennbt;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import ch.spacebase.opennbt.stream.NBTOutputStream;
|
|
||||||
import ch.spacebase.opennbt.tag.*;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.*;
|
|
||||||
|
|
||||||
public class TagBuilder {
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
private List<Tag> tags = new ArrayList<Tag>();
|
|
||||||
|
|
||||||
public TagBuilder() {
|
|
||||||
this("");
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, byte b[]) {
|
|
||||||
this.tags.add(new ByteArrayTag(name, b));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, byte b) {
|
|
||||||
this.tags.add(new ByteTag(name, b));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(CompoundTag compound) {
|
|
||||||
this.tags.add(compound);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, double d) {
|
|
||||||
this.tags.add(new DoubleTag(name, d));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, double d[]) {
|
|
||||||
this.tags.add(new DoubleArrayTag(name, d));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(EndTag tag) {
|
|
||||||
this.tags.add(tag);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, float f) {
|
|
||||||
this.tags.add(new FloatTag(name, f));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, float f[]) {
|
|
||||||
this.tags.add(new FloatArrayTag(name, f));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, int i[]) {
|
|
||||||
this.tags.add(new IntArrayTag(name, i));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, int i) {
|
|
||||||
this.tags.add(new IntTag(name, i));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T extends Tag> TagBuilder append(String name, Class<T> clazz, List<T> l) {
|
|
||||||
this.tags.add(new ListTag<T>(name, clazz, l));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, long l) {
|
|
||||||
this.tags.add(new LongTag(name, l));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, long l[]) {
|
|
||||||
this.tags.add(new LongArrayTag(name, l));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, short s) {
|
|
||||||
this.tags.add(new ShortTag(name, s));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, short s[]) {
|
|
||||||
this.tags.add(new ShortArrayTag(name, s));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, String s) {
|
|
||||||
this.tags.add(new StringTag(name, s));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, String s[]) {
|
|
||||||
this.tags.add(new StringArrayTag(name, s));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, Object o) {
|
|
||||||
this.tags.add(new ObjectTag(name, o));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(String name, Object o[]) {
|
|
||||||
this.tags.add(new ObjectArrayTag(name, o));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TagBuilder append(TagBuilder builder) {
|
|
||||||
this.tags.add(builder.toCompoundTag());
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CompoundTag toCompoundTag() {
|
|
||||||
Map<String, Tag> tagMap = new HashMap<String, Tag>();
|
|
||||||
for(Tag tag : this.tags) {
|
|
||||||
tagMap.put(tag.getName(), tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new CompoundTag(this.name, tagMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Tag> toList() {
|
|
||||||
return new ArrayList<Tag>(this.tags);
|
|
||||||
}
|
|
||||||
|
|
||||||
public NBTOutputStream toOutputStream(String file) {
|
|
||||||
File nbt = new File(file);
|
|
||||||
|
|
||||||
try {
|
|
||||||
if(!nbt.exists()) {
|
|
||||||
if(nbt.getParentFile() != null) nbt.getParentFile().mkdirs();
|
|
||||||
nbt.createNewFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
NBTOutputStream out = new NBTOutputStream(new FileOutputStream(nbt));
|
|
||||||
for(Tag tag : this.tags) {
|
|
||||||
out.writeTag(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
} catch(IOException e) {
|
|
||||||
System.out.println("Failed to create NBTOutputStream for file \"" + file + "\" from a TagBuilder.");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
14
src/main/java/ch/spacebase/opennbt/TagRegisterException.java
Normal file
14
src/main/java/ch/spacebase/opennbt/TagRegisterException.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package ch.spacebase.opennbt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception thrown when an error occurs while registering a tag class.
|
||||||
|
*/
|
||||||
|
public class TagRegisterException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -2022049594558041160L;
|
||||||
|
|
||||||
|
public TagRegisterException(Throwable cause) {
|
||||||
|
super("Failed to register tag.", cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
120
src/main/java/ch/spacebase/opennbt/TagRegistry.java
Normal file
120
src/main/java/ch/spacebase/opennbt/TagRegistry.java
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
package ch.spacebase.opennbt;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import ch.spacebase.opennbt.tag.ByteArrayTag;
|
||||||
|
import ch.spacebase.opennbt.tag.ByteTag;
|
||||||
|
import ch.spacebase.opennbt.tag.CompoundTag;
|
||||||
|
import ch.spacebase.opennbt.tag.DoubleTag;
|
||||||
|
import ch.spacebase.opennbt.tag.FloatTag;
|
||||||
|
import ch.spacebase.opennbt.tag.IntArrayTag;
|
||||||
|
import ch.spacebase.opennbt.tag.IntTag;
|
||||||
|
import ch.spacebase.opennbt.tag.ListTag;
|
||||||
|
import ch.spacebase.opennbt.tag.LongTag;
|
||||||
|
import ch.spacebase.opennbt.tag.ShortTag;
|
||||||
|
import ch.spacebase.opennbt.tag.StringTag;
|
||||||
|
import ch.spacebase.opennbt.tag.Tag;
|
||||||
|
import ch.spacebase.opennbt.tag.custom.DoubleArrayTag;
|
||||||
|
import ch.spacebase.opennbt.tag.custom.FloatArrayTag;
|
||||||
|
import ch.spacebase.opennbt.tag.custom.LongArrayTag;
|
||||||
|
import ch.spacebase.opennbt.tag.custom.SerializableArrayTag;
|
||||||
|
import ch.spacebase.opennbt.tag.custom.SerializableTag;
|
||||||
|
import ch.spacebase.opennbt.tag.custom.ShortArrayTag;
|
||||||
|
import ch.spacebase.opennbt.tag.custom.StringArrayTag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A registry containing different tag classes.
|
||||||
|
*/
|
||||||
|
public class TagRegistry {
|
||||||
|
|
||||||
|
private static final Map<Integer, Class<? extends Tag>> tags = new HashMap<Integer, Class<? extends Tag>>();
|
||||||
|
private static boolean registered = false;
|
||||||
|
|
||||||
|
protected static void registerDefaultTags() throws TagRegisterException {
|
||||||
|
if(registered) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
registered = true;
|
||||||
|
register(CompoundTag.class);
|
||||||
|
register(ListTag.class);
|
||||||
|
|
||||||
|
register(SerializableTag.class);
|
||||||
|
register(StringTag.class);
|
||||||
|
register(ByteTag.class);
|
||||||
|
register(DoubleTag.class);
|
||||||
|
register(FloatTag.class);
|
||||||
|
register(IntTag.class);
|
||||||
|
register(LongTag.class);
|
||||||
|
register(ShortTag.class);
|
||||||
|
|
||||||
|
register(SerializableArrayTag.class);
|
||||||
|
register(StringArrayTag.class);
|
||||||
|
register(ByteArrayTag.class);
|
||||||
|
register(DoubleArrayTag.class);
|
||||||
|
register(FloatArrayTag.class);
|
||||||
|
register(IntArrayTag.class);
|
||||||
|
register(LongArrayTag.class);
|
||||||
|
register(ShortArrayTag.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a tag class.
|
||||||
|
* @param tag Tag class to register.
|
||||||
|
* @throws TagRegisterException If an error occurs while registering the tag.
|
||||||
|
*/
|
||||||
|
public static void register(Class<? extends Tag> tag) throws TagRegisterException {
|
||||||
|
try {
|
||||||
|
Tag t = tag.getDeclaredConstructor(String.class).newInstance("");
|
||||||
|
tags.put(t.getId(), tag);
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new TagRegisterException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the tag class with the given id.
|
||||||
|
* @param id Id of the tag.
|
||||||
|
* @return The tag class with the given id.
|
||||||
|
*/
|
||||||
|
public static Class<? extends Tag> getClassFor(int id) {
|
||||||
|
return tags.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the id of the given tag class.
|
||||||
|
* @param clazz The tag class to get the id for.
|
||||||
|
* @return The id of the given tag class, or -1 if it cannot be found.
|
||||||
|
*/
|
||||||
|
public static int getIdFor(Class<? extends Tag> clazz) {
|
||||||
|
for(int id : tags.keySet()) {
|
||||||
|
if(tags.get(id) == clazz) {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an instance of the tag with the given id, using the String constructor.
|
||||||
|
* @param id Id of the tag.
|
||||||
|
* @param tagName Name to give the tag.
|
||||||
|
* @return The created tag, or null if it could not be created or the type does not exist.
|
||||||
|
*/
|
||||||
|
public static Tag createInstance(int id, String tagName) {
|
||||||
|
if(!tags.containsKey(id)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Class<? extends Tag> clazz = tags.get(id);
|
||||||
|
try {
|
||||||
|
return clazz.getDeclaredConstructor(String.class).newInstance(tagName);
|
||||||
|
} catch(Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,17 +0,0 @@
|
|||||||
package ch.spacebase.opennbt.exception;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class InvalidNBTException extends IOException {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
public InvalidNBTException() {
|
|
||||||
super("Invalid NBT file!");
|
|
||||||
}
|
|
||||||
|
|
||||||
public InvalidNBTException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,303 +0,0 @@
|
|||||||
package ch.spacebase.opennbt.stream;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the OpenNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import java.io.Closeable;
|
|
||||||
import java.io.DataInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.ObjectInputStream;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
import java.util.zip.GZIPInputStream;
|
|
||||||
|
|
||||||
import ch.spacebase.opennbt.NBTConstants;
|
|
||||||
import ch.spacebase.opennbt.NBTUtils;
|
|
||||||
import ch.spacebase.opennbt.tag.ByteArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.ByteTag;
|
|
||||||
import ch.spacebase.opennbt.tag.CompoundTag;
|
|
||||||
import ch.spacebase.opennbt.tag.DoubleTag;
|
|
||||||
import ch.spacebase.opennbt.tag.EndTag;
|
|
||||||
import ch.spacebase.opennbt.tag.FloatTag;
|
|
||||||
import ch.spacebase.opennbt.tag.IntArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.IntTag;
|
|
||||||
import ch.spacebase.opennbt.tag.ListTag;
|
|
||||||
import ch.spacebase.opennbt.tag.LongTag;
|
|
||||||
import ch.spacebase.opennbt.tag.ShortTag;
|
|
||||||
import ch.spacebase.opennbt.tag.StringTag;
|
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.DoubleArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.FloatArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.LongArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.ObjectArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.ObjectTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.ShortArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.StringArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.UnknownTag;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>This class reads <strong>NBT</strong>, or
|
|
||||||
* <strong>Named Binary Tag</strong> streams, and produces an object graph of
|
|
||||||
* subclasses of the <code>Tag</code> object.</p>
|
|
||||||
*
|
|
||||||
* <p>The NBT format was created by Markus Persson, and the specification may
|
|
||||||
* be found at <a href="http://www.minecraft.net/docs/NBT.txt">
|
|
||||||
* http://www.minecraft.net/docs/NBT.txt</a>.</p>
|
|
||||||
*/
|
|
||||||
public final class NBTInputStream implements Closeable {
|
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger("NBTInputStream");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The data input stream.
|
|
||||||
*/
|
|
||||||
private final DataInputStream is;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new <code>NBTInputStream</code>, which will source its data
|
|
||||||
* from the specified input stream.
|
|
||||||
* @param is The input stream.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
public NBTInputStream(InputStream is) throws IOException {
|
|
||||||
this.is = new DataInputStream(new GZIPInputStream(is));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads an NBT tag from the stream.
|
|
||||||
* @return The tag that was read.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
public Tag readTag() throws IOException {
|
|
||||||
return readTag(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads an NBT from the stream.
|
|
||||||
* @param depth The depth of this tag.
|
|
||||||
* @return The tag that was read.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private Tag readTag(int depth) throws IOException {
|
|
||||||
int type = is.readByte() & 0xFF;
|
|
||||||
|
|
||||||
String name;
|
|
||||||
if(type != NBTConstants.TYPE_END) {
|
|
||||||
int nameLength = is.readShort() & 0xFFFF;
|
|
||||||
byte[] nameBytes = new byte[nameLength];
|
|
||||||
is.readFully(nameBytes);
|
|
||||||
name = new String(nameBytes, NBTConstants.CHARSET);
|
|
||||||
} else {
|
|
||||||
name = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return readTagPayload(type, name, depth);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads the payload of a tag, given the name and type.
|
|
||||||
* @param type The type.
|
|
||||||
* @param name The name.
|
|
||||||
* @param depth The depth.
|
|
||||||
* @return The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
||||||
private Tag readTagPayload(int type, String name, int depth) throws IOException {
|
|
||||||
switch(type) {
|
|
||||||
case NBTConstants.TYPE_END:
|
|
||||||
if(depth == 0) {
|
|
||||||
throw new IOException("TAG_End found without a TAG_Compound/TAG_List tag preceding it.");
|
|
||||||
} else {
|
|
||||||
return new EndTag();
|
|
||||||
}
|
|
||||||
case NBTConstants.TYPE_BYTE:
|
|
||||||
return new ByteTag(name, is.readByte());
|
|
||||||
case NBTConstants.TYPE_SHORT:
|
|
||||||
return new ShortTag(name, is.readShort());
|
|
||||||
case NBTConstants.TYPE_INT:
|
|
||||||
return new IntTag(name, is.readInt());
|
|
||||||
case NBTConstants.TYPE_LONG:
|
|
||||||
return new LongTag(name, is.readLong());
|
|
||||||
case NBTConstants.TYPE_FLOAT:
|
|
||||||
return new FloatTag(name, is.readFloat());
|
|
||||||
case NBTConstants.TYPE_DOUBLE:
|
|
||||||
return new DoubleTag(name, is.readDouble());
|
|
||||||
case NBTConstants.TYPE_BYTE_ARRAY:
|
|
||||||
int length = is.readInt();
|
|
||||||
byte[] bytes = new byte[length];
|
|
||||||
is.readFully(bytes);
|
|
||||||
return new ByteArrayTag(name, bytes);
|
|
||||||
case NBTConstants.TYPE_STRING:
|
|
||||||
length = is.readShort();
|
|
||||||
bytes = new byte[length];
|
|
||||||
is.readFully(bytes);
|
|
||||||
return new StringTag(name, new String(bytes, NBTConstants.CHARSET));
|
|
||||||
case NBTConstants.TYPE_LIST:
|
|
||||||
int childType = is.readByte();
|
|
||||||
length = is.readInt();
|
|
||||||
|
|
||||||
Class<? extends Tag> oclass = NBTUtils.getTypeClass(childType);
|
|
||||||
|
|
||||||
List<Tag> tagList = new ArrayList<Tag>();
|
|
||||||
for(int i = 0; i < length; i++) {
|
|
||||||
Tag tag = readTagPayload(childType, "", depth + 1);
|
|
||||||
|
|
||||||
if(tag instanceof EndTag) {
|
|
||||||
throw new IOException("TAG_End not permitted in a list.");
|
|
||||||
} else if(!oclass.isInstance(tag)) {
|
|
||||||
throw new IOException("Mixed types within a list.");
|
|
||||||
}
|
|
||||||
|
|
||||||
tagList.add(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ListTag(name, oclass, tagList);
|
|
||||||
case NBTConstants.TYPE_COMPOUND:
|
|
||||||
Map<String, Tag> tagMap = new HashMap<String, Tag>();
|
|
||||||
while(true) {
|
|
||||||
Tag tag = readTag(depth + 1);
|
|
||||||
if(tag instanceof EndTag) {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
tagMap.put(tag.getName(), tag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new CompoundTag(name, tagMap);
|
|
||||||
case NBTConstants.TYPE_INT_ARRAY:
|
|
||||||
length = is.readInt();
|
|
||||||
int[] data = new int[length];
|
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
data[i] = is.readInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new IntArrayTag(name, data);
|
|
||||||
case NBTConstants.TYPE_DOUBLE_ARRAY:
|
|
||||||
length = is.readInt();
|
|
||||||
double[] dat = new double[length];
|
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
dat[i] = is.readDouble();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new DoubleArrayTag(name, dat);
|
|
||||||
case NBTConstants.TYPE_FLOAT_ARRAY:
|
|
||||||
length = is.readInt();
|
|
||||||
float[] floats = new float[length];
|
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
floats[i] = is.readFloat();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new FloatArrayTag(name, floats);
|
|
||||||
case NBTConstants.TYPE_LONG_ARRAY:
|
|
||||||
length = is.readInt();
|
|
||||||
long[] longs = new long[length];
|
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
longs[i] = is.readLong();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new LongArrayTag(name, longs);
|
|
||||||
case NBTConstants.TYPE_OBJECT_ARRAY:
|
|
||||||
length = is.readInt();
|
|
||||||
Object[] objs = new Object[length];
|
|
||||||
|
|
||||||
ObjectInputStream str = new ObjectInputStream(is);
|
|
||||||
|
|
||||||
for(int i = 0; i < length; i++) {
|
|
||||||
try {
|
|
||||||
objs[i] = str.readObject();
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
logger.severe("Class not found while reading ObjectTag!");
|
|
||||||
e.printStackTrace();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ObjectArrayTag(name, objs);
|
|
||||||
case NBTConstants.TYPE_OBJECT:
|
|
||||||
str = new ObjectInputStream(is);
|
|
||||||
Object o = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
o = str.readObject();
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
logger.severe("Class not found while reading ObjectTag!");
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ObjectTag(name, o);
|
|
||||||
case NBTConstants.TYPE_SHORT_ARRAY:
|
|
||||||
long time = System.currentTimeMillis();
|
|
||||||
length = is.readInt();
|
|
||||||
short[] shorts = new short[length];
|
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
shorts[i] = is.readShort();
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Took " + (System.currentTimeMillis() - time) + "ms to read a short array.");
|
|
||||||
return new ShortArrayTag(name, shorts);
|
|
||||||
case NBTConstants.TYPE_STRING_ARRAY:
|
|
||||||
length = is.readInt();
|
|
||||||
String[] strings = new String[length];
|
|
||||||
|
|
||||||
for(int i = 0; i < length; i++) {
|
|
||||||
int size = is.readShort();
|
|
||||||
bytes = new byte[size];
|
|
||||||
is.readFully(bytes);
|
|
||||||
strings[i] = new String(bytes, NBTConstants.CHARSET);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new StringArrayTag(name, strings);
|
|
||||||
default:
|
|
||||||
logger.warning("Unknown tag found while reading.");
|
|
||||||
return new UnknownTag(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() throws IOException {
|
|
||||||
is.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,419 +0,0 @@
|
|||||||
package ch.spacebase.opennbt.stream;
|
|
||||||
|
|
||||||
import java.io.Closeable;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.ObjectOutputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
import java.util.zip.GZIPOutputStream;
|
|
||||||
|
|
||||||
import ch.spacebase.opennbt.NBTConstants;
|
|
||||||
import ch.spacebase.opennbt.NBTUtils;
|
|
||||||
import ch.spacebase.opennbt.tag.ByteArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.ByteTag;
|
|
||||||
import ch.spacebase.opennbt.tag.CompoundTag;
|
|
||||||
import ch.spacebase.opennbt.tag.DoubleTag;
|
|
||||||
import ch.spacebase.opennbt.tag.EndTag;
|
|
||||||
import ch.spacebase.opennbt.tag.FloatTag;
|
|
||||||
import ch.spacebase.opennbt.tag.IntArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.IntTag;
|
|
||||||
import ch.spacebase.opennbt.tag.ListTag;
|
|
||||||
import ch.spacebase.opennbt.tag.LongTag;
|
|
||||||
import ch.spacebase.opennbt.tag.ShortTag;
|
|
||||||
import ch.spacebase.opennbt.tag.StringTag;
|
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.DoubleArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.FloatArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.LongArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.ObjectArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.ObjectTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.ShortArrayTag;
|
|
||||||
import ch.spacebase.opennbt.tag.custom.StringArrayTag;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the OpenNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>This class writes <strong>NBT</strong>, or
|
|
||||||
* <strong>Named Binary Tag</strong> <code>Tag</code> objects to an underlying
|
|
||||||
* <code>OutputStream</code>.</p>
|
|
||||||
*
|
|
||||||
* <p>The NBT format was created by Markus Persson, and the specification may
|
|
||||||
* be found at <a href="http://www.minecraft.net/docs/NBT.txt">
|
|
||||||
* http://www.minecraft.net/docs/NBT.txt</a>.</p>
|
|
||||||
*/
|
|
||||||
public final class NBTOutputStream implements Closeable {
|
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger("NBTOutputStream");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The output stream.
|
|
||||||
*/
|
|
||||||
private final DataOutputStream os;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new <code>NBTOutputStream</code>, which will write data to the
|
|
||||||
* specified underlying output stream.
|
|
||||||
* @param os The output stream.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
public NBTOutputStream(OutputStream os) throws IOException {
|
|
||||||
this.os = new DataOutputStream(new GZIPOutputStream(os));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a tag.
|
|
||||||
* @param tag The tag to write.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
public void writeTag(Tag tag) throws IOException {
|
|
||||||
int type = NBTUtils.getTypeCode(tag.getClass());
|
|
||||||
if(type == NBTConstants.TYPE_UNKNOWN) {
|
|
||||||
logger.warning("Unknown tag found while writing, ignoring...");
|
|
||||||
}
|
|
||||||
|
|
||||||
String name = tag.getName();
|
|
||||||
byte[] nameBytes = name.getBytes(NBTConstants.CHARSET);
|
|
||||||
|
|
||||||
os.writeByte(type);
|
|
||||||
os.writeShort(nameBytes.length);
|
|
||||||
os.write(nameBytes);
|
|
||||||
|
|
||||||
if(type == NBTConstants.TYPE_END) {
|
|
||||||
throw new IOException("Named TAG_End not permitted.");
|
|
||||||
}
|
|
||||||
|
|
||||||
writeTagPayload(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes tag payload.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeTagPayload(Tag tag) throws IOException {
|
|
||||||
int type = NBTUtils.getTypeCode(tag.getClass());
|
|
||||||
switch(type) {
|
|
||||||
case NBTConstants.TYPE_END:
|
|
||||||
writeEndTagPayload((EndTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_BYTE:
|
|
||||||
writeByteTagPayload((ByteTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_SHORT:
|
|
||||||
writeShortTagPayload((ShortTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_INT:
|
|
||||||
writeIntTagPayload((IntTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_LONG:
|
|
||||||
writeLongTagPayload((LongTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_FLOAT:
|
|
||||||
writeFloatTagPayload((FloatTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_DOUBLE:
|
|
||||||
writeDoubleTagPayload((DoubleTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_BYTE_ARRAY:
|
|
||||||
writeByteArrayTagPayload((ByteArrayTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_STRING:
|
|
||||||
writeStringTagPayload((StringTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_LIST:
|
|
||||||
writeListTagPayload((ListTag<?>) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_COMPOUND:
|
|
||||||
writeCompoundTagPayload((CompoundTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_INT_ARRAY:
|
|
||||||
writeIntArrayTagPayload((IntArrayTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_DOUBLE_ARRAY:
|
|
||||||
writeDoubleArrayTagPayload((DoubleArrayTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_FLOAT_ARRAY:
|
|
||||||
writeFloatArrayTagPayload((FloatArrayTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_LONG_ARRAY:
|
|
||||||
writeLongArrayTagPayload((LongArrayTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_OBJECT_ARRAY:
|
|
||||||
writeObjectArrayTagPayload((ObjectArrayTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_OBJECT:
|
|
||||||
writeObjectTagPayload((ObjectTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_SHORT_ARRAY:
|
|
||||||
writeShortArrayTagPayload((ShortArrayTag) tag);
|
|
||||||
break;
|
|
||||||
case NBTConstants.TYPE_STRING_ARRAY:
|
|
||||||
writeStringArrayTagPayload((StringArrayTag) tag);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
logger.warning("Unknown tag found while writing, ignoring...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a <code>TAG_Byte</code> tag.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeByteTagPayload(ByteTag tag) throws IOException {
|
|
||||||
os.writeByte(tag.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a <code>TAG_Byte_Array</code> tag.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeByteArrayTagPayload(ByteArrayTag tag) throws IOException {
|
|
||||||
byte[] bytes = tag.getValue();
|
|
||||||
os.writeInt(bytes.length);
|
|
||||||
os.write(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a <code>TAG_Compound</code> tag.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeCompoundTagPayload(CompoundTag tag) throws IOException {
|
|
||||||
for(Tag childTag : tag.values()) {
|
|
||||||
writeTag(childTag);
|
|
||||||
}
|
|
||||||
os.writeByte((byte) 0); // end tag - better way?
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a <code>TAG_List</code> tag.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeListTagPayload(ListTag<?> tag) throws IOException {
|
|
||||||
Class<? extends Tag> clazz = tag.getType();
|
|
||||||
int size = tag.size();
|
|
||||||
|
|
||||||
os.writeByte(NBTUtils.getTypeCode(clazz));
|
|
||||||
os.writeInt(size);
|
|
||||||
for(Tag t : tag) {
|
|
||||||
this.writeTagPayload(t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a <code>TAG_String</code> tag.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeStringTagPayload(StringTag tag) throws IOException {
|
|
||||||
byte[] bytes = tag.getValue() != null ? tag.getValue().getBytes(NBTConstants.CHARSET) : new byte[0];
|
|
||||||
os.writeShort(bytes.length);
|
|
||||||
os.write(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a <code>TAG_Double</code> tag.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeDoubleTagPayload(DoubleTag tag) throws IOException {
|
|
||||||
os.writeDouble(tag.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a <code>TAG_Float</code> tag.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeFloatTagPayload(FloatTag tag) throws IOException {
|
|
||||||
os.writeFloat(tag.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a <code>TAG_Long</code> tag.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeLongTagPayload(LongTag tag) throws IOException {
|
|
||||||
os.writeLong(tag.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a <code>TAG_Int</code> tag.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeIntTagPayload(IntTag tag) throws IOException {
|
|
||||||
os.writeInt(tag.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a <code>TAG_Short</code> tag.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeShortTagPayload(ShortTag tag) throws IOException {
|
|
||||||
os.writeShort(tag.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes a <code>TAG_Empty</code> tag.
|
|
||||||
* @param tag The tag.
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeEndTagPayload(EndTag tag) {
|
|
||||||
/* empty */
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Writes a <code>TAG_Int_Array<code> tag.
|
|
||||||
* @param tag The tag
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeIntArrayTagPayload(IntArrayTag tag) throws IOException {
|
|
||||||
int[] data = tag.getValue();
|
|
||||||
|
|
||||||
os.writeInt(data.length);
|
|
||||||
|
|
||||||
for (int i = 0; i < data.length; i++) {
|
|
||||||
os.writeInt(data[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Writes a <code>TAG_Double_Array<code> tag.
|
|
||||||
* @param tag The tag
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeDoubleArrayTagPayload(DoubleArrayTag tag) throws IOException {
|
|
||||||
double[] data = tag.getValue();
|
|
||||||
|
|
||||||
os.writeInt(data.length);
|
|
||||||
|
|
||||||
for (int i = 0; i < data.length; i++) {
|
|
||||||
os.writeDouble(data[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Writes a <code>TAG_Float_Array<code> tag.
|
|
||||||
* @param tag The tag
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeFloatArrayTagPayload(FloatArrayTag tag) throws IOException {
|
|
||||||
float[] data = tag.getValue();
|
|
||||||
|
|
||||||
os.writeInt(data.length);
|
|
||||||
|
|
||||||
for (int i = 0; i < data.length; i++) {
|
|
||||||
os.writeFloat(data[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Writes a <code>TAG_Long_Array<code> tag.
|
|
||||||
* @param tag The tag
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeLongArrayTagPayload(LongArrayTag tag) throws IOException {
|
|
||||||
long[] data = tag.getValue();
|
|
||||||
|
|
||||||
os.writeInt(data.length);
|
|
||||||
|
|
||||||
for (int i = 0; i < data.length; i++) {
|
|
||||||
os.writeLong(data[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Writes a <code>TAG_Object_Array<code> tag.
|
|
||||||
* @param tag The tag
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeObjectArrayTagPayload(ObjectArrayTag tag) throws IOException {
|
|
||||||
Object[] data = tag.getValue();
|
|
||||||
|
|
||||||
os.writeInt(data.length);
|
|
||||||
ObjectOutputStream str = new ObjectOutputStream(os);
|
|
||||||
|
|
||||||
for (int i = 0; i < data.length; i++) {
|
|
||||||
str.writeObject(data[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Writes a <code>TAG_Object<code> tag.
|
|
||||||
* @param tag The tag
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeObjectTagPayload(ObjectTag tag) throws IOException {
|
|
||||||
(new ObjectOutputStream(os)).writeObject(tag.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Writes a <code>TAG_Short_Array<code> tag.
|
|
||||||
* @param tag The tag
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeShortArrayTagPayload(ShortArrayTag tag) throws IOException {
|
|
||||||
short[] data = tag.getValue();
|
|
||||||
|
|
||||||
os.writeInt(data.length);
|
|
||||||
|
|
||||||
for (int i = 0; i < data.length; i++) {
|
|
||||||
os.writeShort(data[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Writes a <code>TAG_String_Array<code> tag.
|
|
||||||
* @param tag The tag
|
|
||||||
* @throws IOException if an I/O error occurs.
|
|
||||||
*/
|
|
||||||
private void writeStringArrayTagPayload(StringArrayTag tag) throws IOException {
|
|
||||||
String[] data = tag.getValue();
|
|
||||||
|
|
||||||
os.writeInt(data.length);
|
|
||||||
byte[] bytes;
|
|
||||||
|
|
||||||
for (int i = 0; i < data.length; i++) {
|
|
||||||
bytes = data[i].getBytes(NBTConstants.CHARSET);
|
|
||||||
os.writeShort(bytes.length);
|
|
||||||
os.write(bytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() throws IOException {
|
|
||||||
os.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,55 +1,28 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
/*
|
import java.io.IOException;
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the OpenNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Byte_Array</code> tag.
|
* A tag containing a byte array.
|
||||||
*/
|
*/
|
||||||
public final class ByteArrayTag extends Tag {
|
public class ByteArrayTag extends Tag {
|
||||||
|
|
||||||
|
private byte[] value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value.
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
*/
|
*/
|
||||||
private final byte[] value;
|
public ByteArrayTag(String name) {
|
||||||
|
this(name, new byte[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
* @param name The name.
|
* @param name The name of the tag.
|
||||||
* @param value The value.
|
* @param value The value of the tag.
|
||||||
*/
|
*/
|
||||||
public ByteArrayTag(String name, byte[] value) {
|
public ByteArrayTag(String name, byte[] value) {
|
||||||
super(name);
|
super(name);
|
||||||
@ -58,34 +31,67 @@ public final class ByteArrayTag extends Tag {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] getValue() {
|
public byte[] getValue() {
|
||||||
return value;
|
return this.value.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public String toString() {
|
* Sets the value of this tag.
|
||||||
String name = getName();
|
* @param value New value of this tag.
|
||||||
String append = "";
|
*/
|
||||||
|
public void setValue(byte[] value) {
|
||||||
if(name != null && !name.equals("")) {
|
if(value == null) {
|
||||||
append = "(\"" + this.getName() + "\")";
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "TAG_Byte_Array" + append + ": " + Arrays.toString(value);
|
this.value = value.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @return The value at the given index.
|
||||||
|
*/
|
||||||
|
public byte getValue(int index) {
|
||||||
|
return this.value[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @param value Value to set.
|
||||||
|
*/
|
||||||
|
public void setValue(int index, byte value) {
|
||||||
|
this.value[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the length of this tag's array.
|
||||||
|
* @return This tag's array length.
|
||||||
|
*/
|
||||||
|
public int length() {
|
||||||
|
return this.value.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public int getId() {
|
||||||
if(!(obj instanceof ByteArrayTag)) return false;
|
return 7;
|
||||||
|
}
|
||||||
ByteArrayTag tag = (ByteArrayTag) obj;
|
|
||||||
|
@Override
|
||||||
return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName());
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
this.value = new byte[in.readInt()];
|
||||||
|
in.readFully(this.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeInt(this.value.length);
|
||||||
|
out.write(this.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public ByteArrayTag clone() {
|
public ByteArrayTag clone() {
|
||||||
byte[] clonedArray = this.getValue().clone();
|
return new ByteArrayTag(this.getName(), this.getValue());
|
||||||
|
|
||||||
return new ByteArrayTag(this.getName(), clonedArray);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,53 +1,28 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
/*
|
import java.io.DataInputStream;
|
||||||
* OpenNBT License
|
import java.io.DataOutputStream;
|
||||||
*
|
import java.io.IOException;
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the OpenNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Byte</code> tag.
|
* A tag containing a byte.
|
||||||
*/
|
*/
|
||||||
public final class ByteTag extends Tag {
|
public class ByteTag extends Tag {
|
||||||
|
|
||||||
/**
|
private byte value;
|
||||||
* The value.
|
|
||||||
*/
|
|
||||||
private final byte value;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
* @param name The name.
|
* @param name The name of the tag.
|
||||||
* @param value The value.
|
*/
|
||||||
|
public ByteTag(String name) {
|
||||||
|
this(name, (byte) 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
|
* @param value The value of the tag.
|
||||||
*/
|
*/
|
||||||
public ByteTag(String name, byte value) {
|
public ByteTag(String name, byte value) {
|
||||||
super(name);
|
super(name);
|
||||||
@ -56,19 +31,33 @@ public final class ByteTag extends Tag {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Byte getValue() {
|
public Byte getValue() {
|
||||||
return value;
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of this tag.
|
||||||
|
* @param value New value of this tag.
|
||||||
|
*/
|
||||||
|
public void setValue(byte value) {
|
||||||
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public int getId() {
|
||||||
String name = getName();
|
return 1;
|
||||||
String append = "";
|
}
|
||||||
if(name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
@Override
|
||||||
}
|
public void read(DataInputStream in) throws IOException {
|
||||||
return "TAG_Byte" + append + ": " + value;
|
this.value = in.readByte();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeByte(this.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public ByteTag clone() {
|
public ByteTag clone() {
|
||||||
return new ByteTag(this.getName(), this.getValue());
|
return new ByteTag(this.getName(), this.getValue());
|
||||||
}
|
}
|
||||||
|
@ -1,126 +1,131 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
/*
|
import java.io.DataInputStream;
|
||||||
* OpenNBT License
|
import java.io.DataOutputStream;
|
||||||
*
|
import java.io.IOException;
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import ch.spacebase.opennbt.NBTUtils;
|
import ch.spacebase.opennbt.NBTIO;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Compound</code> tag.
|
* A compound tag containing other tags.
|
||||||
*/
|
*/
|
||||||
public final class CompoundTag extends Tag {
|
public class CompoundTag extends Tag {
|
||||||
|
|
||||||
|
private Map<String, Tag> value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value.
|
* Creates a tag with the specified name.
|
||||||
*/
|
* @param name The name of the tag.
|
||||||
private final Map<String, Tag> value;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates the tag.
|
|
||||||
* @param name The name.
|
|
||||||
*/
|
*/
|
||||||
public CompoundTag(String name) {
|
public CompoundTag(String name) {
|
||||||
this(name, new HashMap<String, Tag>());
|
this(name, new LinkedHashMap<String, Tag>());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
* @param name The name.
|
* @param name The name of the tag.
|
||||||
* @param value The value.
|
* @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 = value;
|
this.value = new LinkedHashMap<String, Tag>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Tag> getValue() {
|
public Map<String, Tag> getValue() {
|
||||||
return new HashMap<String, Tag>(value);
|
return new LinkedHashMap<String, Tag>(this.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the tag with the specified name.
|
||||||
|
* @param tagName Name of the tag.
|
||||||
|
* @return The tag with the specified name.
|
||||||
|
*/
|
||||||
public Tag get(String tagName) {
|
public Tag get(String tagName) {
|
||||||
return this.value.get(tagName);
|
return this.value.get(tagName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tag put(String tagName, Tag tag) {
|
/**
|
||||||
return this.value.put(tagName, tag);
|
* Puts the tag 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.
|
||||||
|
*/
|
||||||
|
public Tag put(Tag tag) {
|
||||||
|
return this.value.put(tag.getName(), tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a tag from this compound tag.
|
||||||
|
* @param tagName Name of the tag to remove.
|
||||||
|
* @return The removed tag.
|
||||||
|
*/
|
||||||
public Tag remove(String tagName) {
|
public Tag remove(String tagName) {
|
||||||
return this.value.remove(tagName);
|
return this.value.remove(tagName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a set of keys in this compound tag.
|
||||||
|
* @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.
|
||||||
|
* @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.
|
||||||
|
* @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.
|
||||||
|
*/
|
||||||
public void clear() {
|
public void clear() {
|
||||||
this.value.clear();
|
this.value.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public int getId() {
|
||||||
String name = getName();
|
return 10;
|
||||||
String append = "";
|
|
||||||
if(name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
|
||||||
}
|
|
||||||
StringBuilder bldr = new StringBuilder();
|
|
||||||
bldr.append("TAG_Compound" + append + ": " + value.size() + " entries\r\n{\r\n");
|
|
||||||
for(Map.Entry<String, Tag> entry : value.entrySet()) {
|
|
||||||
bldr.append(" " + entry.getValue().toString().replaceAll("\r\n", "\r\n ") + "\r\n");
|
|
||||||
}
|
|
||||||
bldr.append("}");
|
|
||||||
return bldr.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public CompoundTag clone() {
|
@Override
|
||||||
Map<String, Tag> newMap = NBTUtils.cloneMap(this.getValue());
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
List<Tag> tags = NBTIO.readUntilEndTag(in);
|
||||||
|
for(Tag tag : tags) {
|
||||||
|
this.put(tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
NBTIO.writeTags(out, this.value.values());
|
||||||
|
out.writeByte(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompoundTag clone() {
|
||||||
|
Map<String, Tag> newMap = new LinkedHashMap<String, Tag>();
|
||||||
|
for(Entry<String, Tag> entry : this.value.entrySet()) {
|
||||||
|
newMap.put(entry.getKey(), entry.getValue().clone());
|
||||||
|
}
|
||||||
|
|
||||||
return new CompoundTag(this.getName(), newMap);
|
return new CompoundTag(this.getName(), newMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,76 +1,65 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
/*
|
import java.io.DataInputStream;
|
||||||
* OpenNBT License
|
import java.io.DataOutputStream;
|
||||||
*
|
import java.io.IOException;
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Double</code> tag.
|
* A tag containing a double.
|
||||||
*/
|
*/
|
||||||
public final class DoubleTag extends Tag {
|
public class DoubleTag extends Tag {
|
||||||
|
|
||||||
|
private double value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value.
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
*/
|
*/
|
||||||
private final double value;
|
public DoubleTag(String name) {
|
||||||
|
this(name, 0);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
* @param name The name.
|
* @param name The name of the tag.
|
||||||
* @param value The value.
|
* @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 value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public int getId() {
|
||||||
String name = getName();
|
return 6;
|
||||||
String append = "";
|
|
||||||
if(name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
|
||||||
}
|
|
||||||
return "TAG_Double" + append + ": " + value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
this.value = in.readDouble();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeDouble(this.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of this tag.
|
||||||
|
* @param value New value of this tag.
|
||||||
|
*/
|
||||||
|
public void setValue(double value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public DoubleTag clone() {
|
public DoubleTag clone() {
|
||||||
return new DoubleTag(this.getName(), this.getValue());
|
return new DoubleTag(this.getName(), this.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,63 +0,0 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>TAG_End</code> tag.
|
|
||||||
*/
|
|
||||||
public final class EndTag extends Tag {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates the tag.
|
|
||||||
*/
|
|
||||||
public EndTag() {
|
|
||||||
super("");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getValue() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "TAG_End";
|
|
||||||
}
|
|
||||||
|
|
||||||
public EndTag clone() {
|
|
||||||
return new EndTag();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,76 +1,65 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
/*
|
import java.io.DataInputStream;
|
||||||
* OpenNBT License
|
import java.io.DataOutputStream;
|
||||||
*
|
import java.io.IOException;
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Float</code> tag.
|
* A tag containing a float.
|
||||||
*/
|
*/
|
||||||
public final class FloatTag extends Tag {
|
public class FloatTag extends Tag {
|
||||||
|
|
||||||
/**
|
private float value;
|
||||||
* The value.
|
|
||||||
*/
|
|
||||||
private final float value;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
* @param name The name.
|
* @param name The name of the tag.
|
||||||
* @param value The value.
|
*/
|
||||||
|
public FloatTag(String name) {
|
||||||
|
this(name, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
|
* @param value The value of the tag.
|
||||||
*/
|
*/
|
||||||
public FloatTag(String name, float value) {
|
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 value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public int getId() {
|
||||||
String name = getName();
|
return 5;
|
||||||
String append = "";
|
|
||||||
if(name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
|
||||||
}
|
|
||||||
return "TAG_Float" + append + ": " + value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
this.value = in.readFloat();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeFloat(this.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of this tag.
|
||||||
|
* @param value New value of this tag.
|
||||||
|
*/
|
||||||
|
public void setValue(float value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public FloatTag clone() {
|
public FloatTag clone() {
|
||||||
return new FloatTag(this.getName(), this.getValue());
|
return new FloatTag(this.getName(), this.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,109 +1,101 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
import java.io.IOException;
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Int_Array</code> tag.
|
* A tag containing an integer array.
|
||||||
*/
|
*/
|
||||||
public final class IntArrayTag extends Tag {
|
public class IntArrayTag extends Tag {
|
||||||
|
|
||||||
|
private int[] value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value.
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
*/
|
*/
|
||||||
private final int[] value;
|
public IntArrayTag(String name) {
|
||||||
|
this(name, new int[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
*
|
* @param name The name of the tag.
|
||||||
* @param name
|
* @param value The value of the tag.
|
||||||
* The name.
|
|
||||||
* @param value
|
|
||||||
* The value.
|
|
||||||
*/
|
*/
|
||||||
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 value;
|
return this.value.clone();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
StringBuilder hex = new StringBuilder();
|
|
||||||
|
|
||||||
for (int curr : value) {
|
|
||||||
String hexDigits = Integer.toHexString(curr).toUpperCase();
|
|
||||||
|
|
||||||
if (hexDigits.length() == 1) {
|
|
||||||
hex.append("0");
|
|
||||||
}
|
|
||||||
|
|
||||||
hex.append(hexDigits).append(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
String name = getName();
|
|
||||||
String append = "";
|
|
||||||
|
|
||||||
if (name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
|
||||||
}
|
|
||||||
|
|
||||||
return "TAG_Int_Array" + append + ": " + hex.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public int getId() {
|
||||||
if(!(obj instanceof IntArrayTag)) return false;
|
return 11;
|
||||||
|
}
|
||||||
IntArrayTag tag = (IntArrayTag) obj;
|
|
||||||
|
@Override
|
||||||
return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName());
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
this.value = new int[in.readInt()];
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
|
this.value[index] = in.readInt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeInt(this.value.length);
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
|
out.writeInt(this.value[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of this tag.
|
||||||
|
* @param value New value of this tag.
|
||||||
|
*/
|
||||||
|
public void setValue(int[] value) {
|
||||||
|
if(value == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.value = value.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @return The value at the given index.
|
||||||
|
*/
|
||||||
|
public int getValue(int index) {
|
||||||
|
return this.value[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @param value Value to set.
|
||||||
|
*/
|
||||||
|
public void setValue(int index, int value) {
|
||||||
|
this.value[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the length of this tag's array.
|
||||||
|
* @return This tag's array length.
|
||||||
|
*/
|
||||||
|
public int length() {
|
||||||
|
return this.value.length;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IntArrayTag clone() {
|
public IntArrayTag clone() {
|
||||||
int[] clonedArray = this.getValue().clone();
|
return new IntArrayTag(this.getName(), this.getValue());
|
||||||
|
|
||||||
return new IntArrayTag(this.getName(), clonedArray);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,53 +1,28 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
/*
|
import java.io.DataInputStream;
|
||||||
* OpenNBT License
|
import java.io.DataOutputStream;
|
||||||
*
|
import java.io.IOException;
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Int</code> tag.
|
* A tag containing an integer.
|
||||||
*/
|
*/
|
||||||
public final class IntTag extends Tag {
|
public class IntTag extends Tag {
|
||||||
|
|
||||||
/**
|
private int value;
|
||||||
* The value.
|
|
||||||
*/
|
|
||||||
private final int value;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
* @param name The name.
|
* @param name The name of the tag.
|
||||||
* @param value The value.
|
*/
|
||||||
|
public IntTag(String name) {
|
||||||
|
this(name, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
|
* @param value The value of the tag.
|
||||||
*/
|
*/
|
||||||
public IntTag(String name, int value) {
|
public IntTag(String name, int value) {
|
||||||
super(name);
|
super(name);
|
||||||
@ -56,21 +31,35 @@ public final class IntTag extends Tag {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer getValue() {
|
public Integer getValue() {
|
||||||
return value;
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of this tag.
|
||||||
|
* @param value New value of this tag.
|
||||||
|
*/
|
||||||
|
public void setValue(int value) {
|
||||||
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public int getId() {
|
||||||
String name = getName();
|
return 3;
|
||||||
String append = "";
|
|
||||||
if(name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
|
||||||
}
|
|
||||||
return "TAG_Int" + append + ": " + value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
this.value = in.readInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeInt(this.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public IntTag clone() {
|
public IntTag clone() {
|
||||||
return new IntTag(this.getName(), this.getValue());
|
return new IntTag(this.getName(), this.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,74 +1,45 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
/*
|
import java.io.DataInputStream;
|
||||||
* OpenNBT License
|
import java.io.DataOutputStream;
|
||||||
*
|
import java.io.IOException;
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import ch.spacebase.opennbt.NBTUtils;
|
import ch.spacebase.opennbt.TagRegistry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_List</code> tag.
|
* A tag containing a list of tags.
|
||||||
*/
|
*/
|
||||||
public final class ListTag<T extends Tag> extends Tag implements Iterable<T> {
|
public class ListTag<T extends Tag> extends Tag implements Iterable<T> {
|
||||||
|
|
||||||
/**
|
private Class<T> type;
|
||||||
* The type.
|
private List<T> value;
|
||||||
*/
|
|
||||||
private final Class<T> type;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value.
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
*/
|
*/
|
||||||
private final List<T> value;
|
public ListTag(String name) {
|
||||||
|
super(name);
|
||||||
|
this.value = new ArrayList<T>();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
* @param name The name.
|
* @param name The name of the tag.
|
||||||
* @param type The type of item in the list.
|
* @param type Tag type of the list.
|
||||||
*/
|
*/
|
||||||
public ListTag(String name, Class<T> type) {
|
public ListTag(String name, Class<T> type) {
|
||||||
this(name, type, new ArrayList<T>());
|
this(name, type, new ArrayList<T>());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
* @param name The name.
|
* @param name The name of the tag.
|
||||||
* @param type The type of item in the list.
|
* @param type Tag type of the list.
|
||||||
* @param value The value.
|
* @param value The value of the tag.
|
||||||
*/
|
*/
|
||||||
public ListTag(String name, Class<T> type, List<T> value) {
|
public ListTag(String name, Class<T> type, List<T> value) {
|
||||||
super(name);
|
super(name);
|
||||||
@ -76,64 +47,99 @@ public final class ListTag<T extends Tag> extends Tag implements Iterable<T> {
|
|||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the type of item in this list.
|
|
||||||
* @return The type of item in this list.
|
|
||||||
*/
|
|
||||||
public Class<T> getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<T> getValue() {
|
public List<T> getValue() {
|
||||||
return new ArrayList<T>(value);
|
return new ArrayList<T>(this.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean add(T value) {
|
/**
|
||||||
return this.value.add(value);
|
* Adds a tag to this list tag.
|
||||||
|
* @param tag Tag to add.
|
||||||
|
* @return If the list was changed as a result.
|
||||||
|
*/
|
||||||
|
public boolean add(T tag) {
|
||||||
|
return this.value.add(tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean remove(T value) {
|
/**
|
||||||
return this.value.remove(value);
|
* Removes a tag from this list tag.
|
||||||
|
* @param tag Tag to remove.
|
||||||
|
* @return If the list contained the tag.
|
||||||
|
*/
|
||||||
|
public boolean remove(T tag) {
|
||||||
|
return this.value.remove(tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the tag at the given index of this list tag.
|
||||||
|
* @param index Index of the tag.
|
||||||
|
* @return The tag at the given index.
|
||||||
|
*/
|
||||||
public T get(int index) {
|
public T get(int index) {
|
||||||
return this.value.get(index);
|
return this.value.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Iterator<T> iterator() {
|
/**
|
||||||
return this.value.iterator();
|
* Gets the number of tags in 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 String toString() {
|
public Iterator<T> iterator() {
|
||||||
String name = getName();
|
return this.value.iterator();
|
||||||
String append = "";
|
}
|
||||||
if(name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
@Override
|
||||||
|
public int getId() {
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
int id = in.readByte() & 0xFF;
|
||||||
|
this.type = (Class<T>) TagRegistry.getClassFor(id);
|
||||||
|
if(this.type == null) {
|
||||||
|
throw new IOException("Unknown tag ID in ListTag: " + id);
|
||||||
}
|
}
|
||||||
StringBuilder bldr = new StringBuilder();
|
|
||||||
bldr.append("TAG_List" + append + ": " + value.size() + " entries of type " + NBTUtils.getTypeName(type) + "\r\n{\r\n");
|
int count = in.readInt();
|
||||||
for(Tag t : value) {
|
for(int index = 0; index < count; index++) {
|
||||||
bldr.append(" " + t.toString().replaceAll("\r\n", "\r\n ") + "\r\n");
|
Tag tag = TagRegistry.createInstance(id, "");
|
||||||
|
if(tag == null) {
|
||||||
|
throw new IOException("Tag could not be created: \"" + this.type.getSimpleName() + "\" (" + id + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
tag.read(in);
|
||||||
|
this.add((T) tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
int id = TagRegistry.getIdFor(this.type);
|
||||||
|
if(id == -1) {
|
||||||
|
throw new IOException("ListTag contains unregistered tag class.");
|
||||||
|
}
|
||||||
|
|
||||||
|
out.writeByte(id);
|
||||||
|
out.writeInt(this.value.size());
|
||||||
|
for(Tag tag : this.value) {
|
||||||
|
tag.write(out);
|
||||||
}
|
}
|
||||||
bldr.append("}");
|
|
||||||
return bldr.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public ListTag<T> clone() {
|
public ListTag<T> clone() {
|
||||||
List<T> newList = new ArrayList<T>();
|
List<T> newList = new ArrayList<T>();
|
||||||
|
for(T value : this.value) {
|
||||||
for(T value : this.getValue()) {
|
|
||||||
newList.add((T) value.clone());
|
newList.add((T) value.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ListTag<T>(this.getName(), this.getType(), newList);
|
return new ListTag<T>(this.getName(), this.type, newList);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,76 +1,65 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
/*
|
import java.io.DataInputStream;
|
||||||
* OpenNBT License
|
import java.io.DataOutputStream;
|
||||||
*
|
import java.io.IOException;
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Long</code> tag.
|
* A tag containing a long.
|
||||||
*/
|
*/
|
||||||
public final class LongTag extends Tag {
|
public class LongTag extends Tag {
|
||||||
|
|
||||||
/**
|
private long value;
|
||||||
* The value.
|
|
||||||
*/
|
|
||||||
private final long value;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
* @param name The name.
|
* @param name The name of the tag.
|
||||||
* @param value The value.
|
*/
|
||||||
|
public LongTag(String name) {
|
||||||
|
this(name, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
|
* @param value The value of the tag.
|
||||||
*/
|
*/
|
||||||
public LongTag(String name, long value) {
|
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 value;
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of this tag.
|
||||||
|
* @param value New value of this tag.
|
||||||
|
*/
|
||||||
|
public void setValue(long value) {
|
||||||
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public int getId() {
|
||||||
String name = getName();
|
return 4;
|
||||||
String append = "";
|
|
||||||
if(name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
|
||||||
}
|
|
||||||
return "TAG_Long" + append + ": " + value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
this.value = in.readLong();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeLong(this.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public LongTag clone() {
|
public LongTag clone() {
|
||||||
return new LongTag(this.getName(), this.getValue());
|
return new LongTag(this.getName(), this.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,76 +1,65 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
/*
|
import java.io.DataInputStream;
|
||||||
* OpenNBT License
|
import java.io.DataOutputStream;
|
||||||
*
|
import java.io.IOException;
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Short</code> tag.
|
* A tag containing a short.
|
||||||
*/
|
*/
|
||||||
public final class ShortTag extends Tag {
|
public class ShortTag extends Tag {
|
||||||
|
|
||||||
/**
|
private short value;
|
||||||
* The value.
|
|
||||||
*/
|
|
||||||
private final short value;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
* @param name The name.
|
* @param name The name of the tag.
|
||||||
* @param value The value.
|
*/
|
||||||
|
public ShortTag(String name) {
|
||||||
|
this(name, (short) 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
|
* @param value The value of the tag.
|
||||||
*/
|
*/
|
||||||
public ShortTag(String name, short value) {
|
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 value;
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of this tag.
|
||||||
|
* @param value New value of this tag.
|
||||||
|
*/
|
||||||
|
public void setValue(short value) {
|
||||||
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public int getId() {
|
||||||
String name = getName();
|
return 2;
|
||||||
String append = "";
|
|
||||||
if(name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
|
||||||
}
|
|
||||||
return "TAG_Short" + append + ": " + value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
this.value = in.readShort();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeShort(this.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public ShortTag clone() {
|
public ShortTag clone() {
|
||||||
return new ShortTag(this.getName(), this.getValue());
|
return new ShortTag(this.getName(), this.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,53 +1,30 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
/*
|
import java.io.DataInputStream;
|
||||||
* OpenNBT License
|
import java.io.DataOutputStream;
|
||||||
*
|
import java.io.IOException;
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
import ch.spacebase.opennbt.NBTIO;
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_String</code> tag.
|
* A tag containing a string.
|
||||||
*/
|
*/
|
||||||
public final class StringTag extends Tag {
|
public class StringTag extends Tag {
|
||||||
|
|
||||||
/**
|
private String value;
|
||||||
* The value.
|
|
||||||
*/
|
|
||||||
private final String value;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
* @param name The name.
|
* @param name The name of the tag.
|
||||||
* @param value The value.
|
*/
|
||||||
|
public StringTag(String name) {
|
||||||
|
this(name, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
|
* @param value The value of the tag.
|
||||||
*/
|
*/
|
||||||
public StringTag(String name, String value) {
|
public StringTag(String name, String value) {
|
||||||
super(name);
|
super(name);
|
||||||
@ -56,19 +33,37 @@ public final class StringTag extends Tag {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getValue() {
|
public String getValue() {
|
||||||
return value;
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of this tag.
|
||||||
|
* @param value New value of this tag.
|
||||||
|
*/
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public int getId() {
|
||||||
String name = getName();
|
return 8;
|
||||||
String append = "";
|
|
||||||
if(name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
|
||||||
}
|
|
||||||
return "TAG_String" + append + ": " + value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
byte[] bytes = new byte[in.readShort()];
|
||||||
|
in.readFully(bytes);
|
||||||
|
this.value = new String(bytes, NBTIO.CHARSET);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
byte[] bytes = this.value.getBytes(NBTIO.CHARSET);
|
||||||
|
out.writeShort(bytes.length);
|
||||||
|
out.write(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public StringTag clone() {
|
public StringTag clone() {
|
||||||
return new StringTag(this.getName(), this.getValue());
|
return new StringTag(this.getName(), this.getValue());
|
||||||
}
|
}
|
||||||
|
@ -1,51 +1,22 @@
|
|||||||
package ch.spacebase.opennbt.tag;
|
package ch.spacebase.opennbt.tag;
|
||||||
|
|
||||||
/*
|
import java.io.DataInputStream;
|
||||||
* OpenNBT License
|
import java.io.DataOutputStream;
|
||||||
*
|
import java.io.IOException;
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
import java.lang.reflect.Array;
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a single NBT tag.
|
* Represents an NBT tag.
|
||||||
|
*
|
||||||
|
* All tags must have a constructor with a single string parameter for reading tags.
|
||||||
|
* 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of this tag.
|
* Creates a tag with the specified name.
|
||||||
*/
|
|
||||||
private final String name;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates the tag with the specified name.
|
|
||||||
* @param name The name.
|
* @param name The name.
|
||||||
*/
|
*/
|
||||||
public Tag(String name) {
|
public Tag(String name) {
|
||||||
@ -57,7 +28,7 @@ public abstract class Tag implements Cloneable {
|
|||||||
* @return The name of this tag.
|
* @return The name of this tag.
|
||||||
*/
|
*/
|
||||||
public final String getName() {
|
public final String getName() {
|
||||||
return name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,15 +37,89 @@ public abstract class Tag implements Cloneable {
|
|||||||
*/
|
*/
|
||||||
public abstract Object getValue();
|
public abstract Object getValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the type id of this tag.
|
||||||
|
* @return The tag's id.
|
||||||
|
*/
|
||||||
|
public abstract int getId();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads this tag from an input stream.
|
||||||
|
* @param in Stream to write to.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public abstract void read(DataInputStream in) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes this tag to an output stream.
|
||||||
|
* @param out Stream to write to.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
|
public abstract void write(DataOutputStream out) throws IOException;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if(!(obj instanceof Tag)) return false;
|
if(!(obj instanceof Tag)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
Tag tag = (Tag) obj;
|
Tag tag = (Tag) obj;
|
||||||
|
if(!this.getName().equals(tag.getName())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return this.getValue().equals(tag.getValue()) && this.getName().equals(tag.getName());
|
if(this.getValue() == null) {
|
||||||
|
return tag.getValue() == null;
|
||||||
|
} else if(tag.getValue() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.getValue().getClass().isArray() && tag.getValue().getClass().isArray()) {
|
||||||
|
int length = Array.getLength(this.getValue());
|
||||||
|
if(Array.getLength(tag.getValue()) != length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int index = 0; index < length; index++) {
|
||||||
|
Object o = Array.get(this.getValue(), index);
|
||||||
|
Object other = Array.get(tag.getValue(), index);
|
||||||
|
if(o == null && other != null || o != null && !o.equals(other)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.getValue().equals(tag.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String name = this.getName() != null && !this.getName().equals("") ? "(" + this.getName() + ")" : "";
|
||||||
|
String value = "";
|
||||||
|
if(this.getValue() != null) {
|
||||||
|
value = this.getValue().toString();
|
||||||
|
if(this.getValue().getClass().isArray()) {
|
||||||
|
StringBuilder build = new StringBuilder();
|
||||||
|
build.append("[");
|
||||||
|
for(int index = 0; index < Array.getLength(this.getValue()); index++) {
|
||||||
|
if(index > 0) {
|
||||||
|
build.append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
build.append(Array.get(this.getValue(), index));
|
||||||
|
}
|
||||||
|
|
||||||
|
build.append("]");
|
||||||
|
value = build.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.getClass().getSimpleName() + name + " { " + value + " }";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public abstract Tag clone();
|
public abstract Tag clone();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,97 +1,103 @@
|
|||||||
package ch.spacebase.opennbt.tag.custom;
|
package ch.spacebase.opennbt.tag.custom;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
import ch.spacebase.opennbt.tag.Tag;
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Double_Array</code> tag.
|
* A tag containing a double array.
|
||||||
*/
|
*/
|
||||||
public final class DoubleArrayTag extends Tag {
|
public class DoubleArrayTag extends Tag {
|
||||||
|
|
||||||
|
private double[] value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value.
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
*/
|
*/
|
||||||
private final double[] value;
|
public DoubleArrayTag(String name) {
|
||||||
|
this(name, new double[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
*
|
* @param name The name of the tag.
|
||||||
* @param name
|
* @param value The value of the tag.
|
||||||
* The name.
|
|
||||||
* @param value
|
|
||||||
* The value.
|
|
||||||
*/
|
*/
|
||||||
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 value;
|
return this.value.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public String toString() {
|
* Sets the value of this tag.
|
||||||
String name = getName();
|
* @param value New value of this tag.
|
||||||
String append = "";
|
*/
|
||||||
|
public void setValue(double[] value) {
|
||||||
if (name != null && !name.equals("")) {
|
if(value == null) {
|
||||||
append = "(\"" + this.getName() + "\")";
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "TAG_Double_Array" + append + ": " + Arrays.toString(value);
|
this.value = value.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @return The value at the given index.
|
||||||
|
*/
|
||||||
|
public double getValue(int index) {
|
||||||
|
return this.value[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @param value Value to set.
|
||||||
|
*/
|
||||||
|
public void setValue(int index, double value) {
|
||||||
|
this.value[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the length of this tag's array.
|
||||||
|
* @return This tag's array length.
|
||||||
|
*/
|
||||||
|
public int length() {
|
||||||
|
return this.value.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public int getId() {
|
||||||
if(!(obj instanceof DoubleArrayTag)) return false;
|
return 60;
|
||||||
|
|
||||||
DoubleArrayTag tag = (DoubleArrayTag) obj;
|
|
||||||
|
|
||||||
return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DoubleArrayTag clone() {
|
public void read(DataInputStream in) throws IOException {
|
||||||
double[] clonedArray = this.getValue().clone();
|
this.value = new double[in.readInt()];
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
return new DoubleArrayTag(this.getName(), clonedArray);
|
this.value[index] = in.readDouble();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeInt(this.value.length);
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
|
out.writeDouble(this.value[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DoubleArrayTag clone() {
|
||||||
|
return new DoubleArrayTag(this.getName(), this.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,97 +1,103 @@
|
|||||||
package ch.spacebase.opennbt.tag.custom;
|
package ch.spacebase.opennbt.tag.custom;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
import ch.spacebase.opennbt.tag.Tag;
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Float_Array</code> tag.
|
* A tag containing a float array.
|
||||||
*/
|
*/
|
||||||
public final class FloatArrayTag extends Tag {
|
public class FloatArrayTag extends Tag {
|
||||||
|
|
||||||
|
private float[] value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value.
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
*/
|
*/
|
||||||
private final float[] value;
|
public FloatArrayTag(String name) {
|
||||||
|
this(name, new float[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
*
|
* @param name The name of the tag.
|
||||||
* @param name
|
* @param value The value of the tag.
|
||||||
* The name.
|
|
||||||
* @param value
|
|
||||||
* The value.
|
|
||||||
*/
|
*/
|
||||||
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 value;
|
return this.value.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public String toString() {
|
* Sets the value of this tag.
|
||||||
String name = getName();
|
* @param value New value of this tag.
|
||||||
String append = "";
|
*/
|
||||||
|
public void setValue(float[] value) {
|
||||||
if (name != null && !name.equals("")) {
|
if(value == null) {
|
||||||
append = "(\"" + this.getName() + "\")";
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "TAG_Float_Array" + append + ": " + Arrays.toString(value);
|
this.value = value.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @return The value at the given index.
|
||||||
|
*/
|
||||||
|
public float getValue(int index) {
|
||||||
|
return this.value[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @param value Value to set.
|
||||||
|
*/
|
||||||
|
public void setValue(int index, float value) {
|
||||||
|
this.value[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the length of this tag's array.
|
||||||
|
* @return This tag's array length.
|
||||||
|
*/
|
||||||
|
public int length() {
|
||||||
|
return this.value.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public int getId() {
|
||||||
if(!(obj instanceof FloatArrayTag)) return false;
|
return 61;
|
||||||
|
|
||||||
FloatArrayTag tag = (FloatArrayTag) obj;
|
|
||||||
|
|
||||||
return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FloatArrayTag clone() {
|
public void read(DataInputStream in) throws IOException {
|
||||||
float[] clonedArray = this.getValue().clone();
|
this.value = new float[in.readInt()];
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
return new FloatArrayTag(this.getName(), clonedArray);
|
this.value[index] = in.readFloat();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeInt(this.value.length);
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
|
out.writeFloat(this.value[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FloatArrayTag clone() {
|
||||||
|
return new FloatArrayTag(this.getName(), this.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,97 +1,103 @@
|
|||||||
package ch.spacebase.opennbt.tag.custom;
|
package ch.spacebase.opennbt.tag.custom;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
import ch.spacebase.opennbt.tag.Tag;
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Long_Array</code> tag.
|
* A tag containing a long array.
|
||||||
*/
|
*/
|
||||||
public final class LongArrayTag extends Tag {
|
public class LongArrayTag extends Tag {
|
||||||
|
|
||||||
|
private long[] value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value.
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
*/
|
*/
|
||||||
private final long[] value;
|
public LongArrayTag(String name) {
|
||||||
|
this(name, new long[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
*
|
* @param name The name of the tag.
|
||||||
* @param name
|
* @param value The value of the tag.
|
||||||
* The name.
|
|
||||||
* @param value
|
|
||||||
* The value.
|
|
||||||
*/
|
*/
|
||||||
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 value;
|
return this.value.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public String toString() {
|
* Sets the value of this tag.
|
||||||
String name = getName();
|
* @param value New value of this tag.
|
||||||
String append = "";
|
*/
|
||||||
|
public void setValue(long[] value) {
|
||||||
if (name != null && !name.equals("")) {
|
if(value == null) {
|
||||||
append = "(\"" + this.getName() + "\")";
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "TAG_Long_Array" + append + ": " + Arrays.toString(value);
|
this.value = value.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @return The value at the given index.
|
||||||
|
*/
|
||||||
|
public long getValue(int index) {
|
||||||
|
return this.value[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @param value Value to set.
|
||||||
|
*/
|
||||||
|
public void setValue(int index, long value) {
|
||||||
|
this.value[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the length of this tag's array.
|
||||||
|
* @return This tag's array length.
|
||||||
|
*/
|
||||||
|
public int length() {
|
||||||
|
return this.value.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public int getId() {
|
||||||
if(!(obj instanceof LongArrayTag)) return false;
|
return 62;
|
||||||
|
|
||||||
LongArrayTag tag = (LongArrayTag) obj;
|
|
||||||
|
|
||||||
return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LongArrayTag clone() {
|
public void read(DataInputStream in) throws IOException {
|
||||||
long[] clonedArray = this.getValue().clone();
|
this.value = new long[in.readInt()];
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
return new LongArrayTag(this.getName(), clonedArray);
|
this.value[index] = in.readLong();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeInt(this.value.length);
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
|
out.writeLong(this.value[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LongArrayTag clone() {
|
||||||
|
return new LongArrayTag(this.getName(), this.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,96 +0,0 @@
|
|||||||
package ch.spacebase.opennbt.tag.custom;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>TAG_Object_Array</code> tag.
|
|
||||||
*/
|
|
||||||
public final class ObjectArrayTag extends Tag {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The value.
|
|
||||||
*/
|
|
||||||
private final Object[] value;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates the tag.
|
|
||||||
*
|
|
||||||
* @param name
|
|
||||||
* The name.
|
|
||||||
* @param value
|
|
||||||
* The value.
|
|
||||||
*/
|
|
||||||
public ObjectArrayTag(String name, Object[] value) {
|
|
||||||
super(name);
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object[] getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
String name = getName();
|
|
||||||
String append = "";
|
|
||||||
|
|
||||||
if (name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
|
||||||
}
|
|
||||||
|
|
||||||
return "TAG_Object_Array" + append + ": " + Arrays.toString(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if(!(obj instanceof ObjectArrayTag)) return false;
|
|
||||||
|
|
||||||
ObjectArrayTag tag = (ObjectArrayTag) obj;
|
|
||||||
|
|
||||||
return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ObjectArrayTag clone() {
|
|
||||||
Object[] clonedArray = this.getValue().clone();
|
|
||||||
return new ObjectArrayTag(this.getName(), clonedArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,78 +0,0 @@
|
|||||||
package ch.spacebase.opennbt.tag.custom;
|
|
||||||
|
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>TAG_Object</code> tag.
|
|
||||||
*/
|
|
||||||
public final class ObjectTag extends Tag {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The value.
|
|
||||||
*/
|
|
||||||
private final Object value;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates the tag.
|
|
||||||
* @param name The name.
|
|
||||||
* @param value The value.
|
|
||||||
*/
|
|
||||||
public ObjectTag(String name, Object value) {
|
|
||||||
super(name);
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
String name = getName();
|
|
||||||
String append = "";
|
|
||||||
if(name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
|
||||||
}
|
|
||||||
return "TAG_Object" + append + ": " + value.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ObjectTag clone() {
|
|
||||||
return new ObjectTag(this.getName(), this.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,112 @@
|
|||||||
|
package ch.spacebase.opennbt.tag.custom;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import ch.spacebase.opennbt.tag.Tag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A tag containing an array of serializable objects.
|
||||||
|
*/
|
||||||
|
public class SerializableArrayTag extends Tag {
|
||||||
|
|
||||||
|
private Serializable[] value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
|
*/
|
||||||
|
public SerializableArrayTag(String name) {
|
||||||
|
this(name, new Serializable[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
|
* @param value The value of the tag.
|
||||||
|
*/
|
||||||
|
public SerializableArrayTag(String name, Serializable[] value) {
|
||||||
|
super(name);
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Serializable[] getValue() {
|
||||||
|
return this.value.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of this tag.
|
||||||
|
* @param value New value of this tag.
|
||||||
|
*/
|
||||||
|
public void setValue(Serializable[] value) {
|
||||||
|
if(value == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.value = value.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @return The value at the given index.
|
||||||
|
*/
|
||||||
|
public Serializable getValue(int index) {
|
||||||
|
return this.value[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @param value Value to set.
|
||||||
|
*/
|
||||||
|
public void setValue(int index, Serializable value) {
|
||||||
|
this.value[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the length of this tag's array.
|
||||||
|
* @return This tag's array length.
|
||||||
|
*/
|
||||||
|
public int length() {
|
||||||
|
return this.value.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getId() {
|
||||||
|
return 63;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
this.value = new Serializable[in.readInt()];
|
||||||
|
ObjectInputStream str = new ObjectInputStream(in);
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
|
try {
|
||||||
|
this.value[index] = (Serializable) str.readObject();
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new IOException("Class not found while reading SerializableArrayTag!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeInt(this.value.length);
|
||||||
|
ObjectOutputStream str = new ObjectOutputStream(out);
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
|
str.writeObject(this.value[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SerializableArrayTag clone() {
|
||||||
|
return new SerializableArrayTag(this.getName(), this.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
package ch.spacebase.opennbt.tag.custom;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import ch.spacebase.opennbt.tag.Tag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A tag containing a serializable object.
|
||||||
|
*/
|
||||||
|
public class SerializableTag extends Tag {
|
||||||
|
|
||||||
|
private Serializable value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
|
*/
|
||||||
|
public SerializableTag(String name) {
|
||||||
|
this(name, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
|
* @param value The value of the tag.
|
||||||
|
*/
|
||||||
|
public SerializableTag(String name, Serializable value) {
|
||||||
|
super(name);
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Serializable getValue() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of this tag.
|
||||||
|
* @param value New value of this tag.
|
||||||
|
*/
|
||||||
|
public void setValue(Serializable value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getId() {
|
||||||
|
return 64;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
ObjectInputStream str = new ObjectInputStream(in);
|
||||||
|
try {
|
||||||
|
this.value = (Serializable) str.readObject();
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new IOException("Class not found while reading SerializableTag!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
ObjectOutputStream str = new ObjectOutputStream(out);
|
||||||
|
str.writeObject(this.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SerializableTag clone() {
|
||||||
|
return new SerializableTag(this.getName(), this.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,97 +1,103 @@
|
|||||||
package ch.spacebase.opennbt.tag.custom;
|
package ch.spacebase.opennbt.tag.custom;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
import ch.spacebase.opennbt.tag.Tag;
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_Short_Array</code> tag.
|
* A tag containing a short array.
|
||||||
*/
|
*/
|
||||||
public final class ShortArrayTag extends Tag {
|
public class ShortArrayTag extends Tag {
|
||||||
|
|
||||||
|
private short[] value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value.
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
*/
|
*/
|
||||||
private final short[] value;
|
public ShortArrayTag(String name) {
|
||||||
|
this(name, new short[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
*
|
* @param name The name of the tag.
|
||||||
* @param name
|
* @param value The value of the tag.
|
||||||
* The name.
|
|
||||||
* @param value
|
|
||||||
* The value.
|
|
||||||
*/
|
*/
|
||||||
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 value;
|
return this.value.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public String toString() {
|
* Sets the value of this tag.
|
||||||
String name = getName();
|
* @param value New value of this tag.
|
||||||
String append = "";
|
*/
|
||||||
|
public void setValue(short[] value) {
|
||||||
if (name != null && !name.equals("")) {
|
if(value == null) {
|
||||||
append = "(\"" + this.getName() + "\")";
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "TAG_Short_Array" + append + ": " + Arrays.toString(value);
|
this.value = value.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @return The value at the given index.
|
||||||
|
*/
|
||||||
|
public short getValue(int index) {
|
||||||
|
return this.value[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @param value Value to set.
|
||||||
|
*/
|
||||||
|
public void setValue(int index, short value) {
|
||||||
|
this.value[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the length of this tag's array.
|
||||||
|
* @return This tag's array length.
|
||||||
|
*/
|
||||||
|
public int length() {
|
||||||
|
return this.value.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public int getId() {
|
||||||
if(!(obj instanceof ShortArrayTag)) return false;
|
return 65;
|
||||||
|
|
||||||
ShortArrayTag tag = (ShortArrayTag) obj;
|
|
||||||
|
|
||||||
return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ShortArrayTag clone() {
|
public void read(DataInputStream in) throws IOException {
|
||||||
short[] clonedArray = this.getValue().clone();
|
this.value = new short[in.readInt()];
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
return new ShortArrayTag(this.getName(), clonedArray);
|
this.value[index] = in.readShort();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeInt(this.value.length);
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
|
out.writeShort(this.value[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ShortArrayTag clone() {
|
||||||
|
return new ShortArrayTag(this.getName(), this.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,97 +1,108 @@
|
|||||||
package ch.spacebase.opennbt.tag.custom;
|
package ch.spacebase.opennbt.tag.custom;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import ch.spacebase.opennbt.NBTIO;
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
import ch.spacebase.opennbt.tag.Tag;
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>TAG_String_Array</code> tag.
|
* A tag containing a string array.
|
||||||
*/
|
*/
|
||||||
public final class StringArrayTag extends Tag {
|
public class StringArrayTag extends Tag {
|
||||||
|
|
||||||
|
private String[] value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value.
|
* Creates a tag with the specified name.
|
||||||
|
* @param name The name of the tag.
|
||||||
*/
|
*/
|
||||||
private final String[] value;
|
public StringArrayTag(String name) {
|
||||||
|
this(name, new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the tag.
|
* Creates a tag with the specified name.
|
||||||
*
|
* @param name The name of the tag.
|
||||||
* @param name
|
* @param value The value of the tag.
|
||||||
* The name.
|
|
||||||
* @param value
|
|
||||||
* The value.
|
|
||||||
*/
|
*/
|
||||||
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 value;
|
return this.value.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public String toString() {
|
* Sets the value of this tag.
|
||||||
String name = getName();
|
* @param value New value of this tag.
|
||||||
String append = "";
|
*/
|
||||||
|
public void setValue(String[] value) {
|
||||||
if (name != null && !name.equals("")) {
|
if(value == null) {
|
||||||
append = "(\"" + this.getName() + "\")";
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "TAG_String_Array" + append + ": " + Arrays.toString(value);
|
this.value = value.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @return The value at the given index.
|
||||||
|
*/
|
||||||
|
public String getValue(int index) {
|
||||||
|
return this.value[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in this tag's array.
|
||||||
|
* @param index Index of the value.
|
||||||
|
* @param value Value to set.
|
||||||
|
*/
|
||||||
|
public void setValue(int index, String value) {
|
||||||
|
this.value[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the length of this tag's array.
|
||||||
|
* @return This tag's array length.
|
||||||
|
*/
|
||||||
|
public int length() {
|
||||||
|
return this.value.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public int getId() {
|
||||||
if(!(obj instanceof StringArrayTag)) return false;
|
return 66;
|
||||||
|
}
|
||||||
StringArrayTag tag = (StringArrayTag) obj;
|
|
||||||
|
@Override
|
||||||
return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName());
|
public void read(DataInputStream in) throws IOException {
|
||||||
|
this.value = new String[in.readInt()];
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
|
byte[] bytes = new byte[in.readShort()];
|
||||||
|
in.readFully(bytes);
|
||||||
|
this.value[index] = new String(bytes, NBTIO.CHARSET);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream out) throws IOException {
|
||||||
|
out.writeInt(this.value.length);
|
||||||
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
|
byte[] bytes = this.value[index].getBytes(NBTIO.CHARSET);
|
||||||
|
out.writeShort(bytes.length);
|
||||||
|
out.write(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StringArrayTag clone() {
|
public StringArrayTag clone() {
|
||||||
String[] clonedArray = this.getValue().clone();
|
return new StringArrayTag(this.getName(), this.getValue());
|
||||||
|
|
||||||
return new StringArrayTag(this.getName(), clonedArray);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,81 +0,0 @@
|
|||||||
package ch.spacebase.opennbt.tag.custom;
|
|
||||||
|
|
||||||
import ch.spacebase.opennbt.tag.Tag;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>TAG_Unknown</code> tag.
|
|
||||||
*/
|
|
||||||
public class UnknownTag extends Tag {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates the tag.
|
|
||||||
* @param name The name.
|
|
||||||
* @param value The value.
|
|
||||||
*/
|
|
||||||
public UnknownTag(String name) {
|
|
||||||
super(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getValue() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
String name = getName();
|
|
||||||
String append = "";
|
|
||||||
if(name != null && !name.equals("")) {
|
|
||||||
append = "(\"" + this.getName() + "\")";
|
|
||||||
}
|
|
||||||
return "TAG_Unknown" + append;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if(!(obj instanceof UnknownTag)) return false;
|
|
||||||
|
|
||||||
UnknownTag tag = (UnknownTag) obj;
|
|
||||||
|
|
||||||
return tag.getName().equals(this.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public UnknownTag clone() {
|
|
||||||
return new UnknownTag(this.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,78 +0,0 @@
|
|||||||
package ch.spacebase.opennbt.utils;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* OpenNBT License
|
|
||||||
*
|
|
||||||
* JNBT Copyright (c) 2010 Graham Edgecombe
|
|
||||||
* OpenNBT Copyright(c) 2012 Steveice10
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* * Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* * Neither the name of the JNBT team nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A hashmap using two ints as keys.
|
|
||||||
*/
|
|
||||||
public class DoubleIntHashMap<T> extends HashMap<Integer, T> {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
public DoubleIntHashMap() {
|
|
||||||
super(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DoubleIntHashMap(int capacity) {
|
|
||||||
super(capacity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public T put(int key1, int key2, T value) {
|
|
||||||
int key = key(key1, key2);
|
|
||||||
return super.put(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public T get(int key1, int key2) {
|
|
||||||
int key = key(key1, key2);
|
|
||||||
return super.get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean containsKey(int key1, int key2) {
|
|
||||||
int key = key(key1, key2);
|
|
||||||
return super.containsKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public T remove(int key1, int key2) {
|
|
||||||
int key = key(key1, key2);
|
|
||||||
return super.remove(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final int key(int x, int z) {
|
|
||||||
return (x & 0xF) << 11 | (z & 0xF) << 7;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user