From 567f278131ad1fc09c3d3af39f60b67ab9151bca Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sun, 24 May 2020 01:44:19 +1200 Subject: [PATCH] Fix sounds broken on non-1.15 versions --- .../reflection/ReflectionManager.java | 12 +++++++++ .../utilities/sounds/DisguiseSoundEnums.java | 14 +++++----- .../disguise/utilities/sounds/SoundGroup.java | 9 ++++--- .../utilities/sounds/SoundManager.java | 26 ++++++++++++------- .../utilities/watchers/CompileMethods.java | 21 +++++++-------- .../utilities/watchers/DisguiseMethods.java | 1 - 6 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java index 084cfe45..cffabd2a 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java @@ -1283,6 +1283,18 @@ public class ReflectionManager { return null; } + public static Object createSoundEffect(String minecraftKey) { + try { + return getNmsConstructor("SoundEffect", getNmsClass("MinecraftKey")) + .newInstance(createMinecraftKey(minecraftKey)); + } + catch (Exception ex) { + ex.printStackTrace(); + } + + return null; + } + public static Object createMinecraftKey(String name) { try { return getNmsClass("MinecraftKey").getConstructor(String.class).newInstance(name); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/sounds/DisguiseSoundEnums.java b/src/main/java/me/libraryaddict/disguise/utilities/sounds/DisguiseSoundEnums.java index 0ea7e317..5ac94199 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/sounds/DisguiseSoundEnums.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/sounds/DisguiseSoundEnums.java @@ -2,13 +2,11 @@ package me.libraryaddict.disguise.utilities.sounds; import lombok.Getter; import me.libraryaddict.disguise.LibsDisguises; -import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; -import me.libraryaddict.disguise.utilities.sounds.SoundGroup; import me.libraryaddict.disguise.utilities.sounds.SoundGroup.SoundType; import org.bukkit.Sound; import java.util.Arrays; -import java.util.LinkedHashMap; +import java.util.HashMap; /** * Only living disguises go in here! @@ -231,10 +229,14 @@ public enum DisguiseSoundEnums { Sound.ENTITY_ZOMBIE_VILLAGER_DEATH, Sound.ENTITY_ZOMBIE_VILLAGER_AMBIENT, Sound.ENTITY_ZOMBIE_INFECT, Sound.ENTITY_ZOMBIE_ATTACK_WOODEN_DOOR, Sound.ENTITY_ZOMBIE_BREAK_WOODEN_DOOR, Sound.ENTITY_ZOMBIE_ATTACK_IRON_DOOR); - - private SoundGroup group = new SoundGroup(name()); + @Getter + private HashMap sounds = new HashMap<>(); DisguiseSoundEnums(Object hurt, Object step, Object death, Object idle, Object... sounds) { + if (LibsDisguises.getInstance() != null) { + throw new IllegalStateException("This cannot be called on a running server"); + } + addSound(hurt, SoundType.HURT); addSound(step, SoundType.STEP); addSound(death, SoundType.DEATH); @@ -266,6 +268,6 @@ public enum DisguiseSoundEnums { } private void addSound(Sound sound, SoundType type) { - group.addSound(ReflectionManager.getCraftSound(sound), type); + sounds.put(sound, type); } } 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 c91e9301..86b3b4c3 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundGroup.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundGroup.java @@ -2,7 +2,6 @@ package me.libraryaddict.disguise.utilities.sounds; import lombok.Getter; import me.libraryaddict.disguise.disguisetypes.Disguise; -import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import org.bukkit.Sound; @@ -33,8 +32,12 @@ public class SoundGroup { } public void addSound(Object sound, SoundType type) { - if (sound instanceof String) { - sound = ReflectionManager.createMinecraftKey((String) sound); + 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(); } disguiseSounds.put(sound, type); 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 253d6786..34e271db 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/sounds/SoundManager.java @@ -2,7 +2,7 @@ package me.libraryaddict.disguise.utilities.sounds; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.utilities.DisguiseUtilities; -import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; +import org.bukkit.Sound; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; @@ -70,25 +70,33 @@ public class SoundManager { } private void loadSounds() { - DisguiseSoundEnums.values(); - } - - /*private void loadSounds() { try (InputStream stream = LibsDisguises.getInstance().getResource("ANTI_PIRACY_ENCODED_WITH_SOUNDS")) { List lines = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines() .collect(Collectors.toList()); for (String line : lines) { - String[] groups = line.split("/"); + String[] groups = line.split("/", -1); SoundGroup group = new SoundGroup(groups[0]); int i = 0; for (SoundGroup.SoundType type : SoundGroup.SoundType.values()) { - String[] sounds = groups[++i].split(","); + String s = groups[++i]; + + if (s.isEmpty()) { + continue; + } + + String[] sounds = s.split(","); for (String sound : sounds) { - group.addSound(sound, type); + try { + Sound actualSound = Sound.valueOf(sound); + + group.addSound(actualSound, type); + } + catch (Exception ignored) { + } } } } @@ -96,5 +104,5 @@ public class SoundManager { catch (IOException | NoClassDefFoundError e) { e.printStackTrace(); } - }*/ + } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/watchers/CompileMethods.java b/src/main/java/me/libraryaddict/disguise/utilities/watchers/CompileMethods.java index 31dd46f5..db5d5c15 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/watchers/CompileMethods.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/watchers/CompileMethods.java @@ -1,6 +1,5 @@ package me.libraryaddict.disguise.utilities.watchers; -import com.comphenix.protocol.wrappers.MinecraftKey; import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.utilities.LibsPremium; import me.libraryaddict.disguise.utilities.reflection.ClassGetter; @@ -9,6 +8,7 @@ import me.libraryaddict.disguise.utilities.reflection.NmsRemovedIn; import me.libraryaddict.disguise.utilities.sounds.DisguiseSoundEnums; import me.libraryaddict.disguise.utilities.sounds.SoundGroup; import org.apache.commons.lang.StringUtils; +import org.bukkit.Sound; import java.io.File; import java.io.PrintWriter; @@ -32,25 +32,22 @@ public class CompileMethods { @CompileMethodsIntfer(user = "%%__USER__%%") public static void main(String[] args) { doMethods(); + doSounds(); } - /*private static void doSounds() { + private static void doSounds() { List list = new ArrayList<>(); - for (DisguiseSoundEnums sound : DisguiseSoundEnums.values()){ - - } - - for (Map.Entry entry : SoundGroup.getGroups().entrySet()) { - StringBuilder sound = new StringBuilder(entry.getKey()); + for (DisguiseSoundEnums e : DisguiseSoundEnums.values()) { + StringBuilder sound = new StringBuilder(e.name()); for (SoundGroup.SoundType type : SoundGroup.SoundType.values()) { sound.append("/"); int i = 0; - for (Map.Entry values : entry.getValue().getDisguiseSounds().entrySet()) { - if (values.getValue() != type) { + for (Map.Entry entry : e.getSounds().entrySet()) { + if (entry.getValue() != type) { continue; } @@ -58,7 +55,7 @@ public class CompileMethods { sound.append(","); } - sound.append(MinecraftKey.fromHandle(values.getKey()).getFullKey()); + sound.append(entry.getKey().name()); } } @@ -73,7 +70,7 @@ public class CompileMethods { catch (Exception ex) { ex.printStackTrace(); } - }*/ + } private static void addClass(ArrayList classes, Class c) { if (classes.contains(c)) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/watchers/DisguiseMethods.java b/src/main/java/me/libraryaddict/disguise/utilities/watchers/DisguiseMethods.java index 11aeef1e..8490f1be 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/watchers/DisguiseMethods.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/watchers/DisguiseMethods.java @@ -6,7 +6,6 @@ import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.utilities.params.ParamInfoManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.asm.WatcherInfo; -import me.libraryaddict.disguise.utilities.sounds.SoundGroup; import java.io.BufferedReader; import java.io.IOException;