diff --git a/src/client/java/dev/badtz/prefixes/client/PrefixClientSounds.java b/src/client/java/dev/badtz/prefixes/client/PrefixClientSounds.java index 0530b6e..0224ca3 100644 --- a/src/client/java/dev/badtz/prefixes/client/PrefixClientSounds.java +++ b/src/client/java/dev/badtz/prefixes/client/PrefixClientSounds.java @@ -10,14 +10,18 @@ import net.minecraft.world.item.ItemStack; public final class PrefixClientSounds { - private PrefixClientSounds() {} + private PrefixClientSounds() {} - public static void playMiningSound(Minecraft minecraft, BlockPos pos, ItemStack stack) { - PrefixApplier.getHitSound(stack) - .ifPresent(sound -> BuiltInRegistries.SOUND_EVENT.get(sound.id()) - .ifPresent(soundEvent -> minecraft.getSoundManager() - .play(new SimpleSoundInstance(soundEvent.value(), - SoundSource.PLAYERS, sound.volume(), sound.pitch(), - SoundInstance.createUnseededRandom(), pos)))); - } + public static void playMiningSound(Minecraft minecraft, BlockPos pos, ItemStack stack) { + PrefixApplier.getHitSound(stack).ifPresent(sound -> BuiltInRegistries.SOUND_EVENT + .get(sound.id()).ifPresent(soundEvent -> { + float pitch = sound.pitch() * PrefixApplier.getRandomPitch( + minecraft.level.getRandom()); + + minecraft.getSoundManager().play(new SimpleSoundInstance( + soundEvent.value(), SoundSource.PLAYERS, + sound.volume(), pitch, + SoundInstance.createUnseededRandom(), pos)); + })); + } } diff --git a/src/main/java/dev/badtz/prefixes/PrefixApplier.java b/src/main/java/dev/badtz/prefixes/PrefixApplier.java index ba97780..bb8dbe6 100644 --- a/src/main/java/dev/badtz/prefixes/PrefixApplier.java +++ b/src/main/java/dev/badtz/prefixes/PrefixApplier.java @@ -11,6 +11,7 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundSource; +import net.minecraft.util.RandomSource; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EquipmentSlotGroup; import net.minecraft.world.entity.ai.attributes.Attribute; @@ -118,11 +119,18 @@ public static Optional getHitSound(ItemStack stack) { .filter(sound -> sound != null); } + public static float getRandomPitch(RandomSource random) { + return 0.85F + random.nextFloat() * 0.30F; + } + public static void playHitSound(ServerLevel level, Entity target, ItemStack stack) { - getHitSound(stack).ifPresent(sound -> BuiltInRegistries.SOUND_EVENT.get(sound.id()) - .ifPresent(soundEvent -> level.playSound(null, target.getX(), target.getY(), - target.getZ(), soundEvent.value(), SoundSource.PLAYERS, sound.volume(), - sound.pitch()))); + getHitSound(stack).ifPresent( + sound -> BuiltInRegistries.SOUND_EVENT.get(sound.id()).ifPresent(soundEvent -> { + float pitch = sound.pitch() * getRandomPitch(level.getRandom()); + + level.playSound(null, target.getX(), target.getY(), target.getZ(), + soundEvent.value(), SoundSource.PLAYERS, sound.volume(), pitch); + })); } private static boolean hasPlayerCustomName(ItemStack stack,