Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I have created custom sound in 1.7.10 mod using sounds.json file and w.playsound function.  It works great for the player who activates the sound but the sound is not heard by other players on the server.  I was not able to figure out from searching how to get w.playsound to play for all players.  Can you take a look at this class and let me know what needs to be added for the sound to play for all players (within hearing distance, obviously).  Thanks very much.

 

package blinkylightsmod;

import java.util.Random;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.world.World;
import net.minecraft.world.IBlockAccess;
import net.minecraft.util.IIcon;

public class BlockStar extends Block {

private boolean playing;

private boolean playing1;

public BlockStar() {
	super(BlinkyLightsMod.star);
	setStepSound(Block.soundTypeGlass);
	setCreativeTab(CreativeTabs.tabDecorations);
	setBlockTextureName("blinkylightsmod:blockStar");
	setBlockName("blockStar");
	setLightLevel(1.0F);
	this.playing = false;
	this.playing1 = false;
}

public boolean renderAsNormalBlock() {
	return false;
}

public boolean isOpaqueCube() {
	return false;
}

public int getRenderType() {
	return 1;
}

public AxisAlignedBB getCollisionBoundingBoxFromPool(World w, int x, int y, int z) {
	return null;
}

public boolean onBlockActivated(World w, int x, int y, int z, EntityPlayer p, int i, float hx, float hy, float hz) {
	if (p.getCurrentEquippedItem() != null && p.getCurrentEquippedItem().getItem() == BlinkyLightsMod.itemCandyCane) {
		w.playSound(x, y, z, "blinkylightsmod:deckthehalls", 1.0F, 1.0F, true);
		return true;
	} else {
		w.playSound(x, y, z, "blinkylightsmod:merrychristmas", 1.0F, 1.0F, true);
		return true;
	}
}
}

World#playSound

does nothing on the server and plays the sound on the client.

World#playSoundAtEntity

,

World#playSoundToNearExcept

and

World#playSoundEffect

send a packet to the appropriate clients telling them to play the sound and do nothing on the client.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Many aspects of sound are counter intuitive (well, most of client-server architecture takes some experience to wrap one's head around).

 

Most of the sounds in the game are decided server-side and then sent to all listeners (world accesses) client-side. You'll see the calls bracketed by "!world.isRemote" (and you will want to do likewise).

 

It's an unusual sound that starts as a decision made client-side so that nobody else can hear it. Clicking on a game-control would be like that, not in the world, so applicable to only the player doing the clicking.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

The way that i did it with my mod is to use the Block Event system. The Block Event system will send the block event to all clients that are near. You then play the sound on each client that receives it.

 

//When its turned on

private void setActive()
{		
	markDirty();
	if (theSound != "" && !soundloc.isNull())
	this.worldObj.addBlockEvent(xCoord, yCoord, zCoord, MCICraft.blockSound.getBlock(),0,0); //<---- Important line
}

 

//Code on the Block

@Override
public boolean onBlockEventReceived(World world, int x, int y, int z, int eventid, int eventparam)
    {
	TileEntity te = world.getTileEntity(x,y,z);

	if (!(te instanceof TileSound))
		return true;

	TileSound ts = (TileSound) te;

	if (ts.getSound() != "")
        world.playSoundEffect(ts.getX(),ts.getY(),ts.getZ(), ts.getSound(), ts.getFullVolume(), ts.getFullPitch()); //<-- Important Line

        return true;
    }

 

 

Note: While my code uses a TileEntity you don't need to use one (If you did you would have to send all the information to the clients tile entity using getDescriptionPacket() and onDataPacket()).

  • Author

My problem is resolved by using w.playSoundEffect instead of w.playSound.  Simple as that.

 

Also, to be clear, it works equally well whether or not it is bracketed by !w.isRemote check.

 

it works equally well whether or not it is bracketed by !w.isRemote check.

Granted; playSoundEffect does nothing on the client side (the subclass of World instance passed in on the server-side is not the same as on client-side). Being paranoid (and wanting my code to remind me of what happens where), I still try to imitate vanilla code by bracketing my calls when I know what side they should run.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.