动态方块音效

基本信息

属性 值
接口 com.gtnewhorizon.gtnhlib.api.IBlockWithCustomSound
源码 src/main/java/com/gtnewhorizon/gtnhlib/api/IBlockWithCustomSound.java
控制的 mixin 组 Mixins.DYNAMIC_BLOCK_SOUNDS
开关配置 GTNHLibConfig.blockSoundMixins(默认 true)
作用面 双方(common mixin)

功能

让方块的播放音效依赖世界状态(meta、TileEntity 等),而不是 1.7.10 那样由 meta 静态决定。源码注释(IBlockWithCustomSound.java:3-5):

Implement this interface on a Block to make its sound dependent on some world state (meta, tile entities, etc). This may be run on the server, so SideOnly(CLIENT) code cannot be used in this method.

接口只有一个方法:

方法 返回 说明
getSound(World world, int x, int y, int z) Block.SoundType 按当前世界状态返回该位置应播放的音效类型

数值

数值名 值
抽象方法数 1
默认方法数 0
覆盖的 mixin 数 7(见下)

由 Mixins.DYNAMIC_BLOCK_SOUNDS 注入的 7 个 mixin(mixins/Mixins.java):

侧 mixin 类
common block_sounds.MixinEntity
common block_sounds.MixinEntityLivingBase
common block_sounds.MixinEntityHorse
common block_sounds.MixinItemBlock
common block_sounds.MixinItemSlab
client block_sounds.MixinPlayerControllerMP
client block_sounds.MixinRenderGlobal

这 7 个类实际位于 mixins/early/block_sounds/ 目录(early 阶段包 com.gtnewhorizon.gtnhlib.mixins.early 下)。

交互

方块实现接口后,音效查询会转到 getSound。因为方法可能在服务端执行,在实现里写客户端专属代码会导致服务端崩溃 —— 这是接口注释明确警告的约束。

blockSoundMixins 配置项带 @Config.RequiresMcRestart(GTNHLibConfig.java:39),因为 mixin 在早期阶段注入,运行时改配置不会重新注入;需重启 Minecraft 生效。

相关条目

  • Mixin 枚举 - DYNAMIC_BLOCK_SOUNDS 条目与其他 26 个注入组的条件/阶段表
  • Mixin 引导流程 - early/late/preinit 三个阶段的加载顺序