| 1 | package net.minecraft.world.level.block.sounds;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.sounds.SoundEvents;
|
| 6 | import net.minecraft.sounds.SoundSource;
|
| 7 | import net.minecraft.tags.BlockTags;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.level.Level;
|
| 10 | import net.minecraft.world.level.block.Blocks;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 13 |
|
| 14 | public class AmbientDesertBlockSoundsPlayer {
|
| 15 | private static final int IDLE_SOUND_CHANCE = 2100;
|
| 16 | private static final int DRY_GRASS_SOUND_CHANCE = 200;
|
| 17 | private static final int DEAD_BUSH_SOUND_CHANCE = 130;
|
| 18 | private static final int DEAD_BUSH_SOUND_BADLANDS_DECREASED_CHANCE = 3;
|
| 19 | private static final int SURROUNDING_BLOCKS_PLAY_SOUND_THRESHOLD = 3;
|
| 20 | private static final int SURROUNDING_BLOCKS_DISTANCE_HORIZONTAL_CHECK = 8;
|
| 21 | private static final int SURROUNDING_BLOCKS_DISTANCE_VERTICAL_CHECK = 5;
|
| 22 | private static final int HORIZONTAL_DIRECTIONS = 4;
|
| 23 |
|
| 24 | public static void playAmbientSandSounds(final Level level, final BlockPos pos, final RandomSource random) {
|
| 25 | if (level.getBlockState(pos.above()).is(Blocks.AIR)) {
|
| 26 | if (random.nextInt(2100) == 0 && shouldPlayAmbientSandSound(level, pos)) {
|
| 27 | level.playLocalSound(pos.getX(), pos.getY(), pos.getZ(), SoundEvents.SAND_IDLE, SoundSource.AMBIENT, 1.0F, 1.0F, false);
|
| 28 | }
|
| 29 | }
|
| 30 | }
|
| 31 |
|
| 32 | public static void playAmbientDryGrassSounds(final Level level, final BlockPos pos, final RandomSource random) {
|
| 33 | if (random.nextInt(200) == 0 && shouldPlayDesertDryVegetationBlockSounds(level, pos.below())) {
|
| 34 | level.playPlayerSound(SoundEvents.DRY_GRASS, SoundSource.AMBIENT, 1.0F, 1.0F);
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | public static void playAmbientDeadBushSounds(final Level level, final BlockPos pos, final RandomSource random) {
|
| 39 | if (random.nextInt(130) == 0) {
|
| 40 | BlockState belowPos = level.getBlockState(pos.below());
|
| 41 | if ((belowPos.is(Blocks.RED_SAND) || belowPos.is(BlockTags.TERRACOTTA)) && random.nextInt(3) != 0) {
|
| 42 | return;
|
| 43 | }
|
| 44 |
|
| 45 | if (shouldPlayDesertDryVegetationBlockSounds(level, pos.below())) {
|
| 46 | level.playLocalSound(pos.getX(), pos.getY(), pos.getZ(), SoundEvents.DEAD_BUSH_IDLE, SoundSource.AMBIENT, 1.0F, 1.0F, false);
|
| 47 | }
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | public static boolean shouldPlayDesertDryVegetationBlockSounds(final Level level, final BlockPos belowPos) {
|
| 52 | return level.getBlockState(belowPos).is(BlockTags.TRIGGERS_AMBIENT_DESERT_DRY_VEGETATION_BLOCK_SOUNDS)
|
| 53 | && level.getBlockState(belowPos.below()).is(BlockTags.TRIGGERS_AMBIENT_DESERT_DRY_VEGETATION_BLOCK_SOUNDS);
|
| 54 | }
|
| 55 |
|
| 56 | private static boolean shouldPlayAmbientSandSound(final Level level, final BlockPos pos) {
|
| 57 | int matchingBlocksFound = 0;
|
| 58 | int sidesChecked = 0;
|
| 59 | BlockPos.MutableBlockPos mutablePos = pos.mutable();
|
| 60 |
|
| 61 | for (Direction dir : Direction.Plane.HORIZONTAL) {
|
| 62 | mutablePos.set(pos).move(dir, 8);
|
| 63 | if (columnContainsTriggeringBlock(level, mutablePos) && matchingBlocksFound++ >= 3) {
|
| 64 | return true;
|
| 65 | }
|
| 66 |
|
| 67 | sidesChecked++;
|
| 68 | int remainingSides = 4 - sidesChecked;
|
| 69 | int potentialMatches = remainingSides + matchingBlocksFound;
|
| 70 | boolean canStillFindRequiredSoundTriggerBlocks = potentialMatches >= 3;
|
| 71 | if (!canStillFindRequiredSoundTriggerBlocks) {
|
| 72 | return false;
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | return false;
|
| 77 | }
|
| 78 |
|
| 79 | private static boolean columnContainsTriggeringBlock(final Level level, final BlockPos.MutableBlockPos mutablePos) {
|
| 80 | int surfaceY = level.getHeight(Heightmap.Types.WORLD_SURFACE, mutablePos) - 1;
|
| 81 | if (Math.abs(surfaceY - mutablePos.getY()) > 5) {
|
| 82 | mutablePos.move(Direction.UP, 6);
|
| 83 | BlockState aboveBlockState = level.getBlockState(mutablePos);
|
| 84 | mutablePos.move(Direction.DOWN);
|
| 85 |
|
| 86 | for (int i = 0; i < 10; i++) {
|
| 87 | BlockState currentBlockState = level.getBlockState(mutablePos);
|
| 88 | if (aboveBlockState.isAir() && canTriggerAmbientDesertSandSounds(currentBlockState)) {
|
| 89 | return true;
|
| 90 | }
|
| 91 |
|
| 92 | aboveBlockState = currentBlockState;
|
| 93 | mutablePos.move(Direction.DOWN);
|
| 94 | }
|
| 95 |
|
| 96 | return false;
|
| 97 | } else {
|
| 98 | boolean hasAirAbove = level.getBlockState(mutablePos.setY(surfaceY + 1)).isAir();
|
| 99 | return hasAirAbove && canTriggerAmbientDesertSandSounds(level.getBlockState(mutablePos.setY(surfaceY)));
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | private static boolean canTriggerAmbientDesertSandSounds(final BlockState blockState) {
|
| 104 | return blockState.is(BlockTags.TRIGGERS_AMBIENT_DESERT_SAND_BLOCK_SOUNDS);
|
| 105 | }
|
| 106 | }
|
| 107 |
|