sandstone icon indicating copy to clipboard operation
sandstone copied to clipboard

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

Open GrantGryczan opened this issue 3 years ago • 4 comments

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

GrantGryczan avatar Dec 18 '21 10:12 GrantGryczan

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

Command-Master avatar Dec 19 '21 08:12 Command-Master

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

GrantGryczan avatar Dec 19 '21 08:12 GrantGryczan

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

TheMrZZ avatar Dec 19 '21 09:12 TheMrZZ

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.

GrantGryczan avatar Dec 19 '21 09:12 GrantGryczan