Jump to content

[1.12]Custom sound plays by command but not using world.playSound()


TheMCJavaFre4k

Recommended Posts

I can't seem to figure out why I'm able to play my custom sound through the command /playsound... but not when calling the function world.play(...);

I know the code is correctly firing on right click and will even work with playing default sounds but not custom ones.

 

Trigger Code:

@Override
	public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {
        IBlockState iblockstate = worldIn.getBlockState(pos);

        if (iblockstate.getBlock() == Blocks.JUKEBOX && !((Boolean)iblockstate.getValue(BlockJukebox.HAS_RECORD)).booleanValue())
        {
        	
            if (!worldIn.isRemote)
            {
                ItemStack itemstack = player.getHeldItem(hand);
                ((BlockJukebox)Blocks.JUKEBOX).insertRecord(worldIn, pos, iblockstate, itemstack);
                player.world.playSound((EntityPlayer)null, pos, SoundHandler.record1, SoundCategory.RECORDS, 1.0f, 1.0f);       
                itemstack.setCount(0);
                player.addStat(StatList.RECORD_PLAYED);
            }

            return EnumActionResult.SUCCESS;
        }
        else
        {
            return EnumActionResult.PASS;
        }
    }

 

SoundHandler class(SoundHandler() called in preInit() to set record1 but can easily be moved up to declaration):

public final class SoundHandler {

	public static SoundEvent record1;

	public SoundHandler(){
		
		record1 = createSoundEvent2("record1");

	}
	
	private static SoundEvent createSoundEvent2(String soundName){
		final ResourceLocation sound = new ResourceLocation(soundName + ".ogg");
		System.out.println("Create Sound2");
		return new SoundEvent(sound).setRegistryName(soundName);
	}

	@Mod.EventBusSubscriber
	public static class Registration{
		@SubscribeEvent
		public static void registerSoundEvents(RegistryEvent.Register<SoundEvent> e){
			
			e.getRegistry().register(record1);
		}
	}

}

 

sounds.json

{
  "record1": {
    "category": "record",
    "sounds": [
      {
        "name": "testsoundmod:record1",
        "stream": true
      }
    ]
  }
}

 

Any help would be appreciated.

Thanks

 

Link to comment
Share on other sites

Try with:

worldIn.playSound(player, pos, SoundHandler.record1, SoundCategory.RECORDS, 1.0f, 1.0f); 

Also I believe you don't need the .ogg when creating the Sounds.

 

public class CreateSoundEvent extends SoundEvent {

    public CreateSoundEvent(String name) {
        super(new ResourceLocation(ModInfo.MODID, name));
        this.setRegistryName(new ResourceLocation(ModInfo.MODID, name));

        ModSounds.SOUNDS.add(this);
    }

}

 

Link to comment
Share on other sites

1 hour ago, American2050 said:

Try with:


worldIn.playSound(player, pos, SoundHandler.record1, SoundCategory.RECORDS, 1.0f, 1.0f); 

Also I believe you don't need the .ogg when creating the Sounds.
 


public class CreateSoundEvent extends SoundEvent {

    public CreateSoundEvent(String name) {
        super(new ResourceLocation(ModInfo.MODID, name));
        this.setRegistryName(new ResourceLocation(ModInfo.MODID, name));

        ModSounds.SOUNDS.add(this);
    }

}

 

 Still no sound using updated play method. But same thing, I can use default Minecraft sounds and it works and I can also play the required sound via command.

 

Thanks for the heads up about no need for the extension. Makes everything a little neater.

Link to comment
Share on other sites

I’ve just recently changed my item class to extend ItemRecord and removed the onItemUse function to see if it is the way I am implementing playSound. I pass in the sound event when creating the item but still the same result. No errors return and the jukebox displays the correct sound event.desc(as I haven’t added it in Lang) but still doesn’t play the audio.

playing through a command still plays the audio after this change.

any ideas are welcome.

Link to comment
Share on other sites

Ive double checked with the documentation(found https://mcforge.readthedocs.io/en/latest/effects/sounds/) and it looks like it should be working since I am using the second method under World option. This should play to all players and if the passed in player is the client, it should play to them aswell. 

The Method I have at the moment:

player.world.playSound(player, pos, SoundHandler.record1, SoundCategory.RECORDS, 1.0f, 1.0f);     

 

Link to comment
Share on other sites

If you're calling playSound from the server (which you were in your original example), you want to pass null as the player reference:

 

player.world.playSound(null, pos, SoundHandler.record1, SoundCategory.RECORDS, 1.0f, 1.0f);   

 

Calling world.playSound (ON THE SERVER) plays the sound for everybody EXCEPT the player passed into it. If you pass null, it plays for everyone.

 

Calling world.playSounds (ON THE CLIENT) plays the sound ONLY for the player passed in. (Which should only be the local player anyway... it is local after all)

 

 

Next, You've got a few issues here with your sound registering.

 

In your SoundHandler class, you need to register with your MODID and sound name, e.g. 

You also don't want .ogg in the regsistry because this ResourceLocation is not a RL to a sound file, but a .json entry describing your sound

 

private static SoundEvent createSoundEvent2(String soundName){
		final ResourceLocation sound = new ResourceLocation(MODID /* whatever your mod id is... */, soundName);
		System.out.println("Create Sound2");
		return new SoundEvent(sound).setRegistryName(soundName);
	}

 

If you don't use your mod id, it will default to looking in Minecraft's sound folder.

 

 

That should get you on the right direction and getting some kinks worked out!

Edited by skeeter144
Link to comment
Share on other sites

4 hours ago, skeeter144 said:

If you're calling playSound from the server (which you were in your original example), you want to pass null as the player reference:

 


player.world.playSound(null, pos, SoundHandler.record1, SoundCategory.RECORDS, 1.0f, 1.0f);   

 

Calling world.playSound (ON THE SERVER) plays the sound for everybody EXCEPT the player passed into it. If you pass null, it plays for everyone.

 

Calling world.playSounds (ON THE CLIENT) plays the sound ONLY for the player passed in. (Which should only be the local player anyway... it is local after all)

 

 

Next, You've got a few issues here with your sound registering.

 

In your SoundHandler class, you need to register with your MODID and sound name, e.g. 

You also don't want .ogg in the regsistry because this ResourceLocation is not a RL to a sound file, but a .json entry describing your sound

 


private static SoundEvent createSoundEvent2(String soundName){
		final ResourceLocation sound = new ResourceLocation(MODID /* whatever your mod id is... */, soundName);
		System.out.println("Create Sound2");
		return new SoundEvent(sound).setRegistryName(soundName);
	}

 

If you don't use your mod id, it will default to looking in Minecraft's sound folder.

 

 

That should get you on the right direction and getting some kinks worked out!

Thanks heaps for your suggestions, It worked. It turns out it was the missing domain for the resourcelocation. Its interesting that it would still play via /playSound command but not using world.playSound method.

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.
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...

×   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.

Announcements



×
×
  • Create New...

Important Information

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