| 1 | package net.minecraft.world.item.enchantment.effects;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.util.ExtraCodecs;
|
| 10 | import net.minecraft.util.Mth;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.util.valueproviders.FloatProvider;
|
| 13 | import net.minecraft.util.valueproviders.FloatProviders;
|
| 14 | import net.minecraft.world.entity.Entity;
|
| 15 | import net.minecraft.world.item.enchantment.EnchantedItemInUse;
|
| 16 | import net.minecraft.world.phys.Vec3;
|
| 17 |
|
| 18 | public record PlaySoundEffect(List<Holder<SoundEvent>> soundEvents, FloatProvider volume, FloatProvider pitch) implements EnchantmentEntityEffect {
|
| 19 | public static final MapCodec<PlaySoundEffect> CODEC = RecordCodecBuilder.mapCodec(
|
| 20 | i -> i.group(
|
| 21 | ExtraCodecs.compactListCodec(SoundEvent.CODEC, SoundEvent.CODEC.sizeLimitedListOf(255)).fieldOf("sound").forGetter(PlaySoundEffect::soundEvents),
|
| 22 | FloatProviders.codec(1.0E-5F, 10.0F).fieldOf("volume").forGetter(PlaySoundEffect::volume),
|
| 23 | FloatProviders.codec(1.0E-5F, 2.0F).fieldOf("pitch").forGetter(PlaySoundEffect::pitch)
|
| 24 | )
|
| 25 | .apply(i, PlaySoundEffect::new)
|
| 26 | );
|
| 27 |
|
| 28 | @Override
|
| 29 | public void apply(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity, final Vec3 position) {
|
| 30 | if (!entity.isSilent()) {
|
| 31 | RandomSource random = entity.getRandom();
|
| 32 | int index = Mth.clamp(enchantmentLevel - 1, 0, this.soundEvents.size() - 1);
|
| 33 | serverLevel.playSound(
|
| 34 | null, position.x(), position.y(), position.z(), this.soundEvents.get(index), entity.getSoundSource(), this.volume.sample(random), this.pitch.sample(random)
|
| 35 | );
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public MapCodec<PlaySoundEffect> codec() {
|
| 41 | return CODEC;
|
| 42 | }
|
| 43 | }
|
| 44 |
|