Posted January 20, 20196 yr I'm working on Hardcore Minecraft Mod and i added Saw to game and i want to have sound when item used so i decided adding the sound but i have an problem with playing sound. Sound Registery works perfectly and i can play sound with /playsound command. Spoiler NetworkHandler public class NetworkHandler { private static List<Class<? extends IMessage>> packets = new ArrayList<>(); public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(HMM.MOD_ID); public static SimpleNetworkWrapper getNetwork() { return INSTANCE; } public static void registerPackets(){ registerMessage(PacketPlaySound.Handler.class,PacketPlaySound.class,Side.SERVER); } private static <REQ extends IMessage, REPLY extends IMessage> void registerMessage(Class<? extends IMessageHandler<REQ, REPLY>> messageHandler, Class<REQ> requestMessageType, Side side){ getNetwork().registerMessage(messageHandler,requestMessageType,packets.size(),side); packets.add(requestMessageType); } } PacketPlaySound public class PacketPlaySound implements IMessage { private String sound; private BlockPos pos; private float volume; private float pitch; public PacketPlaySound(){ System.out.println("Packet cons OK!"); } public PacketPlaySound(String sound, BlockPos pos,float volume,float pitch){ this.sound = sound; this.pos = pos; this.volume = volume; this.pitch = pitch; System.out.println("Packet OK!"); } @Override public void fromBytes(ByteBuf buf) { this.sound = ByteBufUtils.readUTF8String(buf); this.pos = new BlockPos(buf.readDouble(),buf.readDouble(),buf.readDouble()); this.volume = buf.readFloat(); this.pitch = buf.readFloat(); System.out.println("Packet fromBytes OK!"); } @Override public void toBytes(ByteBuf buf) { ByteBufUtils.writeUTF8String(buf,this.sound); buf.writeDouble(this.pos.getX()); buf.writeDouble(this.pos.getY()); buf.writeDouble(this.pos.getZ()); buf.writeFloat(this.volume); buf.writeFloat(this.pitch); System.out.println("Packet toBytes OK!"); } public static class Handler implements IMessageHandler<PacketPlaySound,IMessage>{ @Override public IMessage onMessage(PacketPlaySound message, MessageContext ctx) { System.out.println("Packet onMessage!"); EntityPlayerMP serverPlayer = ctx.getServerHandler().player; serverPlayer.getServerWorld().playSound(serverPlayer,message.pos, RegisteryUtils.getSound(message.sound), SoundCategory.PLAYERS,message.volume,message.pitch); serverPlayer.sendMessage(new TextComponentString("Test the Message!")); return null; } } } ItemSaw private final Random random = new Random(); @Override public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { if(!worldIn.isRemote){ if(hand == EnumHand.MAIN_HAND){ IBlockState iblockstate = worldIn.getBlockState(pos); Block block = iblockstate.getBlock(); if(block instanceof BlockLog){ ItemStack drop; int data = 0; if(block instanceof BlockNewLog){ BlockPlanks.EnumType blockplanks$enumtype = iblockstate.getValue(BlockNewLog.VARIANT); switch (blockplanks$enumtype){ case DARK_OAK: data = 1; break; default: break; } drop = new ItemStack(Blocks.PLANKS,3,4 + data); }else{ BlockPlanks.EnumType blockplanks$enumtype = iblockstate.getValue(BlockOldLog.VARIANT); switch (blockplanks$enumtype){ case BIRCH: data = 2; break; case SPRUCE: data = 1; break; case JUNGLE: data = 3; break; default: break; } drop = new ItemStack(Blocks.PLANKS,3,data); } worldIn.removeTileEntity(pos); worldIn.setBlockToAir(pos); ItemStack is = player.getHeldItem(hand).copy(); boolean breakItem = is.attemptDamageItem(2, random, player instanceof EntityPlayerMP ? (EntityPlayerMP) player : null); System.out.println(breakItem); if (breakItem) { ForgeEventFactory.onPlayerDestroyItem(player, is, null); is = ItemStack.EMPTY; } player.replaceItemInInventory(player.inventory.getSlotFor(player.getHeldItem(hand)),is); player.entityDropItem(drop,0); //Doesn't playing the sound! NetworkHandler.getNetwork().sendToServer(new PacketPlaySound("saw_cut",pos,1.0F,1.0F)); worldIn.playSound(player,pos, Objects.requireNonNull(RegisteryUtils.getSound("saw_cut")),SoundCategory.BLOCKS,1.0f,1.0f); return EnumActionResult.SUCCESS; } } } return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ); } sounds.json { "saw_cut": { "category": "block", "sounds": [ { "name" : "hmm:saw_cut", "stream": true } ] } } System.out's in the PacketPlaySound works perfectly and player recieve "Test the Message" message. I made some search on google and i found i must call sound both client and server for play sound. But i can't figured out. Developing Kodev Minecraft Hardcore Mod! If You're Wondering, My Mod Page. http://minecraft.curseforge.com/projects/minecraft-hardcore-mod
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.