Minecraft Java Edition – How to Create Villager-Specific Dialogue: Command Tips

minecraft-commandsminecraft-java-edition

I'm working on an adventure map, and I'm completely stuck on the dialogue part of it. Is there any way to make certain villagers have certain bits of speech? For example, Villager 1 says "Hello!" and then "How are you?" when right clicked, and Villager 2 says "Greetings!" when right clicked. I've got the right click detection working, but when I try it out, they both speak at the same time. Is there a workaround for this, preferably with one command block? I'm using the latest 1.14 snapshot.

Best Answer

Firstly, make a scoreboard objective for talking to villagers.

/scoreboard objectives add talk minecraft.custom:minecraft.talked_to_villager

Then, run this in a repeat command:

execute as @a[scores={talk=1..}] at @s positioned ^ ^ ^2 run tag @e[type=villager,limit=1,sort=nearest] add talk

That executes all players who have right clicked a villager to tag the nearest villager from two blocks in their direction with the talk tag. (Keep in mind that for this to work the villagers must not have any trades. I'm not sure if this works in 1.14, but if so summon them with {Offers:{}})

For this next part to work each villager must have a unique tag or score. Run this in a chain command:

execute as @e[type=villager,tag=talk,tag=<uniquetag>] at @s run tellraw @a[distance=..4] <insert text>

Run that command for each villager before continuing.

Then remove the talk tag from all villagers and reset all talk scores in repeat command blocks.

tag @e remove talk
scoreboard players reset @a talk