Skip to content

Commit aa555f7

Browse files
authored
add sound converter for versions greater 1.16.4 (#1027)
1 parent 0d50905 commit aa555f7

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,9 +953,59 @@ public Vector getSpecific(Object generic) {
953953
private static MethodAccessor getSoundEffect = null;
954954
private static FieldAccessor soundKey = null;
955955

956+
private static MethodAccessor getSoundEffectByKey = null;
957+
private static MethodAccessor getSoundEffectBySound = null;
958+
private static MethodAccessor getSoundByEffect = null;
959+
956960
private static Map<String, Sound> soundIndex = null;
957961

958962
public static EquivalentConverter<Sound> getSoundConverter() {
963+
// Try to create sound converter for new versions greater 1.16.4
964+
try {
965+
if (getSoundEffectByKey == null || getSoundEffectBySound == null || getSoundByEffect == null) {
966+
Class<?> craftSound = MinecraftReflection.getCraftSoundClass();
967+
FuzzyReflection fuzzy = FuzzyReflection.fromClass(craftSound, true);
968+
969+
getSoundEffectByKey = Accessors.getMethodAccessor(fuzzy.getMethodByParameters(
970+
"getSoundEffect",
971+
MinecraftReflection.getSoundEffectClass(),
972+
new Class<?>[]{String.class}
973+
));
974+
975+
getSoundEffectBySound = Accessors.getMethodAccessor(fuzzy.getMethodByParameters(
976+
"getSoundEffect",
977+
MinecraftReflection.getSoundEffectClass(),
978+
new Class<?>[]{Sound.class}
979+
));
980+
981+
getSoundByEffect = Accessors.getMethodAccessor(fuzzy.getMethodByParameters(
982+
"getBukkit",
983+
Sound.class,
984+
new Class<?>[]{MinecraftReflection.getSoundEffectClass()}
985+
));
986+
}
987+
988+
return ignoreNull(new EquivalentConverter<Sound>() {
989+
990+
@Override
991+
public Class<Sound> getSpecificType() {
992+
return Sound.class;
993+
}
994+
995+
@Override
996+
public Object getGeneric(Sound specific) {
997+
return getSoundEffectBySound.invoke(null, specific);
998+
}
999+
1000+
@Override
1001+
public Sound getSpecific(Object generic) {
1002+
return (Sound) getSoundByEffect.invoke(null, generic);
1003+
}
1004+
});
1005+
} catch (Exception e) {
1006+
}
1007+
1008+
// Fall back to sound converter from legacy versions before 1.16.4
9591009
if (getSound == null || getSoundEffect == null) {
9601010
Class<?> craftSound = MinecraftReflection.getCraftSoundClass();
9611011
FuzzyReflection fuzzy = FuzzyReflection.fromClass(craftSound, true);

0 commit comments

Comments
 (0)