Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MinecraftForge
GitHub Repository: MinecraftForge/MinecraftForge
Path: blob/1.21.x/src/main/java/net/minecraftforge/event/PlayLevelSoundEvent.java
7212 views
1
/*
2
* Copyright (c) Forge Development LLC and contributors
3
* SPDX-License-Identifier: LGPL-2.1-only
4
*/
5
6
package net.minecraftforge.event;
7
8
import net.minecraft.client.player.LocalPlayer;
9
import net.minecraft.core.Holder;
10
import net.minecraft.sounds.SoundEvent;
11
import net.minecraft.sounds.SoundSource;
12
import net.minecraft.world.entity.Entity;
13
import net.minecraft.world.level.Level;
14
import net.minecraft.world.phys.Vec3;
15
import net.minecraftforge.common.MinecraftForge;
16
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
17
import net.minecraftforge.eventbus.api.bus.EventBus;
18
import net.minecraftforge.eventbus.api.event.InheritableEvent;
19
import net.minecraftforge.eventbus.api.event.MutableEvent;
20
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
21
import org.jetbrains.annotations.NotNull;
22
import org.jetbrains.annotations.Nullable;
23
import java.util.Objects;
24
25
/**
26
* PlayLevelSoundEvent is fired when a sound is played on a {@link Level}.
27
* This event is fired from {@link Level#playSound}, {@link Level#playSeededSound}, and {@link LocalPlayer#playSound}.
28
* <p>
29
* {@link #getLevel()} contains the level the sound is being played in.
30
* {@link #getSound()} contains the sound event to be played.
31
* {@link #getOriginalVolume()} contains the original volume for the sound to be played at.
32
* {@link #getOriginalPitch()} contains the original pitch for the sound to be played at.
33
* {@link #getNewVolume()} contains the volume the sound will be played at.
34
* {@link #getNewPitch()} contains the pitch the sound will be played at.
35
* <p>
36
* This event is {@link Cancelable cancelable}.
37
* If this event is canceled, the sound is not played.
38
* <p>
39
* This event does not have a result.
40
* <p>
41
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.
42
*/
43
public sealed class PlayLevelSoundEvent extends MutableEvent implements Cancellable, InheritableEvent {
44
public static final CancellableEventBus<PlayLevelSoundEvent> BUS = CancellableEventBus.create(PlayLevelSoundEvent.class);
45
46
private final Level level;
47
private final float originalVolume;
48
private final float originalPitch;
49
private Holder<SoundEvent> sound;
50
private SoundSource source;
51
private float newVolume;
52
private float newPitch;
53
54
public PlayLevelSoundEvent(@NotNull Level level, @NotNull Holder<SoundEvent> sound, @NotNull SoundSource source, float volume, float pitch) {
55
this.level = level;
56
this.sound = sound;
57
this.source = source;
58
this.originalVolume = volume;
59
this.originalPitch = pitch;
60
this.newVolume = volume;
61
this.newPitch = pitch;
62
}
63
64
/**
65
* {@return the level the sound is being played in}
66
*/
67
@NotNull
68
public Level getLevel() {
69
return this.level;
70
}
71
72
/**
73
* {@return the sound event to be played}
74
*/
75
@Nullable
76
public Holder<SoundEvent> getSound() {
77
return this.sound;
78
}
79
80
/**
81
* Sets the sound event to be played.
82
*/
83
public void setSound(@Nullable Holder<SoundEvent> sound) {
84
this.sound = sound;
85
}
86
87
/**
88
* {@return the sound source}
89
*/
90
@NotNull
91
public SoundSource getSource() {
92
return this.source;
93
}
94
95
/**
96
* Sets the sound source.
97
*/
98
public void setSource(@NotNull SoundSource source) {
99
Objects.requireNonNull(source, "Sound source cannot be null");
100
this.source = source;
101
}
102
103
/**
104
* {@return the original volume for the sound to be played at}
105
*/
106
public float getOriginalVolume() {
107
return this.originalVolume;
108
}
109
110
/**
111
* {@return the original pitch for the sound to be played at}
112
*/
113
public float getOriginalPitch() {
114
return this.originalPitch;
115
}
116
117
/**
118
* {@return the volume the sound will be played at}
119
*/
120
public float getNewVolume() {
121
return this.newVolume;
122
}
123
124
/**
125
* Sets the volume the sound will be played at.
126
*/
127
public void setNewVolume(float newVolume) {
128
this.newVolume = newVolume;
129
}
130
131
/**
132
* {@return the pitch the sound will be played at}
133
*/
134
public float getNewPitch() {
135
return this.newPitch;
136
}
137
138
/**
139
* Sets the pitch the sound will be played at.
140
*/
141
public void setNewPitch(float newPitch) {
142
this.newPitch = newPitch;
143
}
144
145
/**
146
* PlayLevelSoundEvent.AtEntity is fired when a sound is played on the {@link Level} at an {@link Entity Entity}'s position.
147
* This event is fired from {@link Level#playSound}, {@link Level#playSeededSound}, and {@link LocalPlayer#playSound}.
148
* <p>
149
* This event is {@link Cancelable cancelable}.
150
* If this event is canceled, the sound is not played.
151
* <p>
152
* This event does not have a result.
153
* <p>
154
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.
155
*/
156
public static final class AtEntity extends PlayLevelSoundEvent {
157
public static final EventBus<AtEntity> BUS = EventBus.create(AtEntity.class);
158
159
private final Entity entity;
160
161
public AtEntity(Level level, @Nullable Entity entity, Holder<SoundEvent> sound, SoundSource source, float volume, float pitch) {
162
super(level, sound, source, volume, pitch);
163
this.entity = entity;
164
}
165
166
/**
167
* {@return the entity the sound is being played on}
168
*/
169
public Entity getEntity() {
170
return this.entity;
171
}
172
}
173
174
/**
175
* PlayLevelSoundEvent.AtPosition is fired when a sound is played on the {@link Level} at a specific position.
176
* This event is fired from {@link Level#playSound} and {@link Level#playSeededSound}.
177
* <p>
178
* This event is {@link Cancelable cancelable}.
179
* If this event is canceled, the sound is not played.
180
* <p>
181
* This event does not have a result.
182
* <p>
183
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.
184
*/
185
public static final class AtPosition extends PlayLevelSoundEvent {
186
public static final EventBus<AtPosition> BUS = EventBus.create(AtPosition.class);
187
188
private final Vec3 position;
189
190
public AtPosition(Level level, Vec3 position, Holder<SoundEvent> sound, SoundSource source, float volume, float pitch) {
191
super(level, sound, source, volume, pitch);
192
this.position = position;
193
}
194
195
/**
196
* {@return the position the sound is being played at}
197
*/
198
public Vec3 getPosition() {
199
return this.position;
200
}
201
}
202
}
203
204