Add utility methods to convert CompoundTags to Maps with non-tag values and ListTags to Lists with non-tag values.

This commit is contained in:
Steveice10 2014-03-25 18:02:01 -07:00
parent c2bdc677fc
commit ff376a4c76
2 changed files with 46 additions and 3 deletions

View File

@ -132,6 +132,29 @@ public class CompoundTag extends Tag implements Iterable<Tag> {
this.value.clear();
}
/**
* Converts this CompoundTag to a Map<String, Object> with non-tag values.
* @return A Map<String, Object> with non-tag values.
*/
public Map<String, Object> toMap() {
Map<String, Object> ret = new HashMap<String, Object>();
for(String name : this.value.keySet()) {
Tag tag = this.value.get(name);
Object o = null;
if(tag instanceof CompoundTag) {
o = ((CompoundTag) tag).toMap();
} else if(tag instanceof ListTag) {
o = ((ListTag) tag).toList();
} else {
o = tag.getValue();
}
ret.put(name, o);
}
return ret;
}
@Override
public Iterator<Tag> iterator() {
return this.values().iterator();

View File

@ -6,9 +6,7 @@ import org.spacehq.opennbt.TagRegistry;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.*;
/**
* A tag containing a list of tags.
@ -112,6 +110,28 @@ public class ListTag<T extends Tag> extends Tag implements Iterable<T> {
return this.value.size();
}
/**
* Converts this CompoundTag to a List<Object> with non-tag values.
* @return A List<Object> with non-tag values.
*/
public List<Object> toList() {
List<Object> ret = new ArrayList<Object>();
for(Tag tag : this.value) {
Object o = null;
if(tag instanceof CompoundTag) {
o = ((CompoundTag) tag).toMap();
} else if(tag instanceof ListTag) {
o = ((ListTag) tag).toList();
} else {
o = tag.getValue();
}
ret.add(o);
}
return ret;
}
@Override
public Iterator<T> iterator() {
return this.value.iterator();