Skip to content

Manual(Scripted) Animations

Goodbird-git edited this page Jul 25, 2024 · 5 revisions

New classes and methods (before 1.20.1)

In order to control geckolib animations from scripts, you need to use a special class called AnimationBuilder.
Here is the interface of this class

public class AnimationBuilder {

    public AnimationBuilder addAnimation(String animationName);

    public AnimationBuilder addRepeatingAnimation(String animationName, int timesToRepeat);

    public AnimationBuilder playOnce(String animationName);

    public AnimationBuilder loop(String animationName);

    public AnimationBuilder playAndHold(String animationName);

    public AnimationBuilder clearAnimations();
}


This class uses Builder design pattern, so you can write chained calls like this

builder.playOnce("anim1").playOnce("anim2").playOnce("anim3").loop("anim4") ...


For more convenient use, some useful methods were added to already existing CNPC API classes
NpcAPI:

public AnimationBuilder createAnimBuilder();

ICustomNpc, IBlockScripted:

public void syncAnimationsFor(IPlayer player, AnimationBuilder builder);

public void syncAnimationsForAll(AnimationBuilder builder);

Simple example

Making an NPC wave his hand when interacted

function interact(event){ //The script should be performed during interact
    var builder = event.API.createAnimBuilder() //We create an animation builder
    builder.playOnce("animation.custom_model.wave") //We add a wave animation into it
    event.npc.syncAnimationsFor(event.player, builder) //We synchronise the animation for the player, who interacted with the npc
}

New classes and methods (after 1.20.1)

In order to control geckolib animations from scripts, you need to use a special class called AnimationBuilder.
Here is the interface of this class

public class RawAnimation {

    public RawAnimation thenPlay(String animationName);

    public RawAnimation thenLoop(String animationName);

    public RawAnimation thenPlayAndHold(String animationName);

    public RawAnimation thenPlayXTimes(String animationName, int playCount);

    public RawAnimation thenWait(int ticks);

}


This class uses Builder design pattern, so you can write chained calls like this

builder.thenPlay("anim1").thenPlay("anim2").thenPlay("anim3").thenLoop("anim4") ...


For more convenient use, some useful methods were added to already existing CNPC API classes
NpcAPI:

public RawAnimation createAnimBuilder();

ICustomNpc, IBlockScripted:

public void syncAnimationsFor(IPlayer player, RawAnimation builder);

public void syncAnimationsForAll(RawAnimation builder);

Simple example

Making an NPC wave his hand when interacted

function interact(event){ //The script should be performed during interact
    var builder = event.API.createAnimBuilder() //We create an animation builder
    builder.thenPlay("wave") //We add a wave animation into it
    event.npc.syncAnimationsFor(event.player, builder) //We synchronise the animation for the player, who interacted with the npc
}

Clone this wiki locally