net.minestom.server.sound.SoundEvent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of minestom-snapshots Show documentation
Show all versions of minestom-snapshots Show documentation
1.20.4 Lightweight Minecraft server
package net.minestom.server.sound;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.kyori.adventure.sound.Sound;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
/**
* Can represent a builtin/vanilla sound or a custom sound.
*/
public sealed interface SoundEvent extends ProtocolObject, Keyed, Sound.Type, SoundEvents permits BuiltinSoundEvent, CustomSoundEvent {
@NotNull NetworkBuffer.Type NETWORK_TYPE = new NetworkBuffer.Type<>() {
@Override
public void write(@NotNull NetworkBuffer buffer, SoundEvent value) {
switch (value) {
case BuiltinSoundEvent soundEvent -> buffer.write(NetworkBuffer.VAR_INT, soundEvent.id() + 1);
case CustomSoundEvent soundEvent -> {
buffer.write(NetworkBuffer.VAR_INT, 0); // Custom sound
buffer.write(NetworkBuffer.STRING, soundEvent.name());
buffer.writeOptional(NetworkBuffer.FLOAT, soundEvent.range());
}
}
}
@Override
public SoundEvent read(@NotNull NetworkBuffer buffer) {
int id = buffer.read(NetworkBuffer.VAR_INT) - 1;
if (id != -1) return BuiltinSoundEvent.getId(id);
NamespaceID namespace = NamespaceID.from(buffer.read(NetworkBuffer.STRING));
return new CustomSoundEvent(namespace, buffer.readOptional(NetworkBuffer.FLOAT));
}
};
/**
* Get all the builtin sound events. Resource pack sounds will never be returned from this method.
*/
static @NotNull Collection extends SoundEvent> values() {
return BuiltinSoundEvent.values();
}
/**
* Get a builtin sound event by its namespace ID. Will never return a custom/resource pack sound.
*
* @param namespaceID the namespace ID of the sound event
* @return the sound event, or null if not found
*/
static @Nullable SoundEvent fromNamespaceId(@NotNull String namespaceID) {
return BuiltinSoundEvent.getSafe(namespaceID);
}
/**
* Get a builtin sound event by its namespace ID. Will never return a custom/resource pack sound.
*
* @param namespaceID the namespace ID of the sound event
* @return the sound event, or null if not found
*/
static @Nullable SoundEvent fromNamespaceId(@NotNull NamespaceID namespaceID) {
return fromNamespaceId(namespaceID.asString());
}
/**
* Get a builtin sound event by its protocol ID. Will never return a custom/resource pack sound.
*
* @param id the ID of the sound event
* @return the sound event, or null if not found
*/
static @Nullable SoundEvent fromId(int id) {
return BuiltinSoundEvent.getId(id);
}
/**
* Create a custom sound event. The namespace should match a sound provided in the resource pack.
*
* @param namespaceID the namespace ID of the custom sound event
* @param range the range of the sound event, or null for (legacy) dynamic range
* @return the custom sound event
*/
static @NotNull SoundEvent of(@NotNull String namespaceID, @Nullable Float range) {
return new CustomSoundEvent(NamespaceID.from(namespaceID), range);
}
/**
* Create a custom sound event. The {@link NamespaceID} should match a sound provided in the resource pack.
* @param namespaceID the namespace ID of the custom sound event
* @param range the range of the sound event, or null for (legacy) dynamic range
* @return the custom sound event
*/
static @NotNull SoundEvent of(@NotNull NamespaceID namespaceID, @Nullable Float range) {
return new CustomSoundEvent(namespaceID, range);
}
@Contract(pure = true)
@NotNull NamespaceID namespace();
@Contract(pure = true)
default @NotNull String name() {
return namespace().asString();
}
@Override
@Contract(pure = true)
default @NotNull Key key() {
return namespace();
}
}