LibsDisguises/src/main/java/me/libraryaddict/disguise/utilities/DisguiseSound.java

310 lines
5.9 KiB
Java
Raw Normal View History

package me.libraryaddict.disguise.utilities;
2020-02-13 22:06:49 +01:00
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Sound;
2020-02-13 22:06:49 +01:00
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
2020-02-13 22:06:49 +01:00
import java.util.List;
import java.util.Map.Entry;
2020-02-13 22:06:49 +01:00
import java.util.stream.Collectors;
/**
* Only living disguises go in here!
*/
public enum DisguiseSound {
2020-02-13 22:06:49 +01:00
ARMOR_STAND,
2020-02-13 22:06:49 +01:00
ARROW,
2020-02-13 22:06:49 +01:00
BAT,
2020-02-13 22:06:49 +01:00
BEE,
2019-12-14 01:10:11 +01:00
2020-02-13 22:06:49 +01:00
BLAZE,
2020-02-13 22:06:49 +01:00
BOAT,
2020-02-13 22:06:49 +01:00
CAVE_SPIDER,
2020-02-13 22:06:49 +01:00
CHICKEN,
2020-02-13 22:06:49 +01:00
COD,
2020-02-13 22:06:49 +01:00
COW,
2020-02-13 22:06:49 +01:00
CREEPER,
2020-02-13 22:06:49 +01:00
DOLPHIN,
2020-02-13 22:06:49 +01:00
DONKEY,
2020-02-13 22:06:49 +01:00
DROWNED,
2020-02-13 22:06:49 +01:00
ELDER_GUARDIAN,
2020-02-13 22:06:49 +01:00
ENDER_DRAGON,
2020-02-13 22:06:49 +01:00
ENDERMAN,
2020-02-13 22:06:49 +01:00
ENDERMITE,
2020-02-13 22:06:49 +01:00
EVOKER,
2020-02-13 22:06:49 +01:00
EVOKER_FANGS,
2020-02-13 22:06:49 +01:00
GHAST,
2020-02-13 22:06:49 +01:00
GIANT,
2020-02-13 22:06:49 +01:00
GUARDIAN,
2020-02-13 22:06:49 +01:00
HORSE,
2020-02-13 22:06:49 +01:00
HUSK,
2020-02-13 22:06:49 +01:00
ILLUSIONER,
2020-02-13 22:06:49 +01:00
IRON_GOLEM,
2020-02-13 22:06:49 +01:00
LLAMA,
2020-02-13 22:06:49 +01:00
MAGMA_CUBE,
2020-02-13 22:06:49 +01:00
MULE,
2020-02-13 22:06:49 +01:00
MUSHROOM_COW,
2020-02-13 22:06:49 +01:00
OCELOT,
2020-02-13 22:06:49 +01:00
PARROT,
2020-02-13 22:06:49 +01:00
PIG,
2020-02-13 22:06:49 +01:00
PIG_ZOMBIE,
2020-02-13 22:06:49 +01:00
PLAYER,
2020-02-13 22:06:49 +01:00
PHANTOM,
2020-02-13 22:06:49 +01:00
POLAR_BEAR,
2020-02-13 22:06:49 +01:00
PUFFERFISH,
2020-02-13 22:06:49 +01:00
RABBIT,
2020-02-13 22:06:49 +01:00
SALMON,
2017-06-23 23:21:34 +02:00
2020-02-13 22:06:49 +01:00
SHEEP,
2020-02-13 22:06:49 +01:00
SHULKER,
2020-02-13 22:06:49 +01:00
SILVERFISH,
2020-02-13 22:06:49 +01:00
SKELETON,
2020-02-13 22:06:49 +01:00
SKELETON_HORSE,
2020-02-13 22:06:49 +01:00
SLIME,
2020-02-13 22:06:49 +01:00
SNOWMAN,
2020-02-13 22:06:49 +01:00
SPIDER,
2020-02-13 22:06:49 +01:00
STRAY,
2020-02-13 22:06:49 +01:00
SQUID,
2020-02-13 22:06:49 +01:00
TROPICAL_FISH,
2020-02-13 22:06:49 +01:00
TURTLE,
2020-02-13 22:06:49 +01:00
VEX,
2020-02-13 22:06:49 +01:00
VILLAGER,
2020-02-13 22:06:49 +01:00
VINDICATOR,
2020-02-13 22:06:49 +01:00
WITCH,
2020-02-13 22:06:49 +01:00
WITHER,
2020-02-13 22:06:49 +01:00
WITHER_SKELETON,
2020-02-13 22:06:49 +01:00
WOLF,
2020-02-13 22:06:49 +01:00
ZOMBIE,
2020-02-13 22:06:49 +01:00
ZOMBIE_HORSE,
2020-02-13 22:06:49 +01:00
ZOMBIE_VILLAGER;
public enum SoundType {
2017-06-23 23:21:34 +02:00
CANCEL,
DEATH,
HURT,
IDLE,
STEP
}
2020-02-13 22:06:49 +01:00
static {
try (InputStream stream = LibsDisguises.getInstance().getResource("ANTI_PIRACY_ENCODED_WITH_SOUNDS")) {
List<String> lines = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines()
.collect(Collectors.toList());
for (String s : lines) {
String[] spl = s.split(":", -1);
DisguiseSound sound = DisguiseSound.valueOf(spl[0]);
for (int i = 1; i <= SoundType.values().length; i++) {
if (spl[i].isEmpty()) {
continue;
}
String[] split = spl[i].split(",");
for (String sName : split) {
try {
sound.addSound(Sound.valueOf(sName), SoundType.values()[i - 1]);
}
catch (IllegalArgumentException ex) {
}
}
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}
public static DisguiseSound getType(String name) {
try {
return valueOf(name);
}
catch (Exception ex) {
return null;
}
}
private float damageSoundVolume = 1F;
private LinkedHashMap<Object, SoundType> disguiseSounds = new LinkedHashMap<>();
2020-02-06 00:15:20 +01:00
private Sound parseSound(String name) {
try {
return Sound.valueOf(name);
}
catch (Exception ex) {
}
return null;
}
private void addSound(Object sound, SoundType type) {
if (sound == null) {
return;
}
2020-02-06 00:15:20 +01:00
if (sound instanceof String[]) {
for (String s : (String[]) sound) {
Sound so = parseSound(s);
if (so == null) {
continue;
}
addSound(so, type);
}
} else if (sound instanceof String) {
Sound so = parseSound((String) sound);
if (so == null) {
return;
}
addSound(so, type);
} else if (sound instanceof Sound[]) {
for (Sound s : (Sound[]) sound) {
2020-02-06 00:15:20 +01:00
if (s == null) {
continue;
}
addSound(s, type);
2017-06-25 21:36:09 +02:00
}
2020-02-06 00:15:20 +01:00
} else if (sound instanceof Sound) {
addSound((Sound) sound, type);
} else {
throw new IllegalArgumentException("Was given an unknown object " + sound);
2017-06-25 21:36:09 +02:00
}
}
private void addSound(Sound sound, SoundType type) {
Object soundEffect = ReflectionManager.getCraftSound(sound);
if (disguiseSounds.containsKey(soundEffect)) {
DisguiseUtilities.getLogger().severe("Already doing " + sound);
}
disguiseSounds.put(soundEffect, type);
}
public float getDamageAndIdleSoundVolume() {
return damageSoundVolume;
}
2020-02-06 00:15:20 +01:00
public void setDamageAndIdleSoundVolume(float strength) {
this.damageSoundVolume = strength;
}
public Object getSound(SoundType type) {
if (type == null) {
return null;
}
for (Entry<Object, SoundType> entry : disguiseSounds.entrySet()) {
if (entry.getValue() != type) {
continue;
}
return entry.getKey();
}
return null;
}
public SoundType getSound(Object sound) {
if (sound == null) {
return null;
}
return disguiseSounds.get(sound);
}
/**
* 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;
}
}