mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 390815b790 | |||
| 760ac815df | |||
| 216050a701 | |||
| 6d0ee35197 | |||
| 494491cf94 | |||
| 00e88b50ff | |||
| 033236c89b | |||
| 791fa6e5b4 | |||
| 51c287a0a7 | |||
| ab2f898164 | |||
| 4f4b203240 | |||
| c94fef893f | |||
| c2a08c6c72 | |||
| dd7a6c819f | |||
| e9c010981e | |||
| 78d52e1aae | |||
| 42ccc23347 | |||
| 85578e3d8c | |||
| dc4d3664d3 | |||
| 2e4cb5480a | |||
| 4b7a7aeae7 | |||
| 132ca81635 | |||
| 80747ac922 | |||
| 0d10a06547 |
@@ -0,0 +1,3 @@
|
|||||||
|
# 欢迎使用 EasyConfiguration !
|
||||||
|
|
||||||
|
这个项目刚刚创建,详细的Javadoc与开发指南还在补充,请给我一点时间~
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
|
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
|
||||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||||
|
|
||||||
name: "Project Deploy"
|
name: "Deploy & Publish"
|
||||||
|
|
||||||
on:
|
on:
|
||||||
# 支持手动触发构建
|
# 支持手动触发构建
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
|
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
|
||||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||||
|
|
||||||
name: Project Build & Tests
|
name: Build & Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
# 支持手动触发构建
|
# 支持手动触发构建
|
||||||
|
|||||||
@@ -1,21 +1,165 @@
|
|||||||
MIT License
|
GNU LESSER GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
Copyright (c) 2022 Carm
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
This version of the GNU Lesser General Public License incorporates
|
||||||
copies or substantial portions of the Software.
|
the terms and conditions of version 3 of the GNU General Public
|
||||||
|
License, supplemented by the additional permissions listed below.
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
0. Additional Definitions.
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
General Public License.
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
"The Library" refers to a covered work governed by this License,
|
||||||
|
other than an Application or a Combined Work as defined below.
|
||||||
|
|
||||||
|
An "Application" is any work that makes use of an interface provided
|
||||||
|
by the Library, but which is not otherwise based on the Library.
|
||||||
|
Defining a subclass of a class defined by the Library is deemed a mode
|
||||||
|
of using an interface provided by the Library.
|
||||||
|
|
||||||
|
A "Combined Work" is a work produced by combining or linking an
|
||||||
|
Application with the Library. The particular version of the Library
|
||||||
|
with which the Combined Work was made is also called the "Linked
|
||||||
|
Version".
|
||||||
|
|
||||||
|
The "Minimal Corresponding Source" for a Combined Work means the
|
||||||
|
Corresponding Source for the Combined Work, excluding any source code
|
||||||
|
for portions of the Combined Work that, considered in isolation, are
|
||||||
|
based on the Application, and not on the Linked Version.
|
||||||
|
|
||||||
|
The "Corresponding Application Code" for a Combined Work means the
|
||||||
|
object code and/or source code for the Application, including any data
|
||||||
|
and utility programs needed for reproducing the Combined Work from the
|
||||||
|
Application, but excluding the System Libraries of the Combined Work.
|
||||||
|
|
||||||
|
1. Exception to Section 3 of the GNU GPL.
|
||||||
|
|
||||||
|
You may convey a covered work under sections 3 and 4 of this License
|
||||||
|
without being bound by section 3 of the GNU GPL.
|
||||||
|
|
||||||
|
2. Conveying Modified Versions.
|
||||||
|
|
||||||
|
If you modify a copy of the Library, and, in your modifications, a
|
||||||
|
facility refers to a function or data to be supplied by an Application
|
||||||
|
that uses the facility (other than as an argument passed when the
|
||||||
|
facility is invoked), then you may convey a copy of the modified
|
||||||
|
version:
|
||||||
|
|
||||||
|
a) under this License, provided that you make a good faith effort to
|
||||||
|
ensure that, in the event an Application does not supply the
|
||||||
|
function or data, the facility still operates, and performs
|
||||||
|
whatever part of its purpose remains meaningful, or
|
||||||
|
|
||||||
|
b) under the GNU GPL, with none of the additional permissions of
|
||||||
|
this License applicable to that copy.
|
||||||
|
|
||||||
|
3. Object Code Incorporating Material from Library Header Files.
|
||||||
|
|
||||||
|
The object code form of an Application may incorporate material from
|
||||||
|
a header file that is part of the Library. You may convey such object
|
||||||
|
code under terms of your choice, provided that, if the incorporated
|
||||||
|
material is not limited to numerical parameters, data structure
|
||||||
|
layouts and accessors, or small macros, inline functions and templates
|
||||||
|
(ten or fewer lines in length), you do both of the following:
|
||||||
|
|
||||||
|
a) Give prominent notice with each copy of the object code that the
|
||||||
|
Library is used in it and that the Library and its use are
|
||||||
|
covered by this License.
|
||||||
|
|
||||||
|
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||||
|
document.
|
||||||
|
|
||||||
|
4. Combined Works.
|
||||||
|
|
||||||
|
You may convey a Combined Work under terms of your choice that,
|
||||||
|
taken together, effectively do not restrict modification of the
|
||||||
|
portions of the Library contained in the Combined Work and reverse
|
||||||
|
engineering for debugging such modifications, if you also do each of
|
||||||
|
the following:
|
||||||
|
|
||||||
|
a) Give prominent notice with each copy of the Combined Work that
|
||||||
|
the Library is used in it and that the Library and its use are
|
||||||
|
covered by this License.
|
||||||
|
|
||||||
|
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||||
|
document.
|
||||||
|
|
||||||
|
c) For a Combined Work that displays copyright notices during
|
||||||
|
execution, include the copyright notice for the Library among
|
||||||
|
these notices, as well as a reference directing the user to the
|
||||||
|
copies of the GNU GPL and this license document.
|
||||||
|
|
||||||
|
d) Do one of the following:
|
||||||
|
|
||||||
|
0) Convey the Minimal Corresponding Source under the terms of this
|
||||||
|
License, and the Corresponding Application Code in a form
|
||||||
|
suitable for, and under terms that permit, the user to
|
||||||
|
recombine or relink the Application with a modified version of
|
||||||
|
the Linked Version to produce a modified Combined Work, in the
|
||||||
|
manner specified by section 6 of the GNU GPL for conveying
|
||||||
|
Corresponding Source.
|
||||||
|
|
||||||
|
1) Use a suitable shared library mechanism for linking with the
|
||||||
|
Library. A suitable mechanism is one that (a) uses at run time
|
||||||
|
a copy of the Library already present on the user's computer
|
||||||
|
system, and (b) will operate properly with a modified version
|
||||||
|
of the Library that is interface-compatible with the Linked
|
||||||
|
Version.
|
||||||
|
|
||||||
|
e) Provide Installation Information, but only if you would otherwise
|
||||||
|
be required to provide such information under section 6 of the
|
||||||
|
GNU GPL, and only to the extent that such information is
|
||||||
|
necessary to install and execute a modified version of the
|
||||||
|
Combined Work produced by recombining or relinking the
|
||||||
|
Application with a modified version of the Linked Version. (If
|
||||||
|
you use option 4d0, the Installation Information must accompany
|
||||||
|
the Minimal Corresponding Source and Corresponding Application
|
||||||
|
Code. If you use option 4d1, you must provide the Installation
|
||||||
|
Information in the manner specified by section 6 of the GNU GPL
|
||||||
|
for conveying Corresponding Source.)
|
||||||
|
|
||||||
|
5. Combined Libraries.
|
||||||
|
|
||||||
|
You may place library facilities that are a work based on the
|
||||||
|
Library side by side in a single library together with other library
|
||||||
|
facilities that are not Applications and are not covered by this
|
||||||
|
License, and convey such a combined library under terms of your
|
||||||
|
choice, if you do both of the following:
|
||||||
|
|
||||||
|
a) Accompany the combined library with a copy of the same work based
|
||||||
|
on the Library, uncombined with any other library facilities,
|
||||||
|
conveyed under the terms of this License.
|
||||||
|
|
||||||
|
b) Give prominent notice with the combined library that part of it
|
||||||
|
is a work based on the Library, and explaining where to find the
|
||||||
|
accompanying uncombined form of the same work.
|
||||||
|
|
||||||
|
6. Revised Versions of the GNU Lesser General Public License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions
|
||||||
|
of the GNU Lesser General Public License from time to time. Such new
|
||||||
|
versions will be similar in spirit to the present version, but may
|
||||||
|
differ in detail to address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Library as you received it specifies that a certain numbered version
|
||||||
|
of the GNU Lesser General Public License "or any later version"
|
||||||
|
applies to it, you have the option of following the terms and
|
||||||
|
conditions either of that published version or of any later version
|
||||||
|
published by the Free Software Foundation. If the Library as you
|
||||||
|
received it does not specify a version number of the GNU Lesser
|
||||||
|
General Public License, you may choose any version of the GNU Lesser
|
||||||
|
General Public License ever published by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Library as you received it specifies that a proxy can decide
|
||||||
|
whether future versions of the GNU Lesser General Public License shall
|
||||||
|
apply, that proxy's public statement of acceptance of any version is
|
||||||
|
permanent authorization for you to choose that version for the
|
||||||
|
Library.
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
# EasyConfiguration
|
# EasyConfiguration
|
||||||
|
|
||||||
[](https://github.com/CarmJos/EasyConfiguration/releases)
|
[](https://github.com/CarmJos/EasyConfiguration/releases)
|
||||||
[](https://opensource.org/licenses/MIT)
|
[](https://www.gnu.org/licenses/lgpl-3.0.html)
|
||||||
[](https://github.com/CarmJos/EasyConfiguration/actions/workflows/maven.yml)
|
[](https://github.com/CarmJos/EasyConfiguration/actions/workflows/maven.yml)
|
||||||
[](https://www.codefactor.io/repository/github/carmjos/easyconfiguration)
|
[](https://www.codefactor.io/repository/github/carmjos/easyconfiguration)
|
||||||

|

|
||||||
@@ -23,11 +23,11 @@
|
|||||||
|
|
||||||
## 开发
|
## 开发
|
||||||
|
|
||||||
详细开发介绍请 [点击这里](.documentation/README.md) , JavaDoc(最新Release) 请 [点击这里](https://carmjos.github.io/EasyConfiguration) 。
|
详细开发介绍请 [点击这里](.documentation/README.md) , JavaDoc(最新Release) 请 [点击这里](https://CarmJos.github.io/EasyConfiguration) 。
|
||||||
|
|
||||||
### 示例代码
|
### 示例代码
|
||||||
|
|
||||||
您可以 [点击这里](impl/yaml/src/test/java/config/source/TestConfiguration.java) 查看部分代码演示,更多演示详见 [开发介绍](.documentation/README.md) 。
|
您可以 [点击这里](impl/yaml/src/test/java/config/source/DemoConfiguration.java) 查看部分代码演示,更多演示详见 [开发介绍](.documentation/README.md) 。
|
||||||
|
|
||||||
### 依赖方式
|
### 依赖方式
|
||||||
|
|
||||||
@@ -97,43 +97,6 @@
|
|||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
<details>
|
|
||||||
<summary>平台依赖版本</summary>
|
|
||||||
|
|
||||||
**基于Spigot实现的版本**
|
|
||||||
|
|
||||||
```xml
|
|
||||||
|
|
||||||
<project>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
|
||||||
<artifactId>easyconfiguration-spigot</artifactId>
|
|
||||||
<version>[LATEST RELEASE]</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</project>
|
|
||||||
```
|
|
||||||
|
|
||||||
**基于Bungee实现的版本 (不支持自动注释)**
|
|
||||||
|
|
||||||
```xml
|
|
||||||
|
|
||||||
<project>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
|
||||||
<artifactId>easyconfiguration-bungee</artifactId>
|
|
||||||
<version>[LATEST RELEASE]</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</project>
|
|
||||||
```
|
|
||||||
|
|
||||||
</details>
|
|
||||||
|
|
||||||
#### Gradle 依赖
|
#### Gradle 依赖
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@@ -173,24 +136,14 @@ dependencies {
|
|||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
<details>
|
## 衍生项目
|
||||||
<summary>平台依赖版本</summary>
|
|
||||||
|
|
||||||
```groovy
|
### [**MineConfiguration**](https://github.com/CarmJos/MineConfiguration) (by @CarmJos )
|
||||||
|
|
||||||
dependencies {
|
EasyConfiguration for MineCraft!
|
||||||
|
开始在 MineCraft 相关服务器平台上轻松(做)配置吧!
|
||||||
|
|
||||||
// 用于Spigot服务端的版本
|
目前支持 BungeeCord, Bukkit(Spigot) 服务端,后续将支持更多平台。
|
||||||
api "cc.carm.lib:easyconfiguration-spigot:[LATEST RELEASE]"
|
|
||||||
|
|
||||||
// 用于BungeeCord服务端的版本,不支持注解。
|
|
||||||
// 如需注解,可选择使用 `easyconfiguration-yaml` 并打包。
|
|
||||||
api "cc.carm.lib:easyconfiguration-bungee:[LATEST RELEASE]"
|
|
||||||
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
</details>
|
|
||||||
|
|
||||||
## 支持与捐赠
|
## 支持与捐赠
|
||||||
|
|
||||||
@@ -202,23 +155,4 @@ dependencies {
|
|||||||
|
|
||||||
## 开源协议
|
## 开源协议
|
||||||
|
|
||||||
本项目源码采用 [The MIT License](https://opensource.org/licenses/MIT) 开源协议。
|
本项目源码采用 [GNU LESSER GENERAL PUBLIC LICENSE](https://www.gnu.org/licenses/lgpl-3.0.html) 开源协议。
|
||||||
|
|
||||||
<details>
|
|
||||||
<summary>关于 MIT 协议</summary>
|
|
||||||
|
|
||||||
> MIT 协议可能是几大开源协议中最宽松的一个,核心条款是:
|
|
||||||
>
|
|
||||||
> 该软件及其相关文档对所有人免费,可以任意处置,包括使用,复制,修改,合并,发表,分发,再授权,或者销售。唯一的限制是,软件中必须包含上述版 权和许可提示。
|
|
||||||
>
|
|
||||||
> 这意味着:
|
|
||||||
>
|
|
||||||
> - 你可以自由使用,复制,修改,可以用于自己的项目。
|
|
||||||
> - 可以免费分发或用来盈利。
|
|
||||||
> - 唯一的限制是必须包含许可声明。
|
|
||||||
>
|
|
||||||
> MIT 协议是所有开源许可中最宽松的一个,除了必须包含许可声明外,再无任何限制。
|
|
||||||
>
|
|
||||||
> *以上文字来自 [五种开源协议GPL,LGPL,BSD,MIT,Apache](https://www.oschina.net/question/54100_9455) 。*
|
|
||||||
|
|
||||||
</details>
|
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.0</version>
|
<version>2.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
@@ -2,15 +2,21 @@ package cc.carm.lib.configuration.core;
|
|||||||
|
|
||||||
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
||||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置文件类初始化方法
|
||||||
|
* 用于初始化 {@link ConfigurationRoot} 中的每个 {@link ConfigValue} 对象
|
||||||
|
*
|
||||||
|
* @param <T> {@link ConfigurationProvider} 配置文件的数据来源
|
||||||
|
* @author CarmJos
|
||||||
|
*/
|
||||||
public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
||||||
|
|
||||||
protected final @NotNull T provider;
|
protected final @NotNull T provider;
|
||||||
@@ -19,94 +25,145 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
|||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize(Class<? extends ConfigurationRoot> rootClazz) {
|
/**
|
||||||
initialize(rootClazz, true);
|
* 初始化指定类以及其子类的所有 {@link ConfigValue} 对象。
|
||||||
|
*
|
||||||
|
* @param clazz 配置文件类,须继承于 {@link ConfigurationRoot} 。
|
||||||
|
* @param saveDefaults 是否写入默认值(默认为 true)。
|
||||||
|
*/
|
||||||
|
public void initialize(@NotNull Class<? extends ConfigurationRoot> clazz, boolean saveDefaults) {
|
||||||
|
initialize(clazz, saveDefaults, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize(Class<? extends ConfigurationRoot> rootClazz, boolean saveDefault) {
|
/**
|
||||||
String rootSection = null;
|
* 初始化指定类的所有 {@link ConfigValue} 对象。
|
||||||
|
*
|
||||||
ConfigPath sectionAnnotation = rootClazz.getAnnotation(ConfigPath.class);
|
* @param clazz 配置文件类,须继承于 {@link ConfigurationRoot} 。
|
||||||
if (sectionAnnotation != null && sectionAnnotation.value().length() > 1) {
|
* @param saveDefaults 是否写入默认值(默认为 true)。
|
||||||
rootSection = sectionAnnotation.value();
|
* @param loadSubClasses 是否加载内部子类(默认为 true)。
|
||||||
}
|
*/
|
||||||
|
public void initialize(@NotNull Class<? extends ConfigurationRoot> clazz,
|
||||||
if (rootSection != null) {
|
boolean saveDefaults, boolean loadSubClasses) {
|
||||||
//Not usable for null section.
|
initializeClass(
|
||||||
ConfigComment comments = rootClazz.getAnnotation(ConfigComment.class);
|
clazz, null,
|
||||||
if (comments != null && comments.value().length > 0) {
|
null, null, null,
|
||||||
provider.setComments(rootSection, comments.value());
|
saveDefaults, loadSubClasses
|
||||||
}
|
);
|
||||||
}
|
if (saveDefaults) {
|
||||||
|
|
||||||
for (Class<?> innerClass : rootClazz.getDeclaredClasses()) {
|
|
||||||
initSection(rootSection, innerClass, saveDefault);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Field field : rootClazz.getDeclaredFields()) {
|
|
||||||
initValue(rootSection, rootClazz, field, saveDefault);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (saveDefault) {
|
|
||||||
try {
|
try {
|
||||||
provider.save();
|
provider.save();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initSection(String parentSection, Class<?> clazz, boolean saveDefault) {
|
protected void initializeClass(@NotNull Class<?> clazz,
|
||||||
if (!Modifier.isStatic(clazz.getModifiers()) || !Modifier.isPublic(clazz.getModifiers())) return;
|
@Nullable String parentPath, @Nullable String fieldName,
|
||||||
|
@Nullable ConfigPath fieldPath, @Nullable ConfigComment filedComments,
|
||||||
String section = getSectionPath(clazz.getSimpleName(), parentSection, clazz.getAnnotation(ConfigPath.class));
|
boolean saveDefaults, boolean loadSubClasses) {
|
||||||
ConfigComment comments = clazz.getAnnotation(ConfigComment.class);
|
String path = getClassPath(clazz, parentPath, fieldName, fieldPath);
|
||||||
if (comments != null && comments.value().length > 0) {
|
this.provider.setComment(path, getClassComments(clazz, filedComments));
|
||||||
provider.setComments(parentSection, comments.value());
|
for (Field field : clazz.getDeclaredFields()) {
|
||||||
|
initializeField(clazz, field, path, saveDefaults, loadSubClasses);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Field field : clazz.getDeclaredFields()) initValue(section, clazz, field, saveDefault);
|
Class<?>[] classes = clazz.getDeclaredClasses();
|
||||||
for (Class<?> innerClass : clazz.getDeclaredClasses()) initSection(section, innerClass, saveDefault);
|
if (loadSubClasses && classes.length > 0) {
|
||||||
|
// 逆向加载,保持顺序。
|
||||||
|
for (int i = classes.length - 1; i >= 0; i--) {
|
||||||
|
initializeClass(
|
||||||
|
classes[i], path, classes[i].getSimpleName(),
|
||||||
|
null, null,
|
||||||
|
saveDefaults, true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initValue(String parentSection, Class<?> clazz, Field field, boolean saveDefault) {
|
|
||||||
|
protected void initializeValue(@NotNull ConfigValue<?> value, @NotNull String path,
|
||||||
|
@Nullable ConfigCommentInfo comments, boolean saveDefaults) {
|
||||||
|
value.initialize(provider, saveDefaults, path, comments);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeField(@NotNull Class<?> source, @NotNull Field field, @Nullable String parent,
|
||||||
|
boolean saveDefaults, boolean loadSubClasses) {
|
||||||
try {
|
try {
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
Object object = field.get(clazz);
|
Object object = field.get(source);
|
||||||
if (object instanceof ConfigValue<?>) {
|
if (object instanceof ConfigValue<?>) {
|
||||||
initializeValue(
|
initializeValue(
|
||||||
(ConfigValue<?>) object, saveDefault,
|
(ConfigValue<?>) object, getFieldPath(field, parent),
|
||||||
getSectionPath(field.getName(), parentSection, field.getAnnotation(ConfigPath.class)),
|
ConfigCommentInfo.fromAnnotation(field.getAnnotation(ConfigComment.class)),
|
||||||
Optional.ofNullable(field.getAnnotation(ConfigComment.class))
|
saveDefaults
|
||||||
.map(ConfigComment::value).orElse(new String[0])
|
);
|
||||||
|
} else if (object instanceof Class<?>) {
|
||||||
|
initializeClass(
|
||||||
|
(Class<?>) object, parent, field.getName(),
|
||||||
|
field.getAnnotation(ConfigPath.class),
|
||||||
|
field.getAnnotation(ConfigComment.class),
|
||||||
|
saveDefaults, loadSubClasses
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (IllegalAccessException ignored) {
|
} catch (IllegalAccessException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void setComments(@Nullable String path, @Nullable ConfigCommentInfo comments) {
|
||||||
|
if (comments != null) this.provider.setComment(path, comments);
|
||||||
|
}
|
||||||
|
|
||||||
public void initializeValue(@NotNull ConfigValue<?> value, boolean saveDefault,
|
protected static @Nullable ConfigCommentInfo getClassComments(@NotNull Class<?> clazz,
|
||||||
@NotNull String path, @NotNull String[] comments) {
|
@Nullable ConfigComment fieldAnnotation) {
|
||||||
value.initialize(provider, path, comments);
|
ConfigCommentInfo classComments = ConfigCommentInfo.fromAnnotation(clazz.getAnnotation(ConfigComment.class));
|
||||||
if (saveDefault && value.getDefaultValue() != null && !provider.getConfiguration().contains(path)) {
|
if (classComments != null) return classComments;
|
||||||
value.setDefault();
|
return ConfigCommentInfo.fromAnnotation(fieldAnnotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static @Nullable String getClassPath(@NotNull Class<?> clazz,
|
||||||
|
@Nullable String parentPath,
|
||||||
|
@Nullable String filedName,
|
||||||
|
@Nullable ConfigPath fieldAnnotation) {
|
||||||
|
@NotNull String parent = parentPath != null ? parentPath + "." : "";
|
||||||
|
boolean fromRoot = false;
|
||||||
|
|
||||||
|
// 先获取 Class 对应的路径注解 其优先度最高。
|
||||||
|
ConfigPath clazzAnnotation = clazz.getAnnotation(ConfigPath.class);
|
||||||
|
if (clazzAnnotation != null) {
|
||||||
|
fromRoot = clazzAnnotation.root();
|
||||||
|
if (clazzAnnotation.value().length() > 0) {
|
||||||
|
return (fromRoot ? "" : parent) + clazzAnnotation.value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getSectionPath(@NotNull String name,
|
if (fieldAnnotation != null) {
|
||||||
@Nullable String parentSection,
|
fromRoot = fromRoot || fieldAnnotation.root();
|
||||||
@Nullable ConfigPath pathAnnotation) {
|
if (fieldAnnotation.value().length() > 0) {
|
||||||
@NotNull String parent = parentSection != null ? parentSection + "." : "";
|
return (fromRoot ? "" : parent) + fieldAnnotation.value();
|
||||||
@NotNull String path = getSectionName(name);
|
}
|
||||||
boolean root = false;
|
}
|
||||||
|
|
||||||
|
// 再由 fieldName 获取路径
|
||||||
|
if (filedName != null) return (fromRoot ? "" : parent) + getPathFromName(filedName);
|
||||||
|
else return null; // 不满足上述条件 且 无 fieldName,则说明是根路径。
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static @NotNull String getFieldPath(@NotNull Field field, @Nullable String parentPath) {
|
||||||
|
@NotNull String parent = parentPath != null ? parentPath + "." : "";
|
||||||
|
boolean fromRoot = false;
|
||||||
|
|
||||||
|
// 先获取 Field 对应的路径注解 其优先度最高。
|
||||||
|
ConfigPath pathAnnotation = field.getAnnotation(ConfigPath.class);
|
||||||
if (pathAnnotation != null) {
|
if (pathAnnotation != null) {
|
||||||
if (pathAnnotation.value().length() > 0) path = pathAnnotation.value();
|
fromRoot = pathAnnotation.root();
|
||||||
root = pathAnnotation.root();
|
if (pathAnnotation.value().length() > 0) {
|
||||||
|
return (fromRoot ? "" : parent) + pathAnnotation.value();
|
||||||
}
|
}
|
||||||
return (root ? "" : parent) + path;
|
}
|
||||||
|
|
||||||
|
// 最后再通过 fieldName 自动生成路径
|
||||||
|
return (fromRoot ? "" : parent) + getPathFromName(field.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -116,7 +173,7 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
|||||||
* @param name 源名称
|
* @param name 源名称
|
||||||
* @return 全小写,以“-”链接 的 路径名称
|
* @return 全小写,以“-”链接 的 路径名称
|
||||||
*/
|
*/
|
||||||
public static String getSectionName(String name) {
|
public static String getPathFromName(String name) {
|
||||||
return name.replaceAll("[A-Z]", "-$0") // 将驼峰转换为蛇形;
|
return name.replaceAll("[A-Z]", "-$0") // 将驼峰转换为蛇形;
|
||||||
.replaceAll("-(.*)", "$1") // 若首字母也为大写,则也会被转换,需要去掉第一个横线
|
.replaceAll("-(.*)", "$1") // 若首字母也为大写,则也会被转换,需要去掉第一个横线
|
||||||
.replaceAll("_-([A-Z])", "_$1") // 因为命名中可能包含 _,因此需要被特殊处理一下
|
.replaceAll("_-([A-Z])", "_$1") // 因为命名中可能包含 _,因此需要被特殊处理一下
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
package cc.carm.lib.configuration.core;
|
package cc.carm.lib.configuration.core;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置文件类的根节点,用于标注该类用于记录配置文件中的配置信息。
|
||||||
|
*/
|
||||||
public abstract class ConfigurationRoot {
|
public abstract class ConfigurationRoot {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,4 +14,32 @@ public @interface ConfigComment {
|
|||||||
@NotNull
|
@NotNull
|
||||||
String[] value() default "";
|
String[] value() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 首行换行,即会在注释开始前进行一次换行,与上方配置分离,优化观感。
|
||||||
|
* 如:
|
||||||
|
* <blockquote><pre>
|
||||||
|
* some-key: "SomeValue"
|
||||||
|
*
|
||||||
|
* # 注释第一行
|
||||||
|
* # 注释第二行
|
||||||
|
* startWrap: true
|
||||||
|
* </pre></blockquote>
|
||||||
|
*
|
||||||
|
* @return 是否在结尾添加换行符
|
||||||
|
*/
|
||||||
|
boolean startWrap() default true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 末尾换行,即会在注释结束后进行一次换行,如:
|
||||||
|
* <blockquote><pre>
|
||||||
|
* # 注释第一行
|
||||||
|
* # 注释第二行
|
||||||
|
*
|
||||||
|
* endWrap: true
|
||||||
|
* </pre></blockquote>
|
||||||
|
* <p> 该功能可用于编写配置文件的顶部注释。
|
||||||
|
*
|
||||||
|
* @return 是否在结尾添加换行符
|
||||||
|
*/
|
||||||
|
boolean endWrap() default false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public @interface ConfigPath {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 指定路径的值。
|
* 指定路径的值。
|
||||||
* 若不指定,则会通过 {@link ConfigInitializer#getSectionName(String)}自动生成当前路径的值。
|
* 若不指定,则会通过 {@link ConfigInitializer#getPathFromName(String)} 自动生成当前路径的值。
|
||||||
*
|
*
|
||||||
* @return 路径的值
|
* @return 路径的值
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package cc.carm.lib.configuration.core.builder;
|
package cc.carm.lib.configuration.core.builder;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -13,6 +14,9 @@ public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T
|
|||||||
protected @Nullable String path;
|
protected @Nullable String path;
|
||||||
|
|
||||||
protected @NotNull String[] comments = new String[0];
|
protected @NotNull String[] comments = new String[0];
|
||||||
|
protected boolean startWrap = true;
|
||||||
|
protected boolean endWrap = false;
|
||||||
|
|
||||||
protected @Nullable T defaultValue;
|
protected @Nullable T defaultValue;
|
||||||
|
|
||||||
public AbstractConfigBuilder(Class<? super P> providerClass) {
|
public AbstractConfigBuilder(Class<? super P> providerClass) {
|
||||||
@@ -38,10 +42,34 @@ public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T
|
|||||||
return getThis();
|
return getThis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public @NotNull B setStartWarp(boolean enable) {
|
||||||
|
this.startWrap = enable;
|
||||||
|
return getThis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull B startWarp() {
|
||||||
|
return setStartWarp(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull B setEndWarp(boolean enable) {
|
||||||
|
this.endWrap = enable;
|
||||||
|
return getThis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull B endWarp() {
|
||||||
|
return setEndWarp(true);
|
||||||
|
}
|
||||||
|
|
||||||
public @NotNull B defaults(@Nullable T defaultValue) {
|
public @NotNull B defaults(@Nullable T defaultValue) {
|
||||||
this.defaultValue = defaultValue;
|
this.defaultValue = defaultValue;
|
||||||
return getThis();
|
return getThis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected @Nullable ConfigCommentInfo buildComments() {
|
||||||
|
ConfigCommentInfo info = ConfigCommentInfo.of(this.comments, this.startWrap, this.endWrap);
|
||||||
|
if (info.equals(ConfigCommentInfo.defaults())) return null;
|
||||||
|
else return info;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,46 +1,12 @@
|
|||||||
package cc.carm.lib.configuration.core.builder;
|
package cc.carm.lib.configuration.core.builder;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
public abstract class CommonConfigBuilder<T, B extends CommonConfigBuilder<T, B>>
|
public abstract class CommonConfigBuilder<T, B extends CommonConfigBuilder<T, B>>
|
||||||
extends AbstractConfigBuilder<T, B, ConfigurationProvider<?>> {
|
extends AbstractConfigBuilder<T, B, ConfigurationProvider<?>> {
|
||||||
|
|
||||||
protected @Nullable ConfigurationProvider<?> provider;
|
|
||||||
protected @Nullable String path;
|
|
||||||
|
|
||||||
protected @NotNull String[] comments = new String[0];
|
|
||||||
protected @Nullable T defaultValue;
|
|
||||||
|
|
||||||
public CommonConfigBuilder() {
|
public CommonConfigBuilder() {
|
||||||
super(ConfigurationProvider.class);
|
super(ConfigurationProvider.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract @NotNull B getThis();
|
|
||||||
|
|
||||||
public abstract @NotNull ConfigValue<?> build();
|
|
||||||
|
|
||||||
public @NotNull B from(@Nullable ConfigurationProvider<?> provider) {
|
|
||||||
this.provider = provider;
|
|
||||||
return getThis();
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull B path(@Nullable String path) {
|
|
||||||
this.path = path;
|
|
||||||
return getThis();
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull B comments(@NotNull String... comments) {
|
|
||||||
this.comments = comments;
|
|
||||||
return getThis();
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull B defaults(@Nullable T defaultValue) {
|
|
||||||
this.defaultValue = defaultValue;
|
|
||||||
return getThis();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -31,7 +31,7 @@ public class ConfigListBuilder<V> {
|
|||||||
return from(
|
return from(
|
||||||
Object.class, ConfigDataFunction.identity(),
|
Object.class, ConfigDataFunction.identity(),
|
||||||
ConfigDataFunction.castObject(valueClass),
|
ConfigDataFunction.castObject(valueClass),
|
||||||
ConfigDataFunction.identity(), ConfigDataFunction.toObject()
|
ConfigDataFunction.toObject(), ConfigDataFunction.toObject()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-1
@@ -5,6 +5,8 @@ import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
|||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, SourceListBuilder<S, V>> {
|
public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, SourceListBuilder<S, V>> {
|
||||||
@@ -30,6 +32,11 @@ public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, Source
|
|||||||
this.valueSerializer = valueSerializer;
|
this.valueSerializer = valueSerializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public final @NotNull SourceListBuilder<S, V> defaults(@NotNull V... values) {
|
||||||
|
return defaults(new ArrayList<>(Arrays.asList(values)));
|
||||||
|
}
|
||||||
|
|
||||||
public @NotNull SourceListBuilder<S, V> parseSource(ConfigDataFunction<Object, S> sourceParser) {
|
public @NotNull SourceListBuilder<S, V> parseSource(ConfigDataFunction<Object, S> sourceParser) {
|
||||||
this.sourceParser = sourceParser;
|
this.sourceParser = sourceParser;
|
||||||
return this;
|
return this;
|
||||||
@@ -58,7 +65,7 @@ public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, Source
|
|||||||
@Override
|
@Override
|
||||||
public @NotNull ConfiguredList<V> build() {
|
public @NotNull ConfiguredList<V> build() {
|
||||||
return new ConfiguredList<>(
|
return new ConfiguredList<>(
|
||||||
this.provider, this.path, this.comments,
|
this.provider, this.path, this.buildComments(),
|
||||||
this.valueClass, this.defaultValue,
|
this.valueClass, this.defaultValue,
|
||||||
this.sourceParser.andThen(this.valueParser),
|
this.sourceParser.andThen(this.valueParser),
|
||||||
this.valueSerializer.andThen(sourceSerializer)
|
this.valueSerializer.andThen(sourceSerializer)
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ public class SourceMapBuilder<M extends Map<K, V>, S, K, V> extends CommonConfig
|
|||||||
@Override
|
@Override
|
||||||
public @NotNull ConfiguredMap<K, V> build() {
|
public @NotNull ConfiguredMap<K, V> build() {
|
||||||
return new ConfiguredMap<>(
|
return new ConfiguredMap<>(
|
||||||
this.provider, this.path, this.comments,
|
this.provider, this.path,this.buildComments(),
|
||||||
this.defaultValue, this.supplier,
|
this.defaultValue, this.supplier,
|
||||||
this.keyClass, this.keyParser,
|
this.keyClass, this.keyParser,
|
||||||
this.valueClass, this.sourceParser.andThen(this.valueParser),
|
this.valueClass, this.sourceParser.andThen(this.valueParser),
|
||||||
|
|||||||
+1
-1
@@ -46,7 +46,7 @@ public class ConfigValueBuilder<V> {
|
|||||||
return from(
|
return from(
|
||||||
Object.class, ConfigDataFunction.identity(),
|
Object.class, ConfigDataFunction.identity(),
|
||||||
ConfigValueParser.castObject(valueClass),
|
ConfigValueParser.castObject(valueClass),
|
||||||
ConfigDataFunction.identity(), ConfigDataFunction.toObject()
|
ConfigDataFunction.toObject(), ConfigDataFunction.toObject()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -45,7 +45,7 @@ public class SectionValueBuilder<V>
|
|||||||
@Override
|
@Override
|
||||||
public @NotNull ConfiguredSection<V> build() {
|
public @NotNull ConfiguredSection<V> build() {
|
||||||
return new ConfiguredSection<>(
|
return new ConfiguredSection<>(
|
||||||
this.provider, this.path, this.comments,
|
this.provider, this.path,this.buildComments(),
|
||||||
this.valueClass, this.defaultValue,
|
this.valueClass, this.defaultValue,
|
||||||
this.parser, this.serializer
|
this.parser, this.serializer
|
||||||
);
|
);
|
||||||
|
|||||||
+2
-1
@@ -3,6 +3,7 @@ package cc.carm.lib.configuration.core.builder.value;
|
|||||||
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
|
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@@ -57,7 +58,7 @@ public class SourceValueBuilder<S, V> extends CommonConfigBuilder<V, SourceValue
|
|||||||
@Override
|
@Override
|
||||||
public @NotNull ConfiguredValue<V> build() {
|
public @NotNull ConfiguredValue<V> build() {
|
||||||
return new ConfiguredValue<>(
|
return new ConfiguredValue<>(
|
||||||
this.provider, this.path, this.comments,
|
this.provider, this.path, this.buildComments(),
|
||||||
this.valueClass, this.defaultValue,
|
this.valueClass, this.defaultValue,
|
||||||
this.valueParser.compose(this.sourceParser),
|
this.valueParser.compose(this.sourceParser),
|
||||||
this.valueSerializer.andThen(sourceSerializer)
|
this.valueSerializer.andThen(sourceSerializer)
|
||||||
|
|||||||
@@ -17,7 +17,12 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static <T> @NotNull ConfigDataFunction<T, ? super T> identity() {
|
static <T> @NotNull ConfigDataFunction<T, T> identity() {
|
||||||
|
return (input) -> input;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Contract(pure = true)
|
||||||
|
static <T> @NotNull ConfigDataFunction<T, T> identity(Class<T> type) {
|
||||||
return (input) -> input;
|
return (input) -> input;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +38,6 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
return (input) -> input;
|
return (input) -> input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static <V> @NotNull ConfigDataFunction<Object, V> castObject(Class<V> valueClass) {
|
static <V> @NotNull ConfigDataFunction<Object, V> castObject(Class<V> valueClass) {
|
||||||
return (input) -> {
|
return (input) -> {
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package cc.carm.lib.configuration.core.source;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
||||||
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class ConfigCommentInfo {
|
||||||
|
|
||||||
|
public static @NotNull ConfigCommentInfo DEFAULT_INFO = of(new String[0], true, false);
|
||||||
|
|
||||||
|
protected final @NotNull String[] comments;
|
||||||
|
protected final boolean startWrap;
|
||||||
|
protected final boolean endWrap;
|
||||||
|
|
||||||
|
public ConfigCommentInfo(@NotNull String[] comments, boolean startWrap, boolean endWrap) {
|
||||||
|
this.comments = comments;
|
||||||
|
this.startWrap = startWrap;
|
||||||
|
this.endWrap = endWrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull String[] getComments() {
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean endWrap() {
|
||||||
|
return endWrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean startWrap() {
|
||||||
|
return startWrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull ConfigCommentInfo of(@NotNull String[] comments, boolean startWrap, boolean endWrap) {
|
||||||
|
return new ConfigCommentInfo(comments, startWrap, endWrap);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull ConfigCommentInfo defaults() {
|
||||||
|
return DEFAULT_INFO;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Contract("!null->!null")
|
||||||
|
public static @Nullable ConfigCommentInfo fromAnnotation(@Nullable ConfigComment comment) {
|
||||||
|
if (comment == null) return null;
|
||||||
|
else return new ConfigCommentInfo(comment.value(), comment.startWrap(), comment.endWrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull ConfigCommentInfo fromValue(@NotNull ConfigValue<?> value) {
|
||||||
|
return Optional.ofNullable(value.getComments()).orElse(defaults());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
ConfigCommentInfo that = (ConfigCommentInfo) o;
|
||||||
|
return startWrap == that.startWrap && endWrap == that.endWrap && Arrays.equals(getComments(), that.getComments());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = Objects.hash(startWrap, endWrap);
|
||||||
|
result = 31 * result + Arrays.hashCode(getComments());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ConfigCommentInfo{" +
|
||||||
|
"comments=" + Arrays.toString(comments) +
|
||||||
|
", startWrap=" + startWrap +
|
||||||
|
", endWrap=" + endWrap +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
-3
@@ -27,14 +27,22 @@ public abstract class ConfigurationProvider<W extends ConfigurationWrapper> {
|
|||||||
|
|
||||||
public abstract void save() throws Exception;
|
public abstract void save() throws Exception;
|
||||||
|
|
||||||
public abstract void setComments(@NotNull String path, @NotNull String... comments);
|
public abstract void setComment(@Nullable String path, @Nullable ConfigCommentInfo comment);
|
||||||
|
|
||||||
public abstract @Nullable String[] getComments(@NotNull String path);
|
public abstract @Nullable ConfigCommentInfo getComment(@Nullable String path);
|
||||||
|
|
||||||
public abstract @NotNull ConfigInitializer<? extends ConfigurationProvider<W>> getInitializer();
|
public abstract @NotNull ConfigInitializer<? extends ConfigurationProvider<W>> getInitializer();
|
||||||
|
|
||||||
public void initialize(Class<? extends ConfigurationRoot> configClazz) {
|
public void initialize(Class<? extends ConfigurationRoot> configClazz) {
|
||||||
getInitializer().initialize(configClazz, true);
|
initialize(configClazz, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initialize(Class<? extends ConfigurationRoot> configClazz, boolean saveDefaults) {
|
||||||
|
getInitializer().initialize(configClazz, saveDefaults);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initialize(Class<? extends ConfigurationRoot> configClazz, boolean saveDefaults, boolean loadSubClasses) {
|
||||||
|
getInitializer().initialize(configClazz, saveDefaults, loadSubClasses);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cc.carm.lib.configuration.core.value;
|
package cc.carm.lib.configuration.core.value;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -17,25 +18,26 @@ public abstract class ConfigValue<T> {
|
|||||||
|
|
||||||
protected @Nullable ConfigurationProvider<?> provider;
|
protected @Nullable ConfigurationProvider<?> provider;
|
||||||
protected @Nullable String configPath;
|
protected @Nullable String configPath;
|
||||||
protected @NotNull String[] comments;
|
protected @Nullable ConfigCommentInfo comments;
|
||||||
|
|
||||||
protected @Nullable T defaultValue;
|
protected @Nullable T defaultValue;
|
||||||
|
|
||||||
public ConfigValue(@Nullable ConfigurationProvider<?> provider,
|
public ConfigValue(@Nullable ConfigurationProvider<?> provider,
|
||||||
@Nullable String configPath, @NotNull String[] comments, @Nullable T defaultValue) {
|
@Nullable String configPath, @Nullable ConfigCommentInfo comments,
|
||||||
|
@Nullable T defaultValue) {
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.configPath = configPath;
|
this.configPath = configPath;
|
||||||
this.comments = comments;
|
this.comments = comments;
|
||||||
this.defaultValue = defaultValue;
|
this.defaultValue = defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize(@NotNull ConfigurationProvider<?> provider, @NotNull String configPath, @NotNull String... comments) {
|
public void initialize(@NotNull ConfigurationProvider<?> provider, boolean saveDefault,
|
||||||
|
@NotNull String configPath, @Nullable ConfigCommentInfo comments) {
|
||||||
if (this.provider == null) this.provider = provider;
|
if (this.provider == null) this.provider = provider;
|
||||||
if (this.configPath == null) this.configPath = configPath;
|
if (this.configPath == null) this.configPath = configPath;
|
||||||
if (this.comments.length == 0) this.comments = comments;
|
if (this.comments == null) this.comments = comments;
|
||||||
|
if (saveDefault) setDefault();
|
||||||
this.provider.setComments(this.configPath, this.comments);
|
if (getComments() != null) this.provider.setComment(getConfigPath(), getComments());
|
||||||
get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable T getDefaultValue() {
|
public @Nullable T getDefaultValue() {
|
||||||
@@ -46,12 +48,29 @@ public abstract class ConfigValue<T> {
|
|||||||
this.defaultValue = defaultValue;
|
this.defaultValue = defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到该配置的设定值(即读取到的值)。
|
||||||
|
* <br> 若初始化时未写入默认值,则可以通过 {@link #getOrDefault()} 方法在该设定值为空时获取默认值。
|
||||||
|
*
|
||||||
|
* @return 设定值
|
||||||
|
*/
|
||||||
public abstract @Nullable T get();
|
public abstract @Nullable T get();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到该配置的设定值,若不存在,则返回默认值。
|
||||||
|
*
|
||||||
|
* @return 设定值或默认值
|
||||||
|
*/
|
||||||
public @Nullable T getOrDefault() {
|
public @Nullable T getOrDefault() {
|
||||||
return Optional.ofNullable(get()).orElse(getDefaultValue());
|
return getOptional().orElse(getDefaultValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到该配置的非空值。
|
||||||
|
*
|
||||||
|
* @return 非空值
|
||||||
|
* @throws NullPointerException 对应数据为空时抛出
|
||||||
|
*/
|
||||||
public @NotNull T getNotNull() {
|
public @NotNull T getNotNull() {
|
||||||
return Objects.requireNonNull(getOrDefault(), "Value(" + configPath + ") is null.");
|
return Objects.requireNonNull(getOrDefault(), "Value(" + configPath + ") is null.");
|
||||||
}
|
}
|
||||||
@@ -60,12 +79,38 @@ public abstract class ConfigValue<T> {
|
|||||||
return Optional.ofNullable(get());
|
return Optional.ofNullable(get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定该配置的值。
|
||||||
|
* <br> 设定后,不会自动保存配置文件;若需要保存,请调用 {@link ConfigurationProvider#save()} 方法。
|
||||||
|
*
|
||||||
|
* @param value 配置的值
|
||||||
|
*/
|
||||||
public abstract void set(@Nullable T value);
|
public abstract void set(@Nullable T value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化该配置的默认值。
|
||||||
|
* <br> 设定后,不会自动保存配置文件;若需要保存,请调用 {@link ConfigurationProvider#save()} 方法。
|
||||||
|
*/
|
||||||
public void setDefault() {
|
public void setDefault() {
|
||||||
|
setDefault(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将该配置的值设置为默认值。
|
||||||
|
* <br> 设定后,不会自动保存配置文件;若需要保存,请调用 {@link ConfigurationProvider#save()} 方法。
|
||||||
|
*
|
||||||
|
* @param override 是否覆盖已设定的值
|
||||||
|
*/
|
||||||
|
public void setDefault(boolean override) {
|
||||||
|
if (!override && getConfiguration().contains(getConfigPath())) return;
|
||||||
Optional.ofNullable(getDefaultValue()).ifPresent(this::set);
|
Optional.ofNullable(getDefaultValue()).ifPresent(this::set);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断加载的配置是否与默认值相同。
|
||||||
|
*
|
||||||
|
* @return 获取当前值是否为默认值。
|
||||||
|
*/
|
||||||
public boolean isDefault() {
|
public boolean isDefault() {
|
||||||
T defaultValue = getDefaultValue();
|
T defaultValue = getDefaultValue();
|
||||||
T value = get();
|
T value = get();
|
||||||
@@ -100,12 +145,8 @@ public abstract class ConfigValue<T> {
|
|||||||
getConfiguration().set(getConfigPath(), value);
|
getConfiguration().set(getConfigPath(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getComments() {
|
public @Nullable ConfigCommentInfo getComments() {
|
||||||
return comments;
|
return comments;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setComments(String[] comments) {
|
|
||||||
this.comments = comments;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package cc.carm.lib.configuration.core.value.impl;
|
package cc.carm.lib.configuration.core.value.impl;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -12,7 +13,7 @@ public abstract class CachedConfigValue<T> extends ConfigValue<T> {
|
|||||||
protected long parsedTime = -1;
|
protected long parsedTime = -1;
|
||||||
|
|
||||||
public CachedConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
public CachedConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||||
@NotNull String[] comments, @Nullable T defaultValue) {
|
@Nullable ConfigCommentInfo comments, @Nullable T defaultValue) {
|
||||||
super(provider, sectionPath, comments, defaultValue);
|
super(provider, sectionPath, comments, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package cc.carm.lib.configuration.core.value.type;
|
|||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder;
|
import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -22,7 +23,7 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> {
|
|||||||
protected final @NotNull ConfigDataFunction<V, Object> serializer;
|
protected final @NotNull ConfigDataFunction<V, Object> serializer;
|
||||||
|
|
||||||
public ConfiguredList(@Nullable ConfigurationProvider<?> provider,
|
public ConfiguredList(@Nullable ConfigurationProvider<?> provider,
|
||||||
@Nullable String sectionPath, @NotNull String[] comments,
|
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
|
||||||
@NotNull Class<V> valueClass, @Nullable List<V> defaultValue,
|
@NotNull Class<V> valueClass, @Nullable List<V> defaultValue,
|
||||||
@NotNull ConfigDataFunction<Object, V> parser,
|
@NotNull ConfigDataFunction<Object, V> parser,
|
||||||
@NotNull ConfigDataFunction<V, Object> serializer) {
|
@NotNull ConfigDataFunction<V, Object> serializer) {
|
||||||
@@ -34,15 +35,10 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull List<V> get() {
|
public @NotNull List<V> get() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||||
List<V> list = new ArrayList<>();
|
List<V> list = new ArrayList<>();
|
||||||
|
|
||||||
List<?> data = getConfiguration().getList(getConfigPath());
|
List<?> data = getConfiguration().getList(getConfigPath());
|
||||||
if (data == null || data.isEmpty()) return useOrDefault(list);
|
if (data == null || data.isEmpty()) return useOrDefault(list);
|
||||||
|
|
||||||
for (Object dataVal : data) {
|
for (Object dataVal : data) {
|
||||||
if (dataVal == null) continue;
|
if (dataVal == null) continue;
|
||||||
try {
|
try {
|
||||||
@@ -51,7 +47,6 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return updateCache(list);
|
return updateCache(list);
|
||||||
} else if (getCachedValue() != null) return getCachedValue();
|
} else if (getCachedValue() != null) return getCachedValue();
|
||||||
else if (getDefaultValue() != null) return getDefaultValue();
|
else if (getDefaultValue() != null) return getDefaultValue();
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package cc.carm.lib.configuration.core.value.type;
|
|||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder;
|
import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||||
@@ -32,7 +33,7 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> {
|
|||||||
protected final @NotNull ConfigDataFunction<V, Object> valueSerializer;
|
protected final @NotNull ConfigDataFunction<V, Object> valueSerializer;
|
||||||
|
|
||||||
public ConfiguredMap(@Nullable ConfigurationProvider<?> provider,
|
public ConfiguredMap(@Nullable ConfigurationProvider<?> provider,
|
||||||
@Nullable String sectionPath, @NotNull String[] comments,
|
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
|
||||||
@Nullable Map<K, V> defaultValue, @NotNull Supplier<? extends Map<K, V>> supplier,
|
@Nullable Map<K, V> defaultValue, @NotNull Supplier<? extends Map<K, V>> supplier,
|
||||||
@NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser,
|
@NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser,
|
||||||
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<Object, V> valueParser,
|
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<Object, V> valueParser,
|
||||||
@@ -109,9 +110,10 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> {
|
|||||||
Map<String, Object> data = new LinkedHashMap<>();
|
Map<String, Object> data = new LinkedHashMap<>();
|
||||||
for (Map.Entry<K, V> entry : value.entrySet()) {
|
for (Map.Entry<K, V> entry : value.entrySet()) {
|
||||||
try {
|
try {
|
||||||
String key = keySerializer.parse(entry.getKey());
|
data.put(
|
||||||
Object val = valueSerializer.parse(entry.getValue());
|
keySerializer.parse(entry.getKey()),
|
||||||
data.put(key, val);
|
valueSerializer.parse(entry.getValue())
|
||||||
|
);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package cc.carm.lib.configuration.core.value.type;
|
|||||||
import cc.carm.lib.configuration.core.builder.value.SectionValueBuilder;
|
import cc.carm.lib.configuration.core.builder.value.SectionValueBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||||
@@ -24,7 +25,7 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
|
|||||||
protected final @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer;
|
protected final @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer;
|
||||||
|
|
||||||
public ConfiguredSection(@Nullable ConfigurationProvider<?> provider,
|
public ConfiguredSection(@Nullable ConfigurationProvider<?> provider,
|
||||||
@Nullable String sectionPath, @NotNull String[] comments,
|
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
|
||||||
@NotNull Class<V> valueClass, @Nullable V defaultValue,
|
@NotNull Class<V> valueClass, @Nullable V defaultValue,
|
||||||
@NotNull ConfigValueParser<ConfigurationWrapper, V> parser,
|
@NotNull ConfigValueParser<ConfigurationWrapper, V> parser,
|
||||||
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) {
|
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) {
|
||||||
@@ -69,7 +70,6 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
|
|||||||
else {
|
else {
|
||||||
try {
|
try {
|
||||||
setValue(serializer.parse(value));
|
setValue(serializer.parse(value));
|
||||||
// getConfiguration().createSection(getSectionPath(), serializer.parse(value));
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package cc.carm.lib.configuration.core.value.type;
|
|||||||
import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder;
|
import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -30,7 +31,7 @@ public class ConfiguredValue<V> extends CachedConfigValue<V> {
|
|||||||
protected final @NotNull ConfigDataFunction<V, Object> serializer;
|
protected final @NotNull ConfigDataFunction<V, Object> serializer;
|
||||||
|
|
||||||
public ConfiguredValue(@Nullable ConfigurationProvider<?> provider,
|
public ConfiguredValue(@Nullable ConfigurationProvider<?> provider,
|
||||||
@Nullable String sectionPath, @NotNull String[] comments,
|
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
|
||||||
@NotNull Class<V> valueClass, @Nullable V defaultValue,
|
@NotNull Class<V> valueClass, @Nullable V defaultValue,
|
||||||
@NotNull ConfigValueParser<Object, V> parser,
|
@NotNull ConfigValueParser<Object, V> parser,
|
||||||
@NotNull ConfigDataFunction<V, Object> serializer) {
|
@NotNull ConfigDataFunction<V, Object> serializer) {
|
||||||
@@ -52,7 +53,7 @@ public class ConfiguredValue<V> extends CachedConfigValue<V> {
|
|||||||
@Override
|
@Override
|
||||||
public V get() {
|
public V get() {
|
||||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||||
Object value = getConfiguration().get(getConfigPath());
|
Object value = getValue();
|
||||||
if (value == null) return useDefault(); // 获取的值不存在,直接使用默认值。
|
if (value == null) return useDefault(); // 获取的值不存在,直接使用默认值。
|
||||||
try {
|
try {
|
||||||
// 若未出现错误,则直接更新缓存并返回。
|
// 若未出现错误,则直接更新缓存并返回。
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ public class NameTest {
|
|||||||
@Test
|
@Test
|
||||||
public void onTest() {
|
public void onTest() {
|
||||||
|
|
||||||
System.out.println(ConfigInitializer.getSectionName("LoveGames")); // -> love-games
|
System.out.println(ConfigInitializer.getPathFromName("LoveGames")); // -> love-games
|
||||||
System.out.println(ConfigInitializer.getSectionName("EASY_GAME")); // -> easy-game
|
System.out.println(ConfigInitializer.getPathFromName("EASY_GAME")); // -> easy-game
|
||||||
System.out.println(ConfigInitializer.getSectionName("F")); //-? f
|
System.out.println(ConfigInitializer.getPathFromName("F")); //-? f
|
||||||
System.out.println(ConfigInitializer.getSectionName("Test123123")); // -? test123123123
|
System.out.println(ConfigInitializer.getPathFromName("Test123123")); // -? test123123123
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.0</version>
|
<version>2.3.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
@@ -13,8 +13,7 @@
|
|||||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||||
</properties>
|
</properties>
|
||||||
|
<artifactId>easyconfiguration-json</artifactId>
|
||||||
<artifactId>easyconfiguration-spigot</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@@ -27,10 +26,10 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.spigotmc</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>spigot-api</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>1.18.2-R0.1-SNAPSHOT</version>
|
<version>2.9.0</version>
|
||||||
<scope>provided</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
@@ -56,5 +55,4 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
+6
-7
@@ -1,14 +1,14 @@
|
|||||||
package cc.carm.lib.configuration;
|
package cc.carm.lib.configuration;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.bungee.BungeeConfigProvider;
|
import cc.carm.lib.configuration.json.JSONConfigProvider;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class EasyConfiguration {
|
public class EasyConfiguration {
|
||||||
|
|
||||||
public static BungeeConfigProvider from(File file, String source) {
|
public static JSONConfigProvider from(File file, String source) {
|
||||||
BungeeConfigProvider provider = new BungeeConfigProvider(file);
|
JSONConfigProvider provider = new JSONConfigProvider(file);
|
||||||
try {
|
try {
|
||||||
provider.initializeFile(source);
|
provider.initializeFile(source);
|
||||||
provider.initializeConfig();
|
provider.initializeConfig();
|
||||||
@@ -18,17 +18,16 @@ public class EasyConfiguration {
|
|||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BungeeConfigProvider from(File file) {
|
public static JSONConfigProvider from(File file) {
|
||||||
return from(file, file.getName());
|
return from(file, file.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BungeeConfigProvider from(String fileName) {
|
public static JSONConfigProvider from(String fileName) {
|
||||||
return from(fileName, fileName);
|
return from(fileName, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BungeeConfigProvider from(String fileName, String source) {
|
public static JSONConfigProvider from(String fileName, String source) {
|
||||||
return from(new File(fileName), source);
|
return from(new File(fileName), source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package cc.carm.lib.configuration.json;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
|
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonSerializer;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Some code comes from BungeeCord's implementation of the JsonConfiguration.
|
||||||
|
*
|
||||||
|
* @author md_5, CarmJos
|
||||||
|
*/
|
||||||
|
public class JSONConfigProvider extends FileConfigProvider<JSONConfigWrapper> {
|
||||||
|
|
||||||
|
protected final Gson gson = new GsonBuilder()
|
||||||
|
.serializeNulls().disableHtmlEscaping().setPrettyPrinting()
|
||||||
|
.registerTypeAdapter(
|
||||||
|
JSONConfigWrapper.class,
|
||||||
|
(JsonSerializer<JSONConfigWrapper>) (src, typeOfSrc, context) -> context.serialize(src.data)
|
||||||
|
).create();
|
||||||
|
|
||||||
|
protected JSONConfigWrapper configuration;
|
||||||
|
protected ConfigInitializer<JSONConfigProvider> initializer;
|
||||||
|
|
||||||
|
public JSONConfigProvider(@NotNull File file) {
|
||||||
|
super(file);
|
||||||
|
this.initializer = new ConfigInitializer<>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initializeConfig() {
|
||||||
|
LinkedHashMap<?, ?> map = null;
|
||||||
|
|
||||||
|
try (FileInputStream is = new FileInputStream(file)) {
|
||||||
|
map = gson.fromJson(new InputStreamReader(is, StandardCharsets.UTF_8), LinkedHashMap.class);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (map == null) map = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
this.configuration = new JSONConfigWrapper(map);
|
||||||
|
this.initializer = new ConfigInitializer<>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull JSONConfigWrapper getConfiguration() {
|
||||||
|
return this.configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reload() {
|
||||||
|
initializeConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save() throws Exception {
|
||||||
|
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
|
||||||
|
gson.toJson(configuration.data, writer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setComment(@Nullable String path, @Nullable ConfigCommentInfo comment) {
|
||||||
|
// JSON doesn't support comments;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable ConfigCommentInfo getComment(@Nullable String path) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ConfigInitializer<? extends ConfigurationProvider<JSONConfigWrapper>> getInitializer() {
|
||||||
|
return this.initializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
package cc.carm.lib.configuration.json;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Some code comes from BungeeCord's implementation of the JsonConfiguration.
|
||||||
|
*
|
||||||
|
* @author md_5, CarmJos
|
||||||
|
*/
|
||||||
|
public class JSONConfigWrapper implements ConfigurationWrapper {
|
||||||
|
|
||||||
|
private static final char SEPARATOR = '.';
|
||||||
|
protected final Map<String, Object> data;
|
||||||
|
|
||||||
|
JSONConfigWrapper(Map<?, ?> map) {
|
||||||
|
this.data = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
for (Map.Entry<?, ?> entry : map.entrySet()) {
|
||||||
|
String key = (entry.getKey() == null) ? "null" : entry.getKey().toString();
|
||||||
|
|
||||||
|
if (entry.getValue() instanceof Map) {
|
||||||
|
this.data.put(key, new JSONConfigWrapper((Map<?, ?>) entry.getValue()));
|
||||||
|
} else {
|
||||||
|
this.data.put(key, entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Set<String> getKeys(boolean deep) {
|
||||||
|
return getValues(deep).keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Map<String, Object> getValues(boolean deep) {
|
||||||
|
if (deep) {
|
||||||
|
Map<String, Object> values = new LinkedHashMap<>();
|
||||||
|
mapChildrenValues(values, this, null, true);
|
||||||
|
return values;
|
||||||
|
} else return new LinkedHashMap<>(this.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(@NotNull String path, @Nullable Object value) {
|
||||||
|
if (value instanceof Map) {
|
||||||
|
value = new JSONConfigWrapper((Map<?, ?>) value);
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONConfigWrapper section = getSectionFor(path);
|
||||||
|
if (section == this) {
|
||||||
|
if (value == null) {
|
||||||
|
this.data.remove(path);
|
||||||
|
} else {
|
||||||
|
this.data.put(path, value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
section.set(getChild(path), value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean contains(@NotNull String path) {
|
||||||
|
return get(path) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable Object get(@NotNull String path) {
|
||||||
|
JSONConfigWrapper section = getSectionFor(path);
|
||||||
|
return section == this ? data.get(path) : section.get(getChild(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isList(@NotNull String path) {
|
||||||
|
return get(path) instanceof List<?>;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable List<?> getList(@NotNull String path) {
|
||||||
|
Object val = get(path);
|
||||||
|
return (val instanceof List<?>) ? (List<?>) val : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isConfigurationSection(@NotNull String path) {
|
||||||
|
return get(path) instanceof JSONConfigWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
||||||
|
Object val = get(path);
|
||||||
|
return (val instanceof JSONConfigWrapper) ? (JSONConfigWrapper) val : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private JSONConfigWrapper getSectionFor(String path) {
|
||||||
|
int index = path.indexOf(SEPARATOR);
|
||||||
|
if (index == -1) return this;
|
||||||
|
|
||||||
|
String root = path.substring(0, index);
|
||||||
|
Object section = this.data.get(root);
|
||||||
|
if (section == null) {
|
||||||
|
section = new JSONConfigWrapper(new LinkedHashMap<>());
|
||||||
|
this.data.put(root, section);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (JSONConfigWrapper) section;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getChild(String path) {
|
||||||
|
int index = path.indexOf(SEPARATOR);
|
||||||
|
return (index == -1) ? path : path.substring(index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void mapChildrenValues(@NotNull Map<String, Object> output, @NotNull JSONConfigWrapper section,
|
||||||
|
@Nullable String parent, boolean deep) {
|
||||||
|
for (Map.Entry<String, Object> entry : section.data.entrySet()) {
|
||||||
|
String path = (parent == null ? "" : parent + ".") + entry.getKey();
|
||||||
|
output.remove(path);
|
||||||
|
output.put(path, entry.getValue());
|
||||||
|
if (deep && entry.getValue() instanceof JSONConfigWrapper) {
|
||||||
|
this.mapChildrenValues(output, (JSONConfigWrapper) entry.getValue(), path, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package cc.carm.lib.configuration.json;
|
||||||
|
|
||||||
|
public abstract class JSONValue {
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package config;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.EasyConfiguration;
|
||||||
|
import cc.carm.lib.configuration.json.JSONConfigProvider;
|
||||||
|
import config.model.TestModel;
|
||||||
|
import config.source.DemoConfiguration;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
public class JSONConfigTest {
|
||||||
|
|
||||||
|
protected final JSONConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onTest() {
|
||||||
|
|
||||||
|
provider.initialize(DemoConfiguration.class);
|
||||||
|
|
||||||
|
testDemo();
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
provider.getConfiguration().getValues(true).forEach((k, v) -> System.out.println(k + ": " + v));
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
provider.getConfiguration().getValues(false).forEach((k, v) -> System.out.println(k + ": " + v));
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
|
||||||
|
try {
|
||||||
|
provider.save();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void testDemo() {
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
|
||||||
|
System.out.println("> Test Value:");
|
||||||
|
System.out.println("before: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||||
|
DemoConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
||||||
|
System.out.println("after: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||||
|
|
||||||
|
System.out.println("> Test List:");
|
||||||
|
DemoConfiguration.Sub.That.OPERATORS.getNotNull().forEach(System.out::println);
|
||||||
|
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
||||||
|
DemoConfiguration.Sub.That.OPERATORS.set(operators);
|
||||||
|
|
||||||
|
System.out.println("> Test Section:");
|
||||||
|
System.out.println(DemoConfiguration.MODEL_TEST.get());
|
||||||
|
DemoConfiguration.MODEL_TEST.set(TestModel.random());
|
||||||
|
|
||||||
|
System.out.println("> Test Maps:");
|
||||||
|
DemoConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
|
||||||
|
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
|
||||||
|
for (int i = 1; i <= 5; i++) {
|
||||||
|
data.put(i, UUID.randomUUID());
|
||||||
|
}
|
||||||
|
DemoConfiguration.USERS.set(data);
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package config.model;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public abstract class AbstractModel {
|
||||||
|
|
||||||
|
protected final @NotNull String name;
|
||||||
|
|
||||||
|
public AbstractModel(@NotNull String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package config.model;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.TestOnly;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class SomeModel extends AbstractModel {
|
||||||
|
|
||||||
|
int num;
|
||||||
|
|
||||||
|
public SomeModel(@NotNull String name, int num) {
|
||||||
|
super(name);
|
||||||
|
this.num = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "SomeModel{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", num=" + num +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull Map<String, Object> serialize() {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("name", name);
|
||||||
|
map.put("num", num);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestOnly
|
||||||
|
public static SomeModel deserialize(Map<String, ?> args) {
|
||||||
|
return new SomeModel((String) args.get("name"), (Integer) args.get("num"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package config.model;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class TestModel extends AbstractModel {
|
||||||
|
|
||||||
|
public UUID uuid;
|
||||||
|
|
||||||
|
public TestModel(String name, UUID uuid) {
|
||||||
|
super(name);
|
||||||
|
this.uuid = uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUuid(UUID uuid) {
|
||||||
|
this.uuid = uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UUID getUuid() {
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull Map<String, Object> serialize() {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("name", name);
|
||||||
|
Map<String, Object> map2 = new HashMap<>();
|
||||||
|
map2.put("uuid", uuid.toString());
|
||||||
|
map.put("info", map2);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TestModel deserialize(ConfigurationWrapper section) {
|
||||||
|
String name = section.getString("name");
|
||||||
|
if (name == null) throw new NullPointerException("name is null");
|
||||||
|
String uuidString = section.getString("info.uuid");
|
||||||
|
if (uuidString == null) throw new NullPointerException("uuid is null");
|
||||||
|
return new TestModel(name, UUID.fromString(uuidString));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TestModel random() {
|
||||||
|
return new TestModel(UUID.randomUUID().toString().substring(0, 5), UUID.randomUUID());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "TestUser{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", uuid=" + uuid +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package config.source;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||||
|
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
||||||
|
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||||
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
|
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||||
|
|
||||||
|
@ConfigComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"})
|
||||||
|
public class DatabaseConfiguration extends ConfigurationRoot {
|
||||||
|
|
||||||
|
@ConfigPath("driver")
|
||||||
|
protected static final ConfigValue<String> DRIVER_NAME = ConfiguredValue.of(
|
||||||
|
String.class, "com.mysql.cj.jdbc.Driver"
|
||||||
|
);
|
||||||
|
|
||||||
|
protected static final ConfigValue<String> HOST = ConfiguredValue.of(String.class, "127.0.0.1");
|
||||||
|
protected static final ConfigValue<Integer> PORT = ConfiguredValue.of(Integer.class, 3306);
|
||||||
|
|
||||||
|
protected static final ConfigValue<String> DATABASE = ConfiguredValue.of(String.class, "minecraft");
|
||||||
|
protected static final ConfigValue<String> USERNAME = ConfiguredValue.of(String.class, "root");
|
||||||
|
protected static final ConfigValue<String> PASSWORD = ConfiguredValue.of(String.class, "password");
|
||||||
|
|
||||||
|
protected static final ConfigValue<String> EXTRA = ConfiguredValue.of(String.class, "?useSSL=false");
|
||||||
|
|
||||||
|
protected static String buildJDBC() {
|
||||||
|
return String.format("jdbc:mysql://%s:%s/%s%s", HOST.get(), PORT.get(), DATABASE.get(), EXTRA.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+20
-11
@@ -1,7 +1,6 @@
|
|||||||
package config.source;
|
package config.source;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||||
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
|
||||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||||
@@ -15,41 +14,51 @@ import java.util.Map;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class ComplexConfiguration extends ConfigurationRoot {
|
public class DemoConfiguration extends ConfigurationRoot {
|
||||||
|
|
||||||
@ConfigComment({"User测试"})
|
@ConfigPath(root = true)
|
||||||
public static final ConfigValue<TestModel> USER = ConfiguredSection
|
protected static final ConfigValue<Double> VERSION = ConfiguredValue.of(Double.class, 1.0D);
|
||||||
|
|
||||||
|
// 可以直接写静态内部类,并通过 Class<?> 声明。
|
||||||
|
public static final Class<?> SUB_TEST = Sub.class;
|
||||||
|
|
||||||
|
@ConfigPath("user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||||
|
public static final ConfigValue<TestModel> MODEL_TEST = ConfiguredSection
|
||||||
.builder(TestModel.class)
|
.builder(TestModel.class)
|
||||||
.defaults(new TestModel("Carm", UUID.randomUUID()))
|
.defaults(new TestModel("Carm", UUID.randomUUID()))
|
||||||
.parseValue((section, defaultValue) -> TestModel.deserialize(section))
|
.parseValue((section, defaultValue) -> TestModel.deserialize(section))
|
||||||
.serializeValue(TestModel::serialize).build();
|
.serializeValue(TestModel::serialize).build();
|
||||||
|
|
||||||
@ConfigComment({"[ID-UUID] 对照表", "", "用于测试Map类型的解析与序列化保存"})
|
// 子配置文件
|
||||||
|
@ConfigPath("database")
|
||||||
|
public static final Class<?> DB_CONFIG = DatabaseConfiguration.class;
|
||||||
|
|
||||||
public static final ConfigValue<Map<Integer, UUID>> USERS = ConfiguredMap
|
public static final ConfigValue<Map<Integer, UUID>> USERS = ConfiguredMap
|
||||||
.builder(Integer.class, UUID.class).fromString()
|
.builder(Integer.class, UUID.class).fromString()
|
||||||
.parseKey(Integer::parseInt)
|
.parseKey(Integer::parseInt)
|
||||||
.parseValue(v -> Objects.requireNonNull(UUID.fromString(v)))
|
.parseValue(v -> Objects.requireNonNull(UUID.fromString(v)))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
public static class Sub {
|
|
||||||
|
|
||||||
@ConfigPath(value = "uuid", root = true)
|
public static class Sub extends ConfigurationRoot {
|
||||||
|
|
||||||
|
@ConfigPath(value = "uuid-value", root = true)
|
||||||
public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfiguredValue
|
public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfiguredValue
|
||||||
.builder(UUID.class).fromString()
|
.builder(UUID.class).fromString()
|
||||||
.parseValue((data, defaultValue) -> UUID.fromString(data))
|
.parseValue((data, defaultValue) -> UUID.fromString(data))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@ConfigPath("nothing")
|
public static final Class<?> NOTHING = That.class;
|
||||||
public static class That {
|
|
||||||
|
|
||||||
public static final ConfigValue<List<UUID>> Operators = ConfiguredList
|
public static class That extends ConfigurationRoot {
|
||||||
|
|
||||||
|
public static final ConfigValue<List<UUID>> OPERATORS = ConfiguredList
|
||||||
.builder(UUID.class).fromString()
|
.builder(UUID.class).fromString()
|
||||||
.parseValue(s -> Objects.requireNonNull(UUID.fromString(s)))
|
.parseValue(s -> Objects.requireNonNull(UUID.fromString(s)))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
+4
-4
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.0</version>
|
<version>2.3.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
@@ -27,9 +27,9 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>org.bspfsystems</groupId>
|
||||||
<artifactId>yamlconfiguration-commented</artifactId>
|
<artifactId>yamlconfiguration</artifactId>
|
||||||
<version>2.0.2</version>
|
<version>1.0.11</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
package cc.carm.lib.configuration;
|
package cc.carm.lib.configuration;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
import cc.carm.lib.configuration.yaml.YAMLConfigProvider;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class EasyConfiguration {
|
public class EasyConfiguration {
|
||||||
|
|
||||||
public static YamlConfigProvider from(File file, String source) {
|
public static YAMLConfigProvider from(File file, String source) {
|
||||||
YamlConfigProvider provider = new YamlConfigProvider(file);
|
YAMLConfigProvider provider = new YAMLConfigProvider(file);
|
||||||
try {
|
try {
|
||||||
provider.initializeFile(source);
|
provider.initializeFile(source);
|
||||||
provider.initializeConfig();
|
provider.initializeConfig();
|
||||||
@@ -18,15 +18,15 @@ public class EasyConfiguration {
|
|||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static YamlConfigProvider from(File file) {
|
public static YAMLConfigProvider from(File file) {
|
||||||
return from(file, file.getName());
|
return from(file, file.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static YamlConfigProvider from(String fileName) {
|
public static YAMLConfigProvider from(String fileName) {
|
||||||
return from(fileName, fileName);
|
return from(fileName, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static YamlConfigProvider from(String fileName, String source) {
|
public static YAMLConfigProvider from(String fileName, String source) {
|
||||||
return from(new File(fileName), source);
|
return from(new File(fileName), source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,114 @@
|
|||||||
|
package cc.carm.lib.configuration.yaml;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
|
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
|
||||||
|
import org.bspfsystems.yamlconfiguration.file.FileConfiguration;
|
||||||
|
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.StringJoiner;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
import static cc.carm.lib.configuration.yaml.YAMLConfigProvider.SEPARATOR;
|
||||||
|
|
||||||
|
public class YAMLComments {
|
||||||
|
|
||||||
|
Map<String, ConfigCommentInfo> comments = new HashMap<>();
|
||||||
|
|
||||||
|
protected Map<String, ConfigCommentInfo> getComments() {
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(@Nullable String path, @Nullable ConfigCommentInfo comments) {
|
||||||
|
if (comments == null) {
|
||||||
|
getComments().remove(path);
|
||||||
|
} else {
|
||||||
|
getComments().put(path, comments);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ConfigCommentInfo get(@Nullable String path) {
|
||||||
|
return getComments().getOrDefault(path, ConfigCommentInfo.defaults());
|
||||||
|
}
|
||||||
|
|
||||||
|
public @Nullable String buildComments(@NotNull String indents, @Nullable String path) {
|
||||||
|
ConfigCommentInfo comments = get(path);
|
||||||
|
if (!String.join("", comments.getComments()).isEmpty()) {
|
||||||
|
String prefix = comments.startWrap() ? "\n" : "";
|
||||||
|
String suffix = comments.endWrap() ? "\n" : "";
|
||||||
|
StringJoiner joiner = new StringJoiner("\n", prefix, suffix);
|
||||||
|
for (String comment : comments.getComments()) {
|
||||||
|
if (comment.length() == 0) joiner.add(" ");
|
||||||
|
else joiner.add(indents + "# " + comment);
|
||||||
|
}
|
||||||
|
return joiner + "\n";
|
||||||
|
} else {
|
||||||
|
return comments.startWrap() || comments.endWrap() ? "\n" : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从一个文件读取配置并写入注释到某个写入器中。
|
||||||
|
* 该方法的源代码来自 tchristofferson/ConfigUpdater 项目。
|
||||||
|
*
|
||||||
|
* @param source 源配置文件
|
||||||
|
* @param writer 配置写入器
|
||||||
|
* @throws IOException 当写入发生错误时抛出
|
||||||
|
*/
|
||||||
|
public void writeComments(@NotNull YamlConfiguration source, @NotNull BufferedWriter writer) throws IOException {
|
||||||
|
FileConfiguration parserConfig = new YamlConfiguration();
|
||||||
|
|
||||||
|
for (String fullKey : source.getKeys(true)) {
|
||||||
|
String indents = getIndents(fullKey);
|
||||||
|
String comment = buildComments(indents, fullKey);
|
||||||
|
if (comment != null) writer.write(comment);
|
||||||
|
|
||||||
|
Object currentValue = source.get(fullKey);
|
||||||
|
|
||||||
|
String[] splitFullKey = fullKey.split("[" + SEPARATOR + "]");
|
||||||
|
String trailingKey = splitFullKey[splitFullKey.length - 1];
|
||||||
|
|
||||||
|
if (currentValue instanceof ConfigurationSection) {
|
||||||
|
writer.write(indents + trailingKey + ":");
|
||||||
|
|
||||||
|
if (!((ConfigurationSection) currentValue).getKeys(false).isEmpty())
|
||||||
|
writer.write("\n");
|
||||||
|
else
|
||||||
|
writer.write(" {}\n");
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
parserConfig.set(trailingKey, currentValue);
|
||||||
|
String yaml = parserConfig.saveToString();
|
||||||
|
yaml = yaml.substring(0, yaml.length() - 1).replace("\n", "\n" + indents);
|
||||||
|
String toWrite = indents + yaml + "\n";
|
||||||
|
parserConfig.set(trailingKey, null);
|
||||||
|
writer.write(toWrite);
|
||||||
|
}
|
||||||
|
|
||||||
|
String endComment = buildComments("", null);
|
||||||
|
if (endComment != null) writer.write(endComment);
|
||||||
|
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到一个键的缩进。
|
||||||
|
* 该方法的源代码来自 tchristofferson/ConfigUpdater 项目。
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
* @return 该键的缩进文本
|
||||||
|
*/
|
||||||
|
protected static String getIndents(String key) {
|
||||||
|
String[] splitKey = key.split("[" + YAMLConfigProvider.SEPARATOR + "]");
|
||||||
|
return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package cc.carm.lib.configuration.yaml;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
|
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
||||||
|
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> {
|
||||||
|
|
||||||
|
protected static final char SEPARATOR = '.';
|
||||||
|
|
||||||
|
protected final @NotNull YAMLComments comments = new YAMLComments();
|
||||||
|
protected YamlConfiguration configuration;
|
||||||
|
protected ConfigInitializer<YAMLConfigProvider> initializer;
|
||||||
|
|
||||||
|
public YAMLConfigProvider(@NotNull File file) {
|
||||||
|
super(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initializeConfig() {
|
||||||
|
this.configuration = YamlConfiguration.loadConfiguration(file);
|
||||||
|
this.initializer = new ConfigInitializer<>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull YAMLSectionWrapper getConfiguration() {
|
||||||
|
return YAMLSectionWrapper.of(configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reload() throws Exception {
|
||||||
|
configuration.load(getFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save() throws Exception {
|
||||||
|
configuration.save(getFile());
|
||||||
|
|
||||||
|
|
||||||
|
// tchristofferson/ConfigUpdater start
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
|
this.comments.writeComments(configuration, new BufferedWriter(writer));
|
||||||
|
String value = writer.toString(); // config contents
|
||||||
|
|
||||||
|
Path toUpdatePath = getFile().toPath();
|
||||||
|
if (!value.equals(new String(Files.readAllBytes(toUpdatePath), StandardCharsets.UTF_8))) {
|
||||||
|
// if updated contents are not the same as current file contents, update
|
||||||
|
Files.write(toUpdatePath, value.getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
// tchristofferson/ConfigUpdater end
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setComment(@Nullable String path, @Nullable ConfigCommentInfo comments) {
|
||||||
|
this.comments.set(path, comments);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable ConfigCommentInfo getComment(@Nullable String path) {
|
||||||
|
return this.comments.get(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ConfigInitializer<YAMLConfigProvider> getInitializer() {
|
||||||
|
return this.initializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+4
-4
@@ -12,17 +12,17 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class YamlSectionWrapper implements ConfigurationWrapper {
|
public class YAMLSectionWrapper implements ConfigurationWrapper {
|
||||||
|
|
||||||
private final ConfigurationSection section;
|
private final ConfigurationSection section;
|
||||||
|
|
||||||
private YamlSectionWrapper(ConfigurationSection section) {
|
private YAMLSectionWrapper(ConfigurationSection section) {
|
||||||
this.section = section;
|
this.section = section;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract("!null->!null")
|
@Contract("!null->!null")
|
||||||
public static @Nullable YamlSectionWrapper of(@Nullable ConfigurationSection section) {
|
public static @Nullable YAMLSectionWrapper of(@Nullable ConfigurationSection section) {
|
||||||
return section == null ? null : new YamlSectionWrapper(section);
|
return section == null ? null : new YAMLSectionWrapper(section);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -1,31 +1,31 @@
|
|||||||
package cc.carm.lib.configuration.yaml;
|
package cc.carm.lib.configuration.yaml;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||||
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
import cc.carm.lib.configuration.yaml.builder.YAMLConfigBuilder;
|
||||||
import cc.carm.lib.configuration.yaml.YamlSectionWrapper;
|
|
||||||
import cc.carm.lib.configuration.yaml.builder.YamlConfigBuilder;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public abstract class YAMLValue<T> extends CachedConfigValue<T> {
|
public abstract class YAMLValue<T> extends CachedConfigValue<T> {
|
||||||
|
|
||||||
public static @NotNull YamlConfigBuilder builder() {
|
public static @NotNull YAMLConfigBuilder builder() {
|
||||||
return new YamlConfigBuilder();
|
return new YAMLConfigBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
public YAMLValue(@Nullable YamlConfigProvider provider,
|
public YAMLValue(@Nullable YAMLConfigProvider provider,
|
||||||
@Nullable String configPath, @NotNull String[] comments, @Nullable T defaultValue) {
|
@Nullable String configPath, @Nullable ConfigCommentInfo comments,
|
||||||
|
@Nullable T defaultValue) {
|
||||||
super(provider, configPath, comments, defaultValue);
|
super(provider, configPath, comments, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public YamlConfigProvider getYAMLProvider() {
|
public YAMLConfigProvider getYAMLProvider() {
|
||||||
ConfigurationProvider<?> provider = getProvider();
|
ConfigurationProvider<?> provider = getProvider();
|
||||||
if (provider instanceof YamlConfigProvider) return (YamlConfigProvider) getProvider();
|
if (provider instanceof YAMLConfigProvider) return (YAMLConfigProvider) getProvider();
|
||||||
else throw new IllegalStateException("Provider is not a SpigotConfigProvider");
|
else throw new IllegalStateException("Provider is not a YamlConfigProvider");
|
||||||
}
|
}
|
||||||
|
|
||||||
public YamlSectionWrapper getYAMLConfig() {
|
public YAMLSectionWrapper getYAMLConfig() {
|
||||||
return getYAMLProvider().getConfiguration();
|
return getYAMLProvider().getConfiguration();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.yaml;
|
|
||||||
|
|
||||||
import org.bspfsystems.yamlconfiguration.commented.CommentsProvider;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class YamlComments implements CommentsProvider {
|
|
||||||
|
|
||||||
Map<String, String[]> comments = new HashMap<>();
|
|
||||||
|
|
||||||
protected Map<String, String[]> getComments() {
|
|
||||||
return comments;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(@NotNull String path, @NotNull String... comments) {
|
|
||||||
if (comments.length == 0) {
|
|
||||||
getComments().remove(path);
|
|
||||||
} else {
|
|
||||||
getComments().put(path, comments);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public @Nullable String[] get(@NotNull String path) {
|
|
||||||
return getComments().get(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String[] apply(String s) {
|
|
||||||
return get(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.yaml;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
|
||||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
|
||||||
import org.bspfsystems.yamlconfiguration.commented.CommentedYamlConfiguration;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
public class YamlConfigProvider extends FileConfigProvider<YamlSectionWrapper> {
|
|
||||||
|
|
||||||
protected final @NotNull YamlComments comments = new YamlComments();
|
|
||||||
protected CommentedYamlConfiguration configuration;
|
|
||||||
protected ConfigInitializer<YamlConfigProvider> initializer;
|
|
||||||
|
|
||||||
public YamlConfigProvider(@NotNull File file) {
|
|
||||||
super(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initializeConfig() {
|
|
||||||
this.configuration = CommentedYamlConfiguration.loadConfiguration(comments, file);
|
|
||||||
this.initializer = new ConfigInitializer<>(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull YamlSectionWrapper getConfiguration() {
|
|
||||||
return YamlSectionWrapper.of(configuration);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void reload() throws Exception {
|
|
||||||
configuration.load(getFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void save() throws Exception {
|
|
||||||
configuration.save(getFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setComments(@NotNull String path, @NotNull String... comments) {
|
|
||||||
this.comments.set(path, comments);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable String[] getComments(@NotNull String path) {
|
|
||||||
return this.comments.get(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull ConfigInitializer<YamlConfigProvider> getInitializer() {
|
|
||||||
return this.initializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+3
-3
@@ -1,13 +1,13 @@
|
|||||||
package cc.carm.lib.configuration.yaml.builder;
|
package cc.carm.lib.configuration.yaml.builder;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
||||||
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
import cc.carm.lib.configuration.yaml.YAMLConfigProvider;
|
||||||
|
|
||||||
public abstract class AbstractYAMLBuilder<T, B extends AbstractYAMLBuilder<T, B>>
|
public abstract class AbstractYAMLBuilder<T, B extends AbstractYAMLBuilder<T, B>>
|
||||||
extends AbstractConfigBuilder<T, B, YamlConfigProvider> {
|
extends AbstractConfigBuilder<T, B, YAMLConfigProvider> {
|
||||||
|
|
||||||
public AbstractYAMLBuilder() {
|
public AbstractYAMLBuilder() {
|
||||||
super(YamlConfigProvider.class);
|
super(YAMLConfigProvider.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@ import cc.carm.lib.configuration.yaml.builder.serializable.SerializableBuilder;
|
|||||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class YamlConfigBuilder extends ConfigBuilder {
|
public class YAMLConfigBuilder extends ConfigBuilder {
|
||||||
|
|
||||||
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
|
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
|
||||||
return new SerializableBuilder<>(valueClass);
|
return new SerializableBuilder<>(valueClass);
|
||||||
+1
-1
@@ -21,7 +21,7 @@ public class SerializableBuilder<T extends ConfigurationSerializable>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull ConfiguredSerializable<T> build() {
|
public @NotNull ConfiguredSerializable<T> build() {
|
||||||
return new ConfiguredSerializable<>(this.provider, this.path, this.comments, this.valueClass, this.defaultValue);
|
return new ConfiguredSerializable<>(this.provider, this.path, buildComments(), this.valueClass, this.defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+4
-3
@@ -1,7 +1,8 @@
|
|||||||
package cc.carm.lib.configuration.yaml.value;
|
package cc.carm.lib.configuration.yaml.value;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||||
|
import cc.carm.lib.configuration.yaml.YAMLConfigProvider;
|
||||||
import cc.carm.lib.configuration.yaml.YAMLValue;
|
import cc.carm.lib.configuration.yaml.YAMLValue;
|
||||||
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
|
||||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@@ -21,8 +22,8 @@ public class ConfiguredSerializable<T extends ConfigurationSerializable> extends
|
|||||||
|
|
||||||
protected final @NotNull Class<T> valueClass;
|
protected final @NotNull Class<T> valueClass;
|
||||||
|
|
||||||
public ConfiguredSerializable(@Nullable YamlConfigProvider provider,
|
public ConfiguredSerializable(@Nullable YAMLConfigProvider provider,
|
||||||
@Nullable String configPath, @NotNull String[] comments,
|
@Nullable String configPath, @Nullable ConfigCommentInfo comments,
|
||||||
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
|
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
|
||||||
super(provider, configPath, comments, defaultValue);
|
super(provider, configPath, comments, defaultValue);
|
||||||
this.valueClass = valueClass;
|
this.valueClass = valueClass;
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package config;
|
package config;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.EasyConfiguration;
|
import cc.carm.lib.configuration.EasyConfiguration;
|
||||||
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
import cc.carm.lib.configuration.yaml.YAMLConfigProvider;
|
||||||
|
import config.model.AbstractModel;
|
||||||
|
import config.model.SomeModel;
|
||||||
import config.model.TestModel;
|
import config.model.TestModel;
|
||||||
import config.source.ComplexConfiguration;
|
|
||||||
import config.source.DemoConfiguration;
|
import config.source.DemoConfiguration;
|
||||||
import config.source.ImplConfiguration;
|
import config.source.ImplConfiguration;
|
||||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerialization;
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerialization;
|
||||||
@@ -17,16 +18,19 @@ import java.util.stream.IntStream;
|
|||||||
|
|
||||||
public class ConfigTester {
|
public class ConfigTester {
|
||||||
|
|
||||||
|
static {
|
||||||
|
ConfigurationSerialization.registerClass(TestModel.class);
|
||||||
|
ConfigurationSerialization.registerClass(SomeModel.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final YAMLConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onTest() {
|
public void onTest() {
|
||||||
ConfigurationSerialization.registerClass(TestModel.class);
|
provider.initialize(DemoConfiguration.class);
|
||||||
|
|
||||||
YamlConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
|
testDemo();
|
||||||
|
testSerialization();
|
||||||
testDemo(provider);
|
|
||||||
testComplex(provider);
|
|
||||||
testSerialization(provider);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
provider.save();
|
provider.save();
|
||||||
@@ -36,43 +40,40 @@ public class ConfigTester {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void testSerialization(YamlConfigProvider provider) {
|
|
||||||
System.out.println("----------------------------------------------------");
|
|
||||||
provider.initialize(ImplConfiguration.class);
|
|
||||||
System.out.println(ImplConfiguration.TEST.get());
|
|
||||||
ImplConfiguration.TEST.set(TestModel.random());
|
|
||||||
System.out.println("----------------------------------------------------");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void testDemo(YamlConfigProvider provider) {
|
public static void testDemo() {
|
||||||
provider.initialize(DemoConfiguration.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void testComplex(YamlConfigProvider provider) {
|
|
||||||
System.out.println("----------------------------------------------------");
|
System.out.println("----------------------------------------------------");
|
||||||
provider.initialize(ComplexConfiguration.class);
|
|
||||||
|
|
||||||
System.out.println("> Test Value:");
|
System.out.println("> Test Value:");
|
||||||
System.out.println("before: " + ComplexConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
System.out.println("before: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||||
ComplexConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
DemoConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
||||||
System.out.println("after: " + ComplexConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
System.out.println("after: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||||
|
|
||||||
System.out.println("> Test List:");
|
System.out.println("> Test List:");
|
||||||
ComplexConfiguration.Sub.That.Operators.getNotNull().forEach(System.out::println);
|
DemoConfiguration.Sub.That.OPERATORS.getNotNull().forEach(System.out::println);
|
||||||
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
||||||
ComplexConfiguration.Sub.That.Operators.set(operators);
|
DemoConfiguration.Sub.That.OPERATORS.set(operators);
|
||||||
|
|
||||||
System.out.println("> Test Section:");
|
System.out.println("> Test Section:");
|
||||||
System.out.println(ComplexConfiguration.USER.get());
|
System.out.println(DemoConfiguration.MODEL_TEST.get());
|
||||||
ComplexConfiguration.USER.set(TestModel.random());
|
DemoConfiguration.MODEL_TEST.set(TestModel.random());
|
||||||
|
|
||||||
System.out.println("> Test Maps:");
|
System.out.println("> Test Maps:");
|
||||||
ComplexConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
|
DemoConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
|
||||||
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
|
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 1; i <= 5; i++) {
|
||||||
data.put((int) (1000 * Math.random()), UUID.randomUUID());
|
data.put(i, UUID.randomUUID());
|
||||||
|
}
|
||||||
|
DemoConfiguration.USERS.set(data);
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void testSerialization() {
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
AbstractModel model = ImplConfiguration.TEST.get();
|
||||||
|
if (model != null) {
|
||||||
|
System.out.println(model.getName());
|
||||||
}
|
}
|
||||||
ComplexConfiguration.USERS.set(data);
|
|
||||||
System.out.println("----------------------------------------------------");
|
System.out.println("----------------------------------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package config;
|
||||||
|
|
||||||
|
import config.offset.FieldOffset;
|
||||||
|
import config.offset.OffsetUtil;
|
||||||
|
import config.source.DemoConfiguration;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class OffsetTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
//
|
||||||
|
// output(OffsetUtil.getClassMemberOffset(DemoConfiguration.class));
|
||||||
|
// output(OffsetUtil.getClassMemberOffset(DemoConfiguration.Sub.class));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void output(List<FieldOffset> fieldOffsets) {
|
||||||
|
for (FieldOffset fieldOffset : fieldOffsets) {
|
||||||
|
System.out.println(fieldOffset.getOffsetValue() + " -> " + fieldOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package config.model;
|
||||||
|
|
||||||
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public abstract class AbstractModel implements ConfigurationSerializable {
|
||||||
|
|
||||||
|
protected final @NotNull String name;
|
||||||
|
|
||||||
|
public AbstractModel(@NotNull String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package config.model;
|
||||||
|
|
||||||
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||||
|
import org.bspfsystems.yamlconfiguration.serialization.SerializableAs;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.TestOnly;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@SerializableAs("SomeModel")
|
||||||
|
public class SomeModel extends AbstractModel implements ConfigurationSerializable {
|
||||||
|
|
||||||
|
int num;
|
||||||
|
|
||||||
|
public SomeModel(@NotNull String name, int num) {
|
||||||
|
super(name);
|
||||||
|
this.num = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "SomeModel{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", num=" + num +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Map<String, Object> serialize() {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("name", name);
|
||||||
|
map.put("num", num);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestOnly
|
||||||
|
public static SomeModel deserialize(Map<String, ?> args) {
|
||||||
|
return new SomeModel((String) args.get("name"), (Integer) args.get("num"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -11,28 +11,19 @@ import java.util.Map;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@SerializableAs("TestModel")
|
@SerializableAs("TestModel")
|
||||||
public class TestModel implements ConfigurationSerializable {
|
public class TestModel extends AbstractModel implements ConfigurationSerializable {
|
||||||
|
|
||||||
public String name;
|
|
||||||
public UUID uuid;
|
public UUID uuid;
|
||||||
|
|
||||||
public TestModel(String name, UUID uuid) {
|
public TestModel(String name, UUID uuid) {
|
||||||
this.name = name;
|
super(name);
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUuid(UUID uuid) {
|
public void setUuid(UUID uuid) {
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UUID getUuid() {
|
public UUID getUuid() {
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package config.offset;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Chris2018998
|
||||||
|
*/
|
||||||
|
public class FieldOffset implements Comparable<FieldOffset> {
|
||||||
|
private final Field field;
|
||||||
|
private Long offsetValue;
|
||||||
|
private List<FieldOffset> subFieldOffsetList;
|
||||||
|
|
||||||
|
public FieldOffset(Field field) {
|
||||||
|
this.field = field;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getOffsetValue() {
|
||||||
|
return offsetValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOffsetValue(Long offsetValue) {
|
||||||
|
this.offsetValue = offsetValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
if (subFieldOffsetList == null)
|
||||||
|
return field.getName();
|
||||||
|
else {
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append("[");
|
||||||
|
|
||||||
|
for (int i = 0; i < subFieldOffsetList.size(); i++) {
|
||||||
|
FieldOffset offset = subFieldOffsetList.get(i);
|
||||||
|
if (i > 0) builder.append(",");
|
||||||
|
builder.append(field.getName()).append(".").append(offset.toString());
|
||||||
|
}
|
||||||
|
builder.append("]");
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<FieldOffset> getSubFieldOffsetList() {
|
||||||
|
return subFieldOffsetList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubFieldOffsetList(List<FieldOffset> subFieldOffsetList) {
|
||||||
|
this.subFieldOffsetList = subFieldOffsetList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Field getField() {
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(@NotNull FieldOffset that) {
|
||||||
|
return this.offsetValue.compareTo(that.offsetValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package config.offset;
|
||||||
|
|
||||||
|
|
||||||
|
import sun.misc.Unsafe;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Chris2018998
|
||||||
|
*/
|
||||||
|
public class OffsetUtil {
|
||||||
|
private static Unsafe unsafe;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
// 获取 Unsafe 内部的私有的实例化单例对象
|
||||||
|
Field field = Unsafe.class.getDeclaredField("theUnsafe");
|
||||||
|
// 无视权限
|
||||||
|
field.setAccessible(true);
|
||||||
|
unsafe = (Unsafe) field.get(null);
|
||||||
|
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
// try {
|
||||||
|
// unsafe = AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
|
||||||
|
// Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
|
||||||
|
// theUnsafe.setAccessible(true);
|
||||||
|
// return (Unsafe) theUnsafe.get(null);
|
||||||
|
// });
|
||||||
|
// } catch (Throwable e) {
|
||||||
|
// System.err.println("Unable to load unsafe");
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<FieldOffset> getClassMemberOffset(Class<?> beanClass) {
|
||||||
|
List<FieldOffset> offsetsList = new LinkedList<>();
|
||||||
|
for (Field field : beanClass.getDeclaredFields()) {
|
||||||
|
FieldOffset fieldOffset = new FieldOffset(field);
|
||||||
|
offsetsList.add(fieldOffset);
|
||||||
|
if (Modifier.isStatic(field.getModifiers()))
|
||||||
|
fieldOffset.setOffsetValue(unsafe.staticFieldOffset(field));
|
||||||
|
else
|
||||||
|
fieldOffset.setOffsetValue(unsafe.objectFieldOffset(field));
|
||||||
|
Class<?> fieldType = field.getType();
|
||||||
|
if (!fieldType.getName().startsWith("java")) {
|
||||||
|
Field[] subfields = fieldType.getDeclaredFields();
|
||||||
|
if (subfields.length > 0) {
|
||||||
|
fieldOffset.setSubFieldOffsetList(getClassMemberOffset(fieldType));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Collections.sort(offsetsList);
|
||||||
|
return offsetsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package config.source;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||||
|
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
||||||
|
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||||
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
|
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||||
|
|
||||||
|
@ConfigComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"})
|
||||||
|
public class DatabaseConfiguration extends ConfigurationRoot {
|
||||||
|
|
||||||
|
@ConfigPath("driver")
|
||||||
|
@ConfigComment(value = {
|
||||||
|
"数据库驱动配置,请根据数据库类型设置。",
|
||||||
|
"- MySQL: com.mysql.cj.jdbc.Driver",
|
||||||
|
"- MariaDB(推荐): org.mariadb.jdbc.Driver",
|
||||||
|
}, startWrap = false)
|
||||||
|
protected static final ConfigValue<String> DRIVER_NAME = ConfiguredValue.of(
|
||||||
|
String.class, "com.mysql.cj.jdbc.Driver"
|
||||||
|
);
|
||||||
|
|
||||||
|
@ConfigComment(startWrap = false)
|
||||||
|
protected static final ConfigValue<String> HOST = ConfiguredValue.of(String.class, "127.0.0.1");
|
||||||
|
@ConfigComment(startWrap = false)
|
||||||
|
protected static final ConfigValue<Integer> PORT = ConfiguredValue.of(Integer.class, 3306);
|
||||||
|
@ConfigComment(startWrap = false)
|
||||||
|
protected static final ConfigValue<String> DATABASE = ConfiguredValue.of(String.class, "minecraft");
|
||||||
|
@ConfigComment(startWrap = false)
|
||||||
|
protected static final ConfigValue<String> USERNAME = ConfiguredValue.of(String.class, "root");
|
||||||
|
@ConfigComment(startWrap = false)
|
||||||
|
protected static final ConfigValue<String> PASSWORD = ConfiguredValue.of(String.class, "password");
|
||||||
|
@ConfigComment(startWrap = false)
|
||||||
|
protected static final ConfigValue<String> EXTRA = ConfiguredValue.of(String.class, "?useSSL=false");
|
||||||
|
|
||||||
|
protected static String buildJDBC() {
|
||||||
|
return String.format("jdbc:mysql://%s:%s/%s%s", HOST.get(), PORT.get(), DATABASE.get(), EXTRA.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,47 +1,81 @@
|
|||||||
package config.source;
|
package config.source;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||||
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
||||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
|
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||||
|
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
|
||||||
|
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||||
|
import config.model.TestModel;
|
||||||
|
|
||||||
@ConfigPath("database")
|
import java.util.List;
|
||||||
@ConfigComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"})
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
|
@ConfigComment({"给根类添加的注释将显示在文件的末尾。"})
|
||||||
public class DemoConfiguration extends ConfigurationRoot {
|
public class DemoConfiguration extends ConfigurationRoot {
|
||||||
|
|
||||||
@ConfigPath(root = true)
|
@ConfigPath(root = true)
|
||||||
@ConfigComment({
|
@ConfigComment(value = {
|
||||||
"有时候,需要在配置文件最上面显示点东西,",
|
"有时候,需要在配置文件最上面显示点东西,",
|
||||||
"此时就推荐添加一个可以用到但并不重要的参数到最上面",
|
"此时就推荐添加一个可以用到但并不重要的参数到最上面",
|
||||||
"并给他添加对应的注释。"
|
"并给他添加对应的注释。"
|
||||||
})
|
}, startWrap = false, endWrap = true)
|
||||||
protected static final ConfigValue<Double> VERSION = ConfiguredValue.of(Double.class, 1.0D);
|
protected static final ConfigValue<Double> VERSION = ConfiguredValue.of(Double.class, 1.0D);
|
||||||
|
|
||||||
|
|
||||||
@ConfigPath("driver")
|
// 支持通过 Class<?> 变量标注子配置,一并注册。
|
||||||
@ConfigComment({
|
// 注意: 若对应类也有注解,则优先使用类的注解。
|
||||||
"数据库驱动配置,请根据数据库类型设置。",
|
@ConfigPath("impl-test") //支持通过注解修改子配置的主路径,若不修改则以变量名自动生成。
|
||||||
"- MySQL: com.mysql.cj.jdbc.Driver",
|
@ConfigComment("Something...") // 支持给子路径直接打注释
|
||||||
"- MariaDB(推荐): org.mariadb.jdbc.Driver",
|
public static final Class<?> IMPL = ImplConfiguration.class;
|
||||||
})
|
|
||||||
protected static final ConfigValue<String> DRIVER_NAME = ConfiguredValue.of(
|
|
||||||
String.class, "com.mysql.cj.jdbc.Driver"
|
|
||||||
);
|
|
||||||
|
|
||||||
protected static final ConfigValue<String> HOST = ConfiguredValue.of(String.class, "127.0.0.1");
|
// 子配置文件
|
||||||
protected static final ConfigValue<Integer> PORT = ConfiguredValue.of(Integer.class, 3306);
|
@ConfigPath("database")
|
||||||
|
public static final Class<?> DB_CONFIG = DatabaseConfiguration.class;
|
||||||
|
|
||||||
protected static final ConfigValue<String> DATABASE = ConfiguredValue.of(String.class, "minecraft");
|
@ConfigPath("user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||||
protected static final ConfigValue<String> USERNAME = ConfiguredValue.of(String.class, "root");
|
@ConfigComment({"Section类型数据测试"}) // 通过注解给配置添加注释。
|
||||||
protected static final ConfigValue<String> PASSWORD = ConfiguredValue.of(String.class, "password");
|
public static final ConfigValue<TestModel> MODEL_TEST = ConfiguredSection
|
||||||
|
.builder(TestModel.class)
|
||||||
|
.defaults(new TestModel("Carm", UUID.randomUUID()))
|
||||||
|
.parseValue((section, defaultValue) -> TestModel.deserialize(section))
|
||||||
|
.serializeValue(TestModel::serialize).build();
|
||||||
|
|
||||||
protected static final ConfigValue<String> EXTRA = ConfiguredValue.of(String.class, "?useSSL=false");
|
@ConfigComment({"[ID-UUID] 对照表", "", "用于测试Map类型的解析与序列化保存"})
|
||||||
|
public static final ConfigValue<Map<Integer, UUID>> USERS = ConfiguredMap
|
||||||
|
.builder(Integer.class, UUID.class).fromString()
|
||||||
|
.parseKey(Integer::parseInt)
|
||||||
|
.parseValue(v -> Objects.requireNonNull(UUID.fromString(v)))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支持内部类的直接注册。
|
||||||
|
* 注意,需要使用 {@link ConfigInitializer#initialize(Class, boolean, boolean)} 方法,并设定第三个参数为 true。
|
||||||
|
*/
|
||||||
|
public static class Sub extends ConfigurationRoot {
|
||||||
|
|
||||||
|
@ConfigPath(value = "uuid-value", root = true)
|
||||||
|
public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfiguredValue
|
||||||
|
.builder(UUID.class).fromString()
|
||||||
|
.parseValue((data, defaultValue) -> UUID.fromString(data))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
public static class That extends ConfigurationRoot {
|
||||||
|
|
||||||
|
public static final ConfigValue<List<UUID>> OPERATORS = ConfiguredList
|
||||||
|
.builder(UUID.class).fromString()
|
||||||
|
.parseValue(s -> Objects.requireNonNull(UUID.fromString(s)))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
protected static String buildJDBC() {
|
|
||||||
return String.format("jdbc:mysql://%s:%s/%s%s",
|
|
||||||
HOST.get(), PORT.get(), DATABASE.get(), EXTRA.get()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,13 @@ import cc.carm.lib.configuration.core.ConfigurationRoot;
|
|||||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
|
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
|
||||||
|
import config.model.AbstractModel;
|
||||||
import config.model.TestModel;
|
import config.model.TestModel;
|
||||||
|
|
||||||
@ConfigPath("ImplConfiguration")
|
@ConfigPath(root = true)
|
||||||
public class ImplConfiguration extends ConfigurationRoot {
|
public class ImplConfiguration extends ConfigurationRoot {
|
||||||
|
|
||||||
public static final ConfigValue<TestModel> TEST = ConfiguredSerializable.of(TestModel.class);
|
public static final ConfigValue<? extends AbstractModel> TEST = ConfiguredSerializable.of(TestModel.class, TestModel.random());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,68 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<parent>
|
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
|
||||||
<version>1.1.0</version>
|
|
||||||
<relativePath>../../pom.xml</relativePath>
|
|
||||||
</parent>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
|
||||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<artifactId>easyconfiguration-bungee</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
|
||||||
<artifactId>easyconfiguration-core</artifactId>
|
|
||||||
<version>${project.parent.version}</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>net.md-5</groupId>
|
|
||||||
<artifactId>bungeecord-api</artifactId>
|
|
||||||
<version>1.18-R0.1-SNAPSHOT</version>
|
|
||||||
<type>jar</type>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>net.md-5</groupId>
|
|
||||||
<artifactId>bungeecord-api</artifactId>
|
|
||||||
<version>1.18-R0.1-SNAPSHOT</version>
|
|
||||||
<type>javadoc</type>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-source-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
</project>
|
|
||||||
-61
@@ -1,61 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.bungee;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
|
||||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
|
||||||
import net.md_5.bungee.config.Configuration;
|
|
||||||
import net.md_5.bungee.config.ConfigurationProvider;
|
|
||||||
import net.md_5.bungee.config.YamlConfiguration;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> {
|
|
||||||
|
|
||||||
protected Configuration configuration;
|
|
||||||
protected ConfigInitializer<BungeeConfigProvider> initializer;
|
|
||||||
|
|
||||||
public BungeeConfigProvider(@NotNull File file) {
|
|
||||||
super(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initializeConfig() throws IOException {
|
|
||||||
this.configuration = getLoader().load(file);
|
|
||||||
this.initializer = new ConfigInitializer<>(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull BungeeSectionWrapper getConfiguration() {
|
|
||||||
return BungeeSectionWrapper.of(configuration);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void reload() throws Exception {
|
|
||||||
this.configuration = getLoader().load(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void save() throws Exception {
|
|
||||||
getLoader().save(configuration, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setComments(@NotNull String path, @NotNull String... comments) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable String[] getComments(@NotNull String path) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull ConfigInitializer<BungeeConfigProvider> getInitializer() {
|
|
||||||
return this.initializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ConfigurationProvider getLoader() {
|
|
||||||
return ConfigurationProvider.getProvider(YamlConfiguration.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-70
@@ -1,70 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.bungee;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
|
||||||
import net.md_5.bungee.config.Configuration;
|
|
||||||
import org.jetbrains.annotations.Contract;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class BungeeSectionWrapper implements ConfigurationWrapper {
|
|
||||||
|
|
||||||
private final Configuration section;
|
|
||||||
|
|
||||||
private BungeeSectionWrapper(Configuration section) {
|
|
||||||
this.section = section;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract("!null->!null")
|
|
||||||
public static @Nullable BungeeSectionWrapper of(@Nullable Configuration section) {
|
|
||||||
return section == null ? null : new BungeeSectionWrapper(section);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Set<String> getKeys(boolean deep) {
|
|
||||||
return new LinkedHashSet<>(section.getKeys());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Map<String, Object> getValues(boolean deep) {
|
|
||||||
return section.getKeys().stream()
|
|
||||||
.collect(Collectors.toMap(key -> key, section::get, (a, b) -> b, LinkedHashMap::new));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void set(@NotNull String path, @Nullable Object value) {
|
|
||||||
this.section.set(path, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean contains(@NotNull String path) {
|
|
||||||
return this.section.contains(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable Object get(@NotNull String path) {
|
|
||||||
return this.section.get(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isList(@NotNull String path) {
|
|
||||||
return get(path) instanceof List<?>;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable List<?> getList(@NotNull String path) {
|
|
||||||
return this.section.getList(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isConfigurationSection(@NotNull String path) {
|
|
||||||
return true; // No provided functions :( SRY
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
|
||||||
return of(this.section.getSection(path));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package cc.carm.lib.configuration;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.spigot.SpigotConfigProvider;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class EasyConfiguration {
|
|
||||||
|
|
||||||
|
|
||||||
public static SpigotConfigProvider from(File file, String source) {
|
|
||||||
SpigotConfigProvider provider = new SpigotConfigProvider(file);
|
|
||||||
try {
|
|
||||||
provider.initializeFile(source);
|
|
||||||
provider.initializeConfig();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return provider;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SpigotConfigProvider from(File file) {
|
|
||||||
return from(file, file.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SpigotConfigProvider from(String fileName) {
|
|
||||||
return from(fileName, fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SpigotConfigProvider from(String fileName, String source) {
|
|
||||||
return from(new File(fileName), source);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-1513
File diff suppressed because it is too large
Load Diff
@@ -1,73 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.commented;
|
|
||||||
|
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
|
||||||
import org.yaml.snakeyaml.Yaml;
|
|
||||||
import org.yaml.snakeyaml.constructor.BaseConstructor;
|
|
||||||
import org.yaml.snakeyaml.error.YAMLException;
|
|
||||||
import org.yaml.snakeyaml.nodes.Node;
|
|
||||||
import org.yaml.snakeyaml.nodes.Tag;
|
|
||||||
import org.yaml.snakeyaml.representer.Representer;
|
|
||||||
import org.yaml.snakeyaml.serializer.Serializer;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.io.Writer;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A hacky extension of {@link Yaml} that allows to write comments when dumping.
|
|
||||||
*/
|
|
||||||
public class CommentedYaml extends Yaml {
|
|
||||||
|
|
||||||
private final CommentsProvider commentsProvider;
|
|
||||||
|
|
||||||
public CommentedYaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions, CommentsProvider commentsProvider) {
|
|
||||||
super(constructor, representer, dumperOptions);
|
|
||||||
this.commentsProvider = commentsProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String dump(Object data) {
|
|
||||||
List<Object> list = new ArrayList<>(1);
|
|
||||||
list.add(data);
|
|
||||||
return this.dumpAll(list.iterator());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void dump(Object data, Writer output) {
|
|
||||||
List<Object> list = new ArrayList<>(1);
|
|
||||||
list.add(data);
|
|
||||||
this.dumpAll(list.iterator(), output, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String dumpAll(Iterator<?> data) {
|
|
||||||
StringWriter buffer = new StringWriter();
|
|
||||||
this.dumpAll(data, buffer, null);
|
|
||||||
return buffer.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void dumpAll(Iterator<?> data, Writer output) {
|
|
||||||
this.dumpAll(data, output, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void dumpAll(Iterator<?> data, Writer output, Tag rootTag) {
|
|
||||||
Serializer serializer = new Serializer(new CommentedEmitter(output, this.dumperOptions, this.commentsProvider), this.resolver, this.dumperOptions, rootTag);
|
|
||||||
|
|
||||||
try {
|
|
||||||
serializer.open();
|
|
||||||
|
|
||||||
while (data.hasNext()) {
|
|
||||||
Node node = this.representer.represent(data.next());
|
|
||||||
serializer.serialize(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
serializer.close();
|
|
||||||
} catch (IOException var6) {
|
|
||||||
throw new YAMLException(var6);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-69
@@ -1,69 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.commented;
|
|
||||||
|
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.configuration.file.YamlConstructor;
|
|
||||||
import org.bukkit.configuration.file.YamlRepresenter;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
|
||||||
import org.yaml.snakeyaml.representer.Representer;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.Reader;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A yaml file with comments on certain properties, as returned by the given {@link CommentsProvider}.
|
|
||||||
* Unlike {@link YamlConfiguration}, this class does not provide a header support.
|
|
||||||
*/
|
|
||||||
public class CommentedYamlConfiguration extends YamlConfiguration {
|
|
||||||
|
|
||||||
private final DumperOptions yamlOptions = new DumperOptions();
|
|
||||||
private final Representer yamlRepresenter = new YamlRepresenter();
|
|
||||||
private final CommentedYaml yaml;
|
|
||||||
|
|
||||||
public CommentedYamlConfiguration(CommentsProvider commentsProvider) {
|
|
||||||
this.yaml = new CommentedYaml(new YamlConstructor(), this.yamlRepresenter, this.yamlOptions, commentsProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CommentedYamlConfiguration loadConfiguration(CommentsProvider commentsProvider, File file) {
|
|
||||||
CommentedYamlConfiguration config = new CommentedYamlConfiguration(commentsProvider);
|
|
||||||
|
|
||||||
try {
|
|
||||||
config.load(file);
|
|
||||||
} catch (FileNotFoundException ignored) {
|
|
||||||
} catch (IOException | InvalidConfigurationException var4) {
|
|
||||||
var4.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CommentedYamlConfiguration loadConfiguration(CommentsProvider commentsProvider, Reader reader) {
|
|
||||||
CommentedYamlConfiguration config = new CommentedYamlConfiguration(commentsProvider);
|
|
||||||
|
|
||||||
try {
|
|
||||||
config.load(reader);
|
|
||||||
} catch (IOException | InvalidConfigurationException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull String saveToString() {
|
|
||||||
this.yamlOptions.setIndent(this.options().indent());
|
|
||||||
this.yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
||||||
this.yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
||||||
String dump = this.yaml.dump(this.getValues(false));
|
|
||||||
if (dump.equals("{}\n")) {
|
|
||||||
dump = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
// No header support.
|
|
||||||
|
|
||||||
return dump;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-7
@@ -1,7 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.commented;
|
|
||||||
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
|
|
||||||
public interface CommentsProvider extends Function<String, String[]> {
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.commented.CommentsProvider;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ConfigComments implements CommentsProvider {
|
|
||||||
|
|
||||||
Map<String, String[]> comments = new HashMap<>();
|
|
||||||
|
|
||||||
protected Map<String, String[]> getComments() {
|
|
||||||
return comments;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(@NotNull String path, @NotNull String... comments) {
|
|
||||||
if (comments.length == 0) {
|
|
||||||
getComments().remove(path);
|
|
||||||
} else {
|
|
||||||
getComments().put(path, comments);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public @Nullable String[] get(@NotNull String path) {
|
|
||||||
return getComments().get(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String[] apply(String s) {
|
|
||||||
return get(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-60
@@ -1,60 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
|
||||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
|
||||||
import cc.carm.lib.configuration.commented.CommentedYamlConfiguration;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
public class SpigotConfigProvider extends FileConfigProvider<SpigotSectionWrapper> {
|
|
||||||
|
|
||||||
protected final ConfigComments comments = new ConfigComments();
|
|
||||||
protected ConfigInitializer<SpigotConfigProvider> initializer;
|
|
||||||
protected CommentedYamlConfiguration configuration;
|
|
||||||
|
|
||||||
public SpigotConfigProvider(@NotNull File file) {
|
|
||||||
super(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initializeConfig() {
|
|
||||||
this.configuration = CommentedYamlConfiguration.loadConfiguration(comments, file);
|
|
||||||
this.initializer = new ConfigInitializer<>(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void say() {
|
|
||||||
System.out.println("Hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull SpigotSectionWrapper getConfiguration() {
|
|
||||||
return SpigotSectionWrapper.of(configuration);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void reload() throws Exception {
|
|
||||||
configuration.load(getFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void save() throws Exception {
|
|
||||||
configuration.save(getFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setComments(@NotNull String path, @NotNull String... comments) {
|
|
||||||
this.comments.set(path, comments);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable String[] getComments(@NotNull String path) {
|
|
||||||
return this.comments.get(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull ConfigInitializer<SpigotConfigProvider> getInitializer() {
|
|
||||||
return this.initializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-85
@@ -1,85 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|
||||||
import org.jetbrains.annotations.Contract;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class SpigotSectionWrapper implements ConfigurationWrapper {
|
|
||||||
|
|
||||||
private final ConfigurationSection section;
|
|
||||||
|
|
||||||
private SpigotSectionWrapper(ConfigurationSection section) {
|
|
||||||
this.section = section;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract("!null->!null")
|
|
||||||
public static @Nullable SpigotSectionWrapper of(@Nullable ConfigurationSection section) {
|
|
||||||
return section == null ? null : new SpigotSectionWrapper(section);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Set<String> getKeys(boolean deep) {
|
|
||||||
return new LinkedHashSet<>(section.getKeys(deep));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Map<String, Object> getValues(boolean deep) {
|
|
||||||
return section.getValues(deep);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void set(@NotNull String path, @Nullable Object value) {
|
|
||||||
this.section.set(path, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean contains(@NotNull String path) {
|
|
||||||
return this.section.contains(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable Object get(@NotNull String path) {
|
|
||||||
return this.section.get(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isList(@NotNull String path) {
|
|
||||||
return this.section.isList(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable List<?> getList(@NotNull String path) {
|
|
||||||
return this.section.getList(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isConfigurationSection(@NotNull String path) {
|
|
||||||
return this.section.isConfigurationSection(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
|
||||||
return of(this.section.getConfigurationSection(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz) {
|
|
||||||
return getSerializable(path, clazz, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Contract("_, _, !null -> !null")
|
|
||||||
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz, @Nullable T defaultValue) {
|
|
||||||
return this.section.getSerializable(path, clazz, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
|
||||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
|
||||||
import cc.carm.lib.configuration.spigot.builder.SpigotConfigBuilder;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
public abstract class SpigotValue<T> extends CachedConfigValue<T> {
|
|
||||||
|
|
||||||
public static @NotNull SpigotConfigBuilder builder() {
|
|
||||||
return new SpigotConfigBuilder();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public SpigotValue(@Nullable SpigotConfigProvider provider,
|
|
||||||
@Nullable String configPath, @NotNull String[] comments, @Nullable T defaultValue) {
|
|
||||||
super(provider, configPath, comments, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SpigotConfigProvider getSpigotProvider() {
|
|
||||||
ConfigurationProvider<?> provider = getProvider();
|
|
||||||
if (provider instanceof SpigotConfigProvider) return (SpigotConfigProvider) getProvider();
|
|
||||||
else throw new IllegalStateException("Provider is not a SpigotConfigProvider");
|
|
||||||
}
|
|
||||||
|
|
||||||
public SpigotSectionWrapper getSpigotConfig() {
|
|
||||||
return getSpigotProvider().getConfiguration();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.builder;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
|
||||||
import cc.carm.lib.configuration.spigot.SpigotConfigProvider;
|
|
||||||
|
|
||||||
public abstract class AbstractSpigotBuilder<T, B extends AbstractSpigotBuilder<T, B>>
|
|
||||||
extends AbstractConfigBuilder<T, B, SpigotConfigProvider> {
|
|
||||||
|
|
||||||
public AbstractSpigotBuilder() {
|
|
||||||
super(SpigotConfigProvider.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-14
@@ -1,14 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.builder;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
|
||||||
import cc.carm.lib.configuration.spigot.builder.serializable.SerializableBuilder;
|
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
public class SpigotConfigBuilder extends ConfigBuilder {
|
|
||||||
|
|
||||||
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
|
|
||||||
return new SerializableBuilder<>(valueClass);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-29
@@ -1,29 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.builder.serializable;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.spigot.builder.AbstractSpigotBuilder;
|
|
||||||
import cc.carm.lib.configuration.spigot.value.ConfiguredSerializable;
|
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
public class SerializableBuilder<T extends ConfigurationSerializable>
|
|
||||||
extends AbstractSpigotBuilder<T, SerializableBuilder<T>> {
|
|
||||||
|
|
||||||
protected final @NotNull Class<T> valueClass;
|
|
||||||
|
|
||||||
public SerializableBuilder(@NotNull Class<T> valueClass) {
|
|
||||||
this.valueClass = valueClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected @NotNull SerializableBuilder<T> getThis() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull ConfiguredSerializable<T> build() {
|
|
||||||
return new ConfiguredSerializable<>(this.provider, this.path, this.comments, this.valueClass, this.defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
-4
@@ -1,4 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.item;
|
|
||||||
|
|
||||||
public class ItemConfiguration {
|
|
||||||
}
|
|
||||||
-4
@@ -1,4 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.value;
|
|
||||||
|
|
||||||
public class ConfiguredItem {
|
|
||||||
}
|
|
||||||
-52
@@ -1,52 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.value;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.spigot.SpigotConfigProvider;
|
|
||||||
import cc.carm.lib.configuration.spigot.SpigotValue;
|
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends SpigotValue<T> {
|
|
||||||
|
|
||||||
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass) {
|
|
||||||
return of(valueClass, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass,
|
|
||||||
@Nullable V defaultValue) {
|
|
||||||
return builder().ofSerializable(valueClass).defaults(defaultValue).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final @NotNull Class<T> valueClass;
|
|
||||||
|
|
||||||
public ConfiguredSerializable(@Nullable SpigotConfigProvider provider,
|
|
||||||
@Nullable String configPath, @NotNull String[] comments,
|
|
||||||
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
|
|
||||||
super(provider, configPath, comments, defaultValue);
|
|
||||||
this.valueClass = valueClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable T get() {
|
|
||||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
|
||||||
try {
|
|
||||||
// 若未出现错误,则直接更新缓存并返回。
|
|
||||||
return updateCache(getSpigotConfig().getSerializable(getConfigPath(), valueClass, getDefaultValue()));
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 出现了解析错误,提示并返回默认值。
|
|
||||||
e.printStackTrace();
|
|
||||||
return useDefault();
|
|
||||||
}
|
|
||||||
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void set(@Nullable T value) {
|
|
||||||
updateCache(value);
|
|
||||||
setValue(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>8</java.version>
|
<java.version>1.8</java.version>
|
||||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
@@ -15,14 +15,11 @@
|
|||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>1.1.0</version>
|
<version>2.3.0</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>core</module>
|
<module>core</module>
|
||||||
|
|
||||||
<module>impl/yaml</module>
|
<module>impl/yaml</module>
|
||||||
|
<module>impl/json</module>
|
||||||
<module>platform/spigot</module>
|
|
||||||
<module>platform/bungee</module>
|
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<name>EasyConfiguration</name>
|
<name>EasyConfiguration</name>
|
||||||
@@ -48,8 +45,8 @@
|
|||||||
|
|
||||||
<licenses>
|
<licenses>
|
||||||
<license>
|
<license>
|
||||||
<name>The MIT License</name>
|
<name>GNU LESSER GENERAL PUBLIC LICENSE</name>
|
||||||
<url>https://opensource.org/licenses/MIT</url>
|
<url>https://www.gnu.org/licenses/lgpl-3.0.html</url>
|
||||||
</license>
|
</license>
|
||||||
</licenses>
|
</licenses>
|
||||||
|
|
||||||
@@ -93,7 +90,7 @@
|
|||||||
<downloadUrl>https://github.com/CarmJos/EasyConfiguration/releases</downloadUrl>
|
<downloadUrl>https://github.com/CarmJos/EasyConfiguration/releases</downloadUrl>
|
||||||
<site>
|
<site>
|
||||||
<id>javadoc</id>
|
<id>javadoc</id>
|
||||||
<name>EasySQL JavaDoc (on Github Pages)</name>
|
<name>EasyConfiguration JavaDoc (on Github Pages)</name>
|
||||||
<url>https://CarmJos.github.io/EasyConfiguration</url>
|
<url>https://CarmJos.github.io/EasyConfiguration</url>
|
||||||
</site>
|
</site>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
@@ -167,7 +164,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
<version>3.3.2</version>
|
<version>3.4.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<classifier>javadoc</classifier>
|
<classifier>javadoc</classifier>
|
||||||
<detectJavaApiLink>false</detectJavaApiLink>
|
<detectJavaApiLink>false</detectJavaApiLink>
|
||||||
|
|||||||
Reference in New Issue
Block a user