From 26d72f27c6dd455d047d3aa5c8f4cf89124cf139 Mon Sep 17 00:00:00 2001 From: chickeneer Date: Mon, 2 Oct 2023 16:25:35 -0500 Subject: [PATCH] Use the player locale api instead of reflection to fix issues #402 --- .../aikar/commands/BukkitCommandManager.java | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/bukkit/src/main/java/co/aikar/commands/BukkitCommandManager.java b/bukkit/src/main/java/co/aikar/commands/BukkitCommandManager.java index 3abe275ea..c14e08e35 100755 --- a/bukkit/src/main/java/co/aikar/commands/BukkitCommandManager.java +++ b/bukkit/src/main/java/co/aikar/commands/BukkitCommandManager.java @@ -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();