diff --git a/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundGroup.java b/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundGroup.java index 84e30e82..f2b003a3 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundGroup.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundGroup.java @@ -2,12 +2,15 @@ package me.libraryaddict.disguise.utilities.sounds; import lombok.Getter; import me.libraryaddict.disguise.disguisetypes.Disguise; +import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; +import org.apache.commons.lang.math.RandomUtils; import org.bukkit.Sound; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Random; /** * Created by libraryaddict on 23/05/2020. @@ -26,9 +29,17 @@ public class SoundGroup { private float damageSoundVolume = 1F; @Getter private final LinkedHashMap disguiseSounds = new LinkedHashMap<>(); + private boolean customSounds; public SoundGroup(String name) { groups.put(name, this); + + try { + DisguiseType.valueOf(name); + } + catch (Exception ex) { + customSounds = true; + } } public void addSound(Object sound, SoundType type) { @@ -60,6 +71,10 @@ public class SoundGroup { return null; } + if (customSounds) { + return getRandomSound(type); + } + for (Map.Entry entry : disguiseSounds.entrySet()) { if (entry.getValue() != type) { continue; @@ -71,6 +86,29 @@ public class SoundGroup { return null; } + private Object getRandomSound(SoundType type) { + if (type == null) { + return null; + } + + Object[] sounds = new Object[disguiseSounds.size()]; + int i = 0; + + for (Map.Entry entry : disguiseSounds.entrySet()) { + if (entry.getValue() != type) { + continue; + } + + sounds[i++] = entry.getKey(); + } + + if (i == 0) { + return null; + } + + return sounds[RandomUtils.nextInt(i)]; + } + public SoundType getSound(Object sound) { if (sound == null) { return null;