Keyframe Triggers

Sound Keyframes

To manage sound keyframe callbacks, attach an SoundKeyframeHandler instance to your AnimationController using the setSoundKeyframeHandler method. This handler will be invoked at the specified time defined by the keyframe in the animation JSON.

Example

    @Override
    public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {
        controllers.add(
            new AnimationController<>(this,"main_controller",5, state -> PlayState.STOP)
            .setSoundKeyframeHandler(event -> {
                if (event.getKeyframeData().getSound().matches("customSoundkey") && state.getAnimatable().level().isClientSide)
                    state.getAnimatable().level().playLocalSound(
                            this.getX(),
                            this.getY(),
                            this.getZ(),
                            CustomSounds.YOUR_CUSTOM_SOUND,
                            SoundSource.HOSTILE,
                            1.0F, // Volume
                            1.0F, // Pitch
                            true //Distance Delay
                        );
            })
        );
    }

Particle Keyframes

To handle particle keyframe callbacks, attach an ParticleKeyframeHandler instance to your AnimationController using the setParticleKeyframeHandler method. This handler will be triggered at the specified time defined by the keyframe in the animation JSON.

Example

    @Override
    public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {
        controllers.add(
            new AnimationController<>(this, "main_controller", 0, state -> PlayState.STOP)
                .setParticleKeyframeHandler(state -> {
                    if (event.getKeyframeData().getSound().matches("customSoundkey"))
                        state.getAnimatable().level().addParticle(ParticleTypes.ASH, 1, 1, 1, 1, 1, 1);
                })
        );
    }

Custom Instruction Keyframes

Custom instructions help perform actions on your entity unrelated to sound or particles at specific keyframe timings.

To handle custom keyframe callbacks, add an ICustomInstructionListener instance to your AnimationController using the setCustomInstructionKeyframeHandler method. This handler will be triggered at the specified time defined by the keyframe in the animation JSON.

Example

    @Override
    public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {
        controllers.add(
            new AnimationController<>(this, "main_controller", 0, state -> PlayState.STOP)
                .setCustomInstructionKeyframeHandler(state -> {
                    // Use helper method to avoid client-code in common class
                    Player player = ClientUtils.getClientPlayer();

                    if (event.getKeyframeData().getSound().matches("customSoundkey") && player != null)
                        player.displayClientMessage(Component.literal("This is a custom message"), true);
                })
        );
    }

Last updated