4

Basically, I'm trying to make a "One command creation", which is, if you don't know, when you have an entire creation/project in one long command.

My creation makes a box, and then puts command blocks inside that box that will do stuff, all generating from a single command. One of the command blocks in that box has some quotes in it that it needs to be able to run, but the long command that makes the command block can't have those quotes for some reason. How can I get around this?

Here's the entire command (I believe some parts of it might be useful to this but I'm not sure):

summon falling_block ~ ~1 ~ {Time:1,BlockState:{Name:redstone_block},Passengers:[{id:armor_stand,Health:0,Passengers:[{id:falling_block,Time:1,BlockState:{Name:activator_rail},Passengers:[{id:command_block_minecart,Command:'gamerule commandBlockOutput false'},{id:command_block_minecart,Command:'data merge block ~ ~-2 ~ {auto:0}'},{id:command_block_minecart,Command:'fill ~3 ~-2 ~-2 ~9 ~3 ~2 white_concrete hollow'},{id:command_block_minecart,Command:'fill ~3 ~-1 ~-2 ~9 ~2 ~2 light_gray_stained_glass replace white_concrete'},{id:command_block_minecart,Command:'setblock ~4 ~-1 ~-1 repeating_command_block[facing=east]{Command:"tag @e[nbt={OnGround:1b,Item:{id:"minecraft:iron_block",Count:3b}}] add irondoor1"}'},{id:command_block_minecart,Command:'data merge block ~4 ~-1 ~-1 {auto:1b}'},{id:command_block_minecart,Command:'setblock ~ ~1 ~ command_block{auto:1,Command:"fill ~ ~ ~ ~ ~-2 ~ air"}'},{id:command_block_minecart,Command:'kill @e[type=command_block_minecart,distance=..1]'}]}]}]}

And here is the bit of it that makes the command block in question:

{id:command_block_minecart,Command:'setblock ~4 ~-1 ~-1 repeating_command_block[facing=east]{Command:"tag @e[nbt={OnGround:1b,Item:{id:"minecraft:iron_block",Count:3b}}] add irondoor1"}'}

Little explanation - It summons a redstone block with an activator rail on top, thus powering the activator rail. Then it spawns a command block in a minecart (CBM) which goes on a powered activator rail, thus activating the command in the CBM. That command sets a block inside my box to be a repeating command block with a command. The problem is in this command - specifically, at id:"minecraft:iron_block". The problem is that the repeating command block needs those quote marks, but the long command can't have those quotes or it doesn't work.

Apologies if anything was confusing. If anyone can solve this issue I'd greatly appreciate it, as this is only one of many times this has happened.

4
4

You are going into a mess of escaping.

You have a double quoted string inside a double quoted string inside a single quoted string.

Let's work backwards from the innermost layer:

"minecraft:iron_block"

Then you move onto the next layer:

"tag @e[nbt={OnGround:1b,Item:{id:"minecraft:iron_block",Count:3b}}] add irondoor1"

In order to put those inner quotation marks as part of the outer string, put a backslash \ before them:

"tag @e[nbt={OnGround:1b,Item:{id:\"minecraft:iron_block\",Count:3b}}] add irondoor1"

Then, you want to put all of that inside a single quoted string:

'setblock ~4 ~-1 ~-1 repeating_command_block[facing=east]{Command:"tag @e[nbt={OnGround:1b,Item:{id:\"minecraft:iron_block\",Count:3b}}] add irondoor1"}'

The outer quotation marks of the Command are fine and correct because you are using a single quoted string. However, the escape sequence of \" is no longer valid here because you have it inside another string. You then need to escape the backslash as well by placing two in a row: \\"

So try this:

{id:command_block_minecart,Command:'setblock ~4 ~-1 ~-1 repeating_command_block[facing=east]{Command:"tag @e[nbt={OnGround:1b,Item:{id:\\"minecraft:iron_block\\",Count:3b}}] add irondoor1"}'}
3
  • I tought you would need to escape both \ and ", and having \\\" in the end. Why is that not the case? I consider the use of escaping as, if you have \, consider the next char as just a string.
    – BunnyMerz
    Apr 17 at 21:15
  • 1
    @BrunoRodrigues No, you don't need to escape the quotation mark, you only need to escape the backslash. Notice that the full outer string is single-quoted, meaning you don't need to escape the quotation mark, in fact \" is an invalid escape sequence. Apr 17 at 21:29
  • Thank you so much, I didn't even know what an escape sequence was. After a quick google, I understand what it is, and this works. Apr 19 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.