mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-06-29 12:14:42 +02:00
Move all files! Hahah
This commit is contained in:
@ -0,0 +1,52 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
/**
|
||||
* The {@code TAG_Byte_Array} tag.
|
||||
*/
|
||||
public final class ByteArrayTag extends Tag {
|
||||
private final byte[] value;
|
||||
|
||||
/**
|
||||
* Creates the tag with an empty name.
|
||||
*
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public ByteArrayTag(final byte[] value) {
|
||||
super();
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public ByteArrayTag(final String name, final byte[] value) {
|
||||
super(name);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder hex = new StringBuilder();
|
||||
for (final byte b : this.value) {
|
||||
final String hexDigits = Integer.toHexString(b).toUpperCase();
|
||||
if (hexDigits.length() == 1) {
|
||||
hex.append("0");
|
||||
}
|
||||
hex.append(hexDigits).append(" ");
|
||||
}
|
||||
final String name = getName();
|
||||
String append = "";
|
||||
if ((name != null) && !name.equals("")) {
|
||||
append = "(\"" + this.getName() + "\")";
|
||||
}
|
||||
return "TAG_Byte_Array" + append + ": " + hex;
|
||||
}
|
||||
}
|
44
src/main/java/com/intellectualcrafters/jnbt/ByteTag.java
Normal file
44
src/main/java/com/intellectualcrafters/jnbt/ByteTag.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
/**
|
||||
* The {@code TAG_Byte} tag.
|
||||
*/
|
||||
public final class ByteTag extends Tag {
|
||||
private final byte value;
|
||||
|
||||
/**
|
||||
* Creates the tag with an empty name.
|
||||
*
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public ByteTag(final byte value) {
|
||||
super();
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public ByteTag(final String name, final byte value) {
|
||||
super(name);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final String name = getName();
|
||||
String append = "";
|
||||
if ((name != null) && !name.equals("")) {
|
||||
append = "(\"" + this.getName() + "\")";
|
||||
}
|
||||
return "TAG_Byte" + append + ": " + this.value;
|
||||
}
|
||||
}
|
380
src/main/java/com/intellectualcrafters/jnbt/CompoundTag.java
Normal file
380
src/main/java/com/intellectualcrafters/jnbt/CompoundTag.java
Normal file
@ -0,0 +1,380 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The {@code TAG_Compound} tag.
|
||||
*/
|
||||
public final class CompoundTag extends Tag {
|
||||
private final Map<String, Tag> value;
|
||||
|
||||
/**
|
||||
* Creates the tag with an empty name.
|
||||
*
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public CompoundTag(final Map<String, Tag> value) {
|
||||
super();
|
||||
this.value = Collections.unmodifiableMap(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public CompoundTag(final String name, final Map<String, Tag> value) {
|
||||
super(name);
|
||||
this.value = Collections.unmodifiableMap(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this compound tag contains the given key.
|
||||
*
|
||||
* @param key the given key
|
||||
*
|
||||
* @return true if the tag contains the given key
|
||||
*/
|
||||
public boolean containsKey(final String key) {
|
||||
return this.value.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Tag> getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new compound tag with the given values.
|
||||
*
|
||||
* @param value the value
|
||||
*
|
||||
* @return the new compound tag
|
||||
*/
|
||||
public CompoundTag setValue(final Map<String, Tag> value) {
|
||||
return new CompoundTag(getName(), value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a compound tag builder.
|
||||
*
|
||||
* @return the builder
|
||||
*/
|
||||
public CompoundTagBuilder createBuilder() {
|
||||
return new CompoundTagBuilder(new HashMap<String, Tag>(this.value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a byte array named with the given key. <p> If the key does not exist or its value is not a byte array
|
||||
* tag, then an empty byte array will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return a byte array
|
||||
*/
|
||||
public byte[] getByteArray(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof ByteArrayTag) {
|
||||
return ((ByteArrayTag) tag).getValue();
|
||||
} else {
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a byte named with the given key. <p> If the key does not exist or its value is not a byte tag, then
|
||||
* {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return a byte
|
||||
*/
|
||||
public byte getByte(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof ByteTag) {
|
||||
return ((ByteTag) tag).getValue();
|
||||
} else {
|
||||
return (byte) 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a double named with the given key. <p> If the key does not exist or its value is not a double tag, then
|
||||
* {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return a double
|
||||
*/
|
||||
public double getDouble(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof DoubleTag) {
|
||||
return ((DoubleTag) tag).getValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a double named with the given key, even if it's another type of number. <p> If the key does not exist or
|
||||
* its value is not a number, then {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return a double
|
||||
*/
|
||||
public double asDouble(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof ByteTag) {
|
||||
return ((ByteTag) tag).getValue();
|
||||
} else if (tag instanceof ShortTag) {
|
||||
return ((ShortTag) tag).getValue();
|
||||
} else if (tag instanceof IntTag) {
|
||||
return ((IntTag) tag).getValue();
|
||||
} else if (tag instanceof LongTag) {
|
||||
return ((LongTag) tag).getValue();
|
||||
} else if (tag instanceof FloatTag) {
|
||||
return ((FloatTag) tag).getValue();
|
||||
} else if (tag instanceof DoubleTag) {
|
||||
return ((DoubleTag) tag).getValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a float named with the given key. <p> If the key does not exist or its value is not a float tag, then
|
||||
* {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return a float
|
||||
*/
|
||||
public float getFloat(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof FloatTag) {
|
||||
return ((FloatTag) tag).getValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a {@code int[]} named with the given key. <p> If the key does not exist or its value is not an int array
|
||||
* tag, then an empty array will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return an int array
|
||||
*/
|
||||
public int[] getIntArray(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof IntArrayTag) {
|
||||
return ((IntArrayTag) tag).getValue();
|
||||
} else {
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an int named with the given key. <p> If the key does not exist or its value is not an int tag, then
|
||||
* {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return an int
|
||||
*/
|
||||
public int getInt(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof IntTag) {
|
||||
return ((IntTag) tag).getValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an int named with the given key, even if it's another type of number. <p> If the key does not exist or
|
||||
* its value is not a number, then {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return an int
|
||||
*/
|
||||
public int asInt(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof ByteTag) {
|
||||
return ((ByteTag) tag).getValue();
|
||||
} else if (tag instanceof ShortTag) {
|
||||
return ((ShortTag) tag).getValue();
|
||||
} else if (tag instanceof IntTag) {
|
||||
return ((IntTag) tag).getValue();
|
||||
} else if (tag instanceof LongTag) {
|
||||
return ((LongTag) tag).getValue().intValue();
|
||||
} else if (tag instanceof FloatTag) {
|
||||
return ((FloatTag) tag).getValue().intValue();
|
||||
} else if (tag instanceof DoubleTag) {
|
||||
return ((DoubleTag) tag).getValue().intValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tags named with the given key. <p> If the key does not exist or its value is not a list tag,
|
||||
* then an empty list will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return a list of tags
|
||||
*/
|
||||
public List<Tag> getList(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof ListTag) {
|
||||
return ((ListTag) tag).getValue();
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a {@code TagList} named with the given key. <p> If the key does not exist or its value is not a list
|
||||
* tag, then an empty tag list will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return a tag list instance
|
||||
*/
|
||||
public ListTag getListTag(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof ListTag) {
|
||||
return (ListTag) tag;
|
||||
} else {
|
||||
return new ListTag(key, StringTag.class, Collections.<Tag> emptyList());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tags named with the given key. <p> If the key does not exist or its value is not a list tag,
|
||||
* then an empty list will be returned. If the given key references a list but the list of of a different type, then
|
||||
* an empty list will also be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
* @param listType the class of the contained type
|
||||
* @param <T> the type of list
|
||||
*
|
||||
* @return a list of tags
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Tag> List<T> getList(final String key, final Class<T> listType) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof ListTag) {
|
||||
final ListTag listTag = (ListTag) tag;
|
||||
if (listTag.getType().equals(listType)) {
|
||||
return (List<T>) listTag.getValue();
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a long named with the given key. <p> If the key does not exist or its value is not a long tag, then
|
||||
* {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return a long
|
||||
*/
|
||||
public long getLong(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof LongTag) {
|
||||
return ((LongTag) tag).getValue();
|
||||
} else {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a long named with the given key, even if it's another type of number. <p> If the key does not exist or
|
||||
* its value is not a number, then {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return a long
|
||||
*/
|
||||
public long asLong(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof ByteTag) {
|
||||
return ((ByteTag) tag).getValue();
|
||||
} else if (tag instanceof ShortTag) {
|
||||
return ((ShortTag) tag).getValue();
|
||||
} else if (tag instanceof IntTag) {
|
||||
return ((IntTag) tag).getValue();
|
||||
} else if (tag instanceof LongTag) {
|
||||
return ((LongTag) tag).getValue();
|
||||
} else if (tag instanceof FloatTag) {
|
||||
return ((FloatTag) tag).getValue().longValue();
|
||||
} else if (tag instanceof DoubleTag) {
|
||||
return ((DoubleTag) tag).getValue().longValue();
|
||||
} else {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a short named with the given key. <p> If the key does not exist or its value is not a short tag, then
|
||||
* {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return a short
|
||||
*/
|
||||
public short getShort(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof ShortTag) {
|
||||
return ((ShortTag) tag).getValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string named with the given key. <p> If the key does not exist or its value is not a string tag, then
|
||||
* {@code ""} will be returned. </p>
|
||||
*
|
||||
* @param key the key
|
||||
*
|
||||
* @return a string
|
||||
*/
|
||||
public String getString(final String key) {
|
||||
final Tag tag = this.value.get(key);
|
||||
if (tag instanceof StringTag) {
|
||||
return ((StringTag) tag).getValue();
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final String name = getName();
|
||||
String append = "";
|
||||
if ((name != null) && !name.equals("")) {
|
||||
append = "(\"" + this.getName() + "\")";
|
||||
}
|
||||
final StringBuilder bldr = new StringBuilder();
|
||||
bldr.append("TAG_Compound").append(append).append(": ").append(this.value.size()).append(" entries\r\n{\r\n");
|
||||
for (final Map.Entry<String, Tag> entry : this.value.entrySet()) {
|
||||
bldr.append(" ").append(entry.getValue().toString().replaceAll("\r\n", "\r\n ")).append("\r\n");
|
||||
}
|
||||
bldr.append("}");
|
||||
return bldr.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,197 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Helps create compound tags.
|
||||
*/
|
||||
public class CompoundTagBuilder {
|
||||
private final Map<String, Tag> entries;
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*/
|
||||
CompoundTagBuilder() {
|
||||
this.entries = new HashMap<String, Tag>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance and use the given map (which will be modified).
|
||||
*
|
||||
* @param value the value
|
||||
*/
|
||||
CompoundTagBuilder(final Map<String, Tag> value) {
|
||||
checkNotNull(value);
|
||||
this.entries = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder instance.
|
||||
*
|
||||
* @return a new builder
|
||||
*/
|
||||
public static CompoundTagBuilder create() {
|
||||
return new CompoundTagBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the given key and tag into the compound tag.
|
||||
*
|
||||
* @param key they key
|
||||
* @param value the value
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public CompoundTagBuilder put(final String key, final Tag value) {
|
||||
checkNotNull(key);
|
||||
checkNotNull(value);
|
||||
this.entries.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the given key and value into the compound tag as a {@code ByteArrayTag}.
|
||||
*
|
||||
* @param key they key
|
||||
* @param value the value
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public CompoundTagBuilder putByteArray(final String key, final byte[] value) {
|
||||
return put(key, new ByteArrayTag(key, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the given key and value into the compound tag as a {@code ByteTag}.
|
||||
*
|
||||
* @param key they key
|
||||
* @param value the value
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public CompoundTagBuilder putByte(final String key, final byte value) {
|
||||
return put(key, new ByteTag(key, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the given key and value into the compound tag as a {@code DoubleTag}.
|
||||
*
|
||||
* @param key they key
|
||||
* @param value the value
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public CompoundTagBuilder putDouble(final String key, final double value) {
|
||||
return put(key, new DoubleTag(key, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the given key and value into the compound tag as a {@code FloatTag}.
|
||||
*
|
||||
* @param key they key
|
||||
* @param value the value
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public CompoundTagBuilder putFloat(final String key, final float value) {
|
||||
return put(key, new FloatTag(key, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the given key and value into the compound tag as a {@code IntArrayTag}.
|
||||
*
|
||||
* @param key they key
|
||||
* @param value the value
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public CompoundTagBuilder putIntArray(final String key, final int[] value) {
|
||||
return put(key, new IntArrayTag(key, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the given key and value into the compound tag as an {@code IntTag}.
|
||||
*
|
||||
* @param key they key
|
||||
* @param value the value
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public CompoundTagBuilder putInt(final String key, final int value) {
|
||||
return put(key, new IntTag(key, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the given key and value into the compound tag as a {@code LongTag}.
|
||||
*
|
||||
* @param key they key
|
||||
* @param value the value
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public CompoundTagBuilder putLong(final String key, final long value) {
|
||||
return put(key, new LongTag(key, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the given key and value into the compound tag as a {@code ShortTag}.
|
||||
*
|
||||
* @param key they key
|
||||
* @param value the value
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public CompoundTagBuilder putShort(final String key, final short value) {
|
||||
return put(key, new ShortTag(key, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the given key and value into the compound tag as a {@code StringTag}.
|
||||
*
|
||||
* @param key they key
|
||||
* @param value the value
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public CompoundTagBuilder putString(final String key, final String value) {
|
||||
return put(key, new StringTag(key, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Put all the entries from the given map into this map.
|
||||
*
|
||||
* @param value the map of tags
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public CompoundTagBuilder putAll(final Map<String, ? extends Tag> value) {
|
||||
checkNotNull(value);
|
||||
for (final Map.Entry<String, ? extends Tag> entry : value.entrySet()) {
|
||||
put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an unnamed compound tag with this builder's entries.
|
||||
*
|
||||
* @return the new compound tag
|
||||
*/
|
||||
public CompoundTag build() {
|
||||
return new CompoundTag(new HashMap<String, Tag>(this.entries));
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a new compound tag with this builder's entries.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
*
|
||||
* @return the created compound tag
|
||||
*/
|
||||
public CompoundTag build(final String name) {
|
||||
return new CompoundTag(name, new HashMap<String, Tag>(this.entries));
|
||||
}
|
||||
}
|
44
src/main/java/com/intellectualcrafters/jnbt/DoubleTag.java
Normal file
44
src/main/java/com/intellectualcrafters/jnbt/DoubleTag.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
/**
|
||||
* The {@code TAG_Double} tag.
|
||||
*/
|
||||
public final class DoubleTag extends Tag {
|
||||
private final double value;
|
||||
|
||||
/**
|
||||
* Creates the tag with an empty name.
|
||||
*
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public DoubleTag(final double value) {
|
||||
super();
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public DoubleTag(final String name, final double value) {
|
||||
super(name);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final String name = getName();
|
||||
String append = "";
|
||||
if ((name != null) && !name.equals("")) {
|
||||
append = "(\"" + this.getName() + "\")";
|
||||
}
|
||||
return "TAG_Double" + append + ": " + this.value;
|
||||
}
|
||||
}
|
23
src/main/java/com/intellectualcrafters/jnbt/EndTag.java
Normal file
23
src/main/java/com/intellectualcrafters/jnbt/EndTag.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
/**
|
||||
* The {@code TAG_End} 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";
|
||||
}
|
||||
}
|
44
src/main/java/com/intellectualcrafters/jnbt/FloatTag.java
Normal file
44
src/main/java/com/intellectualcrafters/jnbt/FloatTag.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
/**
|
||||
* The {@code TAG_Float} tag.
|
||||
*/
|
||||
public final class FloatTag extends Tag {
|
||||
private final float value;
|
||||
|
||||
/**
|
||||
* Creates the tag with an empty name.
|
||||
*
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public FloatTag(final float value) {
|
||||
super();
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public FloatTag(final String name, final float value) {
|
||||
super(name);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final String name = getName();
|
||||
String append = "";
|
||||
if ((name != null) && !name.equals("")) {
|
||||
append = "(\"" + this.getName() + "\")";
|
||||
}
|
||||
return "TAG_Float" + append + ": " + this.value;
|
||||
}
|
||||
}
|
56
src/main/java/com/intellectualcrafters/jnbt/IntArrayTag.java
Normal file
56
src/main/java/com/intellectualcrafters/jnbt/IntArrayTag.java
Normal file
@ -0,0 +1,56 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* The {@code TAG_Int_Array} tag.
|
||||
*/
|
||||
public final class IntArrayTag extends Tag {
|
||||
private final int[] value;
|
||||
|
||||
/**
|
||||
* Creates the tag with an empty name.
|
||||
*
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public IntArrayTag(final int[] value) {
|
||||
super();
|
||||
checkNotNull(value);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public IntArrayTag(final String name, final int[] value) {
|
||||
super(name);
|
||||
checkNotNull(value);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder hex = new StringBuilder();
|
||||
for (final int b : this.value) {
|
||||
final String hexDigits = Integer.toHexString(b).toUpperCase();
|
||||
if (hexDigits.length() == 1) {
|
||||
hex.append("0");
|
||||
}
|
||||
hex.append(hexDigits).append(" ");
|
||||
}
|
||||
final String name = getName();
|
||||
String append = "";
|
||||
if ((name != null) && !name.equals("")) {
|
||||
append = "(\"" + this.getName() + "\")";
|
||||
}
|
||||
return "TAG_Int_Array" + append + ": " + hex;
|
||||
}
|
||||
}
|
44
src/main/java/com/intellectualcrafters/jnbt/IntTag.java
Normal file
44
src/main/java/com/intellectualcrafters/jnbt/IntTag.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
/**
|
||||
* The {@code TAG_Int} tag.
|
||||
*/
|
||||
public final class IntTag extends Tag {
|
||||
private final int value;
|
||||
|
||||
/**
|
||||
* Creates the tag with an empty name.
|
||||
*
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public IntTag(final int value) {
|
||||
super();
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public IntTag(final String name, final int value) {
|
||||
super(name);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final String name = getName();
|
||||
String append = "";
|
||||
if ((name != null) && !name.equals("")) {
|
||||
append = "(\"" + this.getName() + "\")";
|
||||
}
|
||||
return "TAG_Int" + append + ": " + this.value;
|
||||
}
|
||||
}
|
393
src/main/java/com/intellectualcrafters/jnbt/ListTag.java
Normal file
393
src/main/java/com/intellectualcrafters/jnbt/ListTag.java
Normal file
@ -0,0 +1,393 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
|
||||
/**
|
||||
* The {@code TAG_List} tag.
|
||||
*/
|
||||
public final class ListTag extends Tag {
|
||||
private final Class<? extends Tag> type;
|
||||
private final List<Tag> value;
|
||||
|
||||
/**
|
||||
* Creates the tag with an empty name.
|
||||
*
|
||||
* @param type the type of tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public ListTag(final Class<? extends Tag> type, final List<? extends Tag> value) {
|
||||
super();
|
||||
checkNotNull(value);
|
||||
this.type = type;
|
||||
this.value = Collections.unmodifiableList(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
* @param type the type of tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public ListTag(final String name, final Class<? extends Tag> type, final List<? extends Tag> value) {
|
||||
super(name);
|
||||
checkNotNull(value);
|
||||
this.type = type;
|
||||
this.value = Collections.unmodifiableList(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the type of item in this list.
|
||||
*
|
||||
* @return The type of item in this list.
|
||||
*/
|
||||
public Class<? extends Tag> getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Tag> getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new list tag with this tag's name and type.
|
||||
*
|
||||
* @param list the new list
|
||||
*
|
||||
* @return a new list tag
|
||||
*/
|
||||
public ListTag setValue(final List<Tag> list) {
|
||||
return new ListTag(getName(), getType(), list);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the tag if it exists at the given index.
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return the tag or null
|
||||
*/
|
||||
public Tag getIfExists(final int index) {
|
||||
try {
|
||||
return this.value.get(index);
|
||||
} catch (final NoSuchElementException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a byte array named with the given index. <p> If the index does not exist or its value is not a byte
|
||||
* array tag, then an empty byte array will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return a byte array
|
||||
*/
|
||||
public byte[] getByteArray(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof ByteArrayTag) {
|
||||
return ((ByteArrayTag) tag).getValue();
|
||||
} else {
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a byte named with the given index. <p> If the index does not exist or its value is not a byte tag, then
|
||||
* {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return a byte
|
||||
*/
|
||||
public byte getByte(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof ByteTag) {
|
||||
return ((ByteTag) tag).getValue();
|
||||
} else {
|
||||
return (byte) 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a double named with the given index. <p> If the index does not exist or its value is not a double tag,
|
||||
* then {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return a double
|
||||
*/
|
||||
public double getDouble(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof DoubleTag) {
|
||||
return ((DoubleTag) tag).getValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a double named with the given index, even if it's another type of number. <p> If the index does not
|
||||
* exist or its value is not a number, then {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return a double
|
||||
*/
|
||||
public double asDouble(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof ByteTag) {
|
||||
return ((ByteTag) tag).getValue();
|
||||
} else if (tag instanceof ShortTag) {
|
||||
return ((ShortTag) tag).getValue();
|
||||
} else if (tag instanceof IntTag) {
|
||||
return ((IntTag) tag).getValue();
|
||||
} else if (tag instanceof LongTag) {
|
||||
return ((LongTag) tag).getValue();
|
||||
} else if (tag instanceof FloatTag) {
|
||||
return ((FloatTag) tag).getValue();
|
||||
} else if (tag instanceof DoubleTag) {
|
||||
return ((DoubleTag) tag).getValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a float named with the given index. <p> If the index does not exist or its value is not a float tag,
|
||||
* then {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return a float
|
||||
*/
|
||||
public float getFloat(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof FloatTag) {
|
||||
return ((FloatTag) tag).getValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a {@code int[]} named with the given index. <p> If the index does not exist or its value is not an int
|
||||
* array tag, then an empty array will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return an int array
|
||||
*/
|
||||
public int[] getIntArray(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof IntArrayTag) {
|
||||
return ((IntArrayTag) tag).getValue();
|
||||
} else {
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an int named with the given index. <p> If the index does not exist or its value is not an int tag, then
|
||||
* {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return an int
|
||||
*/
|
||||
public int getInt(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof IntTag) {
|
||||
return ((IntTag) tag).getValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an int named with the given index, even if it's another type of number. <p> If the index does not exist
|
||||
* or its value is not a number, then {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return an int
|
||||
*/
|
||||
public int asInt(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof ByteTag) {
|
||||
return ((ByteTag) tag).getValue();
|
||||
} else if (tag instanceof ShortTag) {
|
||||
return ((ShortTag) tag).getValue();
|
||||
} else if (tag instanceof IntTag) {
|
||||
return ((IntTag) tag).getValue();
|
||||
} else if (tag instanceof LongTag) {
|
||||
return ((LongTag) tag).getValue().intValue();
|
||||
} else if (tag instanceof FloatTag) {
|
||||
return ((FloatTag) tag).getValue().intValue();
|
||||
} else if (tag instanceof DoubleTag) {
|
||||
return ((DoubleTag) tag).getValue().intValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tags named with the given index. <p> If the index does not exist or its value is not a list
|
||||
* tag, then an empty list will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return a list of tags
|
||||
*/
|
||||
public List<Tag> getList(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof ListTag) {
|
||||
return ((ListTag) tag).getValue();
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a {@code TagList} named with the given index. <p> If the index does not exist or its value is not a list
|
||||
* tag, then an empty tag list will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return a tag list instance
|
||||
*/
|
||||
public ListTag getListTag(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof ListTag) {
|
||||
return (ListTag) tag;
|
||||
} else {
|
||||
return new ListTag(StringTag.class, Collections.<Tag> emptyList());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tags named with the given index. <p> If the index does not exist or its value is not a list
|
||||
* tag, then an empty list will be returned. If the given index references a list but the list of of a different
|
||||
* type, then an empty list will also be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
* @param listType the class of the contained type
|
||||
* @param <T> the NBT type
|
||||
*
|
||||
* @return a list of tags
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Tag> List<T> getList(final int index, final Class<T> listType) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof ListTag) {
|
||||
final ListTag listTag = (ListTag) tag;
|
||||
if (listTag.getType().equals(listType)) {
|
||||
return (List<T>) listTag.getValue();
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a long named with the given index. <p> If the index does not exist or its value is not a long tag, then
|
||||
* {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return a long
|
||||
*/
|
||||
public long getLong(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof LongTag) {
|
||||
return ((LongTag) tag).getValue();
|
||||
} else {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a long named with the given index, even if it's another type of number. <p> If the index does not exist
|
||||
* or its value is not a number, then {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return a long
|
||||
*/
|
||||
public long asLong(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof ByteTag) {
|
||||
return ((ByteTag) tag).getValue();
|
||||
} else if (tag instanceof ShortTag) {
|
||||
return ((ShortTag) tag).getValue();
|
||||
} else if (tag instanceof IntTag) {
|
||||
return ((IntTag) tag).getValue();
|
||||
} else if (tag instanceof LongTag) {
|
||||
return ((LongTag) tag).getValue();
|
||||
} else if (tag instanceof FloatTag) {
|
||||
return ((FloatTag) tag).getValue().longValue();
|
||||
} else if (tag instanceof DoubleTag) {
|
||||
return ((DoubleTag) tag).getValue().longValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a short named with the given index. <p> If the index does not exist or its value is not a short tag,
|
||||
* then {@code 0} will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return a short
|
||||
*/
|
||||
public short getShort(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof ShortTag) {
|
||||
return ((ShortTag) tag).getValue();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string named with the given index. <p> If the index does not exist or its value is not a string tag,
|
||||
* then {@code ""} will be returned. </p>
|
||||
*
|
||||
* @param index the index
|
||||
*
|
||||
* @return a string
|
||||
*/
|
||||
public String getString(final int index) {
|
||||
final Tag tag = getIfExists(index);
|
||||
if (tag instanceof StringTag) {
|
||||
return ((StringTag) tag).getValue();
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final String name = getName();
|
||||
String append = "";
|
||||
if ((name != null) && !name.equals("")) {
|
||||
append = "(\"" + this.getName() + "\")";
|
||||
}
|
||||
final StringBuilder bldr = new StringBuilder();
|
||||
bldr.append("TAG_List").append(append).append(": ").append(this.value.size()).append(" entries of type ").append(NBTUtils.getTypeName(this.type)).append("\r\n{\r\n");
|
||||
for (final Tag t : this.value) {
|
||||
bldr.append(" ").append(t.toString().replaceAll("\r\n", "\r\n ")).append("\r\n");
|
||||
}
|
||||
bldr.append("}");
|
||||
return bldr.toString();
|
||||
}
|
||||
}
|
114
src/main/java/com/intellectualcrafters/jnbt/ListTagBuilder.java
Normal file
114
src/main/java/com/intellectualcrafters/jnbt/ListTagBuilder.java
Normal file
@ -0,0 +1,114 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Helps create list tags.
|
||||
*/
|
||||
public class ListTagBuilder {
|
||||
private final Class<? extends Tag> type;
|
||||
private final List<Tag> entries;
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*
|
||||
* @param type of tag contained in this list
|
||||
*/
|
||||
ListTagBuilder(final Class<? extends Tag> type) {
|
||||
checkNotNull(type);
|
||||
this.type = type;
|
||||
this.entries = new ArrayList<Tag>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder instance.
|
||||
*
|
||||
* @param type
|
||||
*
|
||||
* @return a new builder
|
||||
*/
|
||||
public static ListTagBuilder create(final Class<? extends Tag> type) {
|
||||
return new ListTagBuilder(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder instance.
|
||||
*
|
||||
* @param entries
|
||||
* @param <T>
|
||||
*
|
||||
* @return a new builder
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <T extends Tag> ListTagBuilder createWith(final T... entries) {
|
||||
checkNotNull(entries);
|
||||
if (entries.length == 0) {
|
||||
throw new IllegalArgumentException("This method needs an array of at least one entry");
|
||||
}
|
||||
final Class<? extends Tag> type = entries[0].getClass();
|
||||
for (int i = 1; i < entries.length; i++) {
|
||||
if (!type.isInstance(entries[i])) {
|
||||
throw new IllegalArgumentException("An array of different tag types was provided");
|
||||
}
|
||||
}
|
||||
final ListTagBuilder builder = new ListTagBuilder(type);
|
||||
builder.addAll(Arrays.asList(entries));
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given tag.
|
||||
*
|
||||
* @param value the tag
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public ListTagBuilder add(final Tag value) {
|
||||
checkNotNull(value);
|
||||
if (!this.type.isInstance(value)) {
|
||||
throw new IllegalArgumentException(value.getClass().getCanonicalName() + " is not of expected type " + this.type.getCanonicalName());
|
||||
}
|
||||
this.entries.add(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all the tags in the given list.
|
||||
*
|
||||
* @param value a list of tags
|
||||
*
|
||||
* @return this object
|
||||
*/
|
||||
public ListTagBuilder addAll(final Collection<? extends Tag> value) {
|
||||
checkNotNull(value);
|
||||
for (final Tag v : value) {
|
||||
add(v);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an unnamed list tag with this builder's entries.
|
||||
*
|
||||
* @return the new list tag
|
||||
*/
|
||||
public ListTag build() {
|
||||
return new ListTag(this.type, new ArrayList<Tag>(this.entries));
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a new list tag with this builder's entries.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
*
|
||||
* @return the created list tag
|
||||
*/
|
||||
public ListTag build(final String name) {
|
||||
return new ListTag(name, this.type, new ArrayList<Tag>(this.entries));
|
||||
}
|
||||
}
|
44
src/main/java/com/intellectualcrafters/jnbt/LongTag.java
Normal file
44
src/main/java/com/intellectualcrafters/jnbt/LongTag.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
/**
|
||||
* The {@code TAG_Long} tag.
|
||||
*/
|
||||
public final class LongTag extends Tag {
|
||||
private final long value;
|
||||
|
||||
/**
|
||||
* Creates the tag with an empty name.
|
||||
*
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public LongTag(final long value) {
|
||||
super();
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public LongTag(final String name, final long value) {
|
||||
super(name);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final String name = getName();
|
||||
String append = "";
|
||||
if ((name != null) && !name.equals("")) {
|
||||
append = "(\"" + this.getName() + "\")";
|
||||
}
|
||||
return "TAG_Long" + append + ": " + this.value;
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PlotSquared - A plot manager and world generator for the Bukkit API /
|
||||
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
|
||||
// /
|
||||
// This program is free software; you can redistribute it and/or modify /
|
||||
// it under the terms of the GNU General Public License as published by /
|
||||
// the Free Software Foundation; either version 3 of the License, or /
|
||||
// (at your option) any later version. /
|
||||
// /
|
||||
// This program is distributed in the hope that it will be useful, /
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
|
||||
// GNU General Public License for more details. /
|
||||
// /
|
||||
// You should have received a copy of the GNU General Public License /
|
||||
// along with this program; if not, write to the Free Software Foundation, /
|
||||
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
|
||||
// /
|
||||
// You can contact us via: support@intellectualsites.com /
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* A class which holds constant values.
|
||||
*/
|
||||
public final class NBTConstants {
|
||||
public static final Charset CHARSET = Charset.forName("UTF-8");
|
||||
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;
|
||||
|
||||
/**
|
||||
* Default private constructor.
|
||||
*/
|
||||
private NBTConstants() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a type ID to its corresponding {@link Tag} class.
|
||||
*
|
||||
* @param id type ID
|
||||
*
|
||||
* @return tag class
|
||||
*
|
||||
* @throws IllegalArgumentException thrown if the tag ID is not valid
|
||||
*/
|
||||
public static Class<? extends Tag> getClassFromType(final int id) {
|
||||
switch (id) {
|
||||
case TYPE_END:
|
||||
return EndTag.class;
|
||||
case TYPE_BYTE:
|
||||
return ByteTag.class;
|
||||
case TYPE_SHORT:
|
||||
return ShortTag.class;
|
||||
case TYPE_INT:
|
||||
return IntTag.class;
|
||||
case TYPE_LONG:
|
||||
return LongTag.class;
|
||||
case TYPE_FLOAT:
|
||||
return FloatTag.class;
|
||||
case TYPE_DOUBLE:
|
||||
return DoubleTag.class;
|
||||
case TYPE_BYTE_ARRAY:
|
||||
return ByteArrayTag.class;
|
||||
case TYPE_STRING:
|
||||
return StringTag.class;
|
||||
case TYPE_LIST:
|
||||
return ListTag.class;
|
||||
case TYPE_COMPOUND:
|
||||
return CompoundTag.class;
|
||||
case TYPE_INT_ARRAY:
|
||||
return IntArrayTag.class;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown tag type ID of " + id);
|
||||
}
|
||||
}
|
||||
}
|
145
src/main/java/com/intellectualcrafters/jnbt/NBTInputStream.java
Normal file
145
src/main/java/com/intellectualcrafters/jnbt/NBTInputStream.java
Normal file
@ -0,0 +1,145 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This class reads <strong>NBT</strong>, or <strong>Named Binary Tag</strong> streams, and produces an object graph of
|
||||
* subclasses of the {@code Tag} object. The NBT format was created by Markus Persson, and the specification
|
||||
* may be found at @linktourl http://www.minecraft.net/docs/NBT.txt"> http://www.minecraft.net/docs/NBT.txt.
|
||||
*/
|
||||
public final class NBTInputStream implements Closeable {
|
||||
private final DataInputStream is;
|
||||
|
||||
/**
|
||||
* Creates a new {@code NBTInputStream}, 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(final InputStream is) throws IOException {
|
||||
this.is = new DataInputStream(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(final int depth) throws IOException {
|
||||
final int type = this.is.readByte() & 0xFF;
|
||||
String name;
|
||||
if (type != NBTConstants.TYPE_END) {
|
||||
final int nameLength = this.is.readShort() & 0xFFFF;
|
||||
final byte[] nameBytes = new byte[nameLength];
|
||||
this.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.
|
||||
*/
|
||||
private Tag readTagPayload(final int type, final String name, final 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, this.is.readByte());
|
||||
case NBTConstants.TYPE_SHORT:
|
||||
return new ShortTag(name, this.is.readShort());
|
||||
case NBTConstants.TYPE_INT:
|
||||
return new IntTag(name, this.is.readInt());
|
||||
case NBTConstants.TYPE_LONG:
|
||||
return new LongTag(name, this.is.readLong());
|
||||
case NBTConstants.TYPE_FLOAT:
|
||||
return new FloatTag(name, this.is.readFloat());
|
||||
case NBTConstants.TYPE_DOUBLE:
|
||||
return new DoubleTag(name, this.is.readDouble());
|
||||
case NBTConstants.TYPE_BYTE_ARRAY:
|
||||
int length = this.is.readInt();
|
||||
byte[] bytes = new byte[length];
|
||||
this.is.readFully(bytes);
|
||||
return new ByteArrayTag(name, bytes);
|
||||
case NBTConstants.TYPE_STRING:
|
||||
length = this.is.readShort();
|
||||
bytes = new byte[length];
|
||||
this.is.readFully(bytes);
|
||||
return new StringTag(name, new String(bytes, NBTConstants.CHARSET));
|
||||
case NBTConstants.TYPE_LIST:
|
||||
final int childType = this.is.readByte();
|
||||
length = this.is.readInt();
|
||||
final List<Tag> tagList = new ArrayList<Tag>();
|
||||
for (int i = 0; i < length; ++i) {
|
||||
final Tag tag = readTagPayload(childType, "", depth + 1);
|
||||
if (tag instanceof EndTag) {
|
||||
throw new IOException("TAG_End not permitted in a list.");
|
||||
}
|
||||
tagList.add(tag);
|
||||
}
|
||||
return new ListTag(name, NBTUtils.getTypeClass(childType), tagList);
|
||||
case NBTConstants.TYPE_COMPOUND:
|
||||
final Map<String, Tag> tagMap = new HashMap<String, Tag>();
|
||||
while (true) {
|
||||
final 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 = this.is.readInt();
|
||||
final int[] data = new int[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
data[i] = this.is.readInt();
|
||||
}
|
||||
return new IntArrayTag(name, data);
|
||||
default:
|
||||
throw new IOException("Invalid tag type: " + type + ".");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.is.close();
|
||||
}
|
||||
}
|
281
src/main/java/com/intellectualcrafters/jnbt/NBTOutputStream.java
Normal file
281
src/main/java/com/intellectualcrafters/jnbt/NBTOutputStream.java
Normal file
@ -0,0 +1,281 @@
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PlotSquared - A plot manager and world generator for the Bukkit API /
|
||||
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
|
||||
// /
|
||||
// This program is free software; you can redistribute it and/or modify /
|
||||
// it under the terms of the GNU General Public License as published by /
|
||||
// the Free Software Foundation; either version 3 of the License, or /
|
||||
// (at your option) any later version. /
|
||||
// /
|
||||
// This program is distributed in the hope that it will be useful, /
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
|
||||
// GNU General Public License for more details. /
|
||||
// /
|
||||
// You should have received a copy of the GNU General Public License /
|
||||
// along with this program; if not, write to the Free Software Foundation, /
|
||||
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
|
||||
// /
|
||||
// You can contact us via: support@intellectualsites.com /
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <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
|
||||
* @linktourl http://www.minecraft.net/docs/NBT.txt
|
||||
* </p>
|
||||
*
|
||||
* @author Graham Edgecombe
|
||||
*/
|
||||
public final class NBTOutputStream implements Closeable {
|
||||
/**
|
||||
* 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(final OutputStream os) throws IOException {
|
||||
this.os = new DataOutputStream(os);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a tag.
|
||||
*
|
||||
* @param tag The tag to write.
|
||||
*
|
||||
* @throws IOException if an I/O error occurs.
|
||||
*/
|
||||
public void writeTag(final Tag tag) throws IOException {
|
||||
final int type = NBTUtils.getTypeCode(tag.getClass());
|
||||
final String name = tag.getName();
|
||||
final byte[] nameBytes = name.getBytes(NBTConstants.CHARSET);
|
||||
this.os.writeByte(type);
|
||||
this.os.writeShort(nameBytes.length);
|
||||
this.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(final Tag tag) throws IOException {
|
||||
final 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;
|
||||
default:
|
||||
throw new IOException("Invalid tag type: " + type + ".");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a <code>TAG_Byte</code> tag.
|
||||
*
|
||||
* @param tag The tag.
|
||||
*
|
||||
* @throws IOException if an I/O error occurs.
|
||||
*/
|
||||
private void writeByteTagPayload(final ByteTag tag) throws IOException {
|
||||
this.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(final ByteArrayTag tag) throws IOException {
|
||||
final byte[] bytes = tag.getValue();
|
||||
this.os.writeInt(bytes.length);
|
||||
this.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(final CompoundTag tag) throws IOException {
|
||||
for (final Tag childTag : tag.getValue().values()) {
|
||||
writeTag(childTag);
|
||||
}
|
||||
this.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(final ListTag tag) throws IOException {
|
||||
final Class<? extends Tag> clazz = tag.getType();
|
||||
final List<Tag> tags = tag.getValue();
|
||||
final int size = tags.size();
|
||||
this.os.writeByte(NBTUtils.getTypeCode(clazz));
|
||||
this.os.writeInt(size);
|
||||
for (final Tag tag1 : tags) {
|
||||
writeTagPayload(tag1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a <code>TAG_String</code> tag.
|
||||
*
|
||||
* @param tag The tag.
|
||||
*
|
||||
* @throws IOException if an I/O error occurs.
|
||||
*/
|
||||
private void writeStringTagPayload(final StringTag tag) throws IOException {
|
||||
final byte[] bytes = tag.getValue().getBytes(NBTConstants.CHARSET);
|
||||
this.os.writeShort(bytes.length);
|
||||
this.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(final DoubleTag tag) throws IOException {
|
||||
this.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(final FloatTag tag) throws IOException {
|
||||
this.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(final LongTag tag) throws IOException {
|
||||
this.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(final IntTag tag) throws IOException {
|
||||
this.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(final ShortTag tag) throws IOException {
|
||||
this.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(final EndTag tag) {
|
||||
/* empty */
|
||||
}
|
||||
|
||||
private void writeIntArrayTagPayload(final IntArrayTag tag) throws IOException {
|
||||
final int[] data = tag.getValue();
|
||||
this.os.writeInt(data.length);
|
||||
for (final int element : data) {
|
||||
this.os.writeInt(element);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.os.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush output
|
||||
* @throws IOException
|
||||
*/
|
||||
public void flush() throws IOException {
|
||||
this.os.flush();
|
||||
}
|
||||
}
|
151
src/main/java/com/intellectualcrafters/jnbt/NBTUtils.java
Normal file
151
src/main/java/com/intellectualcrafters/jnbt/NBTUtils.java
Normal file
@ -0,0 +1,151 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A class which contains NBT-related utility methods.
|
||||
*/
|
||||
public final class NBTUtils {
|
||||
/**
|
||||
* Default private constructor.
|
||||
*/
|
||||
private NBTUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the type name of a tag.
|
||||
*
|
||||
* @param clazz the tag class
|
||||
*
|
||||
* @return The type name.
|
||||
*/
|
||||
public static String getTypeName(final 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 {
|
||||
throw new IllegalArgumentException("Invalid tag classs (" + clazz.getName() + ").");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(final 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 {
|
||||
throw new IllegalArgumentException("Invalid tag classs (" + clazz.getName() + ").");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(final 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;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid tag type : " + type + ".");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get child tag of a NBT structure.
|
||||
*
|
||||
* @param items the map to read from
|
||||
* @param key the key to look for
|
||||
* @param expected the expected NBT class type
|
||||
* @param <T>
|
||||
*
|
||||
* @return child tag
|
||||
*/
|
||||
public static <T extends Tag> T getChildTag(final Map<String, Tag> items, final String key, final Class<T> expected) throws IllegalArgumentException {
|
||||
if (!items.containsKey(key)) {
|
||||
throw new IllegalArgumentException("Missing a \"" + key + "\" tag");
|
||||
}
|
||||
final Tag tag = items.get(key);
|
||||
if (!expected.isInstance(tag)) {
|
||||
throw new IllegalArgumentException(key + " tag is not of tag type " + expected.getName());
|
||||
}
|
||||
return expected.cast(tag);
|
||||
}
|
||||
}
|
64
src/main/java/com/intellectualcrafters/jnbt/ShortTag.java
Normal file
64
src/main/java/com/intellectualcrafters/jnbt/ShortTag.java
Normal file
@ -0,0 +1,64 @@
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PlotSquared - A plot manager and world generator for the Bukkit API /
|
||||
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
|
||||
// /
|
||||
// This program is free software; you can redistribute it and/or modify /
|
||||
// it under the terms of the GNU General Public License as published by /
|
||||
// the Free Software Foundation; either version 3 of the License, or /
|
||||
// (at your option) any later version. /
|
||||
// /
|
||||
// This program is distributed in the hope that it will be useful, /
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
|
||||
// GNU General Public License for more details. /
|
||||
// /
|
||||
// You should have received a copy of the GNU General Public License /
|
||||
// along with this program; if not, write to the Free Software Foundation, /
|
||||
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
|
||||
// /
|
||||
// You can contact us via: support@intellectualsites.com /
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
/**
|
||||
* The {@code TAG_Short} tag.
|
||||
*/
|
||||
public final class ShortTag extends Tag {
|
||||
private final short value;
|
||||
|
||||
/**
|
||||
* Creates the tag with an empty name.
|
||||
*
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public ShortTag(final short value) {
|
||||
super();
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public ShortTag(final String name, final short value) {
|
||||
super(name);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final String name = getName();
|
||||
String append = "";
|
||||
if ((name != null) && !name.equals("")) {
|
||||
append = "(\"" + this.getName() + "\")";
|
||||
}
|
||||
return "TAG_Short" + append + ": " + this.value;
|
||||
}
|
||||
}
|
48
src/main/java/com/intellectualcrafters/jnbt/StringTag.java
Normal file
48
src/main/java/com/intellectualcrafters/jnbt/StringTag.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* The {@code TAG_String} tag.
|
||||
*/
|
||||
public final class StringTag extends Tag {
|
||||
private final String value;
|
||||
|
||||
/**
|
||||
* Creates the tag with an empty name.
|
||||
*
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public StringTag(final String value) {
|
||||
super();
|
||||
checkNotNull(value);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag.
|
||||
*
|
||||
* @param name the name of the tag
|
||||
* @param value the value of the tag
|
||||
*/
|
||||
public StringTag(final String name, final String value) {
|
||||
super(name);
|
||||
checkNotNull(value);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final String name = getName();
|
||||
String append = "";
|
||||
if ((name != null) && !name.equals("")) {
|
||||
append = "(\"" + this.getName() + "\")";
|
||||
}
|
||||
return "TAG_String" + append + ": " + this.value;
|
||||
}
|
||||
}
|
63
src/main/java/com/intellectualcrafters/jnbt/Tag.java
Normal file
63
src/main/java/com/intellectualcrafters/jnbt/Tag.java
Normal file
@ -0,0 +1,63 @@
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PlotSquared - A plot manager and world generator for the Bukkit API /
|
||||
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
|
||||
// /
|
||||
// This program is free software; you can redistribute it and/or modify /
|
||||
// it under the terms of the GNU General Public License as published by /
|
||||
// the Free Software Foundation; either version 3 of the License, or /
|
||||
// (at your option) any later version. /
|
||||
// /
|
||||
// This program is distributed in the hope that it will be useful, /
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
|
||||
// GNU General Public License for more details. /
|
||||
// /
|
||||
// You should have received a copy of the GNU General Public License /
|
||||
// along with this program; if not, write to the Free Software Foundation, /
|
||||
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
|
||||
// /
|
||||
// You can contact us via: support@intellectualsites.com /
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
/**
|
||||
* Represents a NBT tag.
|
||||
*/
|
||||
public abstract class Tag {
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Create a new tag with an empty name.
|
||||
*/
|
||||
Tag() {
|
||||
this("");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the tag with the specified name.
|
||||
*
|
||||
* @param name the name
|
||||
*/
|
||||
Tag(String name) {
|
||||
if (name == null) {
|
||||
name = "";
|
||||
}
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of this tag.
|
||||
*
|
||||
* @return the name of this tag
|
||||
*/
|
||||
public final String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of this tag.
|
||||
*
|
||||
* @return the value
|
||||
*/
|
||||
public abstract Object getValue();
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.intellectualcrafters.jnbt;
|
||||
|
||||
import org.bukkit.World;
|
||||
|
||||
public class WorldEditUtils {
|
||||
public static void setNBT(final World world, final short id, final byte data, final int x, final int y, final int z, final com.intellectualcrafters.jnbt.CompoundTag tag) {
|
||||
// final LocalWorld bukkitWorld = BukkitUtil.getLocalWorld(world);
|
||||
// I need to somehow convert our CompoundTag to WorldEdit's
|
||||
// final BaseBlock block = new BaseBlock(5, 5, (CompoundTag) tag);
|
||||
// final Vector vector = new Vector(x, y, z);
|
||||
// try {
|
||||
// bukkitWorld.setBlock(vector, block);
|
||||
// }
|
||||
// catch (final WorldEditException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user