Cleaned up and added many methods to the NBT classes.

This commit is contained in:
sk89q 2014-07-14 20:36:27 -07:00
parent 7243b2a05d
commit b5aaef2505
19 changed files with 1232 additions and 233 deletions

View File

@ -19,28 +19,28 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import com.sk89q.jnbt.Tag;
/** /**
* The <code>TAG_Byte_Array</code> tag. * The {@code TAG_Byte_Array} tag.
*
* @author Graham Edgecombe
*
*/ */
public final class ByteArrayTag extends Tag { public final class ByteArrayTag extends Tag {
/**
* The value.
*/
private final byte[] value; private final byte[] value;
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public ByteArrayTag(byte[] value) {
super();
this.value = value;
}
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name the name of the tag
* The name. * @param value the value of the tag
* @param value
* The value.
*/ */
public ByteArrayTag(String name, byte[] value) { public ByteArrayTag(String name, byte[] value) {
super(name); super(name);
@ -67,7 +67,7 @@ public final class ByteArrayTag extends Tag {
if (name != null && !name.equals("")) { if (name != null && !name.equals("")) {
append = "(\"" + this.getName() + "\")"; append = "(\"" + this.getName() + "\")";
} }
return "TAG_Byte_Array" + append + ": " + hex.toString(); return "TAG_Byte_Array" + append + ": " + hex;
} }
} }

View File

@ -19,28 +19,28 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import com.sk89q.jnbt.Tag;
/** /**
* The <code>TAG_Byte</code> tag. * The {@code TAG_Byte} tag.
*
* @author Graham Edgecombe
*
*/ */
public final class ByteTag extends Tag { public final class ByteTag extends Tag {
/**
* The value.
*/
private final byte value; private final byte value;
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public ByteTag(byte value) {
super();
this.value = value;
}
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name the name of the tag
* The name. * @param value the value of the tag
* @param value
* The value.
*/ */
public ByteTag(String name, byte value) { public ByteTag(String name, byte value) {
super(name); super(name);

View File

@ -20,40 +20,401 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.sk89q.jnbt.Tag;
/** /**
* The <code>TAG_Compound</code> tag. * The {@code TAG_Compound} tag.
*
* @author Graham Edgecombe
*
*/ */
public final class CompoundTag extends Tag { public final class CompoundTag extends Tag {
/**
* The value.
*/
private final Map<String, Tag> value; private final Map<String, Tag> value;
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public CompoundTag(Map<String, Tag> value) {
super();
this.value = Collections.unmodifiableMap(value);
}
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name the name of the tag
* The name. * @param value the value of the tag
* @param value
* The value.
*/ */
public CompoundTag(String name, Map<String, Tag> value) { public CompoundTag(String name, Map<String, Tag> value) {
super(name); super(name);
this.value = Collections.unmodifiableMap(value); 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(String key) {
return value.containsKey(key);
}
@Override @Override
public Map<String, Tag> getValue() { public Map<String, Tag> getValue() {
return value; return value;
} }
/**
* Return a new compound tag with the given values.
*
* @param value the value
* @return the new compound tag
*/
public CompoundTag setValue(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>(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(String key) {
Tag tag = 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(String key) {
Tag tag = 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(String key) {
Tag tag = 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(String key) {
Tag tag = 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(String key) {
Tag tag = 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 integer array tag,
* then an empty array will be returned.</p>
*
* @param key the key
* @return an integer array
*/
public int[] getIntegerArray(String key) {
Tag tag = value.get(key);
if (tag instanceof IntArrayTag) {
return ((IntArrayTag) tag).getValue();
} else {
return new int[0];
}
}
/**
* Get an integer named with the given key.
*
* <p>If the key does not exist or its value is not an integer tag,
* then {@code 0} will be returned.</p>
*
* @param key the key
* @return an integer
*/
public int getInteger(String key) {
Tag tag = value.get(key);
if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
} else {
return 0;
}
}
/**
* Get an integer 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 integer
*/
public int asInteger(String key) {
Tag tag = 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(String key) {
Tag tag = 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(String key) {
Tag tag = 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
* @return a list of tags
*/
@SuppressWarnings("unchecked")
public <T extends Tag> List<T> getList(String key, Class<T> listType) {
Tag tag = value.get(key);
if (tag instanceof ListTag) {
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(String key) {
Tag tag = 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(String key) {
Tag tag = 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(String key) {
Tag tag = 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(String key) {
Tag tag = value.get(key);
if (tag instanceof StringTag) {
return ((StringTag) tag).getValue();
} else {
return "";
}
}
@Override @Override
public String toString() { public String toString() {
String name = getName(); String name = getName();
@ -62,12 +423,9 @@ public final class CompoundTag extends Tag {
append = "(\"" + this.getName() + "\")"; append = "(\"" + this.getName() + "\")";
} }
StringBuilder bldr = new StringBuilder(); StringBuilder bldr = new StringBuilder();
bldr.append("TAG_Compound" + append + ": " + value.size() bldr.append("TAG_Compound").append(append).append(": ").append(value.size()).append(" entries\r\n{\r\n");
+ " entries\r\n{\r\n");
for (Map.Entry<String, Tag> entry : value.entrySet()) { for (Map.Entry<String, Tag> entry : value.entrySet()) {
bldr.append(" " bldr.append(" ").append(entry.getValue().toString().replaceAll("\r\n", "\r\n ")).append("\r\n");
+ entry.getValue().toString().replaceAll("\r\n", "\r\n ")
+ "\r\n");
} }
bldr.append("}"); bldr.append("}");
return bldr.toString(); return bldr.toString();

View File

@ -0,0 +1,107 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.jnbt;
import java.util.HashMap;
import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* 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(Map<String, Tag> value) {
checkNotNull(value);
this.entries = value;
}
/**
* Put the given key and tag into the compound tag.
*
* @param key they key
* @param value the value
* @return this object
*/
public CompoundTagBuilder put(String key, Tag value) {
checkNotNull(key);
checkNotNull(value);
entries.put(key, value);
return this;
}
/**
* Put all the entries from the given map into this map.
*
* @param value the map of tags
* @return this object
*/
public CompoundTagBuilder putAll(Map<String, ? extends Tag> value) {
checkNotNull(value);
for (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>(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(String name) {
return new CompoundTag(name, new HashMap<String, Tag>(entries));
}
/**
* Create a new builder instance.
*
* @return a new builder
*/
public static CompoundTagBuilder create() {
return new CompoundTagBuilder();
}
}

View File

@ -19,28 +19,29 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import com.sk89q.jnbt.Tag;
/** /**
* The <code>TAG_Double</code> tag. * The {@code TAG_Double} tag.
*
* @author Graham Edgecombe
* *
*/ */
public final class DoubleTag extends Tag { public final class DoubleTag extends Tag {
/**
* The value.
*/
private final double value; private final double value;
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public DoubleTag(double value) {
super();
this.value = value;
}
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name the name of the tag
* The name. * @param value the value of the tag
* @param value
* The value.
*/ */
public DoubleTag(String name, double value) { public DoubleTag(String name, double value) {
super(name); super(name);

View File

@ -19,8 +19,6 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import com.sk89q.jnbt.Tag;
/** /**
* The <code>TAG_End</code> tag. * The <code>TAG_End</code> tag.
* *
@ -33,7 +31,7 @@ public final class EndTag extends Tag {
* Creates the tag. * Creates the tag.
*/ */
public EndTag() { public EndTag() {
super(""); super();
} }
@Override @Override

View File

@ -19,28 +19,28 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import com.sk89q.jnbt.Tag;
/** /**
* The <code>TAG_Float</code> tag. * The {@code TAG_Float} tag.
*
* @author Graham Edgecombe
*
*/ */
public final class FloatTag extends Tag { public final class FloatTag extends Tag {
/**
* The value.
*/
private final float value; private final float value;
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public FloatTag(float value) {
super();
this.value = value;
}
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name the name of the tag
* The name. * @param value the value of the tag
* @param value
* The value.
*/ */
public FloatTag(String name, float value) { public FloatTag(String name, float value) {
super(name); super(name);

View File

@ -19,31 +19,35 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import com.sk89q.jnbt.Tag; import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* The <code>TAG_Int_Array</code> tag. * The {@code TAG_Int_Array} tag.
*
* @author Graham Edgecombe
*
*/ */
public final class IntArrayTag extends Tag { public final class IntArrayTag extends Tag {
/**
* The value.
*/
private final int[] value; private final int[] value;
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public IntArrayTag(int[] value) {
super();
checkNotNull(value);
this.value = value;
}
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name the name of the tag
* The name. * @param value the value of the tag
* @param value
* The value.
*/ */
public IntArrayTag(String name, int[] value) { public IntArrayTag(String name, int[] value) {
super(name); super(name);
checkNotNull(value);
this.value = value; this.value = value;
} }

View File

@ -19,28 +19,28 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import com.sk89q.jnbt.Tag;
/** /**
* The <code>TAG_Int</code> tag. * The {@code TAG_Int} tag.
*
* @author Graham Edgecombe
*
*/ */
public final class IntTag extends Tag { public final class IntTag extends Tag {
/**
* The value.
*/
private final int value; private final int value;
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public IntTag(int value) {
super();
this.value = value;
}
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name the name of the tag
* The name. * @param value the value of the tag
* @param value
* The value.
*/ */
public IntTag(String name, int value) { public IntTag(String name, int value) {
super(name); super(name);

View File

@ -19,41 +19,44 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import javax.annotation.Nullable;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import com.sk89q.jnbt.NBTUtils; import java.util.NoSuchElementException;
import com.sk89q.jnbt.Tag;
import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* The <code>TAG_List</code> tag. * The {@code TAG_List} tag.
*
* @author Graham Edgecombe
*
*/ */
public final class ListTag extends Tag { public final class ListTag extends Tag {
/**
* The type.
*/
private final Class<? extends Tag> type; private final Class<? extends Tag> type;
private final List<Tag> value;
/** /**
* The value. * Creates the tag with an empty name.
*
* @param type the type of tag
* @param value the value of the tag
*/ */
private final List<Tag> value; public ListTag(Class<? extends Tag> type, List<? extends Tag> value) {
super();
checkNotNull(value);
this.type = type;
this.value = Collections.unmodifiableList(value);
}
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name the name of the tag
* The name. * @param type the type of tag
* @param type * @param value the value of the tag
* The type of item in the list.
* @param value
* The value.
*/ */
public ListTag(String name, Class<? extends Tag> type, List<? extends Tag> value) { public ListTag(String name, Class<? extends Tag> type, List<? extends Tag> value) {
super(name); super(name);
checkNotNull(value);
this.type = type; this.type = type;
this.value = Collections.unmodifiableList(value); this.value = Collections.unmodifiableList(value);
} }
@ -72,6 +75,360 @@ public final class ListTag extends Tag {
return value; return 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(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
*/
@Nullable
public Tag getIfExists(int index) {
try {
return value.get(index);
} catch (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(int index) {
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(int index) {
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(int index) {
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(int index) {
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(int index) {
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 integer array tag,
* then an empty array will be returned.</p>
*
* @param index the index
* @return an integer array
*/
public int[] getIntegerArray(int index) {
Tag tag = getIfExists(index);
if (tag instanceof IntArrayTag) {
return ((IntArrayTag) tag).getValue();
} else {
return new int[0];
}
}
/**
* Get an integer named with the given index.
*
* <p>If the index does not exist or its value is not an integer tag,
* then {@code 0} will be returned.</p>
*
* @param index the index
* @return an integer
*/
public int getInteger(int index) {
Tag tag = getIfExists(index);
if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
} else {
return 0;
}
}
/**
* Get an integer 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 integer
*/
public int asInteger(int index) {
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(int index) {
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(int index) {
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
* @return a list of tags
*/
@SuppressWarnings("unchecked")
public <T extends Tag> List<T> getList(int index, Class<T> listType) {
Tag tag = getIfExists(index);
if (tag instanceof ListTag) {
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(int index) {
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(int index) {
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(int index) {
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(int index) {
Tag tag = getIfExists(index);
if (tag instanceof StringTag) {
return ((StringTag) tag).getValue();
} else {
return "";
}
}
@Override @Override
public String toString() { public String toString() {
String name = getName(); String name = getName();

View File

@ -0,0 +1,129 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.jnbt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* 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(Class<? extends Tag> type) {
checkNotNull(type);
this.type = type;
this.entries = new ArrayList<Tag>();
}
/**
* Add the given tag.
*
* @param value the tag
* @return this object
*/
public ListTagBuilder add(Tag value) {
checkNotNull(value);
if (!type.isInstance(value)) {
throw new IllegalArgumentException(value.getClass().getCanonicalName() + " is not of expected type " + type.getCanonicalName());
}
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(Collection<? extends Tag> value) {
checkNotNull(value);
for (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(type, new ArrayList<Tag>(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(String name) {
return new ListTag(name, type, new ArrayList<Tag>(entries));
}
/**
* Create a new builder instance.
*
* @return a new builder
*/
public static ListTagBuilder create(Class<? extends Tag> type) {
return new ListTagBuilder(type);
}
/**
* Create a new builder instance.
*
* @return a new builder
*/
public static <T extends Tag> ListTagBuilder createWith(T ... entries) {
checkNotNull(entries);
if (entries.length == 0) {
throw new IllegalArgumentException("This method needs an array of at least one entry");
}
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");
}
}
ListTagBuilder builder = new ListTagBuilder(type);
builder.addAll(Arrays.asList(entries));
return builder;
}
}

View File

@ -19,28 +19,29 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import com.sk89q.jnbt.Tag;
/** /**
* The <code>TAG_Long</code> tag. * The {@code TAG_Long} tag.
*
* @author Graham Edgecombe
* *
*/ */
public final class LongTag extends Tag { public final class LongTag extends Tag {
/**
* The value.
*/
private final long value; private final long value;
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public LongTag(long value) {
super();
this.value = value;
}
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name the name of the tag
* The name. * @param value the value of the tag
* @param value
* The value.
*/ */
public LongTag(String name, long value) { public LongTag(String name, long value) {
super(name); super(name);

View File

@ -29,14 +29,8 @@ import java.nio.charset.Charset;
*/ */
public final class NBTConstants { public final class NBTConstants {
/**
* The character set used by NBT (UTF-8).
*/
public static final Charset CHARSET = Charset.forName("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, 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_INT = 3, TYPE_LONG = 4, TYPE_FLOAT = 5, TYPE_DOUBLE = 6,
TYPE_BYTE_ARRAY = 7, TYPE_STRING = 8, TYPE_LIST = 9, TYPE_BYTE_ARRAY = 7, TYPE_STRING = 8, TYPE_LIST = 9,

View File

@ -27,20 +27,6 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.sk89q.jnbt.ByteArrayTag;
import com.sk89q.jnbt.ByteTag;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.DoubleTag;
import com.sk89q.jnbt.EndTag;
import com.sk89q.jnbt.FloatTag;
import com.sk89q.jnbt.IntTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.LongTag;
import com.sk89q.jnbt.NBTConstants;
import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.ShortTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
/** /**
* <p> * <p>
@ -60,19 +46,14 @@ import com.sk89q.jnbt.Tag;
*/ */
public final class NBTInputStream implements Closeable { public final class NBTInputStream implements Closeable {
/**
* The data input stream.
*/
private final DataInputStream is; private final DataInputStream is;
/** /**
* Creates a new <code>NBTInputStream</code>, which will source its data * Creates a new {@code NBTInputStream}, which will source its data
* from the specified input stream. * from the specified input stream.
* *
* @param is * @param is the input stream
* The input stream. * @throws IOException if an I/O error occurs
* @throws IOException
* if an I/O error occurs.
*/ */
public NBTInputStream(InputStream is) throws IOException { public NBTInputStream(InputStream is) throws IOException {
this.is = new DataInputStream(is); this.is = new DataInputStream(is);
@ -82,8 +63,7 @@ public final class NBTInputStream implements Closeable {
* Reads an NBT tag from the stream. * Reads an NBT tag from the stream.
* *
* @return The tag that was read. * @return The tag that was read.
* @throws IOException * @throws IOException if an I/O error occurs.
* if an I/O error occurs.
*/ */
public Tag readTag() throws IOException { public Tag readTag() throws IOException {
return readTag(0); return readTag(0);
@ -92,11 +72,9 @@ public final class NBTInputStream implements Closeable {
/** /**
* Reads an NBT from the stream. * Reads an NBT from the stream.
* *
* @param depth * @param depth the depth of this tag
* The depth of this tag.
* @return The tag that was read. * @return The tag that was read.
* @throws IOException * @throws IOException if an I/O error occurs.
* if an I/O error occurs.
*/ */
private Tag readTag(int depth) throws IOException { private Tag readTag(int depth) throws IOException {
int type = is.readByte() & 0xFF; int type = is.readByte() & 0xFF;
@ -117,18 +95,13 @@ public final class NBTInputStream implements Closeable {
/** /**
* Reads the payload of a tag, given the name and type. * Reads the payload of a tag, given the name and type.
* *
* @param type * @param type the type
* The type. * @param name the name
* @param name * @param depth the depth
* The name. * @return the tag
* @param depth * @throws IOException if an I/O error occurs.
* The depth.
* @return The tag.
* @throws IOException
* if an I/O error occurs.
*/ */
private Tag readTagPayload(int type, String name, int depth) private Tag readTagPayload(int type, String name, int depth) throws IOException {
throws IOException {
switch (type) { switch (type) {
case NBTConstants.TYPE_END: case NBTConstants.TYPE_END:
if (depth == 0) { if (depth == 0) {
@ -197,6 +170,7 @@ public final class NBTInputStream implements Closeable {
} }
} }
@Override
public void close() throws IOException { public void close() throws IOException {
is.close(); is.close();
} }

View File

@ -19,23 +19,31 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.storage.InvalidFormatException; import com.sk89q.worldedit.world.storage.InvalidFormatException;
import java.util.Map; import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* A class which contains NBT-related utility methods. * A class which contains NBT-related utility methods.
* *
* @author Graham Edgecombe
*
*/ */
public final class NBTUtils { public final class NBTUtils {
/**
* Default private constructor.
*/
private NBTUtils() {
}
/** /**
* Gets the type name of a tag. * Gets the type name of a tag.
* *
* @param clazz * @param clazz the tag class
* The tag class.
* @return The type name. * @return The type name.
*/ */
public static String getTypeName(Class<? extends Tag> clazz) { public static String getTypeName(Class<? extends Tag> clazz) {
@ -72,11 +80,9 @@ public final class NBTUtils {
/** /**
* Gets the type code of a tag class. * Gets the type code of a tag class.
* *
* @param clazz * @param clazz the tag class
* The tag class.
* @return The type code. * @return The type code.
* @throws IllegalArgumentException * @throws IllegalArgumentException if the tag class is invalid.
* if the tag class is invalid.
*/ */
public static int getTypeCode(Class<? extends Tag> clazz) { public static int getTypeCode(Class<? extends Tag> clazz) {
if (clazz.equals(ByteArrayTag.class)) { if (clazz.equals(ByteArrayTag.class)) {
@ -112,11 +118,9 @@ public final class NBTUtils {
/** /**
* Gets the class of a type of tag. * Gets the class of a type of tag.
* *
* @param type * @param type the type
* The type.
* @return The class. * @return The class.
* @throws IllegalArgumentException * @throws IllegalArgumentException if the tag type is invalid.
* if the tag type is invalid.
*/ */
public static Class<? extends Tag> getTypeClass(int type) { public static Class<? extends Tag> getTypeClass(int type) {
switch (type) { switch (type) {
@ -151,10 +155,17 @@ public final class NBTUtils {
} }
/** /**
* Default private constructor. * Read a vector from a list tag containing ideally three values: the
* X, Y, and Z components.
*
* <p>For values that are unavailable, their values will be 0.</p>
*
* @param listTag the list tag
* @return a vector
*/ */
private NBTUtils() { public static Vector toVector(ListTag listTag) {
checkNotNull(listTag);
return new Vector(listTag.asDouble(0), listTag.asDouble(1), listTag.asDouble(2));
} }
/** /**

View File

@ -19,28 +19,28 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import com.sk89q.jnbt.Tag;
/** /**
* The <code>TAG_Short</code> tag. * The {@code TAG_Short} tag.
*
* @author Graham Edgecombe
*
*/ */
public final class ShortTag extends Tag { public final class ShortTag extends Tag {
/**
* The value.
*/
private final short value; private final short value;
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public ShortTag(short value) {
super();
this.value = value;
}
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name the name of the tag
* The name. * @param value the value of the tag
* @param value
* The value.
*/ */
public ShortTag(String name, short value) { public ShortTag(String name, short value) {
super(name); super(name);

View File

@ -19,31 +19,35 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import com.sk89q.jnbt.Tag; import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* The <code>TAG_String</code> tag. * The {@code TAG_String} tag.
*
* @author Graham Edgecombe
*
*/ */
public final class StringTag extends Tag { public final class StringTag extends Tag {
/**
* The value.
*/
private final String value; private final String value;
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public StringTag(String value) {
super();
checkNotNull(value);
this.value = value;
}
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name the name of the tag
* The name. * @param value the value of the tag
* @param value
* The value.
*/ */
public StringTag(String name, String value) { public StringTag(String name, String value) {
super(name); super(name);
checkNotNull(value);
this.value = value; this.value = value;
} }

View File

@ -19,33 +19,36 @@
package com.sk89q.jnbt; package com.sk89q.jnbt;
import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* Represents a single NBT tag. * Represents a NBT tag.
*
* @author Graham Edgecombe
*
*/ */
public abstract class Tag { public abstract class Tag {
/**
* The name of this tag.
*/
private final String name; private final String name;
/**
* Create a new tag with an empty name.
*/
Tag() {
this("");
}
/** /**
* Creates the tag with the specified name. * Creates the tag with the specified name.
* *
* @param name * @param name the name
* The name.
*/ */
public Tag(String name) { Tag(String name) {
checkNotNull(name);
this.name = name; this.name = name;
} }
/** /**
* Gets the name of this tag. * Gets the name of this tag.
* *
* @return The name of this tag. * @return the name of this tag
*/ */
public final String getName() { public final String getName() {
return name; return name;
@ -54,7 +57,7 @@ public abstract class Tag {
/** /**
* Gets the value of this tag. * Gets the value of this tag.
* *
* @return The value of this tag. * @return the value
*/ */
public abstract Object getValue(); public abstract Object getValue();

View File

@ -0,0 +1,58 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.storage;
import com.sk89q.jnbt.ListTag;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.util.Location;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Utility methods for working with NBT data used in Minecraft.
*/
public final class NBTConversions {
private NBTConversions() {
}
/**
* Read a {@code Location} from two list tags, the first of which contains
* three numbers for the X, Y, and Z components, and the second of
* which contains two numbers, the yaw and pitch in degrees.
*
* <p>For values that are unavailable, their values will be 0.</p>
*
* @param extent the extent
* @param positionTag the position tag
* @param directionTag the direction tag
* @return a location
*/
public static Location toLocation(Extent extent, ListTag positionTag, ListTag directionTag) {
checkNotNull(extent);
checkNotNull(positionTag);
checkNotNull(directionTag);
return new Location(
extent,
positionTag.asDouble(0), positionTag.asDouble(1), positionTag.asDouble(2),
((float) Math.toRadians(directionTag.asDouble(0))), ((float) Math.toRadians(directionTag.asDouble(1))));
}
}