Skip to content

Commit fb2075b

Browse files
committed
Fix exception for null sounds
Fixes #2276
1 parent 4b78bf6 commit fb2075b

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

src/main/java/com/comphenix/protocol/utility/MinecraftVersion.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public final class MinecraftVersion implements Comparable<MinecraftVersion>, Ser
5858
* Version 1.17 - caves and cliffs part 1
5959
*/
6060
public static final MinecraftVersion CAVES_CLIFFS_1 = new MinecraftVersion("1.17");
61+
/**
62+
* Version 1.16.4
63+
*/
64+
public static final MinecraftVersion NETHER_UPDATE_4 = new MinecraftVersion("1.16.4");
6165
/**
6266
* Version 1.16.2 - breaking change to the nether update
6367
*/

src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,39 +1083,39 @@ public Vector getSpecific(Object generic) {
10831083
});
10841084
}
10851085

1086-
private static MethodAccessor getSound = null;
1087-
private static MethodAccessor getSoundEffect = null;
1088-
private static FieldAccessor soundKey = null;
1086+
static MethodAccessor getSound = null;
1087+
static MethodAccessor getSoundEffect = null;
1088+
static FieldAccessor soundKey = null;
10891089

1090-
private static MethodAccessor getSoundEffectByKey = null;
1091-
private static MethodAccessor getSoundEffectBySound = null;
1092-
private static MethodAccessor getSoundByEffect = null;
1090+
static MethodAccessor getSoundEffectByKey = null;
1091+
static MethodAccessor getSoundEffectBySound = null;
1092+
static MethodAccessor getSoundByEffect = null;
10931093

1094-
private static Map<String, Sound> soundIndex = null;
1094+
static Map<String, Sound> soundIndex = null;
10951095

10961096
public static EquivalentConverter<Sound> getSoundConverter() {
10971097
// Try to create sound converter for new versions greater 1.16.4
1098-
try {
1098+
if (MinecraftVersion.NETHER_UPDATE_4.atOrAbove()) {
10991099
if (getSoundEffectByKey == null || getSoundEffectBySound == null || getSoundByEffect == null) {
11001100
Class<?> craftSound = MinecraftReflection.getCraftSoundClass();
11011101
FuzzyReflection fuzzy = FuzzyReflection.fromClass(craftSound, true);
11021102

11031103
getSoundEffectByKey = Accessors.getMethodAccessor(fuzzy.getMethodByReturnTypeAndParameters(
11041104
"getSoundEffect",
11051105
MinecraftReflection.getSoundEffectClass(),
1106-
new Class<?>[]{String.class}
1106+
String.class
11071107
));
11081108

11091109
getSoundEffectBySound = Accessors.getMethodAccessor(fuzzy.getMethodByReturnTypeAndParameters(
11101110
"getSoundEffect",
11111111
MinecraftReflection.getSoundEffectClass(),
1112-
new Class<?>[]{Sound.class}
1112+
Sound.class
11131113
));
11141114

11151115
getSoundByEffect = Accessors.getMethodAccessor(fuzzy.getMethodByReturnTypeAndParameters(
11161116
"getBukkit",
11171117
Sound.class,
1118-
new Class<?>[]{MinecraftReflection.getSoundEffectClass()}
1118+
MinecraftReflection.getSoundEffectClass()
11191119
));
11201120
}
11211121

@@ -1133,20 +1133,27 @@ public Object getGeneric(Sound specific) {
11331133

11341134
@Override
11351135
public Sound getSpecific(Object generic) {
1136-
return (Sound) getSoundByEffect.invoke(null, generic);
1136+
try {
1137+
return (Sound) getSoundByEffect.invoke(null, generic);
1138+
} catch (IllegalStateException ex) {
1139+
if (ex.getCause() instanceof NullPointerException) {
1140+
// "null" sounds cause NPEs inside getSoundByEffect
1141+
return null;
1142+
}
1143+
throw ex;
1144+
}
11371145
}
11381146
});
1139-
} catch (Exception e) {
11401147
}
11411148

11421149
// Fall back to sound converter from legacy versions before 1.16.4
11431150
if (getSound == null || getSoundEffect == null) {
11441151
Class<?> craftSound = MinecraftReflection.getCraftSoundClass();
11451152
FuzzyReflection fuzzy = FuzzyReflection.fromClass(craftSound, true);
11461153
getSound = Accessors.getMethodAccessor(
1147-
fuzzy.getMethodByReturnTypeAndParameters("getSound", String.class, new Class<?>[]{Sound.class}));
1154+
fuzzy.getMethodByReturnTypeAndParameters("getSound", String.class, Sound.class));
11481155
getSoundEffect = Accessors.getMethodAccessor(fuzzy.getMethodByReturnTypeAndParameters("getSoundEffect",
1149-
MinecraftReflection.getSoundEffectClass(), new Class<?>[]{String.class}));
1156+
MinecraftReflection.getSoundEffectClass(), String.class));
11501157
}
11511158

11521159
return ignoreNull(new EquivalentConverter<Sound>() {

src/test/java/com/comphenix/protocol/events/PacketContainerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,8 @@ public void testSoundCategory() {
570570
@Test
571571
public void testSoundEffects() {
572572
PacketContainer container = new PacketContainer(PacketType.Play.Server.NAMED_SOUND_EFFECT);
573+
container.getSoundEffects().optionRead(0);
574+
573575
container.getSoundEffects().write(0, Sound.ENTITY_CAT_HISS);
574576

575577
assertEquals(container.getSoundEffects().read(0), Sound.ENTITY_CAT_HISS);

0 commit comments

Comments
 (0)