diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/listeners/BukkitConnectionListener.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/listeners/BukkitConnectionListener.java index 3da6280c7..747754454 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/listeners/BukkitConnectionListener.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/listeners/BukkitConnectionListener.java @@ -28,6 +28,7 @@ package me.lucko.luckperms.bukkit.listeners; import me.lucko.luckperms.bukkit.LPBukkitPlugin; import me.lucko.luckperms.bukkit.inject.permissible.LuckPermsPermissible; import me.lucko.luckperms.bukkit.inject.permissible.PermissibleInjector; +import me.lucko.luckperms.bukkit.util.PlayerLocaleUtil; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.locale.Message; import me.lucko.luckperms.common.locale.TranslationManager; @@ -179,7 +180,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme if (this.detectedCraftBukkitOfflineMode) { printCraftBukkitOfflineModeError(); - Component reason = TranslationManager.render(Message.LOADING_STATE_ERROR_CB_OFFLINE_MODE.build(), player.getLocale()); + Component reason = TranslationManager.render(Message.LOADING_STATE_ERROR_CB_OFFLINE_MODE.build(), PlayerLocaleUtil.getLocale(player)); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason)); return; } @@ -190,7 +191,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme " - denying login."); } - Component reason = TranslationManager.render(Message.LOADING_STATE_ERROR.build(), player.getLocale()); + Component reason = TranslationManager.render(Message.LOADING_STATE_ERROR.build(), PlayerLocaleUtil.getLocale(player)); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason)); return; } @@ -208,7 +209,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme this.plugin.getLogger().warn("Exception thrown when setting up permissions for " + player.getUniqueId() + " - " + player.getName() + " - denying login.", t); - Component reason = TranslationManager.render(Message.LOADING_SETUP_ERROR.build(), player.getLocale()); + Component reason = TranslationManager.render(Message.LOADING_SETUP_ERROR.build(), PlayerLocaleUtil.getLocale(player)); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason)); return; } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/util/PlayerLocaleUtil.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/util/PlayerLocaleUtil.java new file mode 100644 index 000000000..19f7af933 --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/util/PlayerLocaleUtil.java @@ -0,0 +1,67 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.bukkit.util; + +import org.bukkit.entity.Player; + +import java.lang.reflect.Method; +import java.util.function.Function; + +public final class PlayerLocaleUtil { + private PlayerLocaleUtil() {} + + private static final Function GET_LOCALE_FUNCTION; + + static { + Function function; + try { + // modern bukkit + Player.class.getMethod("getLocale"); + function = Player::getLocale; + } catch (ReflectiveOperationException ex) { + try { + // legacy spigot method + Method legacyMethod = Player.Spigot.class.getMethod("getLocale"); + function = player -> { + try { + return (String) legacyMethod.invoke(player); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + }; + } catch (ReflectiveOperationException e) { + // fallback + function = player -> null; + } + } + GET_LOCALE_FUNCTION = function; + } + + public static String getLocale(Player player) { + return GET_LOCALE_FUNCTION.apply(player); + } + +}