Verify element type in tag list.

This commit is contained in:
Kristian S. Stangeland 2013-01-07 20:37:21 +01:00
parent 6b3d85af62
commit 38137cea2c

View File

@ -111,27 +111,33 @@ public class NbtList<TType> implements NbtWrapper<List<NbtBase<TType>>>, Iterabl
public List<NbtBase<TType>> getValue() { public List<NbtBase<TType>> getValue() {
if (savedList == null) { if (savedList == null) {
savedList = new ConvertedList<Object, NbtBase<TType>>(container.getValue()) { savedList = new ConvertedList<Object, NbtBase<TType>>(container.getValue()) {
// Check and see if the element is valid
private void verifyElement(NbtBase<TType> element) {
if (element == null)
throw new IllegalArgumentException("Cannot store NULL elements in list.");
if (!element.getName().equals(EMPTY_NAME))
throw new IllegalArgumentException("Cannot add a the named NBT tag " + element + " to a list.");
// Check element type
if (size() > 0) {
if (!element.getType().equals(getElementType())) {
throw new IllegalArgumentException(
"Cannot add " + element + " of " + element.getType() + " to a list of type " + getElementType());
}
} else {
container.setSubType(element.getType());
}
}
@Override @Override
public boolean add(NbtBase<TType> e) { public boolean add(NbtBase<TType> e) {
if (e == null) verifyElement(e);
throw new IllegalArgumentException("Cannot store NULL elements in list.");
if (!e.getName().equals(EMPTY_NAME))
throw new IllegalArgumentException("Cannot add a named NBT tag " + e + " to a list.");
if (size() == 0)
container.setSubType(e.getType());
return super.add(e); return super.add(e);
} }
@Override @Override
public void add(int index, NbtBase<TType> element) { public void add(int index, NbtBase<TType> element) {
if (element == null) verifyElement(element);
throw new IllegalArgumentException("Cannot store NULL elements in list.");
if (!element.getName().equals(EMPTY_NAME))
throw new IllegalArgumentException("Cannot add a the named NBT tag " + element + " to a list.");
if (index == 0)
container.setSubType(element.getType());
super.add(index, element); super.add(index, element);
} }