Fix null player locale causing NPE (#2678)

This commit is contained in:
Luck 2020-10-20 12:25:35 +01:00
parent 7d47b31bcb
commit b9eb022011
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
9 changed files with 47 additions and 41 deletions

View File

@ -36,7 +36,6 @@ import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.translation.GlobalTranslator;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -48,7 +47,6 @@ import org.bukkit.event.player.PlayerQuitEvent;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -129,7 +127,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme
// deny the connection // deny the connection
this.deniedAsyncLogin.add(e.getUniqueId()); 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)); e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason));
this.plugin.getEventDispatcher().dispatchPlayerLoginProcess(e.getUniqueId(), e.getName(), null); this.plugin.getEventDispatcher().dispatchPlayerLoginProcess(e.getUniqueId(), e.getName(), null);
} }
@ -178,7 +176,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme
if (this.detectedCraftBukkitOfflineMode) { if (this.detectedCraftBukkitOfflineMode) {
printCraftBukkitOfflineModeError(); 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)); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason));
return; return;
} }
@ -189,7 +187,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme
" - denying login."); " - 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)); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason));
return; return;
} }
@ -208,7 +206,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme
player.getUniqueId() + " - " + player.getName() + " - denying login."); player.getUniqueId() + " - " + player.getName() + " - denying login.");
t.printStackTrace(); 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)); e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason));
return; return;
} }

View File

@ -28,12 +28,12 @@ package me.lucko.luckperms.bungee.listeners;
import me.lucko.luckperms.bungee.LPBungeePlugin; import me.lucko.luckperms.bungee.LPBungeePlugin;
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.model.User; import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener; import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer; 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.PendingConnection;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.LoginEvent; 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.EventHandler;
import net.md_5.bungee.event.EventPriority; import net.md_5.bungee.event.EventPriority;
import java.util.Locale;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class BungeeConnectionListener extends AbstractConnectionListener implements Listener { public class BungeeConnectionListener extends AbstractConnectionListener implements Listener {
@ -100,7 +99,7 @@ public class BungeeConnectionListener extends AbstractConnectionListener impleme
// there was some error loading // there was some error loading
if (this.plugin.getConfiguration().get(ConfigKeys.CANCEL_FAILED_LOGINS)) { if (this.plugin.getConfiguration().get(ConfigKeys.CANCEL_FAILED_LOGINS)) {
// cancel the login attempt // 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.setCancelReason(BungeeComponentSerializer.get().serialize(reason));
e.setCancelled(true); e.setCancelled(true);
} }
@ -132,7 +131,7 @@ public class BungeeConnectionListener extends AbstractConnectionListener impleme
if (this.plugin.getConfiguration().get(ConfigKeys.CANCEL_FAILED_LOGINS)) { if (this.plugin.getConfiguration().get(ConfigKeys.CANCEL_FAILED_LOGINS)) {
// disconnect the user // 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)); e.getPlayer().disconnect(BungeeComponentSerializer.get().serialize(reason));
} else { } else {
// just send a message // just send a message

View File

@ -178,7 +178,7 @@ public class TranslationsCommand extends SingleCommand {
LanguageInfo(String id, JsonObject data) { LanguageInfo(String id, JsonObject data) {
this.id = id; this.id = id;
this.name = data.get("name").getAsString(); 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.progress = data.get("progress").getAsInt();
this.contributors = new ArrayList<>(); this.contributors = new ArrayList<>();
for (JsonElement contributor : data.get("contributors").getAsJsonArray()) { for (JsonElement contributor : data.get("contributors").getAsJsonArray()) {

View File

@ -30,11 +30,14 @@ import com.google.common.collect.Maps;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.translation.GlobalTranslator; import net.kyori.adventure.translation.GlobalTranslator;
import net.kyori.adventure.translation.TranslationRegistry; import net.kyori.adventure.translation.TranslationRegistry;
import net.kyori.adventure.translation.Translator; import net.kyori.adventure.translation.Translator;
import net.kyori.adventure.util.UTF8ResourceBundleControl; import net.kyori.adventure.util.UTF8ResourceBundleControl;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -137,7 +140,7 @@ public class TranslationManager {
private Map.Entry<Locale, ResourceBundle> loadCustomTranslationFile(Path translationFile) { private Map.Entry<Locale, ResourceBundle> loadCustomTranslationFile(Path translationFile) {
String fileName = translationFile.getFileName().toString(); String fileName = translationFile.getFileName().toString();
String localeString = fileName.substring(0, fileName.length() - ".properties".length()); String localeString = fileName.substring(0, fileName.length() - ".properties".length());
Locale locale = parseLocale(localeString, null); Locale locale = parseLocale(localeString);
if (locale == null) { if (locale == null) {
this.plugin.getLogger().warn("Unknown locale '" + localeString + "' - unable to register."); this.plugin.getLogger().warn("Unknown locale '" + localeString + "' - unable to register.");
@ -159,13 +162,26 @@ public class TranslationManager {
return Maps.immutableEntry(locale, bundle); return Maps.immutableEntry(locale, bundle);
} }
public static Locale parseLocale(String locale, Locale defaultLocale) { public static Component render(Component component) {
return render(component, Locale.getDefault());
}
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) { if (locale == null) {
return defaultLocale; locale = Locale.getDefault();
if (locale == null) {
locale = DEFAULT_LOCALE;
}
}
return GlobalTranslator.render(component, locale);
} }
Locale parsed = Translator.parseLocale(locale); public static @Nullable Locale parseLocale(@Nullable String locale) {
return parsed != null ? parsed : defaultLocale; return locale == null ? null : Translator.parseLocale(locale);
} }
} }

View File

@ -25,12 +25,12 @@
package me.lucko.luckperms.nukkit; 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.Sender;
import me.lucko.luckperms.common.sender.SenderFactory; import me.lucko.luckperms.common.sender.SenderFactory;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.translation.GlobalTranslator;
import net.luckperms.api.util.Tristate; import net.luckperms.api.util.Tristate;
import cn.nukkit.Player; import cn.nukkit.Player;
@ -63,13 +63,11 @@ public class NukkitSenderFactory extends SenderFactory<LPNukkitPlugin, CommandSe
@Override @Override
protected void sendMessage(CommandSender sender, Component message) { protected void sendMessage(CommandSender sender, Component message) {
// Fallback to legacy format // Fallback to legacy format
Locale locale; Locale locale = null;
if (sender instanceof Player) { if (sender instanceof Player) {
locale = ((Player) sender).getLocale(); locale = ((Player) sender).getLocale();
} else {
locale = Locale.getDefault();
} }
Component rendered = GlobalTranslator.render(message, locale); Component rendered = TranslationManager.render(message, locale);
sender.sendMessage(LegacyComponentSerializer.legacySection().serialize(rendered)); sender.sendMessage(LegacyComponentSerializer.legacySection().serialize(rendered));
} }

View File

@ -27,6 +27,7 @@ package me.lucko.luckperms.nukkit.listeners;
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.model.User; import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener; import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener;
import me.lucko.luckperms.nukkit.LPNukkitPlugin; import me.lucko.luckperms.nukkit.LPNukkitPlugin;
@ -35,7 +36,6 @@ import me.lucko.luckperms.nukkit.inject.permissible.PermissibleInjector;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.translation.GlobalTranslator;
import cn.nukkit.Player; import cn.nukkit.Player;
import cn.nukkit.event.EventHandler; import cn.nukkit.event.EventHandler;
@ -47,7 +47,6 @@ import cn.nukkit.event.player.PlayerQuitEvent;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -99,7 +98,7 @@ public class NukkitConnectionListener extends AbstractConnectionListener impleme
// deny the connection // deny the connection
this.deniedAsyncLogin.add(e.getUuid()); this.deniedAsyncLogin.add(e.getUuid());
Component reason = GlobalTranslator.render(Message.LOADING_DATABASE_ERROR.build(), Locale.getDefault()); Component reason = TranslationManager.render(Message.LOADING_DATABASE_ERROR.build());
e.disAllow(LegacyComponentSerializer.legacySection().serialize(reason)); e.disAllow(LegacyComponentSerializer.legacySection().serialize(reason));
this.plugin.getEventDispatcher().dispatchPlayerLoginProcess(e.getUuid(), e.getName(), null); this.plugin.getEventDispatcher().dispatchPlayerLoginProcess(e.getUuid(), e.getName(), null);
} }
@ -150,7 +149,7 @@ public class NukkitConnectionListener extends AbstractConnectionListener impleme
} }
e.setCancelled(); e.setCancelled();
Component reason = GlobalTranslator.render(Message.LOADING_STATE_ERROR.build(), player.getLocale()); Component reason = TranslationManager.render(Message.LOADING_STATE_ERROR.build());
e.setKickMessage(LegacyComponentSerializer.legacySection().serialize(reason)); e.setKickMessage(LegacyComponentSerializer.legacySection().serialize(reason));
return; return;
} }
@ -170,7 +169,7 @@ public class NukkitConnectionListener extends AbstractConnectionListener impleme
t.printStackTrace(); t.printStackTrace();
e.setCancelled(); e.setCancelled();
Component reason = GlobalTranslator.render(Message.LOADING_SETUP_ERROR.build(), player.getLocale()); Component reason = TranslationManager.render(Message.LOADING_SETUP_ERROR.build());
e.setKickMessage(LegacyComponentSerializer.legacySection().serialize(reason)); e.setKickMessage(LegacyComponentSerializer.legacySection().serialize(reason));
return; return;
} }

View File

@ -27,13 +27,13 @@ package me.lucko.luckperms.sponge.listeners;
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.model.User; import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener; import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener;
import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.LPSpongePlugin;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.spongeapi.SpongeComponentSerializer; import net.kyori.adventure.text.serializer.spongeapi.SpongeComponentSerializer;
import net.kyori.adventure.translation.GlobalTranslator;
import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order; import org.spongepowered.api.event.Order;
@ -44,7 +44,6 @@ import org.spongepowered.api.util.Tristate;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -101,7 +100,7 @@ public class SpongeConnectionListener extends AbstractConnectionListener {
e.setCancelled(true); e.setCancelled(true);
e.setMessageCancelled(false); e.setMessageCancelled(false);
Component reason = GlobalTranslator.render(Message.LOADING_DATABASE_ERROR.build(), Locale.getDefault()); Component reason = TranslationManager.render(Message.LOADING_DATABASE_ERROR.build());
e.setMessage(SpongeComponentSerializer.get().serialize(reason)); e.setMessage(SpongeComponentSerializer.get().serialize(reason));
this.plugin.getEventDispatcher().dispatchPlayerLoginProcess(profile.getUniqueId(), username, null); this.plugin.getEventDispatcher().dispatchPlayerLoginProcess(profile.getUniqueId(), username, null);
} }
@ -155,7 +154,7 @@ public class SpongeConnectionListener extends AbstractConnectionListener {
e.setCancelled(true); e.setCancelled(true);
e.setMessageCancelled(false); e.setMessageCancelled(false);
Component reason = GlobalTranslator.render(Message.LOADING_STATE_ERROR.build(), Locale.getDefault()); Component reason = TranslationManager.render(Message.LOADING_STATE_ERROR.build());
e.setMessage(SpongeComponentSerializer.get().serialize(reason)); e.setMessage(SpongeComponentSerializer.get().serialize(reason));
} }
} }

View File

@ -28,13 +28,13 @@ package me.lucko.luckperms.velocity;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import me.lucko.luckperms.common.locale.TranslationManager;
import me.lucko.luckperms.common.sender.Sender; import me.lucko.luckperms.common.sender.Sender;
import me.lucko.luckperms.common.sender.SenderFactory; import me.lucko.luckperms.common.sender.SenderFactory;
import me.lucko.luckperms.velocity.service.CompatibilityUtil; import me.lucko.luckperms.velocity.service.CompatibilityUtil;
import me.lucko.luckperms.velocity.util.AdventureCompat; import me.lucko.luckperms.velocity.util.AdventureCompat;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.translation.GlobalTranslator;
import net.luckperms.api.util.Tristate; import net.luckperms.api.util.Tristate;
import java.util.Locale; import java.util.Locale;
@ -63,13 +63,11 @@ public class VelocitySenderFactory extends SenderFactory<LPVelocityPlugin, Comma
@Override @Override
protected void sendMessage(CommandSource source, Component message) { protected void sendMessage(CommandSource source, Component message) {
Locale locale; Locale locale = null;
if (source instanceof Player) { if (source instanceof Player) {
locale = ((Player) source).getPlayerSettings().getLocale(); locale = ((Player) source).getPlayerSettings().getLocale();
} else {
locale = Locale.getDefault();
} }
Component rendered = GlobalTranslator.render(message, locale); Component rendered = TranslationManager.render(message, locale);
AdventureCompat.sendMessage(source, rendered); AdventureCompat.sendMessage(source, rendered);
} }

View File

@ -34,14 +34,13 @@ import com.velocitypowered.api.proxy.Player;
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.model.User; import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener; import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener;
import me.lucko.luckperms.velocity.LPVelocityPlugin; import me.lucko.luckperms.velocity.LPVelocityPlugin;
import me.lucko.luckperms.velocity.service.PlayerPermissionProvider; import me.lucko.luckperms.velocity.service.PlayerPermissionProvider;
import me.lucko.luckperms.velocity.util.AdventureCompat; import me.lucko.luckperms.velocity.util.AdventureCompat;
import net.kyori.adventure.translation.GlobalTranslator;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -108,7 +107,7 @@ public class VelocityConnectionListener extends AbstractConnectionListener {
public void onPlayerLogin(LoginEvent e) { public void onPlayerLogin(LoginEvent e) {
final Player player = e.getPlayer(); final Player player = e.getPlayer();
if (this.deniedLogin.remove(player.getUniqueId())) { if (this.deniedLogin.remove(player.getUniqueId())) {
e.setResult(AdventureCompat.deniedResult(GlobalTranslator.render(Message.LOADING_DATABASE_ERROR.build(), player.getPlayerSettings().getLocale()))); e.setResult(AdventureCompat.deniedResult(TranslationManager.render(Message.LOADING_DATABASE_ERROR.build(), player.getPlayerSettings().getLocale())));
} }
} }
@ -136,7 +135,7 @@ public class VelocityConnectionListener extends AbstractConnectionListener {
if (this.plugin.getConfiguration().get(ConfigKeys.CANCEL_FAILED_LOGINS)) { if (this.plugin.getConfiguration().get(ConfigKeys.CANCEL_FAILED_LOGINS)) {
// disconnect the user // disconnect the user
e.setResult(AdventureCompat.deniedResult(GlobalTranslator.render(Message.LOADING_STATE_ERROR.build(), player.getPlayerSettings().getLocale()))); e.setResult(AdventureCompat.deniedResult(TranslationManager.render(Message.LOADING_STATE_ERROR.build(), player.getPlayerSettings().getLocale())));
} else { } else {
// just send a message // just send a message
this.plugin.getBootstrap().getScheduler().asyncLater(() -> { this.plugin.getBootstrap().getScheduler().asyncLater(() -> {