From 32e65f4f5176352eb7ebec4df3b3953427ad45c0 Mon Sep 17 00:00:00 2001 From: Wealthyturtle Date: Sun, 1 Sep 2019 20:29:55 +0800 Subject: [PATCH 1/9] Resolve ResourceKeyInvalidException --- src/main/java/ch/njol/skript/effects/EffPlaySound.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/ch/njol/skript/effects/EffPlaySound.java b/src/main/java/ch/njol/skript/effects/EffPlaySound.java index a694fa60100..f451e60ba74 100644 --- a/src/main/java/ch/njol/skript/effects/EffPlaySound.java +++ b/src/main/java/ch/njol/skript/effects/EffPlaySound.java @@ -157,6 +157,8 @@ protected void execute(Event e) { private static void playSound(Player p, Location location, String[] sounds, SoundCategory category, float volume, float pitch) { for (String sound : sounds) { + if(!sound.matches("[a-z0-9/._-]")) //Minecraft only accepts these characters + continue; Sound soundEnum = null; try { soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); @@ -178,6 +180,8 @@ private static void playSound(Player p, Location location, String[] sounds, Soun private static void playSound(Location location, String[] sounds, SoundCategory category, float volume, float pitch) { World w = location.getWorld(); for (String sound : sounds) { + if(!sound.matches("[a-z0-9/._-]")) //Minecraft only accepts these characters + continue; Sound soundEnum = null; try { soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); From 0a1a5d82f3e12bdaeceab1b41771a326f41f1fc8 Mon Sep 17 00:00:00 2001 From: Wealthyturtle Date: Sun, 1 Sep 2019 20:53:19 +0800 Subject: [PATCH 2/9] Format Fix --- src/main/java/ch/njol/skript/effects/EffPlaySound.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffPlaySound.java b/src/main/java/ch/njol/skript/effects/EffPlaySound.java index f451e60ba74..2aea192925a 100644 --- a/src/main/java/ch/njol/skript/effects/EffPlaySound.java +++ b/src/main/java/ch/njol/skript/effects/EffPlaySound.java @@ -157,7 +157,7 @@ protected void execute(Event e) { private static void playSound(Player p, Location location, String[] sounds, SoundCategory category, float volume, float pitch) { for (String sound : sounds) { - if(!sound.matches("[a-z0-9/._-]")) //Minecraft only accepts these characters + if (!sound.matches("[a-z0-9\\/._-]+")) //Minecraft only accepts these characters continue; Sound soundEnum = null; try { @@ -180,7 +180,7 @@ private static void playSound(Player p, Location location, String[] sounds, Soun private static void playSound(Location location, String[] sounds, SoundCategory category, float volume, float pitch) { World w = location.getWorld(); for (String sound : sounds) { - if(!sound.matches("[a-z0-9/._-]")) //Minecraft only accepts these characters + if (!sound.matches("[a-z0-9\\/._-]+")) //Minecraft only accepts these characters continue; Sound soundEnum = null; try { From 4d201ddbd90ce88c8f3814bb7dae114f10194ff2 Mon Sep 17 00:00:00 2001 From: Wealthyturtle Date: Sun, 1 Sep 2019 21:01:09 +0800 Subject: [PATCH 3/9] Cleanup + Lowercase Conversion --- .../ch/njol/skript/effects/EffPlaySound.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffPlaySound.java b/src/main/java/ch/njol/skript/effects/EffPlaySound.java index 2aea192925a..ec27a4b85bb 100644 --- a/src/main/java/ch/njol/skript/effects/EffPlaySound.java +++ b/src/main/java/ch/njol/skript/effects/EffPlaySound.java @@ -55,7 +55,8 @@ public class EffPlaySound extends Effect { private static final boolean SOUND_CATEGORIES_EXIST = Skript.classExists("org.bukkit.SoundCategory"); - + private static final String SOUND_VALID_CHARACTERS = "[a-z0-9\\/._-]+" //Minecraft only accepts these characters + static { if (SOUND_CATEGORIES_EXIST) { Skript.registerEffect(EffPlaySound.class, @@ -157,19 +158,22 @@ protected void execute(Event e) { private static void playSound(Player p, Location location, String[] sounds, SoundCategory category, float volume, float pitch) { for (String sound : sounds) { - if (!sound.matches("[a-z0-9\\/._-]+")) //Minecraft only accepts these characters - continue; + sound = sound.toLowerCase(); Sound soundEnum = null; try { soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { if (soundEnum == null) + if (!sound.matches(SOUND_VALID_CHARACTERS)) + continue; p.playSound(location, sound, category, volume, pitch); else p.playSound(location, soundEnum, category, volume, pitch); } else { if (soundEnum == null) + if (!sound.matches(SOUND_VALID_CHARACTERS)) + continue; p.playSound(location, sound, volume, pitch); else p.playSound(location, soundEnum, volume, pitch); @@ -180,19 +184,22 @@ private static void playSound(Player p, Location location, String[] sounds, Soun private static void playSound(Location location, String[] sounds, SoundCategory category, float volume, float pitch) { World w = location.getWorld(); for (String sound : sounds) { - if (!sound.matches("[a-z0-9\\/._-]+")) //Minecraft only accepts these characters - continue; + sound = sound.toLowerCase(); Sound soundEnum = null; try { soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { if (soundEnum == null) + if (!sound.matches(SOUND_VALID_CHARACTERS)) + continue; w.playSound(location, sound, category, volume, pitch); else w.playSound(location, soundEnum, category, volume, pitch); } else { if (soundEnum == null) + if (!sound.matches(SOUND_VALID_CHARACTERS)) + continue; w.playSound(location, sound, volume, pitch); else w.playSound(location, soundEnum, volume, pitch); From 1b3d3c01f783806421cb6d57c0aadee9b9c13b8e Mon Sep 17 00:00:00 2001 From: Wealthyturtle Date: Sun, 1 Sep 2019 21:03:53 +0800 Subject: [PATCH 4/9] Semicolon ; --- src/main/java/ch/njol/skript/effects/EffPlaySound.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/effects/EffPlaySound.java b/src/main/java/ch/njol/skript/effects/EffPlaySound.java index ec27a4b85bb..516a485ac1f 100644 --- a/src/main/java/ch/njol/skript/effects/EffPlaySound.java +++ b/src/main/java/ch/njol/skript/effects/EffPlaySound.java @@ -55,7 +55,7 @@ public class EffPlaySound extends Effect { private static final boolean SOUND_CATEGORIES_EXIST = Skript.classExists("org.bukkit.SoundCategory"); - private static final String SOUND_VALID_CHARACTERS = "[a-z0-9\\/._-]+" //Minecraft only accepts these characters + private static final String SOUND_VALID_CHARACTERS = "[a-z0-9\\/._-]+"; //Minecraft only accepts these characters static { if (SOUND_CATEGORIES_EXIST) { From 9d23a19be61e51251b5959d4f5a26902743bc74b Mon Sep 17 00:00:00 2001 From: Wealthyturtle Date: Sun, 1 Sep 2019 21:08:55 +0800 Subject: [PATCH 5/9] Fix Brackets Ahhhh --- .../ch/njol/skript/effects/EffPlaySound.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffPlaySound.java b/src/main/java/ch/njol/skript/effects/EffPlaySound.java index 516a485ac1f..48e4bb743df 100644 --- a/src/main/java/ch/njol/skript/effects/EffPlaySound.java +++ b/src/main/java/ch/njol/skript/effects/EffPlaySound.java @@ -164,19 +164,21 @@ private static void playSound(Player p, Location location, String[] sounds, Soun soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { - if (soundEnum == null) + if (soundEnum == null) { if (!sound.matches(SOUND_VALID_CHARACTERS)) continue; p.playSound(location, sound, category, volume, pitch); - else + } else { p.playSound(location, soundEnum, category, volume, pitch); + } } else { - if (soundEnum == null) + if (soundEnum == null) { if (!sound.matches(SOUND_VALID_CHARACTERS)) continue; p.playSound(location, sound, volume, pitch); - else + } else { p.playSound(location, soundEnum, volume, pitch); + } } } } @@ -190,19 +192,21 @@ private static void playSound(Location location, String[] sounds, SoundCategory soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { - if (soundEnum == null) + if (soundEnum == null) { if (!sound.matches(SOUND_VALID_CHARACTERS)) continue; w.playSound(location, sound, category, volume, pitch); - else + } else { w.playSound(location, soundEnum, category, volume, pitch); + } } else { - if (soundEnum == null) + if (soundEnum == null) { if (!sound.matches(SOUND_VALID_CHARACTERS)) continue; w.playSound(location, sound, volume, pitch); - else + } else { w.playSound(location, soundEnum, volume, pitch); + } } } } From 20d565900d1608c2adb32a348905cbfd926eff22 Mon Sep 17 00:00:00 2001 From: Wealthyturtle Date: Sun, 1 Sep 2019 21:20:03 +0800 Subject: [PATCH 6/9] Replace Regex with Catch --- .../ch/njol/skript/effects/EffPlaySound.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffPlaySound.java b/src/main/java/ch/njol/skript/effects/EffPlaySound.java index 48e4bb743df..e83d6d3e0e1 100644 --- a/src/main/java/ch/njol/skript/effects/EffPlaySound.java +++ b/src/main/java/ch/njol/skript/effects/EffPlaySound.java @@ -55,7 +55,6 @@ public class EffPlaySound extends Effect { private static final boolean SOUND_CATEGORIES_EXIST = Skript.classExists("org.bukkit.SoundCategory"); - private static final String SOUND_VALID_CHARACTERS = "[a-z0-9\\/._-]+"; //Minecraft only accepts these characters static { if (SOUND_CATEGORIES_EXIST) { @@ -158,24 +157,25 @@ protected void execute(Event e) { private static void playSound(Player p, Location location, String[] sounds, SoundCategory category, float volume, float pitch) { for (String sound : sounds) { - sound = sound.toLowerCase(); Sound soundEnum = null; try { soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { if (soundEnum == null) { - if (!sound.matches(SOUND_VALID_CHARACTERS)) - continue; - p.playSound(location, sound, category, volume, pitch); + // It may throw an exception if there are invalid characters in the sound name + try { + p.playSound(location, sound, category, volume, pitch); + } catch (Exception ignored) {} } else { p.playSound(location, soundEnum, category, volume, pitch); } } else { if (soundEnum == null) { - if (!sound.matches(SOUND_VALID_CHARACTERS)) - continue; - p.playSound(location, sound, volume, pitch); + // It may throw an exception if there are invalid characters in the sound name + try { + p.playSound(location, sound, volume, pitch); + } catch (Exception ignored) {} } else { p.playSound(location, soundEnum, volume, pitch); } @@ -186,24 +186,25 @@ private static void playSound(Player p, Location location, String[] sounds, Soun private static void playSound(Location location, String[] sounds, SoundCategory category, float volume, float pitch) { World w = location.getWorld(); for (String sound : sounds) { - sound = sound.toLowerCase(); Sound soundEnum = null; try { soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { if (soundEnum == null) { - if (!sound.matches(SOUND_VALID_CHARACTERS)) - continue; - w.playSound(location, sound, category, volume, pitch); + // It may throw an exception if there are invalid characters in the sound name + try { + w.playSound(location, sound, category, volume, pitch); + } catch (Exception ignored) {} } else { w.playSound(location, soundEnum, category, volume, pitch); } } else { if (soundEnum == null) { - if (!sound.matches(SOUND_VALID_CHARACTERS)) - continue; - w.playSound(location, sound, volume, pitch); + // It may throw an exception if there are invalid characters in the sound name + try { + w.playSound(location, sound, volume, pitch); + } catch (Exception ignored) {} } else { w.playSound(location, soundEnum, volume, pitch); } From 1321a6d72dd65e81729e7095d49545205e51491a Mon Sep 17 00:00:00 2001 From: Wealthyturtle Date: Sun, 22 Sep 2019 18:05:45 +0800 Subject: [PATCH 7/9] Revert to Regex --- .../ch/njol/skript/effects/EffPlaySound.java | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffPlaySound.java b/src/main/java/ch/njol/skript/effects/EffPlaySound.java index e83d6d3e0e1..df8f78345ed 100644 --- a/src/main/java/ch/njol/skript/effects/EffPlaySound.java +++ b/src/main/java/ch/njol/skript/effects/EffPlaySound.java @@ -55,6 +55,7 @@ public class EffPlaySound extends Effect { private static final boolean SOUND_CATEGORIES_EXIST = Skript.classExists("org.bukkit.SoundCategory"); + private static final String SOUND_VALID_CHARACTERS = "[a-z0-9\\/._-]+"; // Minecraft only accepts these characters static { if (SOUND_CATEGORIES_EXIST) { @@ -157,25 +158,24 @@ protected void execute(Event e) { private static void playSound(Player p, Location location, String[] sounds, SoundCategory category, float volume, float pitch) { for (String sound : sounds) { + sound = sound.toLowerCase(); Sound soundEnum = null; try { soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { if (soundEnum == null) { - // It may throw an exception if there are invalid characters in the sound name - try { - p.playSound(location, sound, category, volume, pitch); - } catch (Exception ignored) {} + if (!sound.matches(SOUND_VALID_CHARACTERS)) + continue; + p.playSound(location, sound, category, volume, pitch); } else { p.playSound(location, soundEnum, category, volume, pitch); } } else { if (soundEnum == null) { - // It may throw an exception if there are invalid characters in the sound name - try { - p.playSound(location, sound, volume, pitch); - } catch (Exception ignored) {} + if (!sound.matches(SOUND_VALID_CHARACTERS)) + continue; + p.playSound(location, sound, volume, pitch); } else { p.playSound(location, soundEnum, volume, pitch); } @@ -186,25 +186,24 @@ private static void playSound(Player p, Location location, String[] sounds, Soun private static void playSound(Location location, String[] sounds, SoundCategory category, float volume, float pitch) { World w = location.getWorld(); for (String sound : sounds) { + sound = sound.toLowerCase(); Sound soundEnum = null; try { soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { if (soundEnum == null) { - // It may throw an exception if there are invalid characters in the sound name - try { - w.playSound(location, sound, category, volume, pitch); - } catch (Exception ignored) {} + if (!sound.matches(SOUND_VALID_CHARACTERS)) + continue; + w.playSound(location, sound, category, volume, pitch); } else { w.playSound(location, soundEnum, category, volume, pitch); } } else { if (soundEnum == null) { - // It may throw an exception if there are invalid characters in the sound name - try { - w.playSound(location, sound, volume, pitch); - } catch (Exception ignored) {} + if (!sound.matches(SOUND_VALID_CHARACTERS)) + continue; + w.playSound(location, sound, volume, pitch); } else { w.playSound(location, soundEnum, volume, pitch); } From a1bdc3f2e1315543d0b21680edd409c693a369b5 Mon Sep 17 00:00:00 2001 From: Wealthyturtle Date: Fri, 27 Sep 2019 22:06:19 +0800 Subject: [PATCH 8/9] Precompiled Pattern --- .../java/ch/njol/skript/effects/EffPlaySound.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffPlaySound.java b/src/main/java/ch/njol/skript/effects/EffPlaySound.java index df8f78345ed..3c24beac2e0 100644 --- a/src/main/java/ch/njol/skript/effects/EffPlaySound.java +++ b/src/main/java/ch/njol/skript/effects/EffPlaySound.java @@ -20,6 +20,8 @@ package ch.njol.skript.effects; import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.bukkit.Location; import org.bukkit.Sound; @@ -55,7 +57,7 @@ public class EffPlaySound extends Effect { private static final boolean SOUND_CATEGORIES_EXIST = Skript.classExists("org.bukkit.SoundCategory"); - private static final String SOUND_VALID_CHARACTERS = "[a-z0-9\\/._-]+"; // Minecraft only accepts these characters + private static final Pattern SOUND_VALID_PATTERN = Pattern.compile("[a-z0-9\\/._-]+"); // Minecraft only accepts these characters static { if (SOUND_CATEGORIES_EXIST) { @@ -165,7 +167,7 @@ private static void playSound(Player p, Location location, String[] sounds, Soun } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { if (soundEnum == null) { - if (!sound.matches(SOUND_VALID_CHARACTERS)) + if (!SOUND_VALID_PATTERN.matcher(sound).matches()) continue; p.playSound(location, sound, category, volume, pitch); } else { @@ -173,7 +175,7 @@ private static void playSound(Player p, Location location, String[] sounds, Soun } } else { if (soundEnum == null) { - if (!sound.matches(SOUND_VALID_CHARACTERS)) + if (!SOUND_VALID_PATTERN.matcher(sound).matches()) continue; p.playSound(location, sound, volume, pitch); } else { @@ -193,7 +195,7 @@ private static void playSound(Location location, String[] sounds, SoundCategory } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { if (soundEnum == null) { - if (!sound.matches(SOUND_VALID_CHARACTERS)) + if (!SOUND_VALID_PATTERN.matcher(sound).matches()) continue; w.playSound(location, sound, category, volume, pitch); } else { @@ -201,7 +203,7 @@ private static void playSound(Location location, String[] sounds, SoundCategory } } else { if (soundEnum == null) { - if (!sound.matches(SOUND_VALID_CHARACTERS)) + if (!SOUND_VALID_PATTERN.matcher(sound).matches()) continue; w.playSound(location, sound, volume, pitch); } else { From 48afe0c8adbbafb125c1667cd2e01ec496c24def Mon Sep 17 00:00:00 2001 From: Wealthyturtle Date: Fri, 27 Sep 2019 22:50:23 +0800 Subject: [PATCH 9/9] Slightly more efficient lowercase --- src/main/java/ch/njol/skript/effects/EffPlaySound.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffPlaySound.java b/src/main/java/ch/njol/skript/effects/EffPlaySound.java index 3c24beac2e0..8028192b7a1 100644 --- a/src/main/java/ch/njol/skript/effects/EffPlaySound.java +++ b/src/main/java/ch/njol/skript/effects/EffPlaySound.java @@ -160,13 +160,13 @@ protected void execute(Event e) { private static void playSound(Player p, Location location, String[] sounds, SoundCategory category, float volume, float pitch) { for (String sound : sounds) { - sound = sound.toLowerCase(); Sound soundEnum = null; try { soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { if (soundEnum == null) { + sound = sound.toLowerCase(Locale.ENGLISH); if (!SOUND_VALID_PATTERN.matcher(sound).matches()) continue; p.playSound(location, sound, category, volume, pitch); @@ -175,6 +175,7 @@ private static void playSound(Player p, Location location, String[] sounds, Soun } } else { if (soundEnum == null) { + sound = sound.toLowerCase(Locale.ENGLISH); if (!SOUND_VALID_PATTERN.matcher(sound).matches()) continue; p.playSound(location, sound, volume, pitch); @@ -188,13 +189,13 @@ private static void playSound(Player p, Location location, String[] sounds, Soun private static void playSound(Location location, String[] sounds, SoundCategory category, float volume, float pitch) { World w = location.getWorld(); for (String sound : sounds) { - sound = sound.toLowerCase(); Sound soundEnum = null; try { soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException ignored) {} if (SOUND_CATEGORIES_EXIST) { if (soundEnum == null) { + sound = sound.toLowerCase(Locale.ENGLISH); if (!SOUND_VALID_PATTERN.matcher(sound).matches()) continue; w.playSound(location, sound, category, volume, pitch); @@ -203,6 +204,7 @@ private static void playSound(Location location, String[] sounds, SoundCategory } } else { if (soundEnum == null) { + sound = sound.toLowerCase(Locale.ENGLISH); if (!SOUND_VALID_PATTERN.matcher(sound).matches()) continue; w.playSound(location, sound, volume, pitch);