-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigUtil.java
137 lines (123 loc) · 5.52 KB
/
ConfigUtil.java
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package org.example.plugin.util.config;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.config.Configuration;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.logging.Logger;
/**
* Utility class for managing plugin configuration files.
* <p>
* Extends {@link Configuration} to provide custom methods for loading, saving, and managing configuration
* files. It automatically handles the creation of parent directories and copies default configuration files from the
* plugin's resources if they do not exist.
*
* @see <a href="https://github.com/AleksandarHaralanov">Aleksandar's GitHub</a>
*
* @author Aleksandar Haralanov (@AleksandarHaralanov)
*/
public final class ConfigUtil extends Configuration {
private static final Logger logger = Bukkit.getServer().getLogger();
private final File configFile;
private final String pluginName;
/**
* Constructs a new instance of {@code ConfigUtil}.
* <p>
* This constructor initializes the {@link Configuration} object with a file reference for the plugin's configuration
* file. It also sets the plugin name for logging purposes.
*
* @param plugin the plugin instance using this configuration utility
* @param fileName the name of the configuration file to manage (e.g., "config.yml", "settings.yml")
*/
public ConfigUtil(JavaPlugin plugin, String fileName) {
super(new File(plugin.getDataFolder(), fileName));
this.configFile = new File(plugin.getDataFolder(), fileName);
this.pluginName = plugin.getDescription().getName();
}
/**
* Loads the configuration file.
* <ul>
* <li>Creates parent directories if they do not exist.</li>
* <li>Copies the default configuration file from the plugin's resources if the configuration file does not exist.</li>
* <li>Attempts to load the configuration by calling the superclass' {@code load()} method.</li>
* <li>Logs errors if the configuration file cannot be loaded.</li>
* </ul>
* <p>
* This method ensures the configuration file is properly loaded and accessible.
*/
@Override
public void load() {
createParentDirectories();
if (!configFile.exists()) {
copyDefaultConfig();
}
try {
super.load();
} catch (Exception e) {
logger.severe(String.format("[%s] Failed to load configuration '%s': %s", pluginName, configFile.getName(), e.getMessage()));
}
}
/**
* Creates the parent directories for the configuration file if they do not exist.
* <p>
* Logs an error if the directories cannot be created due to an {@link IOException}.
* This method ensures the folder structure for the configuration file is created properly.
*/
private void createParentDirectories() {
try {
Files.createDirectories(configFile.getParentFile().toPath());
} catch (IOException e) {
logger.severe(String.format("[%s] Failed to create default configuration directory: %s", pluginName, e.getMessage()));
}
}
/**
* Copies the default configuration file from the plugin's resources to the target location.
* <p>
* This method looks for a file in the plugin's resources with the same name as the configuration file being managed.
* If found, it copies this file to the plugin's data folder. Logs an error if the default configuration file cannot be found
* or copied due to an {@link IOException}.
*/
private void copyDefaultConfig() {
String resourcePath = "/" + configFile.getName();
try (InputStream input = getClass().getResourceAsStream(resourcePath)) {
if (input == null) {
logger.severe(String.format("[%s] Default configuration '%s' wasn't found.", pluginName, configFile.getName()));
return;
}
Files.copy(input, configFile.toPath());
logger.info(String.format("[%s] Default configuration '%s' created successfully.", pluginName, configFile.getName()));
} catch (IOException e) {
logger.severe(String.format("[%s] Failed to create default configuration '%s': %s", pluginName, configFile.getName(), e.getMessage()));
}
}
/**
* Loads the configuration file and logs the result.
* <p>
* Calls {@link #load()} to load the configuration file and logs a message indicating whether the configuration
* was loaded successfully.
*/
public void loadAndLog() {
try {
this.load();
logger.info(String.format("[%s] Configuration '%s' loaded successfully.", pluginName, configFile.getName()));
} catch (Exception e) {
logger.severe(String.format("[%s] Failed to load configuration '%s': %s", pluginName, configFile.getName(), e.getMessage()));
}
}
/**
* Saves the configuration file and logs the result.
* <p>
* Attempts to save the configuration using the superclass' {@code save()} method and logs a message indicating
* whether the configuration was saved successfully.
*/
public void saveAndLog() {
try {
this.save();
logger.info(String.format("[%s] Configuration '%s' saved successfully.", pluginName, configFile.getName()));
} catch (Exception e) {
logger.severe(String.format("[%s] Failed to save configuration '%s': %s", pluginName, configFile.getName(), e.getMessage()));
}
}
}