Unicopia/src/main/java/com/minelittlepony/unicopia/util/SoundEmitter.java

36 lines
1.2 KiB
Java
Raw Normal View History

2022-09-26 21:13:03 +02:00
package com.minelittlepony.unicopia.util;
import com.minelittlepony.unicopia.EntityConvertable;
2022-09-26 21:13:03 +02:00
import net.minecraft.entity.Entity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
2022-09-29 22:01:56 +02:00
import net.minecraft.util.math.random.Random;
2022-09-26 21:13:03 +02:00
public interface SoundEmitter<E extends Entity> extends EntityConvertable<E> {
2022-09-26 21:13:03 +02:00
default void playSound(SoundEvent sound, float volume, float pitch) {
playSoundAt(asEntity(), sound, volume, pitch);
2022-09-26 21:13:03 +02:00
}
default void playSound(SoundEvent sound, float volume) {
playSound(sound, volume, getRandomPitch());
}
default float getRandomPitch() {
2023-06-03 13:40:54 +02:00
return getRandomPitch(asEntity().getWorld().getRandom());
2022-09-26 21:13:03 +02:00
}
static void playSoundAt(Entity entity, SoundEvent sound, float volume, float pitch) {
2022-09-26 21:13:03 +02:00
playSoundAt(entity, sound, entity.getSoundCategory(), volume, pitch);
}
static void playSoundAt(Entity entity, SoundEvent sound, SoundCategory category, float volume, float pitch) {
2023-06-03 13:40:54 +02:00
entity.getWorld().playSound(null, entity.getX(), entity.getY(), entity.getZ(), sound, category, volume, pitch);
2022-09-26 21:13:03 +02:00
}
2022-09-29 22:01:56 +02:00
static float getRandomPitch(Random rng) {
return (float)rng.nextTriangular(0.5F, 0.2F);
}
2022-09-26 21:13:03 +02:00
}