Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Play Sound at Player Location(Client Side)?


Beetle90
 Share

Recommended Posts

Hi guys, I'm new to the group. It's really great to see one single location for modders to exchange information between one another. I look forward to being a part of the forge community for a long time!

 

My question is about being able to play the creeper primed sound at a player location. I've created a simple command class that runs fine, but It doesn't seem to want to play the sound when I get in-game. Mind helping a newbie out? I noticed there are several versions of the playSound() method and I've tried them all to no avail. There must be something simple that I'm missing. Here's the execute method in question that won't play the sound.

 

I was also wondering if using thread.sleep() is considered alright to insert a delay. I'd love to have a short delay between the sound playing and the rest of my code.

 

 @Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException{
    	if (args.length <= 0)
        {
            throw new WrongUsageException("Must give player name.", new Object[0]);
        }
    	if (sender instanceof EntityPlayer){
    		EntityPlayer entityplayer = args.length >= 1 ? getPlayer(server, sender, args[0]) : getCommandSenderAsPlayer(sender);
    		server.getEntityWorld().playSound(entityplayer.getPosition().getX(), entityplayer.getPosition().getY(), entityplayer.getPosition().getZ(), SoundEvents.ENTITY_CREEPER_PRIMED, SoundCategory.HOSTILE, 1.0F, 0.5F, false); //one of many i've tried.
    	}
    }

 

Thanks for any help!

Link to comment
Share on other sites

Hi guys, I'm new to the group. It's really great to see one single location for modders to exchange information between one another. I look forward to being a part of the forge community for a long time!

 

My question is about being able to play the creeper primed sound at a player location. I've created a simple command class that runs fine, but It doesn't seem to want to play the sound when I get in-game. Mind helping a newbie out? I noticed there are several versions of the playSound() method and I've tried them all to no avail. There must be something simple that I'm missing. Here's the execute method in question that won't play the sound.

 

I was also wondering if using thread.sleep() is considered alright to insert a delay. I'd love to have a short delay between the sound playing and the rest of my code.

 

 @Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException{
    	if (args.length <= 0)
        {
            throw new WrongUsageException("Must give player name.", new Object[0]);
        }
    	if (sender instanceof EntityPlayer){
    		EntityPlayer entityplayer = args.length >= 1 ? getPlayer(server, sender, args[0]) : getCommandSenderAsPlayer(sender);
    		server.getEntityWorld().playSound(entityplayer.getPosition().getX(), entityplayer.getPosition().getY(), entityplayer.getPosition().getZ(), SoundEvents.ENTITY_CREEPER_PRIMED, SoundCategory.HOSTILE, 1.0F, 0.5F, false); //one of many i've tried.
    	}
    }

 

Thanks for any help!

First off no Thread.sleep() is not a valid way of doing that. It puts the whole thread asleep. IE no game play the entire thing pauses. And I wonder why that method does nothing....oh wait

From the World class.

    public void playSound(double x, double y, double z, SoundEvent soundIn, SoundCategory category, float volume, float pitch, boolean distanceDelay)
    {
    }

 

Edit: Off topic, but something I found very funny that can be done.

   

PositionedSoundRecord sound = new PositionedSoundRecord(SoundEvents.ENTITY_CREEPER_PRIMED, SoundCategory.HOSTILE, 1, 1, new BlockPos(0, 0, 0));
    @Override
    @SideOnly(Side.CLIENT)
    public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) {
	if (!Minecraft.getMinecraft().getSoundHandler().isSoundPlaying(sound)) {
		sound = new PositionedSoundRecord(SoundEvents.ENTITY_CREEPER_PRIMED, SoundCategory.HOSTILE, 1, 1, playerIn.getPosition());
		Minecraft.getMinecraft().getSoundHandler().playSound(sound);
	}
    }

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

If it's a delay you want, thread.sleep() definitely won't work as intended, as the post above says. The proper method would depend on what's playing the sounds; if it's an item, consider saving and adding to the countdown in its NBT data; if it's a tile entity, just create an integer in the tile entity's class and add to it in update(); if it's the player itself (or another entity), you might want to look into capabilities.

Link to comment
Share on other sites

The documentation on sounds has a very useful list and explanation of the various methods to play sounds.

 

Thank you everyone for your help!

 

diesieben07, that link was really helpful. Turns out all I needed to do was null the EntityPlayer argument and use a different version of the World's playSound method.

 

Here's the code if anyone falls into a similar issue!

 

entityplayer.worldObj.playSound(null, entityplayer.getPosition().getX(), entityplayer.getPosition().getY(), entityplayer.getPosition().getZ(), SoundEvents.ENTITY_CREEPER_PRIMED, SoundCategory.HOSTILE, 1.0F, 0.5F);

 

 

Link to comment
Share on other sites

I've created a simple command class...

 

Doesn't vanilla MC already have a command to play a sound at a player?

 

From another forum:

If you type 
    /playsound (sound) (Player) [x] [y] [z] [volume] [pitch/speed] [minimumvolume]
then the game will play the sound you entered

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.

Link to comment
Share on other sites

I've created a simple command class...

 

Doesn't vanilla MC already have a command to play a sound at a player?

 

From another forum:

If you type 
    /playsound (sound) (Player) [x] [y] [z] [volume] [pitch/speed] [minimumvolume]
then the game will play the sound you entered

 

Oh! Good to know :)

 

but sadly I still cant find a way to play the sound, wait 1 second, and then perform the rest of my code.

Link to comment
Share on other sites

as mentioned above it could be a TE or a Capability for the player. Once the sounds plays, it enables a state to be set that says "perform my logic after x amount of ticks" which MC 20 ticks is about 1 sec RL time. If you want delays you will have to setup your own counter system. As mentioned before Thread.Sleep() is a no no.

Link to comment
Share on other sites

as mentioned above it could be a TE or a Capability for the player. Once the sounds plays, it enables a state to be set that says "perform my logic after x amount of ticks" which MC 20 ticks is about 1 sec RL time. If you want delays you will have to setup your own counter system. As mentioned before Thread.Sleep() is a no no.

 

Thanks hugo. Ya, I checked up on Capabilities, something that I've never worked with before. My sound is playing through the World object, so I guess I'd have to change it back to an EntityPlayer in order to work on Capabilities from there.

 

So is the logic behind this something like: I create a capability interface and storage, register it, attach it to the EntityPlayer, and lastly make a handler for the capability to fire whenever my command is initiated? I hope I have this right....

Link to comment
Share on other sites

Create a Capability, CapabilityData, and a CapbilityProvider. You then attach it to players or all entities if you wish. You can make an additional handler, but if you store the needed information in the capability you can simply access it wherever you need be. Say like in an update event

 

event.getEntity().getCapability(YourCapabilityHere,null).aFieldOrMethodInsideYourCapability;

 

Feel free to dig around my code which is in my sig to get an idea of how I've implemented capabilities. Most packages are named to contain things that make sense. So comms would be all my packets and proxies.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.