Allow serialisation of empty nbt components

This commit is contained in:
KrystilizeNevaDies 2023-08-29 10:01:30 +10:00
parent 99941e511c
commit 56a6cfc2fb
2 changed files with 5 additions and 3 deletions

View File

@ -12,6 +12,8 @@ import java.util.function.Function;
* Basic serializers for {@link Tag tags}.
*/
final class Serializers {
static final boolean SERIALIZE_EMPTY_COMPOUND = System.getProperty("minestom.serialization.serialize-nbt-compound", "false").equalsIgnoreCase("true");
static final Entry<Byte, NBTByte> BYTE = new Entry<>(NBTType.TAG_Byte, NBTByte::getValue, NBT::Byte);
static final Entry<Boolean, NBTByte> BOOLEAN = new Entry<>(NBTType.TAG_Byte, NBTByte::asBoolean, NBT::Boolean);
static final Entry<Short, NBTShort> SHORT = new Entry<>(NBTType.TAG_Short, NBTShort::getValue, NBT::Short);
@ -31,7 +33,7 @@ final class Serializers {
static <T> Entry<T, NBTCompound> fromTagSerializer(TagSerializer<T> serializer) {
return new Serializers.Entry<>(NBTType.TAG_Compound,
(NBTCompound compound) -> {
if (compound.isEmpty()) return null;
if ((!SERIALIZE_EMPTY_COMPOUND) && compound.isEmpty()) return null;
return serializer.read(TagHandler.fromCompound(compound));
},
(value) -> {

View File

@ -281,7 +281,7 @@ final class TagHandlerImpl implements TagHandler {
this.entries.forValues(entry -> {
final Tag tag = entry.tag;
final NBT nbt = entry.updatedNbt();
if (!tag.entry.isPath() || !((NBTCompound) nbt).isEmpty()) {
if (!tag.entry.isPath() || (!Serializers.SERIALIZE_EMPTY_COMPOUND) && !((NBTCompound) nbt).isEmpty()) {
tmp.put(tag.getKey(), nbt);
}
});
@ -313,7 +313,7 @@ final class TagHandlerImpl implements TagHandler {
tmp.put(tag.getKey(), nbt);
entries.put(tag.index, valueToEntry(result, tag, value));
});
if (tmp.isEmpty() && parent != null)
if ((!Serializers.SERIALIZE_EMPTY_COMPOUND) && tmp.isEmpty() && parent != null)
return null; // Empty child node
result.compound = tmp.toCompound();
return result;