| 1 | package net.minecraft.sounds;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import io.netty.buffer.ByteBuf;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.registries.Registries;
|
| 9 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 10 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 11 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 12 | import net.minecraft.network.codec.StreamCodec;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 |
|
| 15 | /**
|
| 16 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 17 | */
|
| 18 | public record SoundEvent(Identifier location, Optional<Float> fixedRange) {
|
| 19 | public static final Codec<SoundEvent> DIRECT_CODEC = RecordCodecBuilder.create(
|
| 20 | i -> i.group(
|
| 21 | Identifier.CODEC.fieldOf("sound_id").forGetter(SoundEvent::location), Codec.FLOAT.lenientOptionalFieldOf("range").forGetter(SoundEvent::fixedRange)
|
| 22 | )
|
| 23 | .apply(i, SoundEvent::create)
|
| 24 | );
|
| 25 | public static final Codec<Holder<SoundEvent>> CODEC = RegistryCodecs.holder(Registries.SOUND_EVENT, DIRECT_CODEC);
|
| 26 | public static final StreamCodec<ByteBuf, SoundEvent> DIRECT_STREAM_CODEC = StreamCodec.composite(
|
| 27 | Identifier.STREAM_CODEC, SoundEvent::location, ByteBufCodecs.FLOAT.apply(ByteBufCodecs::optional), SoundEvent::fixedRange, SoundEvent::create
|
| 28 | );
|
| 29 | public static final StreamCodec<RegistryFriendlyByteBuf, Holder<SoundEvent>> STREAM_CODEC = ByteBufCodecs.holder(Registries.SOUND_EVENT, DIRECT_STREAM_CODEC);
|
| 30 |
|
| 31 | private static SoundEvent create(final Identifier location, final Optional<Float> range) {
|
| 32 | return range.<SoundEvent>map(r -> createFixedRangeEvent(location, r)).orElseGet(() -> createVariableRangeEvent(location));
|
| 33 | }
|
| 34 |
|
| 35 | /**
|
| 36 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 37 | */
|
| 38 | public static SoundEvent createVariableRangeEvent(final Identifier location) {
|
| 39 | return new SoundEvent(location, Optional.empty());
|
| 40 | }
|
| 41 |
|
| 42 | /**
|
| 43 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 44 | */
|
| 45 | public static SoundEvent createFixedRangeEvent(final Identifier location, final float range) {
|
| 46 | return new SoundEvent(location, Optional.of(range));
|
| 47 | }
|
| 48 |
|
| 49 | public float getRange(final float volume) {
|
| 50 | return this.fixedRange.orElse(volume > 1.0F ? 16.0F * volume : 16.0F);
|
| 51 | }
|
| 52 | }
|
| 53 |
|