Skip to content

Adding a trigger

What to change to add a new animation event to hAnimationsWorks — the enum, the config folder, the listener, and the command wiring.

A trigger is an event that can play an animation. Adding one touches four places. The example adds a LEVEL_UP trigger.


1. Add the enum constant

api/src/main/java/systems/homestead/hanimationsworks/api/AnimationTrigger.java

java
public enum AnimationTrigger {
    JOIN,
    QUIT,
    // ...
    LEVEL_UP,
}

This is the order that matters. Ids are per-trigger, and a bare id with no trigger resolves to the first owner in AnimationTrigger.values() order. Adding a constant changes what an unqualified /hanim play <id> previews when that id exists under several triggers — append rather than insert if you do not want to shift existing behaviour.

The enum name is what appears in commands and completion, lowercased with underscores turned into hyphens: LOW_HEALTH → low-health. It does not have to match the config folder name, but keeping them close saves confusion — see step 2.

2. Add the config folder

core/src/main/resources/defaults/events/level_up/config.yml

yaml
# Level-up animations.
#
# One entry per animation. Every key under `animations` becomes an id that
# /hanim and other plugins can reference by name.
animations:

The folder name is the file-system key. Check how readAll() derives its map key — the reader, not the enum, decides how a folder becomes a trigger, and they should agree.

Every other event folder is discovered automatically. There is no registry to update.

3. Handle the event

core/src/main/java/systems/homestead/hanimationsworks/core/listener/AnimationListeners.java

Add one @EventHandler that funnels through request:

java
@EventHandler
public void onLevelUp(@NotNull PlayerLevelChangeEvent event) {
    if (event.getOldLevel() >= event.getNewLevel()) {
        return;
    }
    request(event.getPlayer(), AnimationTrigger.LEVEL_UP);
}

request(player, trigger) is the single place that asks "does this player want this?" — it consults the preference and plays only if one is stored. Do not call the engine directly from a listener; every handler goes through request so the preference lookup cannot drift between triggers.

Guards worth copying

Several existing handlers carry a note explaining a guard they added because the raw event is too eager. Follow them:

  • onBedEnter — the event fires for a player who merely sat on a bed. Check BedEnterResult.OK, and ignore a cancelled event rather than playing anyway.
  • onCommand — resolveFor answers "did this player choose anything for COMMAND", which is true for the whole session. Compare the chosen value against the command actually typed, or one preference cancels every command on the server.
  • onAdvancementDone — Advancement.getDisplay() is @Nullable and returns null for recipe-unlock advancements. The title is only ever logged, so fall back to the key. requireNonNull here threw on the main thread every time somebody unlocked a recipe.

Deferring, when it matters

If the server applies state after the handler returns — sleep does — schedule past the event, or the animation is built against a state that is about to change:

java
Bukkit.getScheduler().runTask(plugin, () -> { /* ... */ });

onBedEnter does exactly this, one tick later.

4. Wire the command

Nothing to do. BrigadierAnimationCommand completes triggers from AnimationTrigger.values(), so a new constant appears in /hanim picker <trigger>, set, clear, get and list automatically.

Check AnimationCommand — the legacy Bukkit fallback — for the same. Its subcommand list and its onTabComplete trigger list are hand-written arrays, and a trigger that is missing from one of them shows up only on servers where Brigadier cannot register.


Verify

  1. ./gradlew build — all three jars
  2. Add an animation under events/level_up/config.yml
  3. /hanim reload
  4. /hanim list level-up — the count should include it
  5. /hanim test <id> — preview it without triggering the event
  6. /hanim picker — the new event appears in the ring
  7. Trigger it for real

Verifying the artifact, not the source

After building, confirm the change is in the jar:

bash
javap -p -classpath <jar> <fqcn> | grep <newMethod>

A green build against a stale jar is the single most common false positive in this project.