Skip to content

Commit

Permalink
Added cross-version sound fix
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Dec 23, 2024
1 parent f45b510 commit ce334cb
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions eco-api/src/main/java/com/willfp/eco/util/SoundUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.willfp.eco.util;

import com.willfp.eco.core.Prerequisite;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Sound;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Field;

/**
* Utilities / API methods for sounds.
*/
public final class SoundUtils {
/**
* Get a sound in a version-compatible way.
*
* @param name The name of the sound, case-insensitive.
* @return The sound, or null if not found.
*/
@Nullable
public static Sound getSound(@NotNull final String name) {
if (!Prerequisite.HAS_1_21_3.isMet()) {
try {
return Sound.valueOf(name.toUpperCase());
} catch (IllegalArgumentException e) {
return null;
}
}

// First try from registry (preferred)
Sound fromRegistry = Registry.SOUNDS.get(NamespacedKey.minecraft(name.toLowerCase()));
if (fromRegistry != null) {
return fromRegistry;
}

// Next try using reflection (for legacy enum names)
try {
Field field = Sound.class.getDeclaredField(name.toUpperCase());
return (Sound) field.get(null);
} catch (ReflectiveOperationException e) {
return null;
}
}

private SoundUtils() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
}

0 comments on commit ce334cb

Please sign in to comment.