#1230: Move unstructured PDC NBT serialisation to SNBT

The initial implementation of the CraftNBTTagConfigSerialiser attempted
to represent the entire NBT tree in yaml. While the tree structure
itself is nicely represented, the values and their respective types
become increasingly difficult to properly store in the context of
snakeyml/yml in general.

This is mainly due to the fact that nbt offers a lot of different types
compared to yaml, specifically the primitive arrays and different
floating point values are troublesome as they require parsing via mojang
parsers due to their custom format.

To build a future proof system for unstructured nbt in spigot yml,
this commit moves the entire serialisation fully into SNBT,
producing a single string as output rather than a full yml tree.
SNBT remains easily readable and editable for server owners, which was
one of the main criteria during the initial implementation of the
serialiser (preventing the use of bas64ed gzipped nbt bytes), while also
completely using mojangs parsing, eliminating any need for custom
parsing logic in spigot.

Additionally, a string allows for very straight forward handling of
legacy data by simply parsing strings as SNBT and maps/yml trees as
legacy content, depending on what type snakeyml produces after parsing
the yml content, removing the need for a versioning schema.

By: Bjarne Koll <lynxplay101@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2023-09-24 10:12:19 +10:00
parent 07002cbfcd
commit 5692b3f59a
6 changed files with 158 additions and 28 deletions

View File

@@ -1,6 +1,7 @@
package org.bukkit.craftbukkit.inventory;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
@@ -10,6 +11,7 @@ import net.minecraft.nbt.NBTTagCompound;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@@ -164,6 +166,49 @@ public class PersistentDataContainerTest extends AbstractTestingBase {
return itemMeta;
}
/*
Test edge cases with strings
*/
@Test
public void testStringEdgeCases() throws IOException, InvalidConfigurationException {
final ItemStack stack = new ItemStack(Material.DIAMOND);
final ItemMeta meta = stack.getItemMeta();
assertNotNull(meta);
final String arrayLookalike = "[\"UnicornParticle\",\"TotemParticle\",\"AngelParticle\",\"ColorSwitchParticle\"]";
final String jsonLookalike = """
{
"key": 'A value wrapped in single quotes',
"other": "A value with normal quotes",
"array": ["working", "unit", "tests"]
}
""";
final PersistentDataContainer pdc = meta.getPersistentDataContainer();
pdc.set(requestKey("string_int"), PersistentDataType.STRING, "5i");
pdc.set(requestKey("string_true"), PersistentDataType.STRING, "true");
pdc.set(requestKey("string_byte_array"), PersistentDataType.STRING, "[B;-128B]");
pdc.set(requestKey("string_array_lookalike"), PersistentDataType.STRING, arrayLookalike);
pdc.set(requestKey("string_json_lookalike"), PersistentDataType.STRING, jsonLookalike);
stack.setItemMeta(meta);
final YamlConfiguration config = new YamlConfiguration();
config.set("test", stack);
config.load(new StringReader(config.saveToString())); // Reload config from string
final ItemStack loadedStack = config.getItemStack("test");
assertNotNull(loadedStack);
final ItemMeta loadedMeta = loadedStack.getItemMeta();
assertNotNull(loadedMeta);
final PersistentDataContainer loadedPdc = loadedMeta.getPersistentDataContainer();
assertEquals("5i", loadedPdc.get(requestKey("string_int"), PersistentDataType.STRING));
assertEquals("true", loadedPdc.get(requestKey("string_true"), PersistentDataType.STRING));
assertEquals(arrayLookalike, loadedPdc.get(requestKey("string_array_lookalike"), PersistentDataType.STRING));
assertEquals(jsonLookalike, loadedPdc.get(requestKey("string_json_lookalike"), PersistentDataType.STRING));
}
/*
Test complex object storage
*/

View File

@@ -0,0 +1,66 @@
package org.bukkit.craftbukkit.legacy;
import static org.junit.jupiter.api.Assertions.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.craftbukkit.inventory.CraftItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.support.AbstractTestingBase;
import org.junit.jupiter.api.Test;
public class PersistentDataContainerLegacyTest extends AbstractTestingBase {
@Test
public void ensureLegacyParsing() {
CraftItemFactory.instance(); // Initialize craft item factory to register craft item meta serializers
YamlConfiguration legacyConfig = null;
try (final InputStream input = getClass().getClassLoader().getResourceAsStream("pdc/legacy_pdc.yml")) {
assertNotNull(input, "Legacy pdc yaml was null");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
legacyConfig = YamlConfiguration.loadConfiguration(reader);
}
} catch (IOException e) {
fail("Failed to find test resource!");
}
assertNotNull(legacyConfig, "Could not fetch legacy config");
final ItemStack stack = legacyConfig.getItemStack("test");
assertNotNull(stack);
final ItemMeta meta = stack.getItemMeta();
assertNotNull(meta);
final PersistentDataContainer pdc = meta.getPersistentDataContainer();
assertEquals(Byte.valueOf(Byte.MAX_VALUE), pdc.get(key("byte"), PersistentDataType.BYTE), "legacy byte was wrong");
assertEquals(Short.valueOf(Short.MAX_VALUE), pdc.get(key("short"), PersistentDataType.SHORT), "legacy short was wrong");
assertEquals(Integer.valueOf(Integer.MAX_VALUE), pdc.get(key("integer"), PersistentDataType.INTEGER), "legacy integer was wrong");
assertEquals(Long.valueOf(Long.MAX_VALUE), pdc.get(key("long"), PersistentDataType.LONG), "legacy long was wrong");
assertEquals(Float.valueOf(Float.MAX_VALUE), pdc.get(key("float"), PersistentDataType.FLOAT), "legacy float was wrong");
assertEquals(Double.valueOf(Double.MAX_VALUE), pdc.get(key("double"), PersistentDataType.DOUBLE), "legacy double was wrong");
assertEquals("stringy", pdc.get(key("string_simple"), PersistentDataType.STRING), "legacy string-simple was wrong");
assertEquals("What a fun complex string 🔥", pdc.get(key("string_complex"), PersistentDataType.STRING), "legacy string-complex was wrong");
assertArrayEquals(new byte[]{Byte.MIN_VALUE}, pdc.get(key("byte_array"), PersistentDataType.BYTE_ARRAY), "legacy byte array was wrong");
assertArrayEquals(new int[]{Integer.MIN_VALUE}, pdc.get(key("integer_array"), PersistentDataType.INTEGER_ARRAY), "legacy integer array was wrong");
assertArrayEquals(new long[]{Long.MIN_VALUE}, pdc.get(key("long_array"), PersistentDataType.LONG_ARRAY), "legacy long array was wrong");
assertEquals("5", pdc.get(key("string_edge_case_number"), PersistentDataType.STRING), "legacy string edge case number");
assertEquals("\"Hello world\"", pdc.get(key("string_edge_case_quoted"), PersistentDataType.STRING), "legacy string edge case quotes");
}
private NamespacedKey key(String key) {
return new NamespacedKey("test", key);
}
}