Allow defaults in sound groups, allow multiple sounds in the same group

This commit is contained in:
libraryaddict 2020-07-05 17:02:25 +12:00
parent 5a05dffdce
commit 3d4329942c
3 changed files with 49 additions and 23 deletions

View File

@ -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<String, SoundGroup> groups = new LinkedHashMap<>();
private float damageSoundVolume = 1F;
@Getter
private final LinkedHashMap<Object, SoundType> disguiseSounds = new LinkedHashMap<>();
private final LinkedHashMap<Object, SoundType> disguiseSoundTypes = new LinkedHashMap<>();
@Getter
private final LinkedHashMap<SoundType, Object[]> 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,38 +87,27 @@ public class SoundGroup {
return getRandomSound(type);
}
for (Map.Entry<Object, SoundType> entry : disguiseSounds.entrySet()) {
if (entry.getValue() != type) {
continue;
}
return entry.getKey();
}
Object[] sounds = disguiseSounds.get(type);
if (sounds == null) {
return null;
}
return sounds[0];
}
private Object getRandomSound(SoundType type) {
if (type == null) {
return null;
}
Object[] sounds = new Object[disguiseSounds.size()];
int i = 0;
Object[] sounds = disguiseSounds.get(type);
for (Map.Entry<Object, SoundType> 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);
}
/**

View File

@ -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;
}

View File

@ -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: