Do Command Blocks have character limits?
Solution 1:
According to the Minecraft Wiki, there is a limit, but it's a very large one.
See the Command Block page on the Minecraft Wiki:
The text limit for commands in a command block is 32,767 characters, but the text pane can only show a small portion of this amount at a time.
Solution 2:
With client (32,500)
As of 1.9, the character limit when using the command block's interface has been reduced to 32,500 (down from 32,767). This change was to help circumvent crashes when exceeding the packet limit of 32,767 bytes, as more information than just the command is sent to the server when confirming changes (including the various options in the interface).
With server (2^31-1 or 65,535)
The command is stored by the server as an NBT string. Technically the character limit is as much as can fit in Java's String type, being Integer.MAX_VALUE
(2^31-1).
However, when saving the string to, for example, a chunk file, a new character limit comes from Java's DataOutput.writeUTF() method:
If this number is larger than 65535, then a
UTFDataFormatException
is thrown.
If UTFDataFormatException
is thrown, the chunk will fail to save. Therefore for map distribution, the character limit is 65,535. Since the client can only send 32,500 characters of a command, it will not be possible to modify the command block via the interface without trimming the command.
Example
One method to obtain a command block on a vanilla server that reaches the 65,535 limit is to use a loot table. Loot tables can create a command block item containing a preset command, which is all handled server-side (thus no client limit of 32,500).
With the following loot table located at WORLD/data/loot_tables/custom/limit.json
: http://pastebin.com/8tmeeU4M
And the following command to create a chest containing the relevant command block:
/setblock ~ ~1 ~ minecraft:chest 0 replace {LootTable:"custom:limit"}
Placing and activating the command block will run a 65,535-character-long /say
command, and you will see the final character of "1" in the chat (indicating the command was not trimmed). You can then exit the world and re-enter, and the command will still function.
Attempting to add more characters will still allow it to work while the chunk is loaded, but once you exit the world or otherwise unload the chunk, the chunk will fail to save with the following error:
[STDERR]: java.io.UTFDataFormatException: encoded string too long