From 8abb93114f6fcc7aa668910076a146f733520934 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Mon, 7 Jan 2013 20:15:21 +0100 Subject: [PATCH] Ensure that we can create compounds and lists. --- .../protocol/wrappers/nbt/NbtCompound.java | 14 +++----------- .../protocol/wrappers/nbt/NbtFactory.java | 11 ++++++++++- .../protocol/wrappers/nbt/NbtType.java | 7 +++++++ .../protocol/events/PacketContainerTest.java | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtCompound.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtCompound.java index 0da68c11..06e49e36 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtCompound.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtCompound.java @@ -36,10 +36,10 @@ public class NbtCompound implements NbtWrapper>>, Iterabl * @param list - the list of elements to add. * @return The new wrapped NBT compound. */ - public static NbtCompound fromList(String name, Collection> list) { - NbtCompound copy = new NbtCompound(name); + public static NbtCompound fromList(String name, Collection> list) { + NbtCompound copy = fromName(name); - for (NbtBase base : list) + for (NbtBase base : list) copy.getValue().put(base.getName(), base); return copy; } @@ -80,14 +80,6 @@ public class NbtCompound implements NbtWrapper>>, Iterabl return getValue().keySet(); } - /** - * Retrieve a Collection view of the entries in this compound. - * @return A view of each NBT tag in this compound. - */ - public Collection> asCollection(){ - return getValue().values(); - } - @Override public Map> getValue() { // Return a wrapper map diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java index 5a4bf3bf..cbf66f62 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java @@ -165,10 +165,19 @@ public class NbtFactory { * @param list - the list of elements to add. * @return The new wrapped NBT compound. */ - public static NbtCompound ofCompound(String name, Collection> list) { + public static NbtCompound ofCompound(String name, Collection> list) { return NbtCompound.fromList(name, list); } + /** + * Construct a new NBT compound wrapper. + * @param name - the name of the compound wrapper. + * @return The new wrapped NBT compound. + */ + public static NbtCompound ofCompound(String name) { + return NbtCompound.fromName(name); + } + /** * Construct a NBT list of out an array of values. * @param name - name of this list. diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtType.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtType.java index f6f950f2..728ebbe4 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtType.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtType.java @@ -4,6 +4,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.google.common.primitives.Primitives; + /** * Represents all the element types * @@ -88,6 +90,11 @@ public enum NbtType { for (NbtType type : values) { lookup[type.getRawID()] = type; classLookup.put(type.getValueType(), type); + + // Add a wrapper type + if (type.getValueType().isPrimitive()) { + classLookup.put(Primitives.wrap(type.getValueType()), type); + } } } diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java index bbee0a76..f2fa2bff 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java @@ -32,6 +32,8 @@ import com.comphenix.protocol.wrappers.BukkitConverters; import com.comphenix.protocol.wrappers.ChunkPosition; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedWatchableObject; +import com.comphenix.protocol.wrappers.nbt.NbtCompound; +import com.comphenix.protocol.wrappers.nbt.NbtFactory; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -233,6 +235,22 @@ public class PacketContainerTest { assertEquals(testValue, worldAccess.read(0)); } + @Test + public void testGetNbtModifier() { + PacketContainer updateTileEntity = new PacketContainer(132); + + NbtCompound compound = NbtFactory.ofCompound("test"); + compound.put("test", "name"); + compound.put(NbtFactory.ofList("ages", 1, 2, 3)); + + updateTileEntity.getNbtModifier().write(0, compound); + + NbtCompound result = (NbtCompound) updateTileEntity.getNbtModifier().read(0); + + assertEquals(compound.getString("test"), result.getString("test")); + assertEquals(compound.getList("ages"), result.getList("ages")); + } + @Test public void testGetDataWatcherModifier() { PacketContainer mobSpawnPacket = new PacketContainer(Packets.Server.MOB_SPAWN);