How to make an item that plays a sound in a Minecraft mod
level.playSound(null, pos, sound, source, volume, pitch) plays a sound for everyone nearby. Pass null as the first argument so the player who caused it hears it too. Run it on the server; the game sends the sound to clients. The sounds are SoundEvents fields. For your own .ogg file, see the custom sound how-to.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Override use in an Item subclass.
- Call level.playSound on the server with a SoundEvents field.
- Choose volume and pitch; a pitch below 1 sounds deeper.
- Right-click and listen.
What you can change
Section titled “What you can change”The example uses these values. Change them in the code.
| Setting | In the example | What it does |
|---|---|---|
| sound | BELL_BLOCK |
Which vanilla sound. |
| pitch | 1 |
Pitch, 0.5 to 2. |
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Fabric 1.16.5 to 1.20.3, Forge 1.17.1 to 1.20.1
Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Fabric 1.16.5 to 1.20.3, Forge 1.17.1 to 1.20.1”Uses:
Level.playSoundSoundEventsSoundSource
src/main/java/com/example/examplemod/item/BellStickItem.java
package com.example.examplemod.item;
import net.minecraft.sounds.SoundEvents;import net.minecraft.sounds.SoundSource;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Bell Stick: right-click to play BELL_BLOCK at pitch 1.0F.public class BellStickItem extends Item { public BellStickItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { level.playSound(null, player.blockPosition(), SoundEvents.BELL_BLOCK, SoundSource.PLAYERS, 1.0F, 1.0F); return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()); }}It’s registered like any other item, as BellStickItem: see a new item.
Fabric and NeoForge 1.21.2 to 26.3
Section titled “Fabric and NeoForge 1.21.2 to 26.3”Uses:
Level.playSoundSoundEventsSoundSource
src/main/java/com/example/examplemod/item/BellStickItem.java
package com.example.examplemod.item;
import net.minecraft.sounds.SoundEvents;import net.minecraft.sounds.SoundSource;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Bell Stick: right-click to play BELL_BLOCK at pitch 1.0F.public class BellStickItem extends Item { public BellStickItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult use(Level level, Player player, InteractionHand hand) { level.playSound(null, player.blockPosition(), SoundEvents.BELL_BLOCK, SoundSource.PLAYERS, 1.0F, 1.0F); return InteractionResult.SUCCESS; }}It’s registered like any other item, as BellStickItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
World.playSoundSoundEventsSoundCategory
src/main/java/com/example/examplemod/item/BellStickItem.java
package com.example.examplemod.item;
import net.minecraft.util.SoundEvents;import net.minecraft.util.SoundCategory;import net.minecraft.util.Hand;import net.minecraft.util.ActionResult;import net.minecraft.entity.player.PlayerEntity;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.world.World;
// Bell Stick: right-click to play BELL_BLOCK at pitch 1.0F.public class BellStickItem extends Item { public BellStickItem(Item.Properties properties) { super(properties); }
@Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { world.playSound(null, player.blockPosition(), SoundEvents.BELL_BLOCK, SoundCategory.PLAYERS, 1.0F, 1.0F); return ActionResult.sidedSuccess(player.getItemInHand(hand), world.isClientSide()); }}It’s registered like any other item, as BellStickItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
World.playSoundSoundEventsSoundCategory
src/main/java/com/example/examplemod/item/BellStickItem.java
package com.example.examplemod.item;
import net.minecraft.entity.player.EntityPlayer;import net.minecraft.init.SoundEvents;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.util.ActionResult;import net.minecraft.util.EnumActionResult;import net.minecraft.util.EnumHand;import net.minecraft.util.SoundCategory;import net.minecraft.util.SoundEvent;import net.minecraft.world.World;
// Bell Stick: right-click to play BELL_BLOCK at pitch 1.0F.public class BellStickItem extends Item { @Override public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) { world.playSound(null, player.getPosition(), sound("BELL_BLOCK"), SoundCategory.PLAYERS, 1.0F, 1.0F); return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand)); }
// 1.12.2 names its sounds with the group first: BELL_BLOCK is BLOCK_NOTE_BELL, ENDERMAN_TELEPORT is ENTITY_ENDERMEN_TELEPORT. private static SoundEvent sound(String name) { switch (name) { case "ENDERMAN_TELEPORT": return SoundEvents.ENTITY_ENDERMEN_TELEPORT; case "EXPERIENCE_ORB_PICKUP": return SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP; case "ANVIL_USE": return SoundEvents.BLOCK_ANVIL_USE; case "CHEST_OPEN": return SoundEvents.BLOCK_CHEST_OPEN; case "PLAYER_LEVELUP": return SoundEvents.ENTITY_PLAYER_LEVELUP; case "LIGHTNING_BOLT_THUNDER": return SoundEvents.ENTITY_LIGHTNING_THUNDER; case "GENERIC_EXPLODE": return SoundEvents.ENTITY_GENERIC_EXPLODE; default: return SoundEvents.BLOCK_NOTE_BELL; } }}It’s registered like any other item, as BellStickItem: see a new item.
Sources
Section titled “Sources”- https://docs.neoforged.net/docs/1.21.1/items/interactions
- https://docs.neoforged.net/docs/1.21.1/gameeffects/sounds
- https://docs.fabricmc.net/1.21.1/develop/items/first-item
Related how-tos
Section titled “Related how-tos”- A tool that breaks a 3 by 3 area
- A wand that strikes lightning where you look
- An item that changes the block you click
- An item that explodes where you aim
- An item that fills the hunger bar
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.