How do I concatenate two strings in NBT?
This situation is fictional. Please do not suggest an alternative method as described in the Story, please only provide a solution to the Core Question below.
Story
I am creating a chatbot using commands in Minecraft. A grid of buttons representing letters and numbers is on a wall. The player can press these buttons to "type" letters into their query to the chatbot. Each letter is typed by adding a string with that letter to the end of an NBT array. For example, when the 'L' button is pressed, this command is run:
data modify storage chatbot:main CurrentEntry append value "L"
For example, a completed query array for the query "Life is a bowl of cherries" might look like this:
{CurrentEntry:["L","i","f","e"," ","i","s"," ","a"," ","b","o","w","l"," ","o","f"," ","c","h","e","r","r","i","e","s"]}
Once the user is finished entering a query into the machine, they press the Submit button, which starts the analyzing process and allows the chatbot to give an answer.
Problem
I'm in the phase where I'm developing easter eggs, where a specific phrase can be typed into the chatbot to produce a unique response. To do this, I am going to need to concatenate each value in the query array in order to produce a single string with the full query, and then compare that with a template string to see if it matches an easter egg phrase.
Concatenating strings in Minecraft will probably be quite of a hassle. The approach I've chosen to take is to concatenate the first two strings, then concatenate the result with the next string, and so on until I've reached a final string of the entire query.
Core question
How do you concatenate two or more strings from NBT data?
Given two strings in NBT:
{String1:"Life is a ",String2:"bowl of cherries"}
can you concatenate them to produce:
{ResultString:"Life is a bowl of cherries"}
Past trials
- I had the idea of using
/data modify
to append to strings, but it is only for adding to lists, not adding to strings.
See also
Splitting NBT strings
Solution 1:
TL;DR: Vote for MC-183054 if you want this.
I've had a discussion with the developer of Minecraft String Utilities, the data pack that lets you turn NBT strings into a list of characters so that each individual character can be manipulated. This discussion revealed that string concatenation is not possible, and neither is turning the character arrays produced by the data pack back into single strings.
However, it was recently brought up that there is a known method that is being blocked by a bug that must be fixed. If the bug is fixed, this will mean that strong concatenation will finally be possible and open up a whole new realm of what's possible with Minecraft commands.
The method in question involves structure blocks and endermen, specifically, their ability to pick up and place blocks all by themselves.
Notice the NBT format for structure blocks. The author
tag contains the unflattened version of the entity that placed it. Which means, we can rename an enderman to an nbt
JSON component, give it a structure block, wait for it to place it, and check its author
tag for the unflattened string.
Unfortunately, as stated above, there is a bug preventing this method from working. The bug in question is MC-183054.
Have you noticed that an enderman can't spawn a Wither by placing the skulls on? Turns out that applies to all other block updates, including the one that sets the structure block's author
tag.
So, we're almost able to unflatten strings. If you want to be able to do this, please vote for the bug to be fixed!