mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-27 04:35:12 +01:00
#768 Use ConfigMe to set and export messages
- Bukkit's FileConfiguration escapes all special characters making the resulting file hard to use
This commit is contained in:
parent
755f3df33e
commit
264431c214
@ -3,6 +3,7 @@ package fr.xephi.authme.command.executable.authme;
|
|||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
import fr.xephi.authme.command.ExecutableCommand;
|
import fr.xephi.authme.command.ExecutableCommand;
|
||||||
import fr.xephi.authme.initialization.DataFolder;
|
import fr.xephi.authme.initialization.DataFolder;
|
||||||
|
import fr.xephi.authme.message.Messages;
|
||||||
import fr.xephi.authme.service.MessageUpdater;
|
import fr.xephi.authme.service.MessageUpdater;
|
||||||
import fr.xephi.authme.settings.Settings;
|
import fr.xephi.authme.settings.Settings;
|
||||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||||
@ -25,17 +26,22 @@ public class MessagesCommand implements ExecutableCommand {
|
|||||||
@Inject
|
@Inject
|
||||||
@DataFolder
|
@DataFolder
|
||||||
private File dataFolder;
|
private File dataFolder;
|
||||||
|
@Inject
|
||||||
|
private Messages messages;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void executeCommand(CommandSender sender, List<String> arguments) {
|
public void executeCommand(CommandSender sender, List<String> arguments) {
|
||||||
final String language = settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
|
final String language = settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new MessageUpdater(
|
boolean isFileUpdated = new MessageUpdater(
|
||||||
new File(dataFolder, getMessagePath(language)),
|
new File(dataFolder, getMessagePath(language)),
|
||||||
getMessagePath(language),
|
getMessagePath(language),
|
||||||
getMessagePath(DEFAULT_LANGUAGE))
|
getMessagePath(DEFAULT_LANGUAGE))
|
||||||
.executeCopy(sender);
|
.executeCopy(sender);
|
||||||
|
if (isFileUpdated) {
|
||||||
|
messages.reload();
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
sender.sendMessage("Could not update messages: " + e.getMessage());
|
sender.sendMessage("Could not update messages: " + e.getMessage());
|
||||||
ConsoleLogger.logException("Could not update messages:", e);
|
ConsoleLogger.logException("Could not update messages:", e);
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package fr.xephi.authme.service;
|
package fr.xephi.authme.service;
|
||||||
|
|
||||||
|
import com.github.authme.configme.SettingsManager;
|
||||||
|
import com.github.authme.configme.knownproperties.PropertyEntry;
|
||||||
|
import com.github.authme.configme.properties.Property;
|
||||||
|
import com.github.authme.configme.properties.StringProperty;
|
||||||
|
import com.github.authme.configme.resource.YamlFileResource;
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
import fr.xephi.authme.message.MessageKey;
|
import fr.xephi.authme.message.MessageKey;
|
||||||
import fr.xephi.authme.util.FileUtils;
|
import fr.xephi.authme.util.FileUtils;
|
||||||
@ -13,62 +18,82 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates a user's messages file with messages from the JAR files.
|
* Updates a user's messages file with messages from the JAR files.
|
||||||
*/
|
*/
|
||||||
public class MessageUpdater {
|
public class MessageUpdater {
|
||||||
|
|
||||||
private final File userFile;
|
|
||||||
private final FileConfiguration userConfiguration;
|
private final FileConfiguration userConfiguration;
|
||||||
private final FileConfiguration localJarConfiguration;
|
private final FileConfiguration localJarConfiguration;
|
||||||
private final FileConfiguration defaultJarConfiguration;
|
private final FileConfiguration defaultJarConfiguration;
|
||||||
|
|
||||||
|
private final List<PropertyEntry> properties;
|
||||||
|
private final SettingsManager settingsManager;
|
||||||
private boolean hasMissingMessages = false;
|
private boolean hasMissingMessages = false;
|
||||||
|
|
||||||
public MessageUpdater(File userFile, String jarFile, String jarDefaultsFile) throws Exception {
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param userFile messages file in the data folder
|
||||||
|
* @param localJarFile path to messages file in JAR in local language
|
||||||
|
* @param defaultJarFile path to messages file in JAR for default language
|
||||||
|
* @throws Exception if userFile does not exist or no JAR messages file can be loaded
|
||||||
|
*/
|
||||||
|
public MessageUpdater(File userFile, String localJarFile, String defaultJarFile) throws Exception {
|
||||||
if (!userFile.exists()) {
|
if (!userFile.exists()) {
|
||||||
throw new Exception("Local messages file does not exist");
|
throw new Exception("Local messages file does not exist");
|
||||||
}
|
}
|
||||||
this.userFile = userFile;
|
|
||||||
this.userConfiguration = YamlConfiguration.loadConfiguration(userFile);
|
|
||||||
|
|
||||||
localJarConfiguration = loadJarFileOrSendError(jarFile);
|
userConfiguration = YamlConfiguration.loadConfiguration(userFile);
|
||||||
defaultJarConfiguration = jarFile.equals(jarDefaultsFile)
|
localJarConfiguration = loadJarFileOrSendError(localJarFile);
|
||||||
? null
|
defaultJarConfiguration = localJarFile.equals(defaultJarFile) ? null : loadJarFileOrSendError(defaultJarFile);
|
||||||
: loadJarFileOrSendError(jarDefaultsFile);
|
|
||||||
if (localJarConfiguration == null && defaultJarConfiguration == null) {
|
if (localJarConfiguration == null && defaultJarConfiguration == null) {
|
||||||
throw new Exception("Could not load any JAR messages file to copy from");
|
throw new Exception("Could not load any JAR messages file to copy from");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
properties = buildPropertyEntriesForMessageKeys();
|
||||||
|
settingsManager = new SettingsManager(new YamlFileResource(userFile), (r, p) -> true, properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void executeCopy(CommandSender sender) {
|
/**
|
||||||
|
* Copies missing messages to the messages file.
|
||||||
|
*
|
||||||
|
* @param sender sender starting the copy process
|
||||||
|
* @return true if the messages file was updated, false otherwise
|
||||||
|
* @throws Exception if an error occurs during saving
|
||||||
|
*/
|
||||||
|
public boolean executeCopy(CommandSender sender) throws Exception {
|
||||||
copyMissingMessages();
|
copyMissingMessages();
|
||||||
|
|
||||||
if (!hasMissingMessages) {
|
if (!hasMissingMessages) {
|
||||||
sender.sendMessage("No new messages to add");
|
sender.sendMessage("No new messages to add");
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save user configuration file
|
// Save user configuration file
|
||||||
try {
|
try {
|
||||||
userConfiguration.save(userFile);
|
settingsManager.save();
|
||||||
sender.sendMessage("Message file updated with new messages");
|
sender.sendMessage("Message file updated with new messages");
|
||||||
} catch (IOException e) {
|
return true;
|
||||||
sender.sendMessage("Could not save to messages file");
|
} catch (Exception e) {
|
||||||
ConsoleLogger.logException("Could not save new messages to file:", e);
|
throw new Exception("Could not save to messages file: " + StringUtils.formatException(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
private void copyMissingMessages() {
|
private void copyMissingMessages() {
|
||||||
for (MessageKey entry : MessageKey.values()) {
|
for (PropertyEntry entry : properties) {
|
||||||
final String key = entry.getKey();
|
final Property<String> property = (Property<String>) entry.getProperty();
|
||||||
if (!userConfiguration.contains(key)) {
|
String message = userConfiguration.getString(property.getPath());
|
||||||
String jarMessage = getMessageFromJar(key);
|
if (message == null) {
|
||||||
if (jarMessage != null) {
|
hasMissingMessages = true;
|
||||||
hasMissingMessages = true;
|
message = getMessageFromJar(property.getPath());
|
||||||
userConfiguration.set(key, jarMessage);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
settingsManager.setProperty(property, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +102,7 @@ public class MessageUpdater {
|
|||||||
if (message != null) {
|
if (message != null) {
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
return (defaultJarConfiguration == null ? null : defaultJarConfiguration.getString(key));
|
return (defaultJarConfiguration == null) ? null : defaultJarConfiguration.getString(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static FileConfiguration loadJarFileOrSendError(String jarPath) {
|
private static FileConfiguration loadJarFileOrSendError(String jarPath) {
|
||||||
@ -96,6 +121,13 @@ public class MessageUpdater {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<PropertyEntry> buildPropertyEntriesForMessageKeys() {
|
||||||
|
return Arrays.stream(MessageKey.values())
|
||||||
|
.map(key -> new StringProperty(key.getKey(), ""))
|
||||||
|
.map(PropertyEntry::new)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
private static void close(Closeable closeable) {
|
private static void close(Closeable closeable) {
|
||||||
if (closeable != null) {
|
if (closeable != null) {
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user