29
29
import java .util .Map ;
30
30
import java .util .Map .Entry ;
31
31
import java .util .Set ;
32
+ import java .util .concurrent .ConcurrentHashMap ;
32
33
33
34
import org .bukkit .Material ;
34
35
import org .bukkit .Sound ;
@@ -964,10 +965,21 @@ protected Vector getSpecificValue(Object generic) {
964
965
};
965
966
}
966
967
967
- private static MethodAccessor soundGetter = null ;
968
+ private static MethodAccessor getSound = null ;
969
+ private static MethodAccessor getSoundEffect = null ;
968
970
private static FieldAccessor soundKey = null ;
969
971
972
+ private static Map <String , Sound > soundIndex = null ;
973
+
970
974
public static EquivalentConverter <Sound > getSoundConverter () {
975
+ if (getSound == null || getSoundEffect == null ) {
976
+ Class <?> craftSound = MinecraftReflection .getCraftSoundClass ();
977
+ FuzzyReflection fuzzy = FuzzyReflection .fromClass (craftSound , true );
978
+ getSound = Accessors .getMethodAccessor (fuzzy .getMethodByParameters ("getSound" , String .class , new Class <?>[] { Sound .class }));
979
+ getSoundEffect = Accessors .getMethodAccessor (fuzzy .getMethodByParameters ("getSoundEffect" ,
980
+ MinecraftReflection .getSoundEffectClass (), new Class <?>[] { String .class }));
981
+ }
982
+
971
983
return new IgnoreNullConverter <Sound >() {
972
984
973
985
@ Override
@@ -977,26 +989,42 @@ public Class<Sound> getSpecificType() {
977
989
978
990
@ Override
979
991
protected Object getGenericValue (Class <?> genericType , Sound specific ) {
980
- if (soundGetter == null ) {
981
- Class <?> soundEffects = MinecraftReflection .getMinecraftClass ("SoundEffects" );
982
- FuzzyReflection fuzzy = FuzzyReflection .fromClass (soundEffects , true );
983
- soundGetter = Accessors .getMethodAccessor (fuzzy .getMethodByParameters ("getSound" , MinecraftReflection .getSoundEffectClass (), new Class <?>[] { String .class }));
984
- }
985
-
986
- MinecraftKey key = MinecraftKey .fromEnum (specific );
987
- return soundGetter .invoke (null , key .getFullKey ());
992
+ // Getting the SoundEffect is easy, Bukkit provides us the methods
993
+ String key = (String ) getSound .invoke (null , specific );
994
+ return getSoundEffect .invoke (null , key );
988
995
}
989
996
990
997
@ Override
991
998
protected Sound getSpecificValue (Object generic ) {
999
+ // Getting the Sound is a bit more complicated...
992
1000
if (soundKey == null ) {
993
1001
Class <?> soundEffect = generic .getClass ();
994
1002
FuzzyReflection fuzzy = FuzzyReflection .fromClass (soundEffect , true );
995
1003
soundKey = Accessors .getFieldAccessor (fuzzy .getFieldByType ("key" , MinecraftReflection .getMinecraftKeyClass ()));
996
1004
}
997
1005
998
- MinecraftKey key = MinecraftKey .fromHandle (soundKey .get (generic ));
999
- return Sound .valueOf (key .getEnumFormat ());
1006
+ MinecraftKey minecraftKey = MinecraftKey .fromHandle (soundKey .get (generic ));
1007
+ String key = minecraftKey .getKey ();
1008
+
1009
+ // Use our index if it already exists
1010
+ if (soundIndex != null ) {
1011
+ return soundIndex .get (key );
1012
+ }
1013
+
1014
+ // If it doesn't, try to guess the enum name
1015
+ try {
1016
+ return Sound .valueOf (minecraftKey .getEnumFormat ());
1017
+ } catch (IllegalArgumentException ignored ) {
1018
+ }
1019
+
1020
+ // Worst case we index all the sounds and use it later
1021
+ soundIndex = new ConcurrentHashMap <>();
1022
+ for (Sound sound : Sound .values ()) {
1023
+ String index = (String ) getSound .invoke (null , sound );
1024
+ soundIndex .put (index , sound );
1025
+ }
1026
+
1027
+ return soundIndex .get (key );
1000
1028
}
1001
1029
};
1002
1030
}
0 commit comments