Skip to content

Commit c9161d4

Browse files
committed
feat: enhance AutoRegister annotation and improve ConfigManager with file initialization and reload functionality
1 parent 9c7cea5 commit c9161d4

2 files changed

Lines changed: 56 additions & 31 deletions

File tree

src/main/java/com/github/pinont/singularitylib/api/annotation/AutoRegister.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.github.pinont.singularitylib.api.annotation;
22

3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Target;
5+
36
/**
47
* Annotation to mark classes for automatic registration by the plugin.
58
* This annotation is used to mark classes that need to be registered during the plugin's startup process.
@@ -9,6 +12,8 @@
912
* <b>Hint:</b> Use this annotation to register commands, events, or custom items.
1013
* It should only be used when the class extends {@code CustomItem}, {@code SimpleCommand}, or {@code Listener}.
1114
*/
15+
16+
@Target({ElementType.TYPE})
1217
public @interface AutoRegister {
1318
/**
1419
* Indicates that the annotated class should be automatically registered by the plugin.

src/main/java/com/github/pinont/singularitylib/api/manager/ConfigManager.java

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,25 @@
1212

1313
/**
1414
* Manages configuration files for the plugin.
15-
* This class provides functionality to create, load, save, and manipulate YAML configuration files.
15+
* <p>
16+
* This class provides functionality to create, load, save, reload, and manipulate YAML configuration files
17+
* in the plugin's data folder or subfolders.
1618
*/
1719
public class ConfigManager {
1820

21+
/** The configuration file on disk */
1922
private final File configFile;
20-
private final FileConfiguration config;
23+
24+
/** The in-memory representation of the YAML configuration */
25+
private FileConfiguration config;
26+
27+
/** The name of the configuration file */
2128
private final String fileName;
29+
30+
/** Reference to the plugin instance */
2231
private final Plugin plugin = getInstance();
32+
33+
/** True if the config file was just created (first load) */
2334
private boolean isFirstLoad;
2435

2536
/**
@@ -29,59 +40,56 @@ public class ConfigManager {
2940
*/
3041
public ConfigManager(String fileName) {
3142
this.fileName = fileName;
32-
configFile = new File(plugin.getDataFolder(), fileName);
33-
if (!configFile.exists()) {
34-
try {
35-
configFile.getParentFile().mkdirs();
36-
configFile.createNewFile();
37-
isFirstLoad = true;
38-
} catch (IOException e) {
39-
Bukkit.getLogger().warning(e.getMessage());
40-
}
41-
} else {
42-
isFirstLoad = false;
43-
}
44-
config = YamlConfiguration.loadConfiguration(configFile);
43+
this.configFile = new File(plugin.getDataFolder(), fileName);
44+
initializeFile();
45+
this.config = YamlConfiguration.loadConfiguration(configFile);
46+
}
47+
48+
/**
49+
* Creates a ConfigManager for a configuration file in a specific subfolder.
50+
*
51+
* @param subFolder the subfolder where the configuration file should be located
52+
* @param fileName the name of the configuration file
53+
*/
54+
public ConfigManager(String subFolder, String fileName) {
55+
this.fileName = fileName;
56+
this.configFile = new File(plugin.getDataFolder() + "/" + subFolder, fileName);
57+
initializeFile();
58+
this.config = YamlConfiguration.loadConfiguration(configFile);
4559
}
4660

4761
/**
4862
* Checks if a configuration file exists in a specific subfolder.
4963
*
5064
* @param subFolder the subfolder to check in
51-
* @param fileName the name of the configuration file
65+
* @param fileName the name of the configuration file
5266
* @return true if the file exists, false otherwise
5367
*/
5468
public static boolean isExists(String subFolder, String fileName) {
5569
return new File(getInstance().getDataFolder() + "/" + subFolder, fileName).exists();
5670
}
5771

5872
/**
59-
* Creates a ConfigManager for a configuration file in a specific subfolder.
60-
*
61-
* @param subFolder the subfolder where the configuration file should be located
62-
* @param fileName the name of the configuration file
73+
* Initializes the configuration file by creating it if it does not exist.
6374
*/
64-
public ConfigManager(String subFolder, String fileName) {
65-
this.fileName = fileName;
66-
configFile = new File(plugin.getDataFolder() + "/" + subFolder, fileName);
75+
private void initializeFile() {
6776
if (!configFile.exists()) {
6877
try {
6978
configFile.getParentFile().mkdirs();
7079
configFile.createNewFile();
7180
isFirstLoad = true;
7281
} catch (IOException e) {
73-
Bukkit.getLogger().warning(e.getMessage());
82+
Bukkit.getLogger().warning("Failed to create config file: " + e.getMessage());
7483
}
7584
} else {
7685
isFirstLoad = false;
7786
}
78-
config = YamlConfiguration.loadConfiguration(configFile);
7987
}
8088

8189
/**
8290
* Sets a value at the specified path in the configuration.
8391
*
84-
* @param path the configuration path
92+
* @param path the configuration path
8593
* @param value the value to set
8694
*/
8795
public void set(String path, Object value) {
@@ -100,14 +108,27 @@ public Object get(String path) {
100108

101109
/**
102110
* Saves the configuration to the file.
103-
* This method writes all changes made to the configuration back to the file.
111+
* <p>
112+
* Writes all changes made to the configuration back to the disk.
104113
*/
105114
public void saveConfig() {
106115
try {
107116
config.save(configFile);
108-
config.options().copyDefaults(true);
109117
} catch (IOException e) {
110-
Bukkit.getLogger().warning(e.getMessage());
118+
Bukkit.getLogger().warning("Failed to save config file: " + e.getMessage());
119+
}
120+
}
121+
122+
/**
123+
* Reloads the configuration from disk.
124+
* <p>
125+
* Useful when the file is manually modified while the server is running.
126+
*/
127+
public void reloadConfig() {
128+
if (configFile.exists()) {
129+
config = YamlConfiguration.loadConfiguration(configFile);
130+
} else {
131+
Bukkit.getLogger().warning("Config file does not exist: " + fileName);
111132
}
112133
}
113134

@@ -119,7 +140,6 @@ public void saveConfig() {
119140
public FileConfiguration getConfig() {
120141
if (config == null) {
121142
Bukkit.getLogger().warning("An error occurred while loading the config file: " + fileName);
122-
return null;
123143
}
124144
return config;
125145
}
@@ -132,4 +152,4 @@ public FileConfiguration getConfig() {
132152
public boolean isFirstLoad() {
133153
return isFirstLoad;
134154
}
135-
}
155+
}

0 commit comments

Comments
 (0)