From b9eb022011389f7a110cc507571476c5214d9581 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 20 Oct 2020 12:25:35 +0100 Subject: [PATCH] Fix null player locale causing NPE (#2678) --- .../listeners/BukkitConnectionListener.java | 10 +++---- .../listeners/BungeeConnectionListener.java | 7 ++--- .../commands/misc/TranslationsCommand.java | 2 +- .../common/locale/TranslationManager.java | 30 ++++++++++++++----- .../luckperms/nukkit/NukkitSenderFactory.java | 8 ++--- .../listeners/NukkitConnectionListener.java | 9 +++--- .../listeners/SpongeConnectionListener.java | 7 ++--- .../velocity/VelocitySenderFactory.java | 8 ++--- .../listeners/VelocityConnectionListener.java | 7 ++--- 9 files changed, 47 insertions(+), 41 deletions(-) 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 13c228c04..78dccb9dc 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 @@ -36,7 +36,6 @@ import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import net.kyori.adventure.translation.GlobalTranslator; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -48,7 +47,6 @@ import org.bukkit.event.player.PlayerQuitEvent; import java.util.Collections; import java.util.HashSet; -import java.util.Locale; import java.util.Set; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -129,7 +127,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme // deny the connection this.deniedAsyncLogin.add(e.getUniqueId()); - Component reason = GlobalTranslator.render(Message.LOADING_DATABASE_ERROR.build(), Locale.getDefault()); + Component reason = TranslationManager.render(Message.LOADING_DATABASE_ERROR.build()); e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason)); this.plugin.getEventDispatcher().dispatchPlayerLoginProcess(e.getUniqueId(), e.getName(), null); } @@ -178,7 +176,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme if (this.detectedCraftBukkitOfflineMode) { printCraftBukkitOfflineModeError(); - Component reason = GlobalTranslator.render(Message.LOADING_STATE_ERROR_CB_OFFLINE_MODE.build(), TranslationManager.parseLocale(player.getLocale(), Locale.getDefault())); + Component reason = TranslationManager.render(Message.LOADING_STATE_ERROR_CB_OFFLINE_MODE.build(), player.getLocale()); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason)); return; } @@ -189,7 +187,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme " - denying login."); } - Component reason = GlobalTranslator.render(Message.LOADING_STATE_ERROR.build(), TranslationManager.parseLocale(player.getLocale(), Locale.getDefault())); + Component reason = TranslationManager.render(Message.LOADING_STATE_ERROR.build(), player.getLocale()); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason)); return; } @@ -208,7 +206,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme player.getUniqueId() + " - " + player.getName() + " - denying login."); t.printStackTrace(); - Component reason = GlobalTranslator.render(Message.LOADING_SETUP_ERROR.build(), TranslationManager.parseLocale(player.getLocale(), Locale.getDefault())); + Component reason = TranslationManager.render(Message.LOADING_SETUP_ERROR.build(), player.getLocale()); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason)); return; } diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeeConnectionListener.java b/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeeConnectionListener.java index 38467c802..cb85f5daf 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeeConnectionListener.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeeConnectionListener.java @@ -28,12 +28,12 @@ package me.lucko.luckperms.bungee.listeners; import me.lucko.luckperms.bungee.LPBungeePlugin; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.locale.Message; +import me.lucko.luckperms.common.locale.TranslationManager; import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer; -import net.kyori.adventure.translation.GlobalTranslator; import net.md_5.bungee.api.connection.PendingConnection; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.event.LoginEvent; @@ -43,7 +43,6 @@ import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventPriority; -import java.util.Locale; import java.util.concurrent.TimeUnit; public class BungeeConnectionListener extends AbstractConnectionListener implements Listener { @@ -100,7 +99,7 @@ public class BungeeConnectionListener extends AbstractConnectionListener impleme // there was some error loading if (this.plugin.getConfiguration().get(ConfigKeys.CANCEL_FAILED_LOGINS)) { // cancel the login attempt - Component reason = GlobalTranslator.render(Message.LOADING_DATABASE_ERROR.build(), Locale.getDefault()); + Component reason = TranslationManager.render(Message.LOADING_DATABASE_ERROR.build()); e.setCancelReason(BungeeComponentSerializer.get().serialize(reason)); e.setCancelled(true); } @@ -132,7 +131,7 @@ public class BungeeConnectionListener extends AbstractConnectionListener impleme if (this.plugin.getConfiguration().get(ConfigKeys.CANCEL_FAILED_LOGINS)) { // disconnect the user - Component reason = GlobalTranslator.render(Message.LOADING_DATABASE_ERROR.build(), player.getLocale()); + Component reason = TranslationManager.render(Message.LOADING_DATABASE_ERROR.build()); e.getPlayer().disconnect(BungeeComponentSerializer.get().serialize(reason)); } else { // just send a message diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/misc/TranslationsCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/misc/TranslationsCommand.java index fd821a614..2438fa0c3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/misc/TranslationsCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/misc/TranslationsCommand.java @@ -178,7 +178,7 @@ public class TranslationsCommand extends SingleCommand { LanguageInfo(String id, JsonObject data) { this.id = id; this.name = data.get("name").getAsString(); - this.locale = Objects.requireNonNull(TranslationManager.parseLocale(data.get("localeTag").getAsString(), null)); + this.locale = Objects.requireNonNull(TranslationManager.parseLocale(data.get("localeTag").getAsString())); this.progress = data.get("progress").getAsInt(); this.contributors = new ArrayList<>(); for (JsonElement contributor : data.get("contributors").getAsJsonArray()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/locale/TranslationManager.java b/common/src/main/java/me/lucko/luckperms/common/locale/TranslationManager.java index 09dc74522..f2d1b33c2 100644 --- a/common/src/main/java/me/lucko/luckperms/common/locale/TranslationManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/locale/TranslationManager.java @@ -30,11 +30,14 @@ import com.google.common.collect.Maps; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; import net.kyori.adventure.translation.GlobalTranslator; import net.kyori.adventure.translation.TranslationRegistry; import net.kyori.adventure.translation.Translator; import net.kyori.adventure.util.UTF8ResourceBundleControl; +import org.checkerframework.checker.nullness.qual.Nullable; + import java.io.BufferedReader; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -137,7 +140,7 @@ public class TranslationManager { private Map.Entry loadCustomTranslationFile(Path translationFile) { String fileName = translationFile.getFileName().toString(); String localeString = fileName.substring(0, fileName.length() - ".properties".length()); - Locale locale = parseLocale(localeString, null); + Locale locale = parseLocale(localeString); if (locale == null) { this.plugin.getLogger().warn("Unknown locale '" + localeString + "' - unable to register."); @@ -159,13 +162,26 @@ public class TranslationManager { return Maps.immutableEntry(locale, bundle); } - public static Locale parseLocale(String locale, Locale defaultLocale) { - if (locale == null) { - return defaultLocale; - } + public static Component render(Component component) { + return render(component, Locale.getDefault()); + } - Locale parsed = Translator.parseLocale(locale); - return parsed != null ? parsed : defaultLocale; + public static Component render(Component component, @Nullable String locale) { + return render(component, parseLocale(locale)); + } + + public static Component render(Component component, @Nullable Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + if (locale == null) { + locale = DEFAULT_LOCALE; + } + } + return GlobalTranslator.render(component, locale); + } + + public static @Nullable Locale parseLocale(@Nullable String locale) { + return locale == null ? null : Translator.parseLocale(locale); } } diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/NukkitSenderFactory.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/NukkitSenderFactory.java index dc72b2bd8..3ac1e2be2 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/NukkitSenderFactory.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/NukkitSenderFactory.java @@ -25,12 +25,12 @@ package me.lucko.luckperms.nukkit; +import me.lucko.luckperms.common.locale.TranslationManager; import me.lucko.luckperms.common.sender.Sender; import me.lucko.luckperms.common.sender.SenderFactory; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import net.kyori.adventure.translation.GlobalTranslator; import net.luckperms.api.util.Tristate; import cn.nukkit.Player; @@ -63,13 +63,11 @@ public class NukkitSenderFactory extends SenderFactory {