Class SFX
This class provides:
- Convenience methods for playing a sound given a
Stringidentifier for aPlayeror at aLocation. - Support for namespaced sound keys (e.g.
"minecraft:block.stone.place") via the String-based Bukkit sound API. - Utilities to derive the correct placement/break sound for a block/material using
BlockData.getSoundGroup().
Identifier formats
- Enum style:
"BLOCK_STONE_PLACE"(resolved toSoundthroughGetSound(String)). - Namespaced:
"minecraft:block.stone.place"(detected by the presence of a'.'and played viaPlayNamespace(String, float, float, Player)orPlayNamespace(String, float, float, Location)).
Server version notes
On 1.17/1.17.1 this class plays enum sounds using the legacy overload
Player.playSound(Location, Sound, float, float). On later versions it uses
Player.playSound(Player, Sound, float, float).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.bukkit.SoundResolves a string sound name to aSoundfromRegistry.SOUNDS.static StringgetSoundBreak(org.bukkit.block.Block block) Returns a break sound identifier for the given block.static StringgetSoundBreak(org.bukkit.Material material) Returns a break sound identifier for the given material.static StringgetSoundPlace(org.bukkit.block.Block block) Returns a placement sound identifier for the given block.static StringgetSoundPlace(org.bukkit.Material material) Returns a placement sound identifier for the given material.static voidPlays a sound for a player.static voidPlays a sound at a location.static voidPlayNamespace(String sound, float volume, float pitch, org.bukkit.entity.Player player) Plays a namespaced/string sound for a player in theSoundCategory.MASTERcategory.static voidPlayNamespace(String sound, float volume, float pitch, org.bukkit.Location location) Plays a namespaced/string sound at a location in theSoundCategory.MASTERcategory.
-
Constructor Details
-
SFX
public SFX()
-
-
Method Details
-
Play
Plays a sound for a player.Behavior:
- If
soundisnullor empty, nothing is played. - For server versions 1.17/1.17.1: plays the resolved
Soundat the player's location. - For later versions: if
soundcontains a dot, treats it as a namespaced key and callsPlayNamespace(String, float, float, Player); otherwise resolves toSoundviaGetSound(String)and plays it to the player.
- Parameters:
sound- sound identifier (enum style like"BLOCK_NOTE_BLOCK_PLING", or a namespaced key like"minecraft:entity.player.levelup")volume- volume multiplierpitch- pitch multiplierplayer- target player
- If
-
PlayNamespace
public static void PlayNamespace(String sound, float volume, float pitch, org.bukkit.entity.Player player) Plays a namespaced/string sound for a player in theSoundCategory.MASTERcategory.- Parameters:
sound- namespaced sound key (e.g."minecraft:block.stone.place")volume- volume multiplierpitch- pitch multiplierplayer- target player
-
Play
Plays a sound at a location.If
soundcontains a dot, it is treated as a namespaced key and this method delegates toPlayNamespace(String, float, float, Location). Otherwisesoundis resolved to aSoundviaGetSound(String)and played in the location's world.- Parameters:
sound- sound identifier (enum style or namespaced)volume- volume multiplierpitch- pitch multiplierlocation- target location
-
PlayNamespace
public static void PlayNamespace(String sound, float volume, float pitch, org.bukkit.Location location) Plays a namespaced/string sound at a location in theSoundCategory.MASTERcategory.- Parameters:
sound- namespaced sound key (e.g."minecraft:block.stone.place")volume- volume multiplierpitch- pitch multiplierlocation- target location (must have a non-null world)
-
GetSound
Resolves a string sound name to aSoundfromRegistry.SOUNDS.Matching is performed using
String.equalsIgnoreCase(String)againstSound#toString().- Parameters:
sound- enum-like sound name (typically uppercase), e.g."BLOCK_STONE_PLACE"- Returns:
- the resolved
Sound, ornullwhen not found
-
getSoundPlace
Returns a placement sound identifier for the given block.Uses
BlockData.getSoundGroup()and reflectively invokesSoundGroup#getPlaceSound()to remain compatible with server implementations where the concrete return type may change. If a sound cannot be determined, a safe default is returned.- Parameters:
block- block to inspect- Returns:
- a sound identifier (usually enum-style); never
null
-
getSoundPlace
Returns a placement sound identifier for the given material.- Parameters:
material- material to inspect- Returns:
- a sound identifier;
nullifmaterialisnullor not a block
-
getSoundBreak
Returns a break sound identifier for the given block.- Parameters:
block- block to inspect- Returns:
- a sound identifier (usually enum-style); never
null
-
getSoundBreak
Returns a break sound identifier for the given material.- Parameters:
material- material to inspect- Returns:
- a sound identifier;
nullifmaterialisnullor not a block
-