我们本次对于游戏模组中方块、生物的声音事件(SoundEvent)进行设置。
1.util.handlers 新建 SoundsHandler 类
在 SoundsHandler.java 中编写 声音事件信息
package com.Joy187.newmod.util.handlers;
import com.Joy187.newmod.util.Reference;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
public class SoundsHandler {
//你所要进行设置的声音事件
public static SoundEvent ENTITY_RA3_AMBIENT,ENTITY_RA3_HURT,ENTITY_RA3_DEATH;
//注册声音事件
public static void registerSounds() {
ENTITY_RA3_AMBIENT = registerSound("entity.ra3.ambient");
ENTITY_RA3_HURT = registerSound( "entity.ra3.hurt");
ENTITY_RA3_DEATH = registerSound( "entity.ra3.death");
}
private static SoundEvent registerSound(String name) {
ResourceLocation location = new ResourceLocation(Reference.Mod_ID, name);
SoundEvent event = new SoundEvent(location);
event.setRegistryName(name);
ForgeRegistries.SOUND_EVENTS.register(event);
return event;
}
}
2. 修改 RegistryHandler.java 中的 initRegistries() 函数。添加声音注册信息
public static void initRegistries() {
SoundsHandler.registerSounds();
}
3.在Main.java 的 Init() 函数中添加初始化语句。
@EventHandler
public static void Init(FMLInitializationEvent event)
{
ModRecipes.init();
ModSpawn.registerSpawnList();
//添加初始化语句
RegistryHandler.initRegistries();
}
4.在 resource 中新建sounds 包 -> 在sounds包中新建entity -> 在entity包中新建 生物名称 的包(以ra3为例)
5.我们可以制作相关音频文件,导出格式要是.ogg文件,放入这个包中
对于ogg文件的转化,可以使用 Audacity 或 格式工厂
6.在resource包中新建sounds.json文件
将我们设置的声音事件在json文件中进行注册:
{
"entity.ra3.ambient":{
"category":"entity",
"subtitle" :"entity.ra3.ambient",
"sounds":[{ "name": "joymod:entity/ra3/ambient", "stream":true }]
},
"entity.ra3.hurt":{
"category":"entity",
"subtitle" :"entity.ra3.hurt",
"sounds":[{ "name": "joymod:entity/ra3/hurt", "stream":true }]
},
"entity.ra3.death":{
"category":"entity",
"subtitle" :"entity.ra3.death",
"sounds":[{ "name": "joymod:entity/ra3/death", "stream":true }]
}
}
7.在我们的生物类中添加三个函数,对音效进行修改:
@Override
protected SoundEvent getAmbientSound() {
return SoundsHandler.ENTITY_RA3_AMBIENT;
}
@Override
protected SoundEvent getHurtSound(DamageSource source) {
return SoundsHandler.ENTITY_RA3_HURT;
}
@Override
protected SoundEvent getDeathSound() {
return SoundsHandler.ENTITY_RA3_DEATH;
}