Refactor message handlers into injectable components (preparation for #1467)
This commit is contained in:
@@ -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