Skip to content

Commit 44d5797

Browse files
[1.13.1.0]现在支持多个不同的配置类使用同一个配置文件,插件主类新增几个ConfigWrapper相关方法
1 parent a9f2593 commit 44d5797

File tree

7 files changed

+132
-21
lines changed

7 files changed

+132
-21
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
java.sourceCompatibility = JavaVersion.VERSION_1_8
22
java.targetCompatibility = JavaVersion.VERSION_1_8
33
rootProject.group = "com.crypticlib"
4-
rootProject.version = "1.13.0.6"
4+
rootProject.version = "1.13.1.0"
55
//当全项目重构时更新大版本号,当添加模块或有较大更改时更新子版本号,当bug修复和功能补充时更新小版本号
66

77
var repositoryUrl = "https://repo.crypticlib.com:8081/repository/"

platform/bukkit/src/main/java/crypticlib/BukkitPlugin.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
public abstract class BukkitPlugin extends JavaPlugin {
2828

2929
protected final PluginScanner pluginScanner = PluginScanner.INSTANCE;
30-
protected final Map<String, BukkitConfigContainer> configContainerMap = new ConcurrentHashMap<>();
30+
protected final Map<Class<?>, BukkitConfigContainer> configContainerMap = new ConcurrentHashMap<>();
31+
protected final Map<String, BukkitConfigWrapper> configWrapperMap = new ConcurrentHashMap<>();
3132
protected final String defaultConfigFileName = "config.yml";
3233

3334
public BukkitPlugin() {
@@ -49,9 +50,9 @@ public final void onLoad() {
4950
String path = configHandler.path();
5051
if (!path.endsWith(".yml") && !path.endsWith(".yaml"))
5152
path += ".yml";
52-
BukkitConfigWrapper configWrapper = new BukkitConfigWrapper(this, path);
53+
BukkitConfigWrapper configWrapper = getConfigWrapperOrCreate(path);
5354
BukkitConfigContainer configContainer = new BukkitConfigContainer(configClass, configWrapper);
54-
configContainerMap.put(path, configContainer);
55+
configContainerMap.put(configClass, configContainer);
5556
configContainer.reload();
5657
}
5758
);
@@ -148,8 +149,8 @@ public final void reloadPlugin() {
148149

149150
@Override
150151
public final @NotNull FileConfiguration getConfig() {
151-
if (configContainerMap.containsKey(defaultConfigFileName)) {
152-
return configContainerMap.get(defaultConfigFileName).configWrapper().config();
152+
if (configWrapperMap.containsKey(defaultConfigFileName)) {
153+
return configWrapperMap.get(defaultConfigFileName).config();
153154
}
154155
throw new UnsupportedOperationException("No default config file");
155156
}
@@ -161,16 +162,52 @@ public final void saveConfig() {
161162

162163
@Override
163164
public final void saveDefaultConfig() {
164-
BukkitConfigContainer defConfig = new BukkitConfigContainer(this.getClass(), new BukkitConfigWrapper(this, defaultConfigFileName));
165+
BukkitConfigContainer defConfig = new BukkitConfigContainer(this.getClass(), getConfigWrapperOrCreate(defaultConfigFileName));
165166
defConfig.reload();
166-
configContainerMap.put(defaultConfigFileName, defConfig);
167+
configContainerMap.put(this.getClass(), defConfig);
167168
}
168169

169170
@Override
170171
public final void reloadConfig() {
172+
configWrapperMap.forEach((path, wrapper) -> wrapper.reloadConfig());
171173
configContainerMap.forEach((path, container) -> container.reload());
172174
}
173175

176+
/**
177+
* 获取插件的配置文件
178+
* @param path 配置文件相对于插件文件夹的路径
179+
*/
180+
public final Optional<BukkitConfigWrapper> getConfigWrapper(String path) {
181+
return Optional.ofNullable(configWrapperMap.get(path));
182+
}
183+
184+
/**
185+
* 获取插件的配置文件,如果不存在时会创建文件
186+
* @param path 配置文件相对于插件文件夹的路径
187+
*/
188+
public final BukkitConfigWrapper getConfigWrapperOrCreate(String path) {
189+
Optional<BukkitConfigWrapper> configWrapperOpt = getConfigWrapper(path);
190+
return configWrapperOpt.orElseGet(() -> {
191+
if (!path.endsWith(".yml") && !path.endsWith(".yaml")) {
192+
throw new UnsupportedOperationException("Bukkit only support yaml config");
193+
}
194+
BukkitConfigWrapper configWrapper = new BukkitConfigWrapper(this, path);
195+
configWrapperMap.put(path, configWrapper);
196+
return configWrapper;
197+
});
198+
}
199+
200+
/**
201+
* 删除插件缓存的一个配置文件
202+
* 被删除的配置文件将不会在reload时自动重载
203+
* 请谨慎操作
204+
* @param path 配置文件相对于插件文件夹的路径
205+
* @return 被删除掉的配置文件包装
206+
*/
207+
public final BukkitConfigWrapper removeConfigWrapper(String path) {
208+
return configWrapperMap.remove(path);
209+
}
210+
174211
private void runLifeCycleTasks(LifeCycle lifeCycle) {
175212
List<BukkitLifeCycleTaskWrapper> taskWrappers = new ArrayList<>();
176213
pluginScanner.getAnnotatedClasses(AutoTask.class).forEach(

platform/bukkit/src/main/java/crypticlib/config/BukkitConfigContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public BukkitConfigContainer(@NotNull Class<?> containerClass, @NotNull BukkitCo
1515

1616
@Override
1717
public void reload() {
18-
configWrapper.reloadConfig();
18+
// configWrapper.reloadConfig(); 不再由ConfigContainer进行重载
1919
for (Field field : containerClass.getDeclaredFields()) {
2020
if (!Modifier.isStatic(field.getModifiers()))
2121
continue;

platform/bungee/src/main/java/crypticlib/BungeePlugin.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
public abstract class BungeePlugin extends Plugin {
2727

2828
protected final PluginScanner pluginScanner = PluginScanner.INSTANCE;
29-
protected final Map<String, BungeeConfigContainer> configContainerMap = new ConcurrentHashMap<>();
29+
protected final Map<Class<?>, BungeeConfigContainer> configContainerMap = new ConcurrentHashMap<>();
30+
protected final Map<String, BungeeConfigWrapper> configWrapperMap = new ConcurrentHashMap<>();
3031
protected final String defaultConfigFileName = "config.yml";
3132

3233
public BungeePlugin() {
@@ -48,9 +49,9 @@ public final void onLoad() {
4849
String path = configHandler.path();
4950
if (!path.endsWith(".yml") && !path.endsWith(".yaml") && !path.endsWith(".json"))
5051
path += ".yml";
51-
BungeeConfigWrapper configWrapper = new BungeeConfigWrapper(this, path);
52+
BungeeConfigWrapper configWrapper = getConfigWrapperOrCreate(path);
5253
BungeeConfigContainer configContainer = new BungeeConfigContainer(configClass, configWrapper);
53-
configContainerMap.put(path, configContainer);
54+
configContainerMap.put(configClass, configContainer);
5455
configContainer.reload();
5556
}
5657
);
@@ -144,8 +145,8 @@ public final void reloadPlugin() {
144145
}
145146

146147
public final @NotNull Configuration getConfig() {
147-
if (configContainerMap.containsKey(defaultConfigFileName)) {
148-
return configContainerMap.get(defaultConfigFileName).configWrapper().config();
148+
if (configWrapperMap.containsKey(defaultConfigFileName)) {
149+
return configWrapperMap.get(defaultConfigFileName).config();
149150
}
150151
throw new UnsupportedOperationException("No default config file");
151152
}
@@ -155,15 +156,51 @@ public final void saveConfig() {
155156
}
156157

157158
public final void saveDefaultConfig() {
158-
BungeeConfigContainer defConfig = new BungeeConfigContainer(this.getClass(), new BungeeConfigWrapper(this, defaultConfigFileName));
159+
BungeeConfigContainer defConfig = new BungeeConfigContainer(this.getClass(), getConfigWrapperOrCreate(defaultConfigFileName));
159160
defConfig.reload();
160-
configContainerMap.put(defaultConfigFileName, defConfig);
161+
configContainerMap.put(this.getClass(), defConfig);
161162
}
162163

163164
public final void reloadConfig() {
165+
configWrapperMap.forEach((path, wrapper) -> wrapper.reloadConfig());
164166
configContainerMap.forEach((path, container) -> container.reload());
165167
}
166168

169+
/**
170+
* 获取插件的配置文件
171+
* @param path 配置文件相对于插件文件夹的路径
172+
*/
173+
public final Optional<BungeeConfigWrapper> getConfigWrapper(String path) {
174+
return Optional.ofNullable(configWrapperMap.get(path));
175+
}
176+
177+
/**
178+
* 获取插件的配置文件,如果不存在时会创建文件
179+
* @param path 配置文件相对于插件文件夹的路径
180+
*/
181+
public final BungeeConfigWrapper getConfigWrapperOrCreate(String path) {
182+
Optional<BungeeConfigWrapper> configWrapperOpt = getConfigWrapper(path);
183+
return configWrapperOpt.orElseGet(() -> {
184+
if (!path.endsWith(".yml") && !path.endsWith(".yaml") && !path.endsWith(".json")) {
185+
throw new UnsupportedOperationException("Bungee only support yaml or json config");
186+
}
187+
BungeeConfigWrapper configWrapper = new BungeeConfigWrapper(this, path);
188+
configWrapperMap.put(path, configWrapper);
189+
return configWrapper;
190+
});
191+
}
192+
193+
/**
194+
* 删除插件缓存的一个配置文件
195+
* 被删除的配置文件将不会在reload时自动重载
196+
* 请谨慎操作
197+
* @param path 配置文件相对于插件文件夹的路径
198+
* @return 被删除掉的配置文件包装
199+
*/
200+
public final BungeeConfigWrapper removeConfigWrapper(String path) {
201+
return configWrapperMap.remove(path);
202+
}
203+
167204
private void runLifeCycleTasks(LifeCycle lifeCycle) {
168205
List<BungeeLifeCycleTaskWrapper> taskWrappers = new ArrayList<>();
169206
pluginScanner.getAnnotatedClasses(AutoTask.class).forEach(

platform/bungee/src/main/java/crypticlib/config/BungeeConfigContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public BungeeConfigContainer(@NotNull Class<?> containerClass, @NotNull BungeeCo
1515

1616
@Override
1717
public void reload() {
18-
configWrapper.reloadConfig();
18+
// configWrapper.reloadConfig(); 不再由ConfigContainer进行重载
1919
for (Field field : containerClass.getDeclaredFields()) {
2020
if (!Modifier.isStatic(field.getModifiers()))
2121
continue;

platform/velocity/src/main/java/crypticlib/VelocityPlugin.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public abstract class VelocityPlugin {
3838
protected final Path dataDirectory;
3939
protected final PluginContainer pluginContainer;
4040
protected final ProxyServer proxyServer;
41-
protected final Map<String, VelocityConfigContainer> configContainerMap = new ConcurrentHashMap<>();
41+
protected final Map<Class<?>, VelocityConfigContainer> configContainerMap = new ConcurrentHashMap<>();
42+
protected final Map<String, VelocityConfigWrapper> configWrapperMap = new ConcurrentHashMap<>();
4243

4344
public VelocityPlugin(Logger logger, ProxyServer proxyServer, PluginContainer pluginContainer, Path dataDirectory) {
4445
this.logger = logger;
@@ -64,9 +65,9 @@ public final void onProxyInitialization(ProxyInitializeEvent event) {
6465
String path = configHandler.path();
6566
if (!path.endsWith(".yml") && !path.endsWith(".yaml"))
6667
path += ".yml";
67-
VelocityConfigWrapper configWrapper = new VelocityConfigWrapper(this, path);
68+
VelocityConfigWrapper configWrapper = getConfigWrapperOrCreate(path);
6869
VelocityConfigContainer configContainer = new VelocityConfigContainer(configClass, configWrapper);
69-
configContainerMap.put(configClass.getName(), configContainer);
70+
configContainerMap.put(configClass, configContainer);
7071
configContainer.reload();
7172
}
7273
);
@@ -145,6 +146,7 @@ public final void reloadPlugin() {
145146
}
146147

147148
public final void reloadConfig() {
149+
configWrapperMap.forEach((path, wrapper) -> wrapper.reloadConfig());
148150
configContainerMap.forEach((path, container) -> container.reload());
149151
}
150152

@@ -187,7 +189,42 @@ public Optional<RegisteredServer> getServerOpt(String name) {
187189
public Collection<RegisteredServer> getAllServers() {
188190
return proxyServer.getAllServers();
189191
}
192+
193+
/**
194+
* 获取插件的配置文件
195+
* @param path 配置文件相对于插件文件夹的路径
196+
*/
197+
public final Optional<VelocityConfigWrapper> getConfigWrapper(String path) {
198+
return Optional.ofNullable(configWrapperMap.get(path));
199+
}
190200

201+
/**
202+
* 获取插件的配置文件,如果不存在时会创建文件
203+
* @param path 配置文件相对于插件文件夹的路径
204+
*/
205+
public final VelocityConfigWrapper getConfigWrapperOrCreate(String path) {
206+
Optional<VelocityConfigWrapper> configWrapperOpt = getConfigWrapper(path);
207+
return configWrapperOpt.orElseGet(() -> {
208+
if (!path.endsWith(".yml") && !path.endsWith(".yaml")) {
209+
throw new UnsupportedOperationException("Velocity only support yaml config");
210+
}
211+
VelocityConfigWrapper configWrapper = new VelocityConfigWrapper(this, path);
212+
configWrapperMap.put(path, configWrapper);
213+
return configWrapper;
214+
});
215+
}
216+
217+
/**
218+
* 删除插件缓存的一个配置文件
219+
* 被删除的配置文件将不会在reload时自动重载
220+
* 请谨慎操作
221+
* @param path 配置文件相对于插件文件夹的路径
222+
* @return 被删除掉的配置文件包装
223+
*/
224+
public final VelocityConfigWrapper removeConfigWrapper(String path) {
225+
return configWrapperMap.remove(path);
226+
}
227+
191228
private void runLifeCycleTasks(LifeCycle lifeCycle) {
192229
List<VelocityLifeCycleTaskWrapper> taskWrappers = new ArrayList<>();
193230
pluginScanner.getAnnotatedClasses(AutoTask.class).forEach(

platform/velocity/src/main/java/crypticlib/config/VelocityConfigContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public VelocityConfigContainer(@NotNull Class<?> containerClass, @NotNull Veloci
1515

1616
@Override
1717
public void reload() {
18-
configWrapper.reloadConfig();
18+
// configWrapper.reloadConfig(); 不再由ConfigContainer进行重载
1919
for (Field field : containerClass.getDeclaredFields()) {
2020
if (!Modifier.isStatic(field.getModifiers()))
2121
continue;

0 commit comments

Comments
 (0)