Minecraft – /setblock a command block with nearest player’s name inside

minecraft-commandsminecraft-java-edition

I am using a mod with the custom map I am making. The mod adds some commands, but they are far from perfect. For example, the command syntax goes like this:

/modcommand foo <PlayerName> DoThis

In a command block I put

/modcommand foo feldspar15523 DoThis

And everything works as expected. However, if I do this:

/modcommand foo @p DoThis

The mod thinks that the player's name is literally @p. My idea was to make it so that when I trigger the block, it first makes another Command Block that has the name of the nearest player in it in the right place. Here is what I need in pseudocode:

/setblock ~ ~+5 ~ minecraft:chain_command_block 1 replace {Command:"/modcommand foo "+ @p +" DoThis"}

Best Answer

This is impossible. Minecraft doesn't have variables like strings. The best you can do is to output the command in chat, like this:

/tellraw @s [{"text":"/setblock ~ ~5 ~ chain_command_block 1 replace {Command:\"modcommand foo "},{"selector":"@p"},{"text":" DoThis\"}"}]

If you have the log output open, you can then copy the command into chat and run it. A modded client could also automatically execute it, but not a regular Vanilla client.