Minecraft 1.19.2 Fabric模组开发 11.自定义音效

博客介绍了在Fabric中加入自定义音效的方法。首先在init包新建SoundInit类注册模组音效,并在项目主类初始化该音效类。之后可使用注册的声音名称,如改变物品和生物音效。还需将音效文件放入指定位置,在资源包新建文件并写入音效信息,且名称要对应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们本次在fabric中加入自己制作的音效。

1.init包中新建一个类SoundInit,注册所有的模组音效:

SoundInit.java

package net.joy187.joyggd.init;


import net.joy187.joyggd.ModMain;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class SoundInit {
    //这些是音效名称
    public static SoundEvent PELEBALL;
    public static SoundEvent MUMMYKILL;


    
    private static SoundEvent registerSoundEvent(String name) {
        Identifier id = new Identifier(ModMain.MOD_ID, name);
        return Registry.register(Registry.SOUND_EVENT, id, new SoundEvent(id));
    }
    
    public static void registerSounds() {
    	//每个名称均与后面的字符串一一对应
    	PELEBALL = registerSoundEvent("entity.peleball");
        MUMMYKILL = registerSoundEvent("entity.mummykill");

    }
}
之后在项目主类中初始化该音效类:

ModMain.java

public class ModMain implements ModInitializer {
	@Override
	public void onInitialize() {
		ModConfigs.registerConfigs();
		
		ItemInit.registerModItems();
		BlockInit.registerModBlocks();
		EffectInit.registerEffects();
		
		//添加音效类注册事件
		SoundInit.registerSounds();

		
		ModWorldGen.generateWorldGen();
		GeckoLib.initialize();


	}
    
}

2.之后我们可以将注册的声音名称进行使用:

1.改变物品使用音效
            playerIn.world.playSound(playerIn, playerIn.getBlockPos(), SoundInit.PELEBALL, SoundCategory.PLAYERS, 1.5F, 0.8F / (playerIn.world.getRandom().nextFloat() * 0.4F + 0.8F));

2.改变生物音效:
    protected SoundEvent getAmbientSound() {
        int co=this.random.nextInt(3);
        if(co==1) return SoundInit.PELEBALL;
        return SoundInit.MUMMYKILL; 
    }
    
    protected SoundEvent getHurtSound(DamageSource source) {
        return SoundInit.PELEBALL;
    }

    protected SoundEvent getDeathSound() {
        return SoundInit.MUMMYKILL;
    }
3.将我们的音效文件(.ogg)放入resources\assets\你的modid\sounds\entity中:

cr1.jpg

之后在资源包中新建一个sounds.json文件:

cr2.jpg

将我们之前的音效文件信息写在sounds.json中,注意名称与第一步registerSounds()中的要对应:

sounds.json

{
  "entity.peleball":{
    "category":"entity",
    "subtitle" :"entity.peleball.sub",
    "sounds":[{ "name": "joyggd:entity/peleball", "stream":true }]
  },
  "entity.mummykill":{
    "category":"entity",
    "subtitle" :"entity.mummykill.sub",
    "sounds":[{ "name": "joyggd:entity/mummykill", "stream":true }]
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值