#685: Implement support for PersistentDataContainer arrays

By: Parker Hawke <hawkeboyz2@hotmail.com>
This commit is contained in:
CraftBukkit/Spigot 2020-06-26 10:49:28 +10:00
commit a74fb02e61

View file

@ -14,6 +14,7 @@ import net.minecraft.server.NBTTagDouble;
import net.minecraft.server.NBTTagFloat; import net.minecraft.server.NBTTagFloat;
import net.minecraft.server.NBTTagInt; import net.minecraft.server.NBTTagInt;
import net.minecraft.server.NBTTagIntArray; import net.minecraft.server.NBTTagIntArray;
import net.minecraft.server.NBTTagList;
import net.minecraft.server.NBTTagLong; import net.minecraft.server.NBTTagLong;
import net.minecraft.server.NBTTagLongArray; import net.minecraft.server.NBTTagLongArray;
import net.minecraft.server.NBTTagShort; 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())); 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 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 Passing any other instance of this form to the tag type registry will throw a ClassCastException as defined in TagAdapter#build