From 3d4329942cca992b75a078d2ec1dd5f798c05df0 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sun, 5 Jul 2020 17:02:25 +1200 Subject: [PATCH] Allow defaults in sound groups, allow multiple sounds in the same group --- .../disguise/utilities/sounds/SoundGroup.java | 45 ++++++++++--------- .../utilities/sounds/SoundManager.java | 24 +++++++++- src/main/resources/sounds.yml | 3 ++ 3 files changed, 49 insertions(+), 23 deletions(-) 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 25be8149..5d5104a0 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundGroup.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundGroup.java @@ -7,6 +7,7 @@ import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import org.apache.commons.lang.math.RandomUtils; import org.bukkit.Sound; +import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map; @@ -26,7 +27,9 @@ public class SoundGroup { private final static LinkedHashMap groups = new LinkedHashMap<>(); private float damageSoundVolume = 1F; @Getter - private final LinkedHashMap disguiseSounds = new LinkedHashMap<>(); + private final LinkedHashMap disguiseSoundTypes = new LinkedHashMap<>(); + @Getter + private final LinkedHashMap disguiseSounds = new LinkedHashMap<>(); private boolean customSounds; public SoundGroup(String name) { @@ -53,7 +56,18 @@ public class SoundGroup { return; } - disguiseSounds.put(sound, type); + 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}); + } } public float getDamageAndIdleSoundVolume() { @@ -73,15 +87,13 @@ public class SoundGroup { return getRandomSound(type); } - for (Map.Entry entry : disguiseSounds.entrySet()) { - if (entry.getValue() != type) { - continue; - } + Object[] sounds = disguiseSounds.get(type); - return entry.getKey(); + if (sounds == null) { + return null; } - return null; + return sounds[0]; } private Object getRandomSound(SoundType type) { @@ -89,22 +101,13 @@ public class SoundGroup { return null; } - Object[] sounds = new Object[disguiseSounds.size()]; - int i = 0; + Object[] sounds = disguiseSounds.get(type); - for (Map.Entry entry : disguiseSounds.entrySet()) { - if (entry.getValue() != type) { - continue; - } - - sounds[i++] = entry.getKey(); - } - - if (i == 0) { + if (sounds == null) { return null; } - return sounds[RandomUtils.nextInt(i)]; + return sounds[RandomUtils.nextInt(sounds.length)]; } public SoundType getSound(Object sound) { @@ -112,7 +115,7 @@ public class SoundGroup { return null; } - return disguiseSounds.get(sound); + return disguiseSoundTypes.get(sound); } /** diff --git a/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundManager.java b/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundManager.java index eccecfcd..964061db 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundManager.java @@ -10,6 +10,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; /** @@ -61,8 +62,27 @@ public class SoundManager { for (String sound : list) { if (!sound.matches(".+:.+")) { - DisguiseUtilities.getLogger() - .warning("Invalid sound '" + sound + "'! Must be a minecraft:sound.name"); + SoundGroup subGroup = SoundGroup.getGroup(sound); + + if (subGroup == null) { + DisguiseUtilities.getLogger().warning("Invalid sound '" + sound + + "'! Must be a minecraft:sound.name or SoundGroup name!"); + continue; + } + + Object[] sounds = subGroup.getDisguiseSounds().get(type); + + if (sounds == null) { + DisguiseUtilities.getLogger().warning( + "Sound group '" + sound + "' does not contain a category for " + type + + "! Can't use as default in " + key); + continue; + } + + for (Object obj : sounds) { + group.addSound(obj, type); + } + continue; } diff --git a/src/main/resources/sounds.yml b/src/main/resources/sounds.yml index 8b6d6d34..a0f698e2 100644 --- a/src/main/resources/sounds.yml +++ b/src/main/resources/sounds.yml @@ -7,6 +7,9 @@ GroupName: # If you don't set one of these options, or it is empty. Then no sound will be played for that sound type # So if hurt is missing, it won't play a hurt noise + # Alternatively, you could use "COW" which refers to the "COW" soundgroup, and loads the category from that! + # Idle: + # - COW Idle: - minecraft:some.idle.sound Hurt: