mojira.dev
MC-124008

scores in JSON for CustomName don't display

The bug

CustomName values have been changed to support JSON text. Part of JSON text is the ability to display scores, via syntax such as `{"score":{"name":"@s", "objective":"SomeObjective"}}` . But if you use this in a CustomName for an entity, it doesn't work. It also doesn't error; it just displays as an empty string.

Reproduction steps

  1. Create a dummy scoreboard objective called HP (for this example):

    /scoreboard objectives add HP dummy
  2. Summon a cow with that HP objective in its JSON name:

    /summon minecraft:cow ~ ~ ~ {CustomName:"{\"score\":{\"name\":\"@s\",\"objective\":\"HP\"}}", CustomNameVisible:1b}
  3. Set the cow's HP score via something like

    /scoreboard players set @e[type=cow,limit=1,sort=nearest] HP 10

Watch as the name remains empty instead of showing 10. Changing the selector in the JSON from @s to @e[type=cow,limit=1,sort=nearest] doesn't fix it, either.

Workarounds

Use other means to resolve the text component and then use the /data command to copy the text component.

Linked issues

Comments 7

It is intentional that score and selector components do not work there, they are special for certain places only.

They share the same json syntax. Intentionally omitting this functionality, or is an oversight, either way score and selector should work there.

Perhaps look at the date of the comment, it was posted over a year ago, and it actually directly a quote from dinnerbone.

It is still an issue up to and including snapshot 19w07a

https://bugs.mojang.com/browse/MC-144357?filter=-2

In addition to the custom name not being able to be defined by a scoreboard, lore is also affected by this bug.  (1.14.3 pre-1)

consider a written book with the following pages JSON: 

/give @p written_book{/give @p written_book{pages:[

 "[ {\"text\":\"partA\"}, {\"color\":\"red\",\"text\":\"Redtext\"} ]", "{\"score\":    {\"name\":\"@p\",\"objective\":\"usemap\"}}", "[ {\"score\":{\"name\":\"@p\",\"objective\":\"usemap\"}}, {\"text\":\"Score works properly\"} ]" 

 ], title:Bugtest,author:never2fear,display:{Lore:["{\"text\":\"MC-124008\"}"]}}

and an item spawned with the following identical JSON in its lore field: 

/give @p minecraft:experience_bottle{display:{/give @p minecraft:experience_bottle{display:{ Lore:[

 "[ {\"text\":\"partA\"}, {\"color\":\"red\",\"text\":\"Redtext\"} ]", "{\"score\":{\"name\":\"@p\",\"objective\":\"usemap\"}}", "[ {\"score\":{\"name\":\"@p\",\"objective\":\"usemap\"}}, {\"text\":\"Score is empty string\"} ]" 

]}}

 The book pages' JSON works as intended, where the score, whatever it was when the book was generated, is inserted inline. However, the same expectation does not work with the item lore JSON. An empty string is returned. But no errors. 

Sorry to be bringing this back up but I really think this should be fixed and that this is a serious bug, even though the developer team might not want to face it as one.

So, what is my reasoning?

summon minecraft:zombie x y z {NoAI:1b}
execute positioned x y z run data modify entity @e[type=minecraft:zombie,distance=..2] CustomName set value [{\"nbt\":\"Health\",\"entity\":\"@e[type=minecraft:zombie,distance=..2]\"}]
data modify entity @e[type=minecraft:zombie,distance=..2] CustomNameVisible set value 1b
summon minecraft:zombie x y z {NoAI:1b} 
setblock x y-1 z minecraft:oak_sign replace
execute positioned x y z data modify block ~ ~-1 ~ Text1 set value "[{\"nbt\":\"Health\",\"entity\":\"@e[type=minecraft:zombie,distance=..2]\"}]"
data modify entity @e[type=minecraft:zombie,distance=..2] CustomName set value ""
data modify entity @e[type=minecraft:zombie,distance=..2] CustomName set from block ~ ~1 ~ Text1
data modify entity @e[type=minecraft:zombie,distance=..2] CustomNameVisible set value 1b

So, what's up with this? The first one doesn't work, for reasons this bug described. The entity cannot process selectors in JSON Compounds and that is horrible. The fact that it doesn't give us that freedom makes it so that we have to work around it using the 2nd set of commands, which is horrible for performance, as it sends packets to every client connected to the server and each client then needs to update what the sign says (and the server also needs to do that). Long story short, the fact that selectors are processed in entities' nbt limits our possibilities and forces us to search for over-complicated solutions to simple (but essential) problems (like setting the name of the entity to its health), and hurting the server's performance and the players' experience in the process.

 Workaround using loot tables

The loot table functions set_name and set_lore resolve text components. Therefore you can use them to spawn an item with the desired text component and then copy its display name / lore to the entity where you need the resolved text component.

Example

namespace/loot_tables/my_loot_table.json

{
    "type": "entity",
    "pools": [
        {
            "rolls": 1,
            "entries": [
                {
                    "type": "item",
                    "name": "stone",
                    "functions": [
                        {
                            "function": "set_name",
                            "name": {
                                "score": {
                                    "objective": "my_obj",
                                    "name": "@s"
                                }
                            },
                            "entity": "this"
                        },
                        {
                            "function": "set_nbt",
                            "tag": "{custom_tag:1b}"
                        }
                    ]
                }
            ]
        }
    ]
}

namespace/functions/set_my_obj_score_name.mcfunction

# Spawn outside of world to not interfere with other entities or blocks
loot spawn ~ -255 ~ loot namespace:my_loot_table
data modify entity @s CustomName set from entity @e[type=item,limit=1,y=-255,dx=1,dy=1,dz=1,nbt={Item:{tag:{custom_tag:1b}}}] Item.tag.display.Name
kill @e[type=item,limit=1,y=-255,dx=1,dy=1,dz=1,nbt={Item:{tag:{custom_tag:1b}}}]
data modify entity @s CustomNameVisible set value 1b

Daniel Burnett

(Unassigned)

Community Consensus

Minecraft 18w01a

Retrieved