Appearance
Player events
A hidden advancement with a Trigger runs a function when a player does something: uses an item, hurts a mob, places a block.
ts
new AdvancementDef().criterion("used", Trigger.usingItem(wand)).reward("wand:wand/on_use");TIP
Triggers cost nothing until they fire, so use them before a tick poll for anything a player does.
React to an event
The reward function revokes the advancement so it fires again next time.
ts
import { Datapack, v26_2, Selector, Item, Enchantment, AdvancementDef, Trigger } from "helix";
const dp = new Datapack("wand", v26_2);
const wand = Item.STICK.named("Wand").enchant(Enchantment.KNOCKBACK, 1);
const wandAdv = dp.advancement(
"wand/on_use",
new AdvancementDef().criterion("used", Trigger.usingItem(wand)).reward("wand:wand/on_use"),
);
dp.createFunction("wand/on_use").build((ctx) => {
ctx.tellraw(Selector.self(), "✦ zap ✦");
ctx.advancement().revokeOnly(Selector.self(), wandAdv);
});Compiled output - the real files helix emits for the code above:
txt
tellraw @s {"text":"✦ zap ✦"}
advancement revoke @s only wand:wand/on_usejson
{
"criteria": {
"used": {
"trigger": "minecraft:using_item",
"conditions": {
"item": {
"items": "minecraft:stick",
"components": {
"minecraft:custom_name": {
"text": "Wand"
},
"minecraft:enchantments": {
"minecraft:knockback": 1
}
}
}
}
}
},
"rewards": {
"function": "wand:wand/on_use"
}
}txt
{
"pack": {
"description": "wand\nCreated using Helix",
"min_format": [
107,
1
],
"max_format": [
107,
1
]
}
}Use any vanilla trigger
Trigger.of(id, conditions) takes any vanilla trigger, typed from the game schema.
ts
Trigger.of("bred_animals", { child: { type: EntityType.MULE } });
Trigger.of("fishing_rod_hooked", { rod: Item.FISHING_ROD, entity: { type: EntityType.COD } });Trigger.raw(id, conditions) takes a non-vanilla trigger as JSON.
WARNING
Triggers are per player. Absence ("nobody near"), entity-only events and timers still need a tick check. location is checked about once a second.