Story
I am making a chatbot in Minecraft. Players "type" in their messages to the chatbot using a grid of buttons representing numbers, letters, and symbols. Each button press appends a new string to an NBT list. For example, the L button runs the following command:
data modify storage chatbot:main CurrentEntry append value "L"
Once done, the player presses the Submit button, which concatenates all the values in the array into one string, and submits it to the chatbot.
Problem
To reduce processing times, I have decided to institute a rule that messages may only be two hundred fifty-six characters long or less. I need a way to figure out how many items are in the string, and if there are more than 256, set some sort of flag to be detected by the system as too long.
Core question
How do you get the length of an NBT string?
Given an NBT string…
Input:{String:"Life is a bowl of cherries"}
can you retrieve the number of characters in the string and output it?
Output:{StringLength:26b}
Notes
- If your solution requires concatenating or splitting the string, please do also answer the matching question below.
Best Answer
Here's something interesting about
/data
. When you get the value of the tag using/data get ...
it returns the value in chat, but it also returns the length of that string. So you can use it in combination with/execute store
to store the length. Example:Storage object before the command:
Storage object after the command:
This also works with scoreboard objectives.