User Tools

Site Tools


tutorial:sounds

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:sounds [2020/02/11 19:46] – Remove category from sounds.json. It was removed in Minecraft 1.10 earthcomputertutorial:sounds [2023/01/09 18:20] (current) – [Step 4: Register your sound event] 1.19.3 Registries Change slainlight
Line 7: Line 7:
 if (!world.isClient) { if (!world.isClient) {
       world.playSound(       world.playSound(
-              null, // Player (purpose unknown, edit if you know)+              null, // Player if non-null, will play sound for every nearby player *except* the specified player
               blockPos, // The position of where the sound will come from               blockPos, // The position of where the sound will come from
               SoundEvents.BLOCK_ANVIL_LAND, // The sound that will play, in this case, the sound the anvil plays when it lands.               SoundEvents.BLOCK_ANVIL_LAND, // The sound that will play, in this case, the sound the anvil plays when it lands.
Line 17: Line 17:
 </code> </code>
  
-You can do this when the block is right clicked for example, by overriding ''activate'':+You can do this when the block is right clicked for example, by overriding ''onUse'':
 <code java> <code java>
 public class ExampleBlock extends Block { public class ExampleBlock extends Block {
Line 23: Line 23:
          
     @Override     @Override
-    public boolean activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity placedBy, Hand hand, BlockHitResult blockHitResult) {+    public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity placedBy, Hand hand, BlockHitResult blockHitResult) {
         if (!world.isClient) {         if (!world.isClient) {
             world.playSound(null, blockPos, SoundEvents.BLOCK_ANVIL_LAND, SoundCategory.BLOCKS, 1f, 1f);             world.playSound(null, blockPos, SoundEvents.BLOCK_ANVIL_LAND, SoundCategory.BLOCKS, 1f, 1f);
Line 38: Line 38:
 To play a sound, you first need a sound file. Minecraft uses the ''.ogg'' file format for those.  To play a sound, you first need a sound file. Minecraft uses the ''.ogg'' file format for those. 
 If your sound file is in a different format (like the one in the provided link), you can use an online converter to convert from your format to ''.ogg'' If your sound file is in a different format (like the one in the provided link), you can use an online converter to convert from your format to ''.ogg''
-Now put your .ogg file in the ''resources/assets/modid/sounds'' folder. In the example case the file will be ''resources/assets/tutorial/sounds/my_sound.ogg''.+Make sure your sound has only one channel, otherwise the attenuation effect (gradual loss of volume with distance) won't be applied to it.   
 +Now put your .ogg file in the ''resources/assets/modid/sounds'' folder. In the example case the file will be ''resources/assets/tutorial/sounds/my_sound.ogg'' 
 ==== Step 2: Add a sounds.json file, or add to it if you already have one ==== ==== Step 2: Add a sounds.json file, or add to it if you already have one ====
 Under ''resources/assets/modid'' add a new file named ''sounds.json'', if you do not have one yet. Under ''resources/assets/modid'' add a new file named ''sounds.json'', if you do not have one yet.
Line 69: Line 70:
 public class ExampleMod { public class ExampleMod {
     [...]     [...]
-    public static final Identifier MY_SOUND_ID = new Identifier("tutorial:my_sound"+    public static final Identifier MY_SOUND_ID = new Identifier("tutorial:my_sound"); 
-    public static SoundEvent MY_SOUND_EVENT = new SoundEvent(MY_SOUND_ID);+    public static SoundEvent MY_SOUND_EVENT = SoundEvent.of(MY_SOUND_ID);
 } }
 </code> </code>
 +If you're using version 1.19.2 or below, please replace ''SoundEvent.of'' with ''new SoundEvent''
 ==== Step 4: Register your sound event ==== ==== Step 4: Register your sound event ====
 Register your sound event under the ''SOUND_EVENT'' registry: Register your sound event under the ''SOUND_EVENT'' registry:
Line 79: Line 81:
 public void onInitialize(){ public void onInitialize(){
      [...]      [...]
-     Registry.register(Registry.SOUND_EVENT, ExampleMod.MY_SOUND_ID, MY_SOUND_EVENT);+     Registry.register(Registries.SOUND_EVENT, ExampleMod.MY_SOUND_ID, MY_SOUND_EVENT);
  
 </code> </code>
 +If you're using version 1.19.2 or below, please replace ''Registries'' with ''Registry''
 ==== Step 5: Use your sound event ==== ==== Step 5: Use your sound event ====
-Use the sound event just like we explained at the start (''activate'' is just an example, use it anywhere you have access to ''World'' instance):+Use the sound event just like we explained at the start (''onUse'' is just an example, use it anywhere you have access to ''World'' instance):
 <code java> <code java>
 public class ExampleBlock extends Block { public class ExampleBlock extends Block {
     @Override     @Override
-    public boolean activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity placedBy, Hand hand, BlockHitResult blockHitResult) {+    public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity placedBy, Hand hand, BlockHitResult blockHitResult) {
         if (!world.isClient) {         if (!world.isClient) {
             world.playSound(             world.playSound(
-                    null, // Player (purpose unknown, edit if you know)+                    null, // Player if non-null, will play sound for every nearby player *except* the specified player
                     blockPos, // The position of where the sound will come from                     blockPos, // The position of where the sound will come from
                     ExampleMod.MY_SOUND_EVENT, // The sound that will play                     ExampleMod.MY_SOUND_EVENT, // The sound that will play
tutorial/sounds.1581450411.txt.gz · Last modified: 2020/02/11 19:46 by earthcomputer