1

I want to place a sign on a block with the text '1' on its first line, that also, when you click on the sign whilst holding a piece of paper with the name '1853914142' it runs a command. This is what I've got so far;

/setblock -8 42 189 minecraft:birch_wall_sign[facing=east]{Text1:'{"text":"1","color":"black","clickEvent":{"action":"run_command","value":"/execute if entity @p[nbt={SelectedItem:{id:"minecraft:paper",tag:{display:{Name:\'{"text":"1853914142"}\'}}}}] run say hi"}}'}

Unfortunately, when I run this command inside a command block, it sets the sign correctly, however, no text appears on any of the lines. I'm not entirely sure that the escape sequence is correct, I think it may be the problem but I'm not sure. If anyone has any answers, they would be much appreciated!

1 Answer 1

0

So you have a lot of nesting here. You have JSON inside of NBT inside of a command inside of JSON inside of NBT inside of a command. Let's look at each component separately (outermost to innermost):

setblock -8 42 189 minecraft:birch_wall_sign[facing=east]{Text1:'…'}
{"text":"1","color":"black","clickEvent":{"action":"run_command","value":"…"}}
execute if entity @p[nbt={SelectedItem:{id:"minecraft:paper",tag:{display:{Name:'…'}}}}] run say hi
{"text":"1853914142"}

So we want to insert the contents of the fourth line into the spot marked on line 3, insert the result into line 2, and then line 1.

  1. So to put line 4 into line 3, it's simple enough. We get this:

    execute if entity @p[nbt={
      SelectedItem: {
        id: "minecraft:paper",
        tag: {
          display: {
            Name: '{"text":"1853914142"}'
          }
        }
      }
    }] run say hi
    
  2. Now to put that into line 2, we need to replace all instances of " with \". If we had any \s we'd replace them with \\, but we don't… yet:

    {
      "text": "1",
      "color": "black",
      "clickEvent": {
        "action": "run_command",
        "value":"execute if entity @p[nbt={SelectedItem:{id:\"minecraft:paper\",tag:{display:{Name:'{\"text\":\"1853914142\"}'}}}}] run say hi"
      }
    }
    
  3. And the big finish to put it all together into line 1. Because the string in line 1 is denoted with ', we replace all 's with \', and we replace all \ with \\.

    setblock -8 42 189 minecraft:birch_wall_sign[facing=east]{
      Text1: '{"text":"1","color":"black","clickEvent":{"action":"run_command","value":"execute if entity @p[nbt={SelectedItem:{id:\\"minecraft:paper\\",tag:{display:{Name:\'{\\"text\\":\\"1853914142\\"}\'}}}}] run say hi"}}'
    }
    

    And that's our final answer.

Comparing it with what you originally had at the start:

setblock -8 42 189 minecraft:birch_wall_sign[facing=east]{
  Text1: '{"text":"1","color":"black","clickEvent":{"action":"run_command","value":"execute if entity @p[nbt={SelectedItem:{id:"minecraft:paper",tag:{display:{Name:\'{"text":"1853914142"}\'}}}}] run say hi"}}'
}

Looks like you put just enough escaping so the NBT processor would validate the outer NBT, but the JSON inside of it was messed up, so the sign got confused and displayed nothing.

4
  • Hmmm... It seems that is still not working, in the final answer it seems you missed replacing the "with \\" in the {\\"text":\\"1853914142\\"}. I put in the missing \\ so it became {\\"text\\":\\"1853914142\\"}. Still, the sign does not want to work. All it says when I run the command block is 'Could not set the block'. I am not familiar with escape sequences as I explained in the original question so I am lost as to what to do now. Thank you for your answer however, it was still very useful! Jan 2, 2022 at 0:45
  • @CommandsRule The final code snippet was your original code posted a second time, hence the words “Comparing it with what you originally had at the start.” The words “and that's our final answer” were referring to the code snippet just above that, which is the one you should be taking, and it had additional fixes besides the one you just mentioned. Please read the words carefully to ensure you are taking the correct code from the post!
    – One 2 Many
    Jan 2, 2022 at 2:22
  • And it turns out there was a legitimate mistake when I wrote this: I missed the quotation mark after the word text in the custom name of the item. I changed all the steps so they should be correct.
    – One 2 Many
    Jan 2, 2022 at 2:34
  • It's working now, thank you for your help and for explaining how to properly do escape sequences! Jan 2, 2022 at 6:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .