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