sandstone-mc / sandstone

Sandstone | Next Generation Framework for Minecraft
https://sandstone.dev/
MIT License
173 stars 16 forks source link

`NBT.stringify` does not escape things correctly when used in JSON (as opposed to commands) #131

Open GrantGryczan opened 2 years ago

GrantGryczan commented 2 years ago

NBT.stringify({ test: '\n' }) should output "{test:'\n'}" when used in a JSON file, not "{test:'\\n'}". This input impossible to specify in commands validly though.

Minimal reproduction code:

LootTable('test:book', {
    type: 'command',
    pools: [{
        rolls: 1,
        entries: [{
            type: 'minecraft:item',
            name: 'minecraft:writable_book',
            functions: [{
                function: 'set_nbt',
                tag: NBT.stringify({
                    pages: ['line 1\nline 2']
                })
            }]
        }],
        bonus_rolls: 0
    }]
});

Minecraft does not recognize this NBT as valid. Entering /loot give @s loot test:book will not work. Opening the outputted loot table file shows this error:

image

And Minecraft logs this error:

image

Command-Master commented 2 years ago

I'm fairly sure you actually can't have newlines in NBT text. Are you sure there is any valid output?

GrantGryczan commented 2 years ago

Yes you can, and removing the double slashes fixes it.

TheMrZZ commented 2 years ago

Can you show me a detailed example? Minecraft does not seem to accept \n, while \\n is accepted.

This works: image

This doesn't: image

GrantGryczan commented 2 years ago

This NBT is not in a Minecraft command. It's in a string in a JSON file.

I realize this issue is more complicated that I thought, because it seems escaping needs to be done differently between NBT in a JSON string and NBT in a command.

Edit: Never mind, \n isn't even valid in NBT for a command, escaped or not. It's not a valid escape sequence.