#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

@@ -534,7 +534,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
}
}
Map nbtMap = SerializableMeta.getObject(Map.class, map, BUKKIT_CUSTOM_TAG.BUKKIT, true);
Object nbtMap = SerializableMeta.getObject(Object.class, map, BUKKIT_CUSTOM_TAG.BUKKIT, true); // We read both legacy maps and potential modern snbt strings here
if (nbtMap != null) {
this.persistentDataContainer.putAll((NBTTagCompound) CraftNBTTagConfigSerializer.deserialize(nbtMap));
}

View File

@@ -153,7 +153,7 @@ public class CraftPersistentDataContainer implements PersistentDataContainer {
return hashCode;
}
public Map<String, Object> serialize() {
return (Map<String, Object>) CraftNBTTagConfigSerializer.serialize(toTagCompound());
public String serialize() {
return CraftNBTTagConfigSerializer.serialize(toTagCompound());
}
}

View File

@@ -15,6 +15,8 @@ import net.minecraft.nbt.NBTTagDouble;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.nbt.SnbtPrinterTagVisitor;
import org.jetbrains.annotations.NotNull;
public class CraftNBTTagConfigSerializer {
@@ -23,35 +25,29 @@ public class CraftNBTTagConfigSerializer {
private static final Pattern DOUBLE = Pattern.compile("[-+]?(?:[0-9]+[.]?|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?d", Pattern.CASE_INSENSITIVE);
private static final MojangsonParser MOJANGSON_PARSER = new MojangsonParser(new StringReader(""));
public static Object serialize(NBTBase base) {
if (base instanceof NBTTagCompound) {
Map<String, Object> innerMap = new HashMap<>();
for (String key : ((NBTTagCompound) base).getAllKeys()) {
innerMap.put(key, serialize(((NBTTagCompound) base).get(key)));
}
return innerMap;
} else if (base instanceof NBTTagList) {
List<Object> baseList = new ArrayList<>();
for (int i = 0; i < ((NBTList) base).size(); i++) {
baseList.add(serialize((NBTBase) ((NBTList) base).get(i)));
}
return baseList;
} else if (base instanceof NBTTagString) {
return base.getAsString();
} else if (base instanceof NBTTagInt) { // No need to check for doubles, those are covered by the double itself
return base.toString() + "i";
}
return base.toString();
public static String serialize(@NotNull final NBTBase base) {
final SnbtPrinterTagVisitor snbtVisitor = new SnbtPrinterTagVisitor();
return snbtVisitor.visit(base);
}
public static NBTBase deserialize(Object object) {
public static NBTBase deserialize(final Object object) {
// The new logic expects the top level object to be a single string, holding the entire nbt tag as SNBT.
if (object instanceof final String snbtString) {
try {
return MojangsonParser.parseTag(snbtString);
} catch (final CommandSyntaxException e) {
throw new RuntimeException("Failed to deserialise nbt", e);
}
} else { // Legacy logic is passed to the internal legacy deserialization that attempts to read the old format that *unsuccessfully* attempted to read/write nbt to a full yml tree.
return internalLegacyDeserialization(object);
}
}
private static NBTBase internalLegacyDeserialization(@NotNull final Object object) {
if (object instanceof Map) {
NBTTagCompound compound = new NBTTagCompound();
for (Map.Entry<String, Object> entry : ((Map<String, Object>) object).entrySet()) {
compound.put(entry.getKey(), deserialize(entry.getValue()));
compound.put(entry.getKey(), internalLegacyDeserialization(entry.getValue()));
}
return compound;
@@ -63,7 +59,7 @@ public class CraftNBTTagConfigSerializer {
NBTTagList tagList = new NBTTagList();
for (Object tag : list) {
tagList.add(deserialize(tag));
tagList.add(internalLegacyDeserialization(tag));
}
return tagList;