1.21.6+
To get the complete functionality you want, we need to resolve two tasks:
- run a function in a particular position when a brush... brushes it.
- taking text and injecting it into a
translate JSON text component in a command
Both of these are solvable in multiple ways in Java edition. Below, I am writing one approach to each task.
I will be using the namespace arqade, so you can expect a file structure as follows:
encoder_datapack
- pack.png
- pack.mcmeta
- data
+ arqade
> advancement
...
> dialog
...
> function
...
1. Using a custom advancement to detect brushing
Advancements run in the background and detect when players do something. They are Minecraft Java's version of subscribers. Incidentally, the name of the trigger is very similar to the event name you used: item_used_on_block. In the advancement folder, the following advancement will detect when brushing is used on a sign:
brush_sign.json:
{
"criteria": {
"brush_block": {
"trigger": "item_used_on_block",
"conditions": {
"location": [
{
"condition": "match_tool",
"predicate": {
"items": "brush"
}
},
{
"condition": "location_check",
"predicate": {
"block": {
"blocks": "#all_signs"
}
}
},
{
"condition": "entity_properties",
"entity": "this",
"predicate": {
"flags": {
"is_sneaking": true
}
}
}
]
}
}
},
"rewards": {
"function": "arqade:on_brush_sign"
}
}
The first condition in location checks if the player is using a brush. The second condition checks if the player is using the item on any sign. The third condition checks if the player is sneaking.
Finally, if this is satisfied, the advancement is silently granted (default behavior) and runs the function arqade:on_brush_sign which is a custom function we will define.
In the function folder, we will add the following function definition:
on_brush_sign.mcfunction:
# revoke advancement so it can be triggered again. It is a good idea to show this first
advancement revoke @s only arqade:brush_sign
dialog show @s arqade:get_translate
This is a very basic function that allows the player to trigger the advancement again, and opens a dialog box, where you will input the translation key. I toyed around with a bunch of places from which to take the input text, like directly from the sign or from renaming the brush or another item. Out of all of them, this seems the most reliable and extensible.
The dialog definition is defined in a different file, placed in the dialog folder.
get_translate.json:
{
"type": "confirmation",
"title": "Set sign text",
"inputs": [{
"type": "text",
"key": "translate_key",
"label": "Translation key: "
}],
"yes": {
"label": {"translate": "gui.ok"},
"action": {
"type": "dynamic/run_command",
"template": "function arqade:on_dialog_success {translate_key:\"$(translate_key)\"}"
}
},
"no": {
"label": {"translate": "gui.cancel"}
}
}
This defines the text field with the labels you showed in your behavior pack. I had to add a cancel button to ensure ESC will also cancel the operation and not lock you in.
2. Running a function to modify sign data with the translation key as a macro argument
If you press OK in this dialog, a command with the translation key will be sent, which will carry over this data to the sign. This function runs as the player who ran the command, and at his position. Therefore, the function on_dialog_success needs to find the position of the sign.
on_dialog_success.mcfunction:
# run the function "arqade:encode_translate" as the brushing player at the location of the sign.
# Use a brute force search on the player's right-clicking range in 0.5 block spacing.
$execute anchored eyes positioned ^ ^ ^ if block ~ ~ ~ #minecraft:all_signs run function arqade:encode_translate {translate_key:$(translate_key)}
$execute anchored eyes positioned ^ ^ ^.5 if block ~ ~ ~ #minecraft:all_signs run function arqade:encode_translate {translate_key:$(translate_key)}
$execute anchored eyes positioned ^ ^ ^1 if block ~ ~ ~ #minecraft:all_signs run function arqade:encode_translate {translate_key:$(translate_key)}
$execute anchored eyes positioned ^ ^ ^1.5 if block ~ ~ ~ #minecraft:all_signs run function arqade:encode_translate {translate_key:$(translate_key)}
$execute anchored eyes positioned ^ ^ ^2 if block ~ ~ ~ #minecraft:all_signs run function arqade:encode_translate {translate_key:$(translate_key)}
$execute anchored eyes positioned ^ ^ ^2.5 if block ~ ~ ~ #minecraft:all_signs run function arqade:encode_translate {translate_key:$(translate_key)}
$execute anchored eyes positioned ^ ^ ^3 if block ~ ~ ~ #minecraft:all_signs run function arqade:encode_translate {translate_key:$(translate_key)}
$execute anchored eyes positioned ^ ^ ^3.5 if block ~ ~ ~ #minecraft:all_signs run function arqade:encode_translate {translate_key:$(translate_key)}
$execute anchored eyes positioned ^ ^ ^4 if block ~ ~ ~ #minecraft:all_signs run function arqade:encode_translate {translate_key:$(translate_key)}
$execute anchored eyes positioned ^ ^ ^4.5 if block ~ ~ ~ #minecraft:all_signs run function arqade:encode_translate {translate_key:$(translate_key)}
$execute anchored eyes positioned ^ ^ ^5 if block ~ ~ ~ #minecraft:all_signs run function arqade:encode_translate {translate_key:$(translate_key)}
This can be done more elegantly with some recursion, but the limited interaction range of the player allows us to brute force the same command a few times.
Finally, after the translation key was carried for a few commands, we can finally use the macro argument from the dialog in a data command:
encode_translate.mcfunction:
$data modify block ~ ~ ~ front_text.messages[0] set value {translate:"$(translate_key)"}
This datapack implementation is very extensible. You can add additional arguments in the dialog to include translation keys for the other rows, add substitution arguments to the with JSON component, more stuff for the back of the sign, etc.
Important note on dialogs in datapacks: using /reload is not enough to update a dialog, you need to exit and rejoin the world.
arqade.key, brush it, and then it would get the appropriate translate JSON as the text of the sign? If not, are you looking for a way in-game to register a value for a given translate key for a language?arqade.keyon the sign, brush it, and it would change the text to the translatable/translate tag. (I already would have the appropriate language JSON files set up in my resource pack in an earlier step of my process.)