| 1 | package net.minecraft.client.sounds;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.resources.sounds.SimpleSoundInstance;
|
| 6 | import net.minecraft.client.resources.sounds.SoundInstance;
|
| 7 | import net.minecraft.sounds.SoundEvent;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.sounds.SoundSource;
|
| 10 | import net.minecraft.world.entity.animal.cow.CowSoundVariants.SoundSet;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public final class SoundPreviewHandler {
|
| 15 | private static @Nullable SoundInstance activePreview;
|
| 16 | private static @Nullable SoundSource previousCategory;
|
| 17 |
|
| 18 | public static void preview(final SoundManager soundManager, final SoundSource category, final float volume) {
|
| 19 | stopOtherCategoryPreview(soundManager, category);
|
| 20 | if (canPlaySound(soundManager)) {
|
| 21 | SoundEvent previewSound = switch (category) {
|
| 22 | case RECORDS -> (SoundEvent)SoundEvents.NOTE_BLOCK_GUITAR.value();
|
| 23 | case WEATHER -> SoundEvents.LIGHTNING_BOLT_THUNDER;
|
| 24 | case BLOCKS -> SoundEvents.GRASS_PLACE;
|
| 25 | case HOSTILE -> SoundEvents.ZOMBIE_AMBIENT;
|
| 26 | case NEUTRAL -> (SoundEvent)SoundEvents.COW_SOUNDS.get(SoundSet.CLASSIC).ambientSound().value();
|
| 27 | case PLAYERS -> (SoundEvent)SoundEvents.GENERIC_EAT.value();
|
| 28 | case AMBIENT -> (SoundEvent)SoundEvents.AMBIENT_CAVE.value();
|
| 29 | case UI -> (SoundEvent)SoundEvents.UI_BUTTON_CLICK.value();
|
| 30 | default -> SoundEvents.EMPTY;
|
| 31 | };
|
| 32 | if (previewSound != SoundEvents.EMPTY) {
|
| 33 | activePreview = SimpleSoundInstance.forUI(previewSound, 1.0F, volume);
|
| 34 | soundManager.play(activePreview);
|
| 35 | }
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | private static void stopOtherCategoryPreview(final SoundManager soundManager, final SoundSource category) {
|
| 40 | if (previousCategory != category) {
|
| 41 | previousCategory = category;
|
| 42 | if (activePreview != null) {
|
| 43 | soundManager.stop(activePreview);
|
| 44 | }
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | private static boolean canPlaySound(final SoundManager soundManager) {
|
| 49 | return activePreview == null || !soundManager.isActive(activePreview);
|
| 50 | }
|
| 51 | }
|
| 52 |
|