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

(SOLVED)[1.12.2] Help with PlaySoundEvent


kingdadrules
 Share

Recommended Posts

I want to replace vanilla sounds with custom ones in certain circumstances in my mod.

 

The way to do it appears to be to use PlaySoundEvent & return a new sound.

I have set up a custom SoundEvent successfully, but need help with PlaySoundEvent .

My SoundEvent is registered like so:

public class ModSounds {

    public static SoundEvent sword2Use = new SoundEvent(new ResourceLocation(testmod.modId + ":" + "sword2Use")).setRegistryName("sword2Use");   
    public static void register(IForgeRegistry<SoundEvent> registry) {
        registry.registerAll(               
                sword2Use
                );

        }
}

 

My code so far for the PlaySoundEvent Listener:

public class SoundPlays {
	
		@SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
		@SideOnly(Side.CLIENT)
		public void soundPlay(PlaySoundEvent event) {			
			EntityPlayer player = Minecraft.getMinecraft().player;
			if (player != null 
			&& player.getHeldItemMainhand().getItem() == ModItems.sword2) 				
			{				
				event.setResultSound(??????);									
			}			
	    }
}

I'm stuck with what to return in ??????, as I don't know how to return a SoundEvent as ISound & can't find any documentation or examples to work off.

 

Any help appreciate.

 

 

Edited by kingdadrules
Link to comment
Share on other sites

  • Do not create registry entries in static initializers.
  • Do not use @SideOnly.
  • 7 hours ago, kingdadrules said:

    I'm stuck with what to return in ??????, as I don't know how to return a SoundEvent as ISound & can't find any documentation or examples to work off.

    Look at the implementations of ISound (your IDE has a "type hierarchy" command). Then see how they are used in vanilla.
Link to comment
Share on other sites

Thanks for your advice diesieben07, it definately helped with sorting it & the type hierarchy view will come in very handy.

 

I created my own class implementing ISound, including my custom ResourceLocation & returned this:

MyISound sound =  new MyISound();		
event.setResultSound(sound);				
				

It works fine now. I'm slowly learning!

 

I will look into using the correct method of creating registry entries. I copied the static initializer method from example code at the start of my modding journey & so have used it for registering everything since.

 

 

 

 

 

Link to comment
Share on other sites

I've just come back to this because I afree & realise the way to go is not to write my own implementation.

 

I've looked & looked but I cannot see how to do what I need to do without doing this.

 

I want to be able to return my own custom sound  & also to make some sounds non-fading, which I do in a custom implementation by  returning AttenuationType.LINEAR  from getAttenuationType().

 

Any further help would be welcome & hopefully assist in my minecraft & java learning path.

 

 

 

 

Link to comment
Share on other sites

2 hours ago, kingdadrules said:

I want to be able to return my own custom sound  & also to make some sounds non-fading, which I do in a custom implementation by  returning AttenuationType.LINEAR  from getAttenuationType().

Look at PositionedSoundRecord (the class most often used for sounds, no, not only for records, the name is misleading).

Link to comment
Share on other sites

That was the pointer I needed, I can't understand why I didn't pick this up from my trawling online.

 

ResourceLocation loc = new ResourceLocation(testmod.modId + ":" + "sword2Use");
PositionedSoundRecord MyPSR = new PositionedSoundRecord(loc, SoundCategory.PLAYERS, 1.0f, 1.0f, false, 0, AttenuationType.LINEAR, 
		player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ());

event.setResultSound(MyPSR);

 

Has done the trick.

 

Thanks once again for your help & patience.

 

 

 

