Changes

579 bytes added ,  01:18, 3 October 2021
Added a notice about a trap for newbies where sounds will always play on the client if they exist in the sound file regardless of registry.
Line 17: Line 17:  
</syntaxhighlight>
 
</syntaxhighlight>
   −
Using the example above, it creates a sound called <code>example_sound</code> and references the file <code><nowiki>assets/<modid>/sounds/example_sound_file.ogg</nowiki></code>.  
+
Using the example above, it creates a sound called <code>example_sound</code> and references the file <code><nowiki>assets/<modid>/sounds/example_sound_file.ogg</nowiki></code>.
 +
 
 +
 
 +
{{Tip/Danger | Due to [https://bugs.mojang.com/browse/MC-146721 the way OpenAL (Minecraft's sound engine) works], for your sound to have attenuation - that is, for it to get quieter as the player walks away, it absolutely MUST be mono (have one audio channel). 
 +
 
 +
Stereo sounds are always played at the '''player's''' location, making them ideal for ambient sounds and background music.
 +
 
 +
}} 
 +
 
    
Instead of being constructed as a list of strings, each entry could also be an object which allows a greater control over what and how it is played.
 
Instead of being constructed as a list of strings, each entry could also be an object which allows a greater control over what and how it is played.
Line 61: Line 69:  
For example, to reference <code>example_complex.ogg</code>, you would store the reference name (e.g. <code>example_sound_complex</code>) within a <code>new SoundEvent(new ResourceLocation("examplemod", "example_sound_complex"))</code>. This will locate <code>assets/examplemod/sounds.json</code> and try to find an <code>example_sound_complex</code> object nested within it.
 
For example, to reference <code>example_complex.ogg</code>, you would store the reference name (e.g. <code>example_sound_complex</code>) within a <code>new SoundEvent(new ResourceLocation("examplemod", "example_sound_complex"))</code>. This will locate <code>assets/examplemod/sounds.json</code> and try to find an <code>example_sound_complex</code> object nested within it.
   −
<code>SoundEvent</code>s must be [[Registration|registered]] to be referenced in code.  
+
{{Tip/Warning|<code>SoundEvent</code>s must be [[Registration/1.16|registered]] on the <code>ModEventBus</code> to properly reference a sound on a server. However, <code>SoundEvent</code>s or referencing a sound by name will still work on the client when the sound is not registered as long as the sound exists in <code>sounds.json</code>!}}
    
== Playing Sounds ==
 
== Playing Sounds ==
Line 67: Line 75:  
Vanilla has lots of methods for playing sounds that can be used in different scenarios.
 
Vanilla has lots of methods for playing sounds that can be used in different scenarios.
   −
=== <code>World</code> ===
+
=== <code>Level</code> ===
   −
# <code><nowiki>playSound(PlayerEntity, BlockPos, SoundEvent, SoundCategory, volume, pitch)</nowiki></code>
+
# <code><nowiki>playSound(Player, BlockPos, SoundEvent, SoundCategory, volume, pitch)</nowiki></code>
 
#*Simply forwards to overload (2), adding 0.5 to each coordinate of the BlockPos given.
 
#*Simply forwards to overload (2), adding 0.5 to each coordinate of the BlockPos given.
# <code><nowiki>playSound(PlayerEntity, double x, double y, double z, SoundEvent, SoundCategory, volume, pitch)</nowiki></code>
+
# <code><nowiki>playSound(Player, double x, double y, double z, SoundEvent, SoundCategory, volume, pitch)</nowiki></code>
 
#* <code>Client Behavior</code>: If the passed in player is the client player, plays the sound event to the client player.
 
#* <code>Client Behavior</code>: If the passed in player is the client player, plays the sound event to the client player.
 
#* <code>Server Behavior</code>: Plays the sound event to everyone nearby except the passed in player. Player can be <code>null</code>.
 
#* <code>Server Behavior</code>: Plays the sound event to everyone nearby except the passed in player. Player can be <code>null</code>.
 
#* <code>Usage</code>: The correspondence between the behaviors implies that these two methods are to be called from some player-initiated code that will be run on both logical sides at the same time - the logical client handles playing it to the user and the logical server handles everyone else hearing it without re-playing it to the original user. They can also be used to play any sound in general at any position server-side by calling it on the logical server and passing in a <code>null</code> player, thus letting everyone hear it.
 
#* <code>Usage</code>: The correspondence between the behaviors implies that these two methods are to be called from some player-initiated code that will be run on both logical sides at the same time - the logical client handles playing it to the user and the logical server handles everyone else hearing it without re-playing it to the original user. They can also be used to play any sound in general at any position server-side by calling it on the logical server and passing in a <code>null</code> player, thus letting everyone hear it.
 
# <code><nowiki>playSound(double x, double y, double z, SoundEvent, SoundCategory, volume, pitch, distanceDelay)</nowiki></code>
 
# <code><nowiki>playSound(double x, double y, double z, SoundEvent, SoundCategory, volume, pitch, distanceDelay)</nowiki></code>
#* <code>Client Behavior</code>: Just plays the sound event in the client world. If distanceDelay is true, then delays the sound based on how far it is from the player.
+
#* <code>Client Behavior</code>: Just plays the sound event in the client level. If distanceDelay is true, then delays the sound based on how far it is from the player.
 
#* <code>Server Behavior</code>: Does nothing.
 
#* <code>Server Behavior</code>: Does nothing.
 
#* <code>Usage</code>: This method only works client-side, and thus is useful for sounds sent in custom packets, or other client-only effect-type sounds. Used for thunder.
 
#* <code>Usage</code>: This method only works client-side, and thus is useful for sounds sent in custom packets, or other client-only effect-type sounds. Used for thunder.
   −
=== <code> ClientWorld </code> ===
+
=== <code>ClientLevel</code> ===
    
# <code><nowiki>playSound(BlockPos, SoundEvent, SoundCategory, volume, pitch, distanceDelay)</nowiki></code>
 
# <code><nowiki>playSound(BlockPos, SoundEvent, SoundCategory, volume, pitch, distanceDelay)</nowiki></code>
#* Simply forwards to <code>World</code>‘s overload (3), adding 0.5 to each coordinate of the <code>BlockPos</code> given.
+
#* Simply forwards to <code>Level</code>‘s overload (3), adding 0.5 to each coordinate of the <code>BlockPos</code> given.
   −
=== <code> Entity </code> ===
+
=== <code>Entity</code> ===
    
# <code><nowiki>playSound(SoundEvent, volume, pitch)</nowiki></code>
 
# <code><nowiki>playSound(SoundEvent, volume, pitch)</nowiki></code>
#* Forwards to <code>World</code>‘s overload (2), passing in <code>null</code> as the player.
+
#* Forwards to <code>LEvel</code>‘s overload (2), passing in <code>null</code> as the player.
 
#* <code>Client Behavior</code>: Does nothing.
 
#* <code>Client Behavior</code>: Does nothing.
 
#* <code>Server Behavior</code>: Plays the sound event to everyone at this entity’s position.
 
#* <code>Server Behavior</code>: Plays the sound event to everyone at this entity’s position.
 
#* <code>Usage</code>: Emitting any sound from any non-player entity server-side.
 
#* <code>Usage</code>: Emitting any sound from any non-player entity server-side.
   −
=== <code> PlayerEntity</code> ===
+
=== <code>Player</code> ===
    
# <code><nowiki>playSound(SoundEvent, volume, pitch)</nowiki></code> (overriding the one in <code>[[Sounds#Entity|Entity]]</code>)
 
# <code><nowiki>playSound(SoundEvent, volume, pitch)</nowiki></code> (overriding the one in <code>[[Sounds#Entity|Entity]]</code>)
#* Forwards to <code>World</code>‘s overload (2), passing in <code>this</code> as the player.
+
#* Forwards to <code>Level</code>‘s overload (2), passing in <code>this</code> as the player.
#* <code>Client Behavior</code>: Does nothing, see override in <code>[[Sounds#clientplayerentity|ClientPlayerEntity]]</code>.
+
#* <code>Client Behavior</code>: Does nothing, see override in <code>[[Sounds#localplayer|LocalPlayer]]</code>.
 
#* <code>Server Behavior</code>: Plays the sound to everyone nearby <code>except</code> this player.
 
#* <code>Server Behavior</code>: Plays the sound to everyone nearby <code>except</code> this player.
#* <code>Usage</code>: See <code>[[Sounds#clientplayerentity|ClientPlayerEntity]]</code>.
+
#* <code>Usage</code>: See <code>[[Sounds#localplayer|LocalPlayer]]</code>.
   −
=== <code> ClientPlayerEntity</code> ===
+
=== <code>LocalPlayer</code> ===
   −
# <code><nowiki>playSound(SoundEvent, volume, pitch)</nowiki></code> (overriding the one in <code>[[Sounds#playerentity|PlayerEntity]]</code>)
+
# <code><nowiki>playSound(SoundEvent, volume, pitch)</nowiki></code> (overriding the one in <code>[[Sounds#player|Player]</code>)
#* Forwards to <code>World</code>‘s overload (2), passing in <code>this</code> as the player.
+
#* Forwards to <code>Level</code>‘s overload (2), passing in <code>this</code> as the player.
 
#* <code>Client Behavior</code>: Just plays the Sound Event.
 
#* <code>Client Behavior</code>: Just plays the Sound Event.
 
#* <code>Server Behavior</code>: Method is client-only.
 
#* <code>Server Behavior</code>: Method is client-only.
#* <code>Usage</code>: Just like the ones in <code>World</code>, these two overrides in the player classes seem to be for code that runs together on both sides. The client handles playing the sound to the user, while the server handles everyone else hearing it without re-playing to the original user.
+
#* <code>Usage</code>: Just like the ones in <code>Level</code>, these two overrides in the player classes seem to be for code that runs together on both sides. The client handles playing the sound to the user, while the server handles everyone else hearing it without re-playing to the original user.
       
[[Category:Game Effects]]
 
[[Category:Game Effects]]
2

edits