mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
docs: Add examples
This commit is contained in:
@@ -49,41 +49,58 @@ Check out all code demonstrations [HERE](demo/src/main/java/cc/carm/lib/configur
|
|||||||
For more examples, see the [Development Guide](.doc/README.md).
|
For more examples, see the [Development Guide](.doc/README.md).
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@HeaderComment("Configurations for sample")
|
@ConfigPath(root = true)
|
||||||
interface SampleConfig extends Configuration {
|
@HeaderComments("Configurations for sample")
|
||||||
|
public interface SampleConfig extends Configuration {
|
||||||
|
|
||||||
@InlineComment("Enabled?") // Inline comment
|
@InlineComment("Enabled?") // Inline comment
|
||||||
ConfiguredValue<Boolean> ENABLED = ConfiguredValue.of(true);
|
ConfiguredValue<Boolean> ENABLED = ConfiguredValue.of(true);
|
||||||
|
|
||||||
ConfiguredList<UUID> UUIDS = ConfiguredList.builderOf(UUID.class).fromString()
|
@HeaderComments("Server configurations") // Header comment
|
||||||
.parseValue(UUID::fromString).serializeValue(UUID::toString)
|
ConfiguredValue<Integer> PORT = ConfiguredValue.of(Integer.class);
|
||||||
.defaults(
|
|
||||||
UUID.fromString("00000000-0000-0000-0000-000000000000"),
|
|
||||||
UUID.fromString("00000000-0000-0000-0000-000000000001")
|
|
||||||
).build();
|
|
||||||
|
|
||||||
interface INFO extends Configuration {
|
@HeaderComments({"[ UUID >-----------------------------------", "A lot of UUIDs"})
|
||||||
|
@FooterComments("[ UUID >-----------------------------------")
|
||||||
@HeaderComment("Configure your name!") // Header comment
|
ConfiguredList<UUID> UUIDS = ConfiguredList.builderOf(UUID.class).fromString()
|
||||||
ConfiguredValue<String> NAME = ConfiguredValue.of("Joker");
|
.parse(UUID::fromString).serialize(UUID::toString)
|
||||||
|
.defaults(
|
||||||
|
UUID.fromString("00000000-0000-0000-0000-000000000000"),
|
||||||
|
UUID.fromString("00000000-0000-0000-0000-000000000001")
|
||||||
|
).build();
|
||||||
|
|
||||||
|
@ConfigPath("info") // Custom path
|
||||||
|
interface INFO extends Configuration {
|
||||||
|
|
||||||
|
@HeaderComments("Configure your name!") // Header comment
|
||||||
|
ConfiguredValue<String> NAME = ConfiguredValue.of("Joker");
|
||||||
|
|
||||||
|
@ConfigPath("how-old-are-you") // Custom path
|
||||||
|
ConfiguredValue<Integer> AGE = ConfiguredValue.of(24);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@ConfigPath("year") // Custom path
|
|
||||||
ConfiguredValue<Integer> AGE = ConfiguredValue.of(24);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class Sample {
|
public class Sample {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// 1. Make a configuration provider from a file.
|
// 1. Make a configuration provider from a file.
|
||||||
ConfigurationProvider<?> provider = EasyConfiguration.from("config.yml");
|
ConfigurationHolder<?> holder = YAMLConfigFactory.from("target/config.yml")
|
||||||
|
.resourcePath("configs/sample.yml")
|
||||||
|
.indent(4) // Optional: Set the indentation of the configuration file.
|
||||||
|
.build();
|
||||||
|
|
||||||
// 2. Initialize the configuration classes or instances.
|
// 2. Initialize the configuration classes or instances.
|
||||||
provider.initialize(SampleConfig.class);
|
holder.initialize(SampleConfig.class);
|
||||||
// 3. Enjoy using the configuration!
|
// 3. Enjoy using the configuration!
|
||||||
|
System.out.println("Enabled? -> " + SampleConfig.ENABLED.resolve());
|
||||||
SampleConfig.ENABLED.set(false);
|
SampleConfig.ENABLED.set(false);
|
||||||
System.out.println("Your name is " + SampleConfig.INFO.NAME.getNotNull() + " !");
|
System.out.println("And now? -> " + SampleConfig.ENABLED.resolve());
|
||||||
|
// p.s. Changes not save so enable value will still be true in the next run.
|
||||||
|
|
||||||
|
System.out.println("Your name is " + SampleConfig.INFO.NAME.resolve() + " (age=" + SampleConfig.INFO.AGE.resolve() + ")!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,16 +109,22 @@ public class Sample {
|
|||||||
```yaml
|
```yaml
|
||||||
# Configurations for sample
|
# Configurations for sample
|
||||||
|
|
||||||
enabled: true # Enabled?
|
enabled: true #Enabled?
|
||||||
|
|
||||||
|
# Server configurations
|
||||||
|
port:
|
||||||
|
|
||||||
|
# [ UUID >-----------------------------------
|
||||||
|
# A lot of UUIDs
|
||||||
uuids:
|
uuids:
|
||||||
- 00000000-0000-0000-0000-000000000000
|
- 00000000-0000-0000-0000-000000000000
|
||||||
- 00000000-0000-0000-0000-000000000001
|
- 00000000-0000-0000-0000-000000000001
|
||||||
|
# [ UUID >-----------------------------------
|
||||||
|
|
||||||
info:
|
info:
|
||||||
# Configure your name!
|
# Configure your name!
|
||||||
name: Joker
|
name: Joker
|
||||||
year: 24
|
how-old-are-you: 24
|
||||||
```
|
```
|
||||||
|
|
||||||
### Dependencies
|
### Dependencies
|
||||||
@@ -159,10 +182,10 @@ info:
|
|||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- JSON file-based implementation, compatible with all Java environments. Note: JSON does not support file comments. -->
|
<!-- JSON file-based implementation, compatible with all Java environments. -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyconfiguration-json</artifactId>
|
<artifactId>easyconfiguration-gson</artifactId>
|
||||||
<version>[LATEST RELEASE]</version>
|
<version>[LATEST RELEASE]</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
@@ -205,8 +228,8 @@ dependencies {
|
|||||||
// YAML file-based implementation, compatible with all Java environments.
|
// YAML file-based implementation, compatible with all Java environments.
|
||||||
api "cc.carm.lib:easyconfiguration-yaml:[LATEST RELEASE]"
|
api "cc.carm.lib:easyconfiguration-yaml:[LATEST RELEASE]"
|
||||||
|
|
||||||
// JSON file-based implementation, compatible with all Java environments. Note: JSON does not support file comments.
|
// JSON file-based implementation, compatible with all Java environments.
|
||||||
api "cc.carm.lib:easyconfiguration-json:[LATEST RELEASE]"
|
api "cc.carm.lib:easyconfiguration-gson:[LATEST RELEASE]"
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -220,7 +243,8 @@ dependencies {
|
|||||||
EasyConfiguration for MineCraft!
|
EasyConfiguration for MineCraft!
|
||||||
Easily manage configurations on MineCraft-related server platforms.
|
Easily manage configurations on MineCraft-related server platforms.
|
||||||
|
|
||||||
Currently supports BungeeCord, Bukkit (Spigot) servers, with more platforms to be supported soon.
|
Currently, it supports BungeeCord, Velocity, Bukkit (Spigot) servers,
|
||||||
|
with more platforms to be supported soon.
|
||||||
|
|
||||||
## Support and Donation
|
## Support and Donation
|
||||||
|
|
||||||
|
|||||||
+48
-27
@@ -43,42 +43,57 @@ README LANGUAGES [ [English](README.md) | [**中文**](README_CN.md) ]
|
|||||||
您可以 [点击这里](demo/src/main/java/cc/carm/lib/configuration/demo) 直接查看现有的代码演示,更多复杂情况演示详见 [开发介绍](.doc/README.md) 。
|
您可以 [点击这里](demo/src/main/java/cc/carm/lib/configuration/demo) 直接查看现有的代码演示,更多复杂情况演示详见 [开发介绍](.doc/README.md) 。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@HeaderComment("Configurations for sample")
|
@ConfigPath(root = true)
|
||||||
interface SampleConfig extends Configuration {
|
@HeaderComments("Configurations for sample")
|
||||||
|
public interface SampleConfig extends Configuration {
|
||||||
|
|
||||||
@InlineComment("Enabled?") // 行内注释
|
@InlineComment("Enabled?") // 行后注释
|
||||||
ConfiguredValue<Boolean> ENABLED = ConfiguredValue.of(true);
|
ConfiguredValue<Boolean> ENABLED = ConfiguredValue.of(true);
|
||||||
|
|
||||||
ConfiguredList<UUID> UUIDS = ConfiguredList.builderOf(UUID.class).fromString()
|
@HeaderComments("Server configurations") // 头部注释
|
||||||
.parseValue(UUID::fromString).serializeValue(UUID::toString)
|
ConfiguredValue<Integer> PORT = ConfiguredValue.of(Integer.class);
|
||||||
.defaults(
|
|
||||||
UUID.fromString("00000000-0000-0000-0000-000000000000"),
|
|
||||||
UUID.fromString("00000000-0000-0000-0000-000000000001")
|
|
||||||
).build();
|
|
||||||
|
|
||||||
interface INFO extends Configuration {
|
@HeaderComments({"[ UUID >-----------------------------------", "A lot of UUIDs"})
|
||||||
|
@FooterComments("[ UUID >-----------------------------------")
|
||||||
@HeaderComment("Configure your name!") // 头部注释
|
ConfiguredList<UUID> UUIDS = ConfiguredList.builderOf(UUID.class).fromString()
|
||||||
ConfiguredValue<String> NAME = ConfiguredValue.of("Joker");
|
.parse(UUID::fromString).serialize(UUID::toString)
|
||||||
|
.defaults(
|
||||||
|
UUID.fromString("00000000-0000-0000-0000-000000000000"),
|
||||||
|
UUID.fromString("00000000-0000-0000-0000-000000000001")
|
||||||
|
).build();
|
||||||
|
|
||||||
|
interface INFO extends Configuration {
|
||||||
|
|
||||||
|
@HeaderComments("Configure your name!") // Header comment
|
||||||
|
ConfiguredValue<String> NAME = ConfiguredValue.of("Joker");
|
||||||
|
|
||||||
|
@ConfigPath("how-old-are-you") // 自定义路径
|
||||||
|
ConfiguredValue<Integer> AGE = ConfiguredValue.of(24);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@ConfigPath("year") // 自定义配置路径,若不定义,则按照默认规则生成
|
|
||||||
ConfiguredValue<Integer> AGE = ConfiguredValue.of(24);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class Sample {
|
public class Sample {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// 1. 生成一个 “Provider” 用于给配置类提供源配置的文件。
|
// 1. 生成一个 “holder” 用于给配置类提供源配置的文件。
|
||||||
ConfigurationProvider<?> provider = EasyConfiguration.from("config.yml");
|
ConfigurationHolder<?> holder = YAMLConfigFactory.from("target/config.yml")
|
||||||
// 2. 通过 “Provider” 初始化配置类或配置实例。
|
.resourcePath("configs/sample.yml")
|
||||||
provider.initialize(SampleConfig.class);
|
.indent(4) // Optional: Set the indentation of the configuration file.
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 2. 通过 “holder” 初始化配置类或配置实例。
|
||||||
|
holder.initialize(SampleConfig.class);
|
||||||
// 3. 现在可以享受快捷方便的配置文件使用方式了~
|
// 3. 现在可以享受快捷方便的配置文件使用方式了~
|
||||||
|
System.out.println("Enabled? -> " + SampleConfig.ENABLED.resolve());
|
||||||
SampleConfig.ENABLED.set(false);
|
SampleConfig.ENABLED.set(false);
|
||||||
System.out.println("Your name is " + SampleConfig.INFO.NAME.getNotNull() + " !");
|
System.out.println("And now? -> " + SampleConfig.ENABLED.resolve());
|
||||||
|
// p.s. 在本示例里的更改未保存,因此启用值在下次运行中仍将为 true。
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -86,16 +101,22 @@ public class Sample {
|
|||||||
```yaml
|
```yaml
|
||||||
# Configurations for sample
|
# Configurations for sample
|
||||||
|
|
||||||
enabled: true # Enabled?
|
enabled: true #Enabled?
|
||||||
|
|
||||||
|
# Server configurations
|
||||||
|
port:
|
||||||
|
|
||||||
|
# [ UUID >-----------------------------------
|
||||||
|
# A lot of UUIDs
|
||||||
uuids:
|
uuids:
|
||||||
- 00000000-0000-0000-0000-000000000000
|
- 00000000-0000-0000-0000-000000000000
|
||||||
- 00000000-0000-0000-0000-000000000001
|
- 00000000-0000-0000-0000-000000000001
|
||||||
|
# [ UUID >-----------------------------------
|
||||||
|
|
||||||
info:
|
info:
|
||||||
# Configure your name!
|
# Configure your name!
|
||||||
name: Joker
|
name: Joker
|
||||||
year: 24
|
how-old-are-you: 24
|
||||||
```
|
```
|
||||||
|
|
||||||
### 依赖方式
|
### 依赖方式
|
||||||
@@ -164,7 +185,7 @@ info:
|
|||||||
<!--需要注意的是,JSON不支持文件注释。-->
|
<!--需要注意的是,JSON不支持文件注释。-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyconfiguration-json</artifactId>
|
<artifactId>easyconfiguration-gson</artifactId>
|
||||||
<version>[LATEST RELEASE]</version>
|
<version>[LATEST RELEASE]</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
@@ -225,7 +246,7 @@ dependencies {
|
|||||||
|
|
||||||
//基于JSON文件的实现版本,可用于全部Java环境。
|
//基于JSON文件的实现版本,可用于全部Java环境。
|
||||||
//需要注意的是,JSON不支持文件注释。
|
//需要注意的是,JSON不支持文件注释。
|
||||||
api "cc.carm.lib:easyconfiguration-json:[LATEST RELEASE]"
|
api "cc.carm.lib:easyconfiguration-gson:[LATEST RELEASE]"
|
||||||
|
|
||||||
api "cc.carm.lib:easyconfiguration-hocon:[LATEST RELEASE]"
|
api "cc.carm.lib:easyconfiguration-hocon:[LATEST RELEASE]"
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package sample;
|
|||||||
|
|
||||||
import cc.carm.lib.configuration.Configuration;
|
import cc.carm.lib.configuration.Configuration;
|
||||||
import cc.carm.lib.configuration.annotation.ConfigPath;
|
import cc.carm.lib.configuration.annotation.ConfigPath;
|
||||||
|
import cc.carm.lib.configuration.annotation.FooterComments;
|
||||||
import cc.carm.lib.configuration.annotation.HeaderComments;
|
import cc.carm.lib.configuration.annotation.HeaderComments;
|
||||||
import cc.carm.lib.configuration.annotation.InlineComment;
|
import cc.carm.lib.configuration.annotation.InlineComment;
|
||||||
import cc.carm.lib.configuration.value.standard.ConfiguredList;
|
import cc.carm.lib.configuration.value.standard.ConfiguredList;
|
||||||
@@ -19,6 +20,8 @@ public interface SampleConfig extends Configuration {
|
|||||||
@HeaderComments("Server configurations") // Header comment
|
@HeaderComments("Server configurations") // Header comment
|
||||||
ConfiguredValue<Integer> PORT = ConfiguredValue.of(Integer.class);
|
ConfiguredValue<Integer> PORT = ConfiguredValue.of(Integer.class);
|
||||||
|
|
||||||
|
@HeaderComments({"[ UUID >-----------------------------------", "A lot of UUIDs"})
|
||||||
|
@FooterComments("[ UUID >-----------------------------------")
|
||||||
ConfiguredList<UUID> UUIDS = ConfiguredList.builderOf(UUID.class).fromString()
|
ConfiguredList<UUID> UUIDS = ConfiguredList.builderOf(UUID.class).fromString()
|
||||||
.parse(UUID::fromString).serialize(UUID::toString)
|
.parse(UUID::fromString).serialize(UUID::toString)
|
||||||
.defaults(
|
.defaults(
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ public class SampleTest {
|
|||||||
// 1. Make a configuration provider from a file.
|
// 1. Make a configuration provider from a file.
|
||||||
ConfigurationHolder<?> holder = YAMLConfigFactory.from("target/config.yml")
|
ConfigurationHolder<?> holder = YAMLConfigFactory.from("target/config.yml")
|
||||||
.resourcePath("configs/sample.yml")
|
.resourcePath("configs/sample.yml")
|
||||||
.indent(4) // Optional: Set the indentation of the configuration file.
|
.indent(2) // Optional: Set the indentation of the configuration file.
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// 2. Initialize the configuration classes or instances.
|
// 2. Initialize the configuration classes or instances.
|
||||||
|
|||||||
Reference in New Issue
Block a user