2

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?

1
  • 1
    Please provide the commands you are using so far so we can debug them. – ExpertCoder14 Jun 1 at 2:23
3

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

5
  • Everything you have stated so far is correct, however, you are missing the final puzzle piece. – ExpertCoder14 Jun 1 at 3:36
  • only so much i can from my phone xp I'll do some testing in a couple hours when i get home – Jam Jun 1 at 3:41
  • Should be good now! – Jam Jun 1 at 6:54
  • Thanks so much! – Leon Lavoie Jun 1 at 14:29
  • It worked all right, just got to add "interpret":true – Leon Lavoie Jun 1 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.