Fix getLocale on older Bukkit releases (#2915)

This commit is contained in:
Luck 2021-02-27 15:43:46 +00:00
parent 4ff2c75538
commit 843eaaed0c
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 71 additions and 3 deletions

View File

@ -28,6 +28,7 @@ package me.lucko.luckperms.bukkit.listeners;
import me.lucko.luckperms.bukkit.LPBukkitPlugin; import me.lucko.luckperms.bukkit.LPBukkitPlugin;
import me.lucko.luckperms.bukkit.inject.permissible.LuckPermsPermissible; import me.lucko.luckperms.bukkit.inject.permissible.LuckPermsPermissible;
import me.lucko.luckperms.bukkit.inject.permissible.PermissibleInjector; 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.config.ConfigKeys;
import me.lucko.luckperms.common.locale.Message; import me.lucko.luckperms.common.locale.Message;
import me.lucko.luckperms.common.locale.TranslationManager; import me.lucko.luckperms.common.locale.TranslationManager;
@ -179,7 +180,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme
if (this.detectedCraftBukkitOfflineMode) { if (this.detectedCraftBukkitOfflineMode) {
printCraftBukkitOfflineModeError(); 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)); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason));
return; return;
} }
@ -190,7 +191,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme
" - denying login."); " - 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)); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason));
return; return;
} }
@ -208,7 +209,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme
this.plugin.getLogger().warn("Exception thrown when setting up permissions for " + this.plugin.getLogger().warn("Exception thrown when setting up permissions for " +
player.getUniqueId() + " - " + player.getName() + " - denying login.", t); 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)); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason));
return; return;
} }

View File

@ -0,0 +1,67 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* 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<Player, String> GET_LOCALE_FUNCTION;
static {
Function<Player, String> 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);
}
}