minecraft-src/net/minecraft/world/item/consume_effects/PlaySoundConsumeEffect.java
2025-07-04 03:45:38 +03:00

32 lines
1.3 KiB
Java

package net.minecraft.world.item.consume_effects;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.Holder;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.consume_effects.ConsumeEffect.Type;
import net.minecraft.world.level.Level;
public record PlaySoundConsumeEffect(Holder<SoundEvent> sound) implements ConsumeEffect {
public static final MapCodec<PlaySoundConsumeEffect> CODEC = RecordCodecBuilder.mapCodec(
instance -> instance.group(SoundEvent.CODEC.fieldOf("sound").forGetter(PlaySoundConsumeEffect::sound)).apply(instance, PlaySoundConsumeEffect::new)
);
public static final StreamCodec<RegistryFriendlyByteBuf, PlaySoundConsumeEffect> STREAM_CODEC = StreamCodec.composite(
SoundEvent.STREAM_CODEC, PlaySoundConsumeEffect::sound, PlaySoundConsumeEffect::new
);
@Override
public Type<PlaySoundConsumeEffect> getType() {
return Type.PLAY_SOUND;
}
@Override
public boolean apply(Level level, ItemStack stack, LivingEntity entity) {
level.playSound(null, entity.blockPosition(), this.sound.value(), entity.getSoundSource(), 1.0F, 1.0F);
return true;
}
}