Cleanup reflection

This commit is contained in:
Dreeam 2024-05-30 01:39:40 +08:00
parent 4e54d86712
commit e8010782fd
No known key found for this signature in database
GPG Key ID: 0998F8AFD8F667AB

View File

@ -11,7 +11,7 @@ import java.util.List;
public class I18NUtils {
private static Method spigotMethod;
private static Method spigotGetLocale;
private static final List<String> LOCALE_LIST = Arrays.asList(
"en", "bg", "de", "eo", "es", "et", "eu", "fi", "fr", "gl", "hu", "id", "it", "ja", "ko", "lt", "nl", "pl",
"pt", "ro", "ru", "sk", "sr", "tr", "uk"
@ -19,9 +19,10 @@ public class I18NUtils {
static {
try {
spigotMethod = Player.class.getMethod("spigot");
spigotGetLocale = Player.Spigot.class.getMethod("getLocale");
spigotGetLocale.setAccessible(true);
} catch (NoSuchMethodException e) {
spigotMethod = null;
spigotGetLocale = null;
}
}
@ -35,11 +36,7 @@ public class I18NUtils {
return player.getLocale().toLowerCase();
} else {
try {
Object spigot = spigotMethod.invoke(player);
Method spigotGetLocaleMethod = spigot.getClass().getMethod("getLocale");
spigotGetLocaleMethod.setAccessible(true);
return ((String) spigotGetLocaleMethod.invoke(spigot)).toLowerCase();
return ((String) spigotGetLocale.invoke(player.spigot())).toLowerCase();
} catch (Exception e) {
e.printStackTrace();
return null;