Refactor message handlers into injectable components (preparation for #1467)
This commit is contained in:
@@ -49,7 +49,7 @@ public class MessagesCommand implements ExecutableCommand {
|
||||
try {
|
||||
helpTranslationGenerator.updateHelpFile();
|
||||
sender.sendMessage("Successfully updated the help file");
|
||||
helpMessagesService.reload();
|
||||
helpMessagesService.reloadMessagesFile();
|
||||
} catch (IOException e) {
|
||||
sender.sendMessage("Could not update help file: " + e.getMessage());
|
||||
ConsoleLogger.logException("Could not update help file:", e);
|
||||
@@ -65,7 +65,7 @@ public class MessagesCommand implements ExecutableCommand {
|
||||
getMessagePath(DEFAULT_LANGUAGE))
|
||||
.executeCopy(sender);
|
||||
if (isFileUpdated) {
|
||||
messages.reload();
|
||||
messages.reloadMessagesFile();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
sender.sendMessage("Could not update messages: " + e.getMessage());
|
||||
|
||||
@@ -4,9 +4,7 @@ import com.google.common.base.CaseFormat;
|
||||
import fr.xephi.authme.command.CommandArgumentDescription;
|
||||
import fr.xephi.authme.command.CommandDescription;
|
||||
import fr.xephi.authme.command.CommandUtils;
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
import fr.xephi.authme.message.MessageFileHandlerProvider;
|
||||
import fr.xephi.authme.message.MessageFileHandler;
|
||||
import fr.xephi.authme.message.HelpMessagesFileHandler;
|
||||
import fr.xephi.authme.permission.DefaultPermission;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -16,20 +14,18 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* Manages translatable help messages.
|
||||
*/
|
||||
public class HelpMessagesService implements Reloadable {
|
||||
public class HelpMessagesService {
|
||||
|
||||
private static final String COMMAND_PREFIX = "commands.";
|
||||
private static final String DESCRIPTION_SUFFIX = ".description";
|
||||
private static final String DETAILED_DESCRIPTION_SUFFIX = ".detailedDescription";
|
||||
private static final String DEFAULT_PERMISSIONS_PATH = "common.defaultPermissions.";
|
||||
|
||||
private final MessageFileHandlerProvider messageFileHandlerProvider;
|
||||
private MessageFileHandler messageFileHandler;
|
||||
private final HelpMessagesFileHandler helpMessagesFileHandler;
|
||||
|
||||
@Inject
|
||||
HelpMessagesService(MessageFileHandlerProvider messageFileHandlerProvider) {
|
||||
this.messageFileHandlerProvider = messageFileHandlerProvider;
|
||||
reload();
|
||||
HelpMessagesService(HelpMessagesFileHandler helpMessagesFileHandler) {
|
||||
this.helpMessagesFileHandler = helpMessagesFileHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,7 +36,7 @@ public class HelpMessagesService implements Reloadable {
|
||||
*/
|
||||
public CommandDescription buildLocalizedDescription(CommandDescription command) {
|
||||
final String path = COMMAND_PREFIX + getCommandSubPath(command);
|
||||
if (!messageFileHandler.hasSection(path)) {
|
||||
if (!helpMessagesFileHandler.hasSection(path)) {
|
||||
// Messages file does not have a section for this command - return the provided command
|
||||
return command;
|
||||
}
|
||||
@@ -72,36 +68,39 @@ public class HelpMessagesService implements Reloadable {
|
||||
}
|
||||
|
||||
public String getMessage(HelpMessage message) {
|
||||
return messageFileHandler.getMessage(message.getKey());
|
||||
return helpMessagesFileHandler.getMessage(message.getKey());
|
||||
}
|
||||
|
||||
public String getMessage(HelpSection section) {
|
||||
return messageFileHandler.getMessage(section.getKey());
|
||||
return helpMessagesFileHandler.getMessage(section.getKey());
|
||||
}
|
||||
|
||||
public String getMessage(DefaultPermission defaultPermission) {
|
||||
// e.g. {default_permissions_path}.opOnly for DefaultPermission.OP_ONLY
|
||||
String path = DEFAULT_PERMISSIONS_PATH + getDefaultPermissionsSubPath(defaultPermission);
|
||||
return messageFileHandler.getMessage(path);
|
||||
return helpMessagesFileHandler.getMessage(path);
|
||||
}
|
||||
|
||||
public static String getDefaultPermissionsSubPath(DefaultPermission defaultPermission) {
|
||||
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, defaultPermission.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() {
|
||||
messageFileHandler = messageFileHandlerProvider.initializeHandler(
|
||||
lang -> "messages/help_" + lang + ".yml");
|
||||
}
|
||||
|
||||
private String getText(String path, Supplier<String> defaultTextGetter) {
|
||||
String message = messageFileHandler.getMessageIfExists(path);
|
||||
String message = helpMessagesFileHandler.getMessageIfExists(path);
|
||||
return message == null
|
||||
? defaultTextGetter.get()
|
||||
: message;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Triggers a reload of the help messages file. Note that this method is not needed
|
||||
* to be called for /authme reload.
|
||||
*/
|
||||
public void reloadMessagesFile() {
|
||||
helpMessagesFileHandler.reload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the command subpath for the given command (i.e. the path to the translations for the given
|
||||
* command under "commands").
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
package fr.xephi.authme.message;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import fr.xephi.authme.util.FileUtils;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Handles a YAML message file with a default file fallback.
|
||||
*/
|
||||
public abstract class AbstractMessageFileHandler implements Reloadable {
|
||||
|
||||
private static final String DEFAULT_LANGUAGE = "en";
|
||||
|
||||
@DataFolder
|
||||
@Inject
|
||||
private File dataFolder;
|
||||
|
||||
@Inject
|
||||
private Settings settings;
|
||||
|
||||
private String filename;
|
||||
private FileConfiguration configuration;
|
||||
private final String defaultFile;
|
||||
private FileConfiguration defaultConfiguration;
|
||||
|
||||
protected AbstractMessageFileHandler() {
|
||||
this.defaultFile = createFilePath(DEFAULT_LANGUAGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostConstruct
|
||||
public void reload() {
|
||||
String language = settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
|
||||
filename = createFilePath(language);
|
||||
File messagesFile = initializeFile(filename);
|
||||
configuration = YamlConfiguration.loadConfiguration(messagesFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the message file configuration has an entry at the given path.
|
||||
*
|
||||
* @param path the path to verify
|
||||
* @return true if an entry exists for the path in the messages file, false otherwise
|
||||
*/
|
||||
public boolean hasSection(String path) {
|
||||
return configuration.get(path) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message for the given key.
|
||||
*
|
||||
* @param key the key to retrieve the message for
|
||||
* @return the message
|
||||
*/
|
||||
public String getMessage(String key) {
|
||||
String message = configuration.getString(key);
|
||||
|
||||
if (message == null) {
|
||||
ConsoleLogger.warning("Error getting message with key '" + key + "'. "
|
||||
+ "Please update your config file '" + filename + "' or run " + getUpdateCommand());
|
||||
return getDefault(key);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message for the given key only if it exists,
|
||||
* i.e. without falling back to the default file.
|
||||
*
|
||||
* @param key the key to retrieve the message for
|
||||
* @return the message, or {@code null} if not available
|
||||
*/
|
||||
public String getMessageIfExists(String key) {
|
||||
return configuration.getString(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the message from the default file.
|
||||
*
|
||||
* @param key the key to retrieve the message for
|
||||
* @return the message from the default file
|
||||
*/
|
||||
private String getDefault(String key) {
|
||||
if (defaultConfiguration == null) {
|
||||
InputStream stream = FileUtils.getResourceFromJar(defaultFile);
|
||||
defaultConfiguration = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
|
||||
}
|
||||
String message = defaultConfiguration.getString(key);
|
||||
return message == null
|
||||
? "Error retrieving message '" + key + "'"
|
||||
: message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the path to the messages file for the given language code.
|
||||
*
|
||||
* @param language the language code
|
||||
* @return path to the message file for the given language
|
||||
*/
|
||||
protected abstract String createFilePath(String language);
|
||||
|
||||
/**
|
||||
* @return command with which the messages file can be updated; output when a message is missing from the file
|
||||
*/
|
||||
protected abstract String getUpdateCommand();
|
||||
|
||||
/**
|
||||
* Copies the messages file from the JAR to the local messages/ folder if it doesn't exist.
|
||||
*
|
||||
* @param filePath path to the messages file to use
|
||||
* @return the messages file to use
|
||||
*/
|
||||
@VisibleForTesting
|
||||
File initializeFile(String filePath) {
|
||||
File file = new File(dataFolder, filePath);
|
||||
// Check that JAR file exists to avoid logging an error
|
||||
if (FileUtils.getResourceFromJar(filePath) != null && FileUtils.copyFileFromResource(file, filePath)) {
|
||||
return file;
|
||||
}
|
||||
|
||||
if (FileUtils.copyFileFromResource(file, defaultFile)) {
|
||||
return file;
|
||||
} else {
|
||||
ConsoleLogger.warning("Wanted to copy default messages file '" + defaultFile
|
||||
+ "' from JAR but it didn't exist");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package fr.xephi.authme.message;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* File handler for the help_xx.yml resource.
|
||||
*/
|
||||
public class HelpMessagesFileHandler extends AbstractMessageFileHandler {
|
||||
|
||||
@Inject // Trigger injection in the superclass
|
||||
HelpMessagesFileHandler() {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createFilePath(String language) {
|
||||
return "messages/help_" + language + ".yml";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getUpdateCommand() {
|
||||
return "/authme messages help";
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
package fr.xephi.authme.message;
|
||||
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.util.FileUtils;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Handles a YAML message file with a default file fallback.
|
||||
*/
|
||||
public class MessageFileHandler {
|
||||
|
||||
// regular file
|
||||
private final String filename;
|
||||
private final FileConfiguration configuration;
|
||||
private final String updateAddition;
|
||||
// default file
|
||||
private final String defaultFile;
|
||||
private FileConfiguration defaultConfiguration;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param file the file to use for messages
|
||||
* @param defaultFile the default file from the JAR to use if no message is found
|
||||
* @param updateCommand command to update the messages file (nullable) to show in error messages
|
||||
*/
|
||||
public MessageFileHandler(File file, String defaultFile, String updateCommand) {
|
||||
this.filename = file.getName();
|
||||
this.configuration = YamlConfiguration.loadConfiguration(file);
|
||||
this.defaultFile = defaultFile;
|
||||
this.updateAddition = updateCommand == null
|
||||
? ""
|
||||
: " (or run " + updateCommand + ")";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the message file configuration has an entry at the given path.
|
||||
*
|
||||
* @param path the path to verify
|
||||
* @return true if an entry exists for the path in the messages file, false otherwise
|
||||
*/
|
||||
public boolean hasSection(String path) {
|
||||
return configuration.get(path) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message for the given key.
|
||||
*
|
||||
* @param key the key to retrieve the message for
|
||||
* @return the message
|
||||
*/
|
||||
public String getMessage(String key) {
|
||||
String message = configuration.getString(key);
|
||||
|
||||
if (message == null) {
|
||||
ConsoleLogger.warning("Error getting message with key '" + key + "'. "
|
||||
+ "Please update your config file '" + filename + "'" + updateAddition);
|
||||
return getDefault(key);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message for the given key only if it exists,
|
||||
* i.e. without falling back to the default file.
|
||||
*
|
||||
* @param key the key to retrieve the message for
|
||||
* @return the message, or {@code null} if not available
|
||||
*/
|
||||
public String getMessageIfExists(String key) {
|
||||
return configuration.getString(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the message from the default file.
|
||||
*
|
||||
* @param key the key to retrieve the message for
|
||||
* @return the message from the default file
|
||||
*/
|
||||
private String getDefault(String key) {
|
||||
if (defaultConfiguration == null) {
|
||||
InputStream stream = FileUtils.getResourceFromJar(defaultFile);
|
||||
defaultConfiguration = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
|
||||
}
|
||||
String message = defaultConfiguration.getString(key);
|
||||
return message == null
|
||||
? "Error retrieving message '" + key + "'"
|
||||
: message;
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package fr.xephi.authme.message;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import fr.xephi.authme.util.FileUtils;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Injectable creator of {@link MessageFileHandler} instances.
|
||||
*
|
||||
* @see MessageFileHandler
|
||||
*/
|
||||
public class MessageFileHandlerProvider {
|
||||
|
||||
private static final String DEFAULT_LANGUAGE = "en";
|
||||
|
||||
@Inject
|
||||
@DataFolder
|
||||
private File dataFolder;
|
||||
@Inject
|
||||
private Settings settings;
|
||||
|
||||
MessageFileHandlerProvider() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a message file handler with the messages file of the configured language.
|
||||
* Ensures beforehand that the messages file exists or creates it otherwise.
|
||||
*
|
||||
* @param pathBuilder function taking the configured language code as argument and returning the messages file
|
||||
* @return the message file handler
|
||||
*/
|
||||
public MessageFileHandler initializeHandler(Function<String, String> pathBuilder) {
|
||||
return initializeHandler(pathBuilder, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a message file handler with the messages file of the configured language.
|
||||
* Ensures beforehand that the messages file exists or creates it otherwise.
|
||||
*
|
||||
* @param pathBuilder function taking the configured language code as argument and returning the messages file
|
||||
* @param updateCommand command to run to update the languages file (nullable)
|
||||
* @return the message file handler
|
||||
*/
|
||||
public MessageFileHandler initializeHandler(Function<String, String> pathBuilder, String updateCommand) {
|
||||
String language = settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
|
||||
return new MessageFileHandler(
|
||||
initializeFile(language, pathBuilder),
|
||||
pathBuilder.apply(DEFAULT_LANGUAGE),
|
||||
updateCommand);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the messages file from the JAR if it doesn't exist.
|
||||
*
|
||||
* @param language the configured language code
|
||||
* @param pathBuilder function returning message file name with language as argument
|
||||
* @return the messages file to use
|
||||
*/
|
||||
@VisibleForTesting
|
||||
File initializeFile(String language, Function<String, String> pathBuilder) {
|
||||
String filePath = pathBuilder.apply(language);
|
||||
File file = new File(dataFolder, filePath);
|
||||
// Check that JAR file exists to avoid logging an error
|
||||
if (FileUtils.getResourceFromJar(filePath) != null && FileUtils.copyFileFromResource(file, filePath)) {
|
||||
return file;
|
||||
}
|
||||
|
||||
String defaultFilePath = pathBuilder.apply(DEFAULT_LANGUAGE);
|
||||
if (FileUtils.copyFileFromResource(file, defaultFilePath)) {
|
||||
return file;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package fr.xephi.authme.message;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
import fr.xephi.authme.util.expiring.Duration;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -14,7 +13,7 @@ import java.util.concurrent.TimeUnit;
|
||||
/**
|
||||
* Class for retrieving and sending translatable messages to players.
|
||||
*/
|
||||
public class Messages implements Reloadable {
|
||||
public class Messages {
|
||||
|
||||
// Custom Authme tag replaced to new line
|
||||
private static final String NEWLINE_TAG = "%nl%";
|
||||
@@ -33,16 +32,14 @@ public class Messages implements Reloadable {
|
||||
.put(TimeUnit.HOURS, MessageKey.HOURS)
|
||||
.put(TimeUnit.DAYS, MessageKey.DAYS).build();
|
||||
|
||||
private final MessageFileHandlerProvider messageFileHandlerProvider;
|
||||
private MessageFileHandler messageFileHandler;
|
||||
private MessagesFileHandler messagesFileHandler;
|
||||
|
||||
/*
|
||||
* Constructor.
|
||||
*/
|
||||
@Inject
|
||||
Messages(MessageFileHandlerProvider messageFileHandlerProvider) {
|
||||
this.messageFileHandlerProvider = messageFileHandlerProvider;
|
||||
reload();
|
||||
Messages(MessagesFileHandler messagesFileHandler) {
|
||||
this.messagesFileHandler = messagesFileHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,7 +110,7 @@ public class Messages implements Reloadable {
|
||||
*/
|
||||
private String retrieveMessage(MessageKey key) {
|
||||
return formatMessage(
|
||||
messageFileHandler.getMessage(key.getKey()));
|
||||
messagesFileHandler.getMessage(key.getKey()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,10 +135,12 @@ public class Messages implements Reloadable {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() {
|
||||
this.messageFileHandler = messageFileHandlerProvider
|
||||
.initializeHandler(lang -> "messages/messages_" + lang + ".yml", "/authme messages");
|
||||
/**
|
||||
* Triggers a reload of the messages file. Note that this method is not necessary
|
||||
* to be called for /authme reload.
|
||||
*/
|
||||
public void reloadMessagesFile() {
|
||||
messagesFileHandler.reload();
|
||||
}
|
||||
|
||||
private static String formatMessage(String message) {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package fr.xephi.authme.message;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* File handler for the messages_xx.yml resource.
|
||||
*/
|
||||
public class MessagesFileHandler extends AbstractMessageFileHandler {
|
||||
|
||||
@Inject // Trigger injection in the superclass
|
||||
MessagesFileHandler() {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createFilePath(String language) {
|
||||
return "messages/messages_" + language + ".yml";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getUpdateCommand() {
|
||||
return "/authme messages";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user