From 38137cea2c8ea8c14fcca3d7092a3b2cc4a3895e Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Mon, 7 Jan 2013 20:37:21 +0100 Subject: [PATCH] Verify element type in tag list. --- .../protocol/wrappers/nbt/NbtList.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtList.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtList.java index c33380a1..ac1c0946 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtList.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtList.java @@ -111,27 +111,33 @@ public class NbtList implements NbtWrapper>>, Iterabl public List> getValue() { if (savedList == null) { savedList = new ConvertedList>(container.getValue()) { + // Check and see if the element is valid + private void verifyElement(NbtBase 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 public boolean add(NbtBase e) { - if (e == null) - 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()); - + verifyElement(e); return super.add(e); } @Override public void add(int index, NbtBase 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."); - if (index == 0) - container.setSubType(element.getType()); - + verifyElement(element); super.add(index, element); }