今天我们在MC中制作一张唱片
首先我们要先知道MC中的声音事件:往期教程
1.将我们的音频文件(.ogg格式)导入到resources包的sounds文件夹中
2.在SoundsHandler.java中对声音文件进行注册:
public static SoundEvent RE8MUSIC;
在SoundsHandler.java的registerSounds()函数中注册音频文件
RE8MUSIC = registerSound("entity.gm79.yfds");
3.在Items包中新建ItemMusicDiscRE8类
在ItemMusicDiscRE8.java中编写代码:
package com.Joy187.newmod.items;
import java.util.Map;
import javax.annotation.Nullable;
import com.Joy187.newmod.Main;
import com.Joy187.newmod.init.ModItems;
import com.Joy187.newmod.util.IHasModel;
import com.Joy187.newmod.util.handlers.SoundsHandler;
import com.google.common.collect.Maps;
import net.minecraft.block.BlockJukebox;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemRecord;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatList;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class ItemMusicDiscRE8 extends ItemRecord implements IHasModel{
private final SoundEvent sound;
public ItemMusicDiscRE8(String name, SoundEvent soundIn,CreativeTabs tab) {
super(name, soundIn);
this.maxStackSize=1;
this.sound=soundIn;
setUnlocalizedName(name);
setRegistryName(name);
setCreativeTab(tab);
ModItems.ITEMS.add(this);
// TODO 自动生成的构造函数存根
}
public ActionResult<ItemStack> onItemRightClick(World worldIn,EntityPlayer playerIn, EnumHand handIn) {
ItemStack itemstack = playerIn.getHeldItem(handIn);
//这个就是我们想要播放的音乐文件
playerIn.playSound(SoundsHandler.RE8MUSIC, 1.0f, 1.0f);
// worldIn.playSound(playerIn, playerIn.getPosition(), SoundsHandler.RE8MUSIC, SoundCategory.RECORDS, 1.0f, 1.0f);
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
}
@Override
public void registerModels() {
Main.proxy.registerItemRenderer(this, 0, "inventory");
}
}
4.在ModItems中添加我们的物品Item:
public static final ItemRecord MUSICDISC = new ItemMusicDiscRE8("musicre8", SoundsHandler.RE8MUSIC, Main.ITEM_TAB);
5.在resources包中的models的Item包中添加物品模型信息:
{
"parent": "item/generated",
"textures": {
"layer0": "joymod:items/record_yfds"
}
}
6.sounds.json中添加我们的声音信息,同时在en_us.lang文件中添加物品名称
"entity.gm79.yfds":{
"category":"entity",
"subtitle" :"entity.gm79.yfds",
"sounds":[{ "name": "joymod:entity/gm79/yfds", "stream":true }]
},