Link to comment
Share on other sites

  • 2 years later...

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



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Your java installation is borked. Uninstall Java completely, then reinstall Java.
    • Baubles Interface cannot run on a server. Make sure you are using the latest version. If you are, report this to the mod author.
    • You can find the archive here: https://adoptium.net/archive.html?variant=openjdk8&jvmVariant=hotspot Use build 312.
    • Server stops launching a couple seconds in and spits out a bunch of errors. This is a new server so it didn't just suddenly break, it has never successfully started up and I can't make anything out of this log, [27Jan2022 22:25:37.820] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--gameDir, ., --launchTarget, fmlserver, --fml.forgeVersion, 36.2.20, --fml.mcpVersion, 20210115.111550, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge, nogui] [27Jan2022 22:25:37.823] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 8.0.9+86+master.3cf110c starting: java version 1.8.0_321 by Oracle Corporation [27Jan2022 22:25:38.185] [main/INFO] [net.minecraftforge.fml.loading.FixSSL/CORE]: Added Lets Encrypt root certificates as additional trust [27Jan2022 22:25:38.213] [main/INFO] [mixin/]: SpongePowered MIXIN Subsystem Version=0.8.4 Source=file:/F:/f%20in%20the%20a/create%20above%20and%20beyond/server/libraries/org/spongepowered/mixin/0.8.4/mixin-0.8.4.jar Service=ModLauncher Env=SERVER [27Jan2022 22:25:40.330] [main/ERROR] [mixin/]: Mixin config antiqueatlas.mixins.json does not specify "minVersion" property [27Jan2022 22:25:40.406] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: java.lang.NoSuchMethodError: sun.security.util.ManifestEntryVerifier.<init>(Ljava/util/jar/Manifest;)V [27Jan2022 22:25:40.407] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.SecureJarHandler.createCodeSource(SecureJarHandler.java:66) [27Jan2022 22:25:40.408] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.TransformingClassLoader$DelegatedClassLoader.findClass(TransformingClassLoader.java:275) [27Jan2022 22:25:40.408] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.TransformingClassLoader.loadClass(TransformingClassLoader.java:136) [27Jan2022 22:25:40.409] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.TransformingClassLoader.loadClass(TransformingClassLoader.java:98) [27Jan2022 22:25:40.409] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at java.lang.ClassLoader.loadClass(Unknown Source) [27Jan2022 22:25:40.409] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at java.lang.Class.forName0(Native Method) [27Jan2022 22:25:40.409] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at java.lang.Class.forName(Unknown Source) [27Jan2022 22:25:40.409] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.service.modlauncher.ModLauncherClassProvider.findClass(ModLauncherClassProvider.java:67) [27Jan2022 22:25:40.410] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.platform.MixinConnectorManager.loadConnectors(MixinConnectorManager.java:70) [27Jan2022 22:25:40.410] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.platform.MixinConnectorManager.inject(MixinConnectorManager.java:59) [27Jan2022 22:25:40.410] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.platform.MixinPlatformManager.inject(MixinPlatformManager.java:196) [27Jan2022 22:25:40.411] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.MixinBootstrap.inject(MixinBootstrap.java:202) [27Jan2022 22:25:40.411] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.MixinLaunchPluginLegacy.initializeLaunch(MixinLaunchPluginLegacy.java:201) [27Jan2022 22:25:40.411] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.MixinLaunchPluginLegacy.initializeLaunch(MixinLaunchPluginLegacy.java:195) [27Jan2022 22:25:40.412] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.LaunchPluginHandler.lambda$announceLaunch$9(LaunchPluginHandler.java:97) [27Jan2022 22:25:40.412] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at java.util.HashMap.forEach(Unknown Source) [27Jan2022 22:25:40.412] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.LaunchPluginHandler.announceLaunch(LaunchPluginHandler.java:97) [27Jan2022 22:25:40.413] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:52) [27Jan2022 22:25:40.414] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [27Jan2022 22:25:40.414] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [27Jan2022 22:25:40.414] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [27Jan2022 22:25:40.415] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at net.minecraftforge.server.ServerMain$Runner.runLauncher(ServerMain.java:63) [27Jan2022 22:25:40.417] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at net.minecraftforge.server.ServerMain$Runner.access$100(ServerMain.java:60) [27Jan2022 22:25:40.417] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at net.minecraftforge.server.ServerMain.main(ServerMain.java:57)  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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