Fixed NPE in User when doing #getTranslation(reference, ...variables)

it was due to the null value possibly returned by the LocalesManager#get(user, reference) method when nothing has been found, leading the variables replacement to cause a NPE.

User#getTranslation(reference, ...variables) now does the check and return the reference if the translation found equals null BEFORE trying to replace variables.
User#getTranslationOrNothing(reference, ...variables) works the same as before, it just checks if the returned translation is the same than the reference (therefore it returns the blank String), otherwise it sends the translation.
This commit is contained in:
Florian CUNY 2018-01-03 21:41:30 +01:00
parent f8c1dba503
commit 62480d7249
2 changed files with 13 additions and 7 deletions

View File

@ -151,31 +151,37 @@ public class User {
}
/**
* Gets a translation for this user
* Gets a translation of this reference for this user.
* @param reference
* @param variables
* @return Translated string with colors converted
* @return Translated string with colors converted, or the reference if nothing has been found
*/
public String getTranslation(String reference, String... variables) {
// Get translation
String translation = plugin.getLocalesManager().get(this, reference);
// If no translation has been found, return the reference for debug purposes.
if (translation == null) return reference;
// Then replace variables
if (variables.length > 1) {
for (int i = 0; i < variables.length; i += 2) {
translation = translation.replace(variables[i], variables[i+1]);
}
}
return translation == null ? reference : ChatColor.translateAlternateColorCodes('&', translation);
return ChatColor.translateAlternateColorCodes('&', translation);
}
/**
* Returns blank string if reference is not available
* Gets a translation of this reference for this user.
* @param reference
* @param variables
* @return
* @return Translated string with colors converted, or a blank String if nothing has been found
*/
public String getTranslationOrNothing(String reference, String... variables) {
String translation = getTranslation(reference, variables);
return translation == null ? "" : translation;
return translation.equals(reference) ? "" : translation;
}
/**

View File

@ -30,7 +30,7 @@ public final class LocalesManager {
* Gets the reference from the locale file for this user
* @param user
* @param reference
* @return translated string, or if the translation does not exist, the default language version, or if that does not exist null
* @return the translated string, or if the translation does not exist, the default language version, or if that does not exist null
*/
public String get(User user, String reference) {
BSBLocale locale = languages.get(user.getLocale());