Skip to content

Commit

Permalink
Use the player locale api instead of reflection to fix issues #402
Browse files Browse the repository at this point in the history
  • Loading branch information
chickeneer committed Oct 2, 2023
1 parent d22fae6 commit 26d72f2
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions bukkit/src/main/java/co/aikar/commands/BukkitCommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,33 +315,34 @@ void readPlayerLocale(Player player) {
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();
Expand Down

0 comments on commit 26d72f2

Please sign in to comment.