mirror of
https://github.com/ViaVersion/ViaNBT.git
synced 2024-11-26 12:15:19 +01:00
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:
parent
c2bdc677fc
commit
ff376a4c76
@ -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();
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user