From fb2075b774634bff12f2b451bed27e3d1608b31a Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Thu, 30 Mar 2023 17:07:34 -0500 Subject: [PATCH] Fix exception for null sounds Fixes #2276 --- .../protocol/utility/MinecraftVersion.java | 4 ++ .../protocol/wrappers/BukkitConverters.java | 37 +++++++++++-------- .../protocol/events/PacketContainerTest.java | 2 + 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/comphenix/protocol/utility/MinecraftVersion.java b/src/main/java/com/comphenix/protocol/utility/MinecraftVersion.java index ec58dd84..ee4c6efb 100644 --- a/src/main/java/com/comphenix/protocol/utility/MinecraftVersion.java +++ b/src/main/java/com/comphenix/protocol/utility/MinecraftVersion.java @@ -58,6 +58,10 @@ public final class MinecraftVersion implements Comparable, Ser * Version 1.17 - caves and cliffs part 1 */ public static final MinecraftVersion CAVES_CLIFFS_1 = new MinecraftVersion("1.17"); + /** + * Version 1.16.4 + */ + public static final MinecraftVersion NETHER_UPDATE_4 = new MinecraftVersion("1.16.4"); /** * Version 1.16.2 - breaking change to the nether update */ diff --git a/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java b/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java index 73bbac01..5c896d05 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java +++ b/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java @@ -1083,19 +1083,19 @@ public class BukkitConverters { }); } - private static MethodAccessor getSound = null; - private static MethodAccessor getSoundEffect = null; - private static FieldAccessor soundKey = null; + static MethodAccessor getSound = null; + static MethodAccessor getSoundEffect = null; + static FieldAccessor soundKey = null; - private static MethodAccessor getSoundEffectByKey = null; - private static MethodAccessor getSoundEffectBySound = null; - private static MethodAccessor getSoundByEffect = null; + static MethodAccessor getSoundEffectByKey = null; + static MethodAccessor getSoundEffectBySound = null; + static MethodAccessor getSoundByEffect = null; - private static Map soundIndex = null; + static Map soundIndex = null; public static EquivalentConverter getSoundConverter() { // Try to create sound converter for new versions greater 1.16.4 - try { + if (MinecraftVersion.NETHER_UPDATE_4.atOrAbove()) { if (getSoundEffectByKey == null || getSoundEffectBySound == null || getSoundByEffect == null) { Class craftSound = MinecraftReflection.getCraftSoundClass(); FuzzyReflection fuzzy = FuzzyReflection.fromClass(craftSound, true); @@ -1103,19 +1103,19 @@ public class BukkitConverters { getSoundEffectByKey = Accessors.getMethodAccessor(fuzzy.getMethodByReturnTypeAndParameters( "getSoundEffect", MinecraftReflection.getSoundEffectClass(), - new Class[]{String.class} + String.class )); getSoundEffectBySound = Accessors.getMethodAccessor(fuzzy.getMethodByReturnTypeAndParameters( "getSoundEffect", MinecraftReflection.getSoundEffectClass(), - new Class[]{Sound.class} + Sound.class )); getSoundByEffect = Accessors.getMethodAccessor(fuzzy.getMethodByReturnTypeAndParameters( "getBukkit", Sound.class, - new Class[]{MinecraftReflection.getSoundEffectClass()} + MinecraftReflection.getSoundEffectClass() )); } @@ -1133,10 +1133,17 @@ public class BukkitConverters { @Override public Sound getSpecific(Object generic) { - return (Sound) getSoundByEffect.invoke(null, generic); + try { + return (Sound) getSoundByEffect.invoke(null, generic); + } catch (IllegalStateException ex) { + if (ex.getCause() instanceof NullPointerException) { + // "null" sounds cause NPEs inside getSoundByEffect + return null; + } + throw ex; + } } }); - } catch (Exception e) { } // Fall back to sound converter from legacy versions before 1.16.4 @@ -1144,9 +1151,9 @@ public class BukkitConverters { Class craftSound = MinecraftReflection.getCraftSoundClass(); FuzzyReflection fuzzy = FuzzyReflection.fromClass(craftSound, true); getSound = Accessors.getMethodAccessor( - fuzzy.getMethodByReturnTypeAndParameters("getSound", String.class, new Class[]{Sound.class})); + fuzzy.getMethodByReturnTypeAndParameters("getSound", String.class, Sound.class)); getSoundEffect = Accessors.getMethodAccessor(fuzzy.getMethodByReturnTypeAndParameters("getSoundEffect", - MinecraftReflection.getSoundEffectClass(), new Class[]{String.class})); + MinecraftReflection.getSoundEffectClass(), String.class)); } return ignoreNull(new EquivalentConverter() { diff --git a/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java b/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java index 13233b8a..9cf86978 100644 --- a/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java +++ b/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java @@ -570,6 +570,8 @@ public class PacketContainerTest { @Test public void testSoundEffects() { PacketContainer container = new PacketContainer(PacketType.Play.Server.NAMED_SOUND_EFFECT); + container.getSoundEffects().optionRead(0); + container.getSoundEffects().write(0, Sound.ENTITY_CAT_HISS); assertEquals(container.getSoundEffects().read(0), Sound.ENTITY_CAT_HISS);