PlaySoundConsumeEffect .java

net.minecraft.world.item.consume_effects.PlaySoundConsumeEffect (record, 32 lines): members: sound, apply, getType, STREAM_CODEC, CODEC. strings: "sound".

net.minecraft.world.item.consume_effectsrecord32 linescommon1.3 KB

Implements

API Surface (2)

ConsumeEffect.Type<PlaySoundConsumeEffect> getType()
boolean apply(final Level level, final ItemStack stack, final LivingEntity user)

Fields & Constants (2)

CODEC
STREAM_CODEC

String literals (1)

"sound"

Full Source

PlaySoundConsumeEffect.java32 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.consume_effects;
2
3
import com.mojang.serialization.MapCodec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import net.minecraft.core.Holder;
6
import net.minecraft.network.RegistryFriendlyByteBuf;
7
import net.minecraft.network.codec.StreamCodec;
8
import net.minecraft.sounds.SoundEvent;
9
import net.minecraft.world.entity.LivingEntity;
10
import net.minecraft.world.item.ItemStack;
11
import net.minecraft.world.level.Level;
12
13
public record PlaySoundConsumeEffect(Holder<SoundEvent> sound) implements ConsumeEffect {
14
	public static final MapCodec<PlaySoundConsumeEffect> CODEC = RecordCodecBuilder.mapCodec(
15
		i -> i.group(SoundEvent.CODEC.fieldOf("sound").forGetter(PlaySoundConsumeEffect::sound)).apply(i, PlaySoundConsumeEffect::new)
16
	);
17
	public static final StreamCodec<RegistryFriendlyByteBuf, PlaySoundConsumeEffect> STREAM_CODEC = StreamCodec.composite(
18
		SoundEvent.STREAM_CODEC, PlaySoundConsumeEffect::sound, PlaySoundConsumeEffect::new
19
	);
20
21
	@Override
22
	public ConsumeEffect.Type<PlaySoundConsumeEffect> getType() {
23
		return ConsumeEffect.Type.PLAY_SOUND;
24
	}
25
26
	@Override
27
	public boolean apply(final Level level, final ItemStack stack, final LivingEntity user) {
28
		level.playSound(null, user.blockPosition(), this.sound.value(), user.getSoundSource(), 1.0F, 1.0F);
29
		return true;
30
	}
31
}
32

Decompiled Minecraft 26.3 source of PlaySoundConsumeEffect (net/minecraft/world/item/consume_effects/PlaySoundConsumeEffect.java) with Mojang official mappings, stored verbatim on the site.