Skip to content

Commit

Permalink
feat(utils): getSoundEvent methods return Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Apr 11, 2022
1 parent d197777 commit b5be943
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/main/java/org/auioc/mcmod/arnicalib/utils/game/SoundUtils.java
@@ -1,5 +1,8 @@
package org.auioc.mcmod.arnicalib.utils.game;

import java.util.Optional;
import javax.annotation.Nonnull;
import org.auioc.mcmod.arnicalib.api.game.registry.RegistryEntryException;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
Expand All @@ -8,16 +11,28 @@

public interface SoundUtils {

static SoundEvent getSoundEvent(ResourceLocation id) {
return ForgeRegistries.SOUND_EVENTS.getValue(id);
@Nonnull
static Optional<SoundEvent> getSoundEvent(ResourceLocation id) {
return Optional.ofNullable(ForgeRegistries.SOUND_EVENTS.containsKey(id) ? ForgeRegistries.SOUND_EVENTS.getValue(id) : null);
}

static SoundEvent getSoundEvent(String id) {
@Nonnull
static Optional<SoundEvent> getSoundEvent(String id) {
return getSoundEvent(new ResourceLocation(id));
}

@Nonnull
static SoundEvent getSoundEventOrElseThrow(ResourceLocation id) {
return getSoundEvent(id).orElseThrow(RegistryEntryException.UNKNOWN_SOUND_EVENT.create(id.toString()));
}

@Nonnull
static SoundEvent getSoundEventOrElseThrow(String id) {
return getSoundEvent(id).orElseThrow(RegistryEntryException.UNKNOWN_SOUND_EVENT.create(id));
}

static void playerToPlayer(Player player, String id, SoundSource source, float volume, float pitch) {
player.playNotifySound(getSoundEvent(id), source, volume, pitch);
player.playNotifySound(getSoundEventOrElseThrow(id), source, volume, pitch);
}

static void playerToPlayer(Player player, String id) {
Expand All @@ -29,7 +44,7 @@ static void playerToPlayer(Player player, String id, float volume, float pitch)
}

static void playerToPlayer(Player player, ResourceLocation id, SoundSource source, float volume, float pitch) {
player.playNotifySound(getSoundEvent(id), source, volume, pitch);
player.playNotifySound(getSoundEventOrElseThrow(id), source, volume, pitch);
}

static void playerToPlayer(Player player, ResourceLocation id) {
Expand Down

0 comments on commit b5be943

Please sign in to comment.