2020-05-23 22:48:50 +12:00
|
|
|
package me.libraryaddict.disguise.utilities.sounds;
|
|
|
|
|
|
|
|
import lombok.Getter;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
2020-05-24 12:14:47 +12:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
2020-05-23 22:48:50 +12:00
|
|
|
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
|
2020-05-24 12:14:47 +12:00
|
|
|
import org.apache.commons.lang.math.RandomUtils;
|
2020-05-23 22:48:50 +12:00
|
|
|
import org.bukkit.Sound;
|
|
|
|
|
2020-07-05 17:02:25 +12:00
|
|
|
import java.util.Arrays;
|
2020-05-23 22:48:50 +12:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by libraryaddict on 23/05/2020.
|
|
|
|
*/
|
|
|
|
public class SoundGroup {
|
|
|
|
public enum SoundType {
|
|
|
|
CANCEL,
|
|
|
|
DEATH,
|
|
|
|
HURT,
|
|
|
|
IDLE,
|
|
|
|
STEP
|
|
|
|
}
|
|
|
|
|
|
|
|
@Getter
|
2020-05-24 13:10:48 +12:00
|
|
|
private final static LinkedHashMap<String, SoundGroup> groups = new LinkedHashMap<>();
|
2020-05-23 22:48:50 +12:00
|
|
|
private float damageSoundVolume = 1F;
|
|
|
|
@Getter
|
2020-07-05 17:02:25 +12:00
|
|
|
private final LinkedHashMap<Object, SoundType> disguiseSoundTypes = new LinkedHashMap<>();
|
|
|
|
@Getter
|
|
|
|
private final LinkedHashMap<SoundType, Object[]> disguiseSounds = new LinkedHashMap<>();
|
2020-05-24 12:14:47 +12:00
|
|
|
private boolean customSounds;
|
2020-05-23 22:48:50 +12:00
|
|
|
|
|
|
|
public SoundGroup(String name) {
|
|
|
|
groups.put(name, this);
|
2020-05-24 12:14:47 +12:00
|
|
|
|
|
|
|
try {
|
|
|
|
DisguiseType.valueOf(name);
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
customSounds = true;
|
|
|
|
}
|
2020-05-23 22:48:50 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addSound(Object sound, SoundType type) {
|
2020-05-24 01:44:19 +12:00
|
|
|
if (sound instanceof Sound) {
|
|
|
|
sound = ReflectionManager.getCraftSound((Sound) sound);
|
|
|
|
} else if (sound instanceof String) {
|
|
|
|
sound = ReflectionManager.createSoundEffect((String) sound);
|
|
|
|
} else if (!sound.getClass().getName().equals("SoundEffect")) {
|
|
|
|
throw new IllegalArgumentException();
|
2020-05-23 22:48:50 +12:00
|
|
|
}
|
|
|
|
|
2020-05-24 01:45:13 +12:00
|
|
|
if (sound == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-05 17:02:25 +12:00
|
|
|
disguiseSoundTypes.putIfAbsent(sound, type);
|
|
|
|
|
|
|
|
if (disguiseSounds.containsKey(type)) {
|
|
|
|
Object[] array = disguiseSounds.get(type);
|
|
|
|
|
|
|
|
array = Arrays.copyOf(array, array.length + 1);
|
|
|
|
array[array.length - 1] = sound;
|
|
|
|
|
|
|
|
disguiseSounds.put(type, array);
|
|
|
|
} else {
|
|
|
|
disguiseSounds.put(type, new Object[]{sound});
|
|
|
|
}
|
2020-05-23 22:48:50 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public float getDamageAndIdleSoundVolume() {
|
|
|
|
return damageSoundVolume;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDamageAndIdleSoundVolume(float strength) {
|
|
|
|
this.damageSoundVolume = strength;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object getSound(SoundType type) {
|
|
|
|
if (type == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-24 12:14:47 +12:00
|
|
|
if (customSounds) {
|
|
|
|
return getRandomSound(type);
|
|
|
|
}
|
|
|
|
|
2020-07-05 17:02:25 +12:00
|
|
|
Object[] sounds = disguiseSounds.get(type);
|
2020-05-23 22:48:50 +12:00
|
|
|
|
2020-07-05 17:02:25 +12:00
|
|
|
if (sounds == null) {
|
|
|
|
return null;
|
2020-05-23 22:48:50 +12:00
|
|
|
}
|
|
|
|
|
2020-07-05 17:02:25 +12:00
|
|
|
return sounds[0];
|
2020-05-23 22:48:50 +12:00
|
|
|
}
|
|
|
|
|
2020-05-24 12:14:47 +12:00
|
|
|
private Object getRandomSound(SoundType type) {
|
|
|
|
if (type == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-05 17:02:25 +12:00
|
|
|
Object[] sounds = disguiseSounds.get(type);
|
2020-05-24 12:14:47 +12:00
|
|
|
|
2020-07-05 17:02:25 +12:00
|
|
|
if (sounds == null) {
|
2020-05-24 12:14:47 +12:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-05 17:02:25 +12:00
|
|
|
return sounds[RandomUtils.nextInt(sounds.length)];
|
2020-05-24 12:14:47 +12:00
|
|
|
}
|
|
|
|
|
2020-05-23 22:48:50 +12:00
|
|
|
public SoundType getSound(Object sound) {
|
|
|
|
if (sound == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-05 17:02:25 +12:00
|
|
|
return disguiseSoundTypes.get(sound);
|
2020-05-23 22:48:50 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to check if this sound name is owned by this disguise sound.
|
|
|
|
*/
|
|
|
|
public SoundType getType(Object sound, boolean ignoreDamage) {
|
|
|
|
if (sound == null) {
|
|
|
|
return SoundType.CANCEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SoundType soundType = getSound(sound);
|
|
|
|
|
|
|
|
if (soundType == SoundType.DEATH || (ignoreDamage && soundType == SoundType.HURT)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return soundType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isCancelSound(String sound) {
|
|
|
|
return getSound(sound) == SoundType.CANCEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SoundGroup getGroup(Disguise disguise) {
|
|
|
|
if (disguise.getSoundGroup() != null) {
|
2020-05-24 13:38:29 +12:00
|
|
|
SoundGroup dSoundGroup = getGroup(disguise.getSoundGroup());
|
|
|
|
|
|
|
|
if (dSoundGroup != null) {
|
|
|
|
return dSoundGroup;
|
|
|
|
}
|
2020-05-23 22:48:50 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
return getGroup(disguise.getType().name());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SoundGroup getGroup(String name) {
|
|
|
|
return groups.get(name);
|
|
|
|
}
|
|
|
|
}
|