Fix exception for null sounds

Fixes #2276
This commit is contained in:
Dan Mulloy 2023-03-30 17:07:34 -05:00
parent 4b78bf6a34
commit fb2075b774
No known key found for this signature in database
GPG Key ID: E3B02DE32FB04AC1
3 changed files with 28 additions and 15 deletions

View File

@ -58,6 +58,10 @@ public final class MinecraftVersion implements Comparable<MinecraftVersion>, 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
*/

View File

@ -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<String, Sound> soundIndex = null;
static Map<String, Sound> soundIndex = null;
public static EquivalentConverter<Sound> 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) {
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<Sound>() {

View File

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