#685: Implement support for PersistentDataContainer arrays

This commit is contained in:
Parker Hawke 2020-06-26 10:49:28 +10:00 committed by md_5
parent b900513035
commit c49b67ac91
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11

View File

@ -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