Path: blob/1.21.x/src/main/java/net/minecraftforge/event/PlayLevelSoundEvent.java
7212 views
/*1* Copyright (c) Forge Development LLC and contributors2* SPDX-License-Identifier: LGPL-2.1-only3*/45package net.minecraftforge.event;67import net.minecraft.client.player.LocalPlayer;8import net.minecraft.core.Holder;9import net.minecraft.sounds.SoundEvent;10import net.minecraft.sounds.SoundSource;11import net.minecraft.world.entity.Entity;12import net.minecraft.world.level.Level;13import net.minecraft.world.phys.Vec3;14import net.minecraftforge.common.MinecraftForge;15import net.minecraftforge.eventbus.api.bus.CancellableEventBus;16import net.minecraftforge.eventbus.api.bus.EventBus;17import net.minecraftforge.eventbus.api.event.InheritableEvent;18import net.minecraftforge.eventbus.api.event.MutableEvent;19import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;20import org.jetbrains.annotations.NotNull;21import org.jetbrains.annotations.Nullable;22import java.util.Objects;2324/**25* PlayLevelSoundEvent is fired when a sound is played on a {@link Level}.26* This event is fired from {@link Level#playSound}, {@link Level#playSeededSound}, and {@link LocalPlayer#playSound}.27* <p>28* {@link #getLevel()} contains the level the sound is being played in.29* {@link #getSound()} contains the sound event to be played.30* {@link #getOriginalVolume()} contains the original volume for the sound to be played at.31* {@link #getOriginalPitch()} contains the original pitch for the sound to be played at.32* {@link #getNewVolume()} contains the volume the sound will be played at.33* {@link #getNewPitch()} contains the pitch the sound will be played at.34* <p>35* This event is {@link Cancelable cancelable}.36* If this event is canceled, the sound is not played.37* <p>38* This event does not have a result.39* <p>40* This event is fired on the {@link MinecraftForge#EVENT_BUS}.41*/42public sealed class PlayLevelSoundEvent extends MutableEvent implements Cancellable, InheritableEvent {43public static final CancellableEventBus<PlayLevelSoundEvent> BUS = CancellableEventBus.create(PlayLevelSoundEvent.class);4445private final Level level;46private final float originalVolume;47private final float originalPitch;48private Holder<SoundEvent> sound;49private SoundSource source;50private float newVolume;51private float newPitch;5253public PlayLevelSoundEvent(@NotNull Level level, @NotNull Holder<SoundEvent> sound, @NotNull SoundSource source, float volume, float pitch) {54this.level = level;55this.sound = sound;56this.source = source;57this.originalVolume = volume;58this.originalPitch = pitch;59this.newVolume = volume;60this.newPitch = pitch;61}6263/**64* {@return the level the sound is being played in}65*/66@NotNull67public Level getLevel() {68return this.level;69}7071/**72* {@return the sound event to be played}73*/74@Nullable75public Holder<SoundEvent> getSound() {76return this.sound;77}7879/**80* Sets the sound event to be played.81*/82public void setSound(@Nullable Holder<SoundEvent> sound) {83this.sound = sound;84}8586/**87* {@return the sound source}88*/89@NotNull90public SoundSource getSource() {91return this.source;92}9394/**95* Sets the sound source.96*/97public void setSource(@NotNull SoundSource source) {98Objects.requireNonNull(source, "Sound source cannot be null");99this.source = source;100}101102/**103* {@return the original volume for the sound to be played at}104*/105public float getOriginalVolume() {106return this.originalVolume;107}108109/**110* {@return the original pitch for the sound to be played at}111*/112public float getOriginalPitch() {113return this.originalPitch;114}115116/**117* {@return the volume the sound will be played at}118*/119public float getNewVolume() {120return this.newVolume;121}122123/**124* Sets the volume the sound will be played at.125*/126public void setNewVolume(float newVolume) {127this.newVolume = newVolume;128}129130/**131* {@return the pitch the sound will be played at}132*/133public float getNewPitch() {134return this.newPitch;135}136137/**138* Sets the pitch the sound will be played at.139*/140public void setNewPitch(float newPitch) {141this.newPitch = newPitch;142}143144/**145* PlayLevelSoundEvent.AtEntity is fired when a sound is played on the {@link Level} at an {@link Entity Entity}'s position.146* This event is fired from {@link Level#playSound}, {@link Level#playSeededSound}, and {@link LocalPlayer#playSound}.147* <p>148* This event is {@link Cancelable cancelable}.149* If this event is canceled, the sound is not played.150* <p>151* This event does not have a result.152* <p>153* This event is fired on the {@link MinecraftForge#EVENT_BUS}.154*/155public static final class AtEntity extends PlayLevelSoundEvent {156public static final EventBus<AtEntity> BUS = EventBus.create(AtEntity.class);157158private final Entity entity;159160public AtEntity(Level level, @Nullable Entity entity, Holder<SoundEvent> sound, SoundSource source, float volume, float pitch) {161super(level, sound, source, volume, pitch);162this.entity = entity;163}164165/**166* {@return the entity the sound is being played on}167*/168public Entity getEntity() {169return this.entity;170}171}172173/**174* PlayLevelSoundEvent.AtPosition is fired when a sound is played on the {@link Level} at a specific position.175* This event is fired from {@link Level#playSound} and {@link Level#playSeededSound}.176* <p>177* This event is {@link Cancelable cancelable}.178* If this event is canceled, the sound is not played.179* <p>180* This event does not have a result.181* <p>182* This event is fired on the {@link MinecraftForge#EVENT_BUS}.183*/184public static final class AtPosition extends PlayLevelSoundEvent {185public static final EventBus<AtPosition> BUS = EventBus.create(AtPosition.class);186187private final Vec3 position;188189public AtPosition(Level level, Vec3 position, Holder<SoundEvent> sound, SoundSource source, float volume, float pitch) {190super(level, sound, source, volume, pitch);191this.position = position;192}193194/**195* {@return the position the sound is being played at}196*/197public Vec3 getPosition() {198return this.position;199}200}201}202203204