Skip to content

Playback API zh

kltyton edited this page Dec 1, 2026 · 1 revision

播放同步与淡入镜像 API

语言:English | 中文

PlayerAnimationLibraryMoreRotation 提供通用的服务端→客户端播放同步层,下游模组无需重复编写网络包、同维度广播器、淡入淡出修饰器或左右手镜像逻辑。

服务端播放 API(PalMoreAnimations)

import com.kltyton.playeranimationlibrarymorerotation.PalMoreAnimations;
import com.kltyton.playeranimationlibrarymorerotation.PalMoreFirstPersonOptions;
import com.kltyton.playeranimationlibrarymorerotation.PalMorePlayOptions;
import net.minecraft.resources.Identifier; // 1.20.1 / 1.21.1 为 ResourceLocation
import net.minecraft.server.level.ServerPlayer;

Identifier animation = Identifier.fromNamespaceAndPath("example", "slash");

// 基础播放与停止(自动广播至同维度所有支持客户端)
PalMoreAnimations.play(serverPlayer, animation);
PalMoreAnimations.stop(serverPlayer);

// 快捷重载:淡入 2 ticks、淡出 4 ticks、左右手镜像开启
PalMoreAnimations.play(serverPlayer, animation, 2.0F, 4.0F, true);

// 平滑淡出停止
PalMoreAnimations.stop(serverPlayer, 4.0F);

// 完整选项构建器
PalMorePlayOptions options = PalMorePlayOptions.builder()
        .fadeInTicks(2.0F)
        .fadeOutTicks(4.0F)
        .mirrored(true)
        .firstPersonOptions(PalMoreFirstPersonOptions.SHOW_ARMS_AND_ITEMS)
        .quaternionBlend(true) // true 为四元数 SLERP(默认),false 为欧拉线性混合
        .build();
PalMoreAnimations.play(serverPlayer, animation, options);

客户端播放 API(PalMoreClientAnimations)

仅在物理客户端代码中调用:

import com.kltyton.playeranimationlibrarymorerotation.client.PalMoreClientAnimations;

PalMoreClientAnimations.playLocal(avatar, animation);
PalMoreClientAnimations.playLocal(avatar, animation, options);
PalMoreClientAnimations.stopLocal(avatar, 4.0F);
boolean mirrored = PalMoreClientAnimations.isMirrored(avatar);

1.20.1 与 1.21.1 上实体参数类型为 AbstractClientPlayer,26.x 上为 Avatar。

命名关键帧控制器(PalMoreAnimationController)

为 PAL 原生解析的 timeline、sound_effects、particle_effects 绑定控制器级回调:

import com.kltyton.playeranimationlibrarymorerotation.PalMoreAnimationController;
import com.kltyton.playeranimationlibrarymorerotation.client.PalMoreClientAnimations;

public static final Identifier ATTACK_CONTROLLER =
        Identifier.fromNamespaceAndPath("example", "attack_controller");

public static final PalMoreAnimationController ATTACK_PLAYBACK =
        PalMoreAnimationController.create(ATTACK_CONTROLLER)
                .setCustomInstructionKeyframeHandler(s -> {
                    if ("runAttack;".equals(s.instructions().replaceAll("\\s+", ""))) {
                        // 客户端视觉表现逻辑
                    }
                })
                .setParticleKeyframeHandler(s -> {
                    String effect = s.effect();
                    String locator = s.locator();
                    String script = s.script();
                })
                .setSoundKeyframeHandler(s -> {
                    String sound = s.sound();
                });

// 在客户端初始化阶段注册一次:
PalMoreClientAnimations.registerController(ATTACK_PLAYBACK);

// 服务端使用相同 controller ID 触发:
PalMoreAnimations.play(serverPlayer, animation, ATTACK_CONTROLLER, options);

网络包仅发送 controller ID,Java lambda 回调在各客户端本地解析执行。

网络 Payload 字段

Clientbound payload ID:playeranimationlibrarymorerotation:player_animation

字段 线协议类型 说明
avatarEntityId VarInt 观察者当前维度内的目标玩家实体 ID
animationId Identifier / ResourceLocation 位于 assets/<ns>/player_animations/ 下的动画键名
stop Boolean true 表示停止,false 表示播放
controllerId Identifier / ResourceLocation 命名控制器 ID(缺省为默认控制器)
fadeInTicks Float 淡入或打断混合时长(ticks)
fadeOutTicks Float 淡出时长(ticks)
mirrored Boolean 左右手及四肢镜像开关
firstPersonMask Byte 同步的 PalMoreFirstPersonOptions 位掩码
quaternionBlend Boolean true 启用四元数 SLERP,false 使用欧拉角插值

下游模组无需重复注册此 payload 或客户端接收器。

Clone this wiki locally