Minecraft Random Item Name

minecraft-commandsminecraft-java-edition

Story

I am working on a data pack based over forging items and ran upon this problem:
The NBT tag of the item's name is a string, and I can't (or don't know how to) generate multiple strings or turn a JSON object into a string. Using my current method with /data modify storage and a loot table, I can generate a list of JSON-valid text objects. Then, with those objects i just copy-paste it into the item's name (at display.Name).

Problem

What I have tried so far doesn't work. Upon executing these commands, I get my item, with the NBT as I told it to be, at the good place, but the text just does not show up. I then used an anvil to rename an item, then used /data get to see the NBT and it was a string! The same thing as what i wrote, but there were little 's around it.

Expected output

PREFIX itemName SUFFIX
(the prefix, itemName and suffix are generated using loot tables)

Question

So here is my question: Random modular item name generation, can this be achieved on java 1.16.5? And if yes, how?

Best Answer

The problem is that Minecraft provides next to no string manipulation. What it does instead of concatenation (adding two strings together character by character) is it uses something called JSON strings.

Difference between a string literal and a JSON string

String literals are things such as "minecraft:stone". They are frequently used for IDs and are not changeable.
JSON strings are things like

[\"\",{\"nbt\":\"nbtPath\",\"storage\":\"storagename\"}]

and are frequently surrounded by '
This JSON string is interpreted by Minecraft and indicates that instead of a hardcoded string, it should look at the NBT storage storagename and from that get the value assigned to the NBT path nbtPath.

Visual Strings

There is a third type of string, which I can only call a Visual String. It's format is as follows:

'{"extra":[{"text":"b"},{"text":"c"}],"text":"a"}'

This would, for anything compatible such as bossbars, entity names, item names, and many more, display as abc

Addressing your issue

In order to accomplish your goal of what essentially amounts to concatenating 3 phrases, all you need to do is tell minecraft to look at these 3 NBT values.

How to do this

Assuming your method for generating prefix/name/suffixes creates 3 different NBT storage values, then all you would have to do is generate the 3 values and store them in storagename.prefixPath,storagename.namePath, and storagename.suffixPath respectively
Now, I'm not sure if it'll figure out that storagename.nbtPath should be an array, so you might have to have an extra data modify command to declare storagename.nbtPath to be an empty array.

then what you should be able to do is

setblock <x> <y> <z> oak_sign{Text1:"[\"\",{\"nbt\":\"prefixPath\",\"storage\":\"storagename\"},{\"text\":\" \"},{\"nbt\":\"namePath\",\"storage\":\"storagename\"},{\"text\":\" \"},{\"nbt\":\"suffixPath\",\"storage\":\"storagename\"}]"}

This command will setblock a sign, which acts as a middleman to convert JSON strings to Visual strings, as Visual strings have hardcoded values.

Finally, all you have to do is

data modify <block|entity> pathToItemName set from block <x> <y> <z> Text1

Sorry about the two-stage answer. You should be good to go now