From c49b67ac91fa12a763197432f2160af6321ad95e Mon Sep 17 00:00:00 2001 From: Parker Hawke Date: Fri, 26 Jun 2020 10:49:28 +1000 Subject: [PATCH] #685: Implement support for PersistentDataContainer arrays --- .../CraftPersistentDataTypeRegistry.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataTypeRegistry.java b/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataTypeRegistry.java index 084615e676..1b3b864638 100644 --- a/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataTypeRegistry.java +++ b/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataTypeRegistry.java @@ -14,6 +14,7 @@ import net.minecraft.server.NBTTagDouble; import net.minecraft.server.NBTTagFloat; import net.minecraft.server.NBTTagInt; import net.minecraft.server.NBTTagIntArray; +import net.minecraft.server.NBTTagList; import net.minecraft.server.NBTTagLong; import net.minecraft.server.NBTTagLongArray; import net.minecraft.server.NBTTagShort; @@ -148,6 +149,32 @@ public final class CraftPersistentDataTypeRegistry { return createAdapter(long[].class, NBTTagLongArray.class, array -> new NBTTagLongArray(Arrays.copyOf(array, array.length)), n -> Arrays.copyOf(n.getLongs(), n.size())); } + /* + Complex Arrays + */ + if (Objects.equals(PersistentDataContainer[].class, type)) { + return createAdapter(PersistentDataContainer[].class, NBTTagList.class, + (containerArray) -> { + NBTTagList list = new NBTTagList(); + for (int i = 0; i < containerArray.length; i++) { + list.add(((CraftPersistentDataContainer) containerArray[i]).toTagCompound()); + } + return list; + }, + (tag) -> { + PersistentDataContainer[] containerArray = new CraftPersistentDataContainer[tag.size()]; + for (int i = 0; i < tag.size(); i++) { + CraftPersistentDataContainer container = new CraftPersistentDataContainer(this); + NBTTagCompound compound = tag.getCompound(i); + for (String key : compound.getKeys()) { + container.put(key, compound.get(key)); + } + containerArray[i] = container; + } + return containerArray; + }); + } + /* Note that this will map the interface PersistentMetadataContainer directly to the CraftBukkit implementation Passing any other instance of this form to the tag type registry will throw a ClassCastException as defined in TagAdapter#build