Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/client/java/dev/badtz/prefixes/client/PrefixClientSounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}));
}
}
16 changes: 12 additions & 4 deletions src/main/java/dev/badtz/prefixes/PrefixApplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -118,11 +119,18 @@ public static Optional<PrefixManager.PrefixSound> 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,
Expand Down