| 1 | package net.minecraft.client.resources.sounds;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.sounds.SoundManager;
|
| 6 | import net.minecraft.client.sounds.WeighedSoundEvents;
|
| 7 | import net.minecraft.resources.Identifier;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundSource;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public abstract class AbstractSoundInstance implements SoundInstance {
|
| 15 | protected @Nullable Sound sound;
|
| 16 | protected @Nullable WeighedSoundEvents soundEvent;
|
| 17 | protected final SoundSource source;
|
| 18 | protected final Identifier identifier;
|
| 19 | protected float volume = 1.0F;
|
| 20 | protected float pitch = 1.0F;
|
| 21 | protected double x;
|
| 22 | protected double y;
|
| 23 | protected double z;
|
| 24 | protected boolean looping;
|
| 25 | protected int delay;
|
| 26 | protected SoundInstance.Attenuation attenuation = SoundInstance.Attenuation.LINEAR;
|
| 27 | protected boolean relative;
|
| 28 | protected final RandomSource random;
|
| 29 |
|
| 30 | protected AbstractSoundInstance(final SoundEvent event, final SoundSource source, final RandomSource random) {
|
| 31 | this(event.location(), source, random);
|
| 32 | }
|
| 33 |
|
| 34 | protected AbstractSoundInstance(final Identifier identifier, final SoundSource source, final RandomSource random) {
|
| 35 | this.identifier = identifier;
|
| 36 | this.source = source;
|
| 37 | this.random = random;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public Identifier getIdentifier() {
|
| 42 | return this.identifier;
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public @Nullable WeighedSoundEvents getOrResolve(final SoundManager soundManager) {
|
| 47 | if (this.identifier.equals(SoundManager.INTENTIONALLY_EMPTY_SOUND_LOCATION)) {
|
| 48 | this.sound = SoundManager.INTENTIONALLY_EMPTY_SOUND;
|
| 49 | return SoundManager.INTENTIONALLY_EMPTY_SOUND_EVENT;
|
| 50 | }
|
| 51 |
|
| 52 | this.soundEvent = soundManager.getSoundEvent(this.identifier);
|
| 53 | if (this.soundEvent == null) {
|
| 54 | this.sound = SoundManager.EMPTY_SOUND;
|
| 55 | } else {
|
| 56 | this.sound = this.soundEvent.getSound(this.random);
|
| 57 | }
|
| 58 |
|
| 59 | return this.soundEvent;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public @Nullable Sound getSound() {
|
| 64 | return this.sound;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public @Nullable WeighedSoundEvents getSoundEvent() {
|
| 69 | return this.soundEvent;
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public SoundSource getSource() {
|
| 74 | return this.source;
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public boolean isLooping() {
|
| 79 | return this.looping;
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public int getDelay() {
|
| 84 | return this.delay;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public float getVolume() {
|
| 89 | return this.volume * this.sound.getVolume().sample(this.random);
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public float getPitch() {
|
| 94 | return this.pitch * this.sound.getPitch().sample(this.random);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public double getX() {
|
| 99 | return this.x;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public double getY() {
|
| 104 | return this.y;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public double getZ() {
|
| 109 | return this.z;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public SoundInstance.Attenuation getAttenuation() {
|
| 114 | return this.attenuation;
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public boolean isRelative() {
|
| 119 | return this.relative;
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public String toString() {
|
| 124 | return "SoundInstance[" + this.identifier + "]";
|
| 125 | }
|
| 126 | }
|
| 127 |
|