Use the player locale api instead of reflection to fix issues #402

This commit is contained in:
chickeneer
2023-10-02 16:25:35 -05:00
parent d22fae6bfa
commit 26d72f27c6
@@ -315,33 +315,34 @@ public class BukkitCommandManager extends CommandManager<
return;
}
try {
Field entityField = getEntityField(player);
if (entityField == null) {
return;
}
Object nmsPlayer = entityField.get(player);
if (nmsPlayer != null) {
Field localeField;
try {
localeField = nmsPlayer.getClass().getDeclaredField("locale");
} catch (NoSuchFieldException ignored) {
localeField = nmsPlayer.getClass().getDeclaredField("language");
}
localeField.setAccessible(true);
Object localeString = localeField.get(nmsPlayer);
if (localeString instanceof String) {
UUID playerUniqueId = player.getUniqueId();
if (!localeString.equals(issuersLocaleString.get(playerUniqueId))) {
String[] split = ACFPatterns.UNDERSCORE.split((String) localeString);
Locale locale = split.length > 1 ? new Locale(split[0], split[1]) : new Locale(split[0]);
Locale prev = issuersLocale.put(playerUniqueId, locale);
issuersLocaleString.put(playerUniqueId, (String) localeString);
if (!Objects.equals(locale, prev)) {
this.notifyLocaleChange(getCommandIssuer(player), prev, locale);
Locale locale = null;
try {
locale = player.locale();
} catch (NoSuchMethodError ignored) {
Field entityField = getEntityField(player);
if (entityField != null) {
Object nmsPlayer = entityField.get(player);
if (nmsPlayer != null) {
Field localeField = nmsPlayer.getClass().getDeclaredField("locale");
localeField.setAccessible(true);
Object localeString = localeField.get(nmsPlayer);
if (localeString instanceof String) {
if (!localeString.equals(issuersLocaleString.get(player.getUniqueId()))) {
String[] split = ACFPatterns.UNDERSCORE.split((String) localeString);
locale = split.length > 1 ? new Locale(split[0], split[1]) : new Locale(split[0]);
}
}
}
}
}
if (locale != null) {
UUID playerUniqueId = player.getUniqueId();
Locale prev = issuersLocale.put(playerUniqueId, locale);
issuersLocaleString.put(playerUniqueId, locale.toString());
if (!Objects.equals(locale, prev)) {
this.notifyLocaleChange(getCommandIssuer(player), prev, locale);
}
}
} catch (Exception e) {
cantReadLocale = true;
this.scheduler.cancelLocaleTask();