-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathAbstractMessageFileHandler.java
More file actions
123 lines (105 loc) · 3.98 KB
/
AbstractMessageFileHandler.java
File metadata and controls
123 lines (105 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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.output.ConsoleLoggerFactory;
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 java.io.File;
import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;
/**
* Handles a YAML message file with a default file fallback.
*/
public abstract class AbstractMessageFileHandler implements Reloadable {
private final ConsoleLogger logger = ConsoleLoggerFactory.get(AbstractMessageFileHandler.class);
private final File dataFolder;
private final Settings settings;
private String filename;
private FileConfiguration configuration;
private final String defaultFile;
protected AbstractMessageFileHandler(@DataFolder File dataFolder, Settings settings) {
this.dataFolder = dataFolder;
this.settings = settings;
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);
}
protected String getLanguage() {
return settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
}
protected File getUserLanguageFile() {
return new File(dataFolder, filename);
}
protected String getFilename() {
return filename;
}
/**
* 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);
return message == null
? "Error retrieving message '" + key + "'"
: 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);
}
/**
* 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);
/**
* 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 {
logger.warning("Wanted to copy default messages file '" + defaultFile + "' from JAR but it didn't exist");
return null;
}
}
}