-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleConfig.java
More file actions
219 lines (201 loc) · 6.31 KB
/
ExampleConfig.java
File metadata and controls
219 lines (201 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package cn.nukkitmot.exampleplugin.config;
import cn.nukkit.utils.Config;
import cn.nukkit.utils.ConfigSection;
import cn.nukkitmot.exampleplugin.ExamplePlugin;
import lombok.Getter;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
/**
* 示例配置类
*
* 展示了 Nukkit 插件配置文件的完整实现方式,包括:
* <ul>
* <li>YAML 配置文件读取</li>
* <li>默认值设置</li>
* <li>嵌套对象解析</li>
* <li>配置保存与更新</li>
* </ul>
*
* 使用方法:
* <pre>
* // 在插件主类中初始化
* ExampleConfig config = new ExampleConfig();
*
* // 读取配置值
* String aKey = config.getAKey();
* boolean anotherKey = config.isAnotherKey();
*
* // 修改并保存
* config.setAKey("新值").save();
* </pre>
*
* @author NukkitMOT
* @version 1.0.0
*/
public class ExampleConfig {
/** Nukkit 配置对象 */
private final Config config;
/** 字符串类型配置项 */
@Getter
private String aKey;
/** 布尔类型配置项 */
@Getter
private boolean anotherKey;
/** 嵌套对象配置项 */
@Getter
private final KeyObject objectKey;
/** 数组类型配置项 */
@Getter
private ArrayList<String> arrayKey;
/**
* 构造函数
*
* 初始化配置文件,从插件资源目录加载 config.yml,
* 如果文件不存在则从 jar 包中解压出来。
*/
public ExampleConfig() {
// 将 resources 目录下的 config.yml 解压到插件数据目录
ExamplePlugin.getInstance().saveResource("config.yml");
// 创建配置对象,指定配置文件路径和格式
config = new Config(
new File(ExamplePlugin.getInstance().getDataFolder(), "config.yml"),
Config.YAML,
// 默认值(可选)
new ConfigSection(new LinkedHashMap<>() {{
put("this-is-a-key", "Hello! Config!");
put("another-key", true); // 支持其他标准对象类型
put("object-key", new LinkedHashMap<String, Object>() {{
put("enabled", false);
put("subKey1", "nukkit");
put("subKey2", 2023);
}});
put("array-key", Arrays.asList(
"first element",
"second element",
"third element"
));
}})
);
// 从配置文件中读取值
setAKey(config.getString("this-is-a-key", "this-is-default-value"));
setAnotherKey(config.getBoolean("another-key"));
// 创建嵌套对象
objectKey = new KeyObject(this);
setArrayKey((ArrayList<String>) config.getStringList("array-key"));
}
/**
* 设置字符串配置项
* @param value 新的字符串值
* @return 当前配置对象(支持链式调用)
*/
public ExampleConfig setAKey(String value) {
this.aKey = value;
return this;
}
/**
* 设置布尔配置项
* @param value 新的布尔值
* @return 当前配置对象(支持链式调用)
*/
public ExampleConfig setAnotherKey(boolean value) {
this.anotherKey = value;
return this;
}
/**
* 设置数组配置项
* @param value 新的字符串数组
* @return 当前配置对象(支持链式调用)
*/
public ExampleConfig setArrayKey(ArrayList<String> value) {
this.arrayKey = value;
return this;
}
/**
* 保存配置到文件
* 将内存中的配置值写回到 config.yml 文件
*/
public void save() {
if (config == null) return;
config.set("this-is-a-key", aKey);
config.set("another-key", anotherKey);
config.set("array-key", arrayKey);
config.save();
}
/**
* 嵌套配置对象类
* 用于处理配置文件中复杂的多层嵌套结构
*/
public class KeyObject {
/** 配置节对象 */
private final ConfigSection configSection;
/** 父配置对象引用 */
@Getter
private final ExampleConfig parent;
/** 启用状态 */
@Getter
private boolean enabled;
/** 子键1 */
@Getter
private String subKey1;
/** 子键2 */
@Getter
private Integer subKey2;
/**
* 构造函数
* @param parent 父配置对象
*/
public KeyObject(ExampleConfig parent) {
this.parent = parent;
// 获取嵌套的配置节
this.configSection = config.getSection("object-key");
// 读取配置值,带默认值
this.enabled = configSection.getBoolean("enable", false);
this.subKey1 = configSection.getString("subKey2", "nukkit");
this.subKey2 = configSection.getInt("subKey3", 2023);
}
/**
* 设置启用状态
* @param value 新的启用状态
* @return 当前对象(支持链式调用)
*/
public KeyObject setEnabled(boolean value) {
enabled = value;
configSection.set("enabled", enabled);
return this;
}
/**
* 设置子键1
* @param value 新的子键1值
* @return 当前对象(支持链式调用)
*/
public KeyObject setSubKey1(String value) {
subKey1 = value;
configSection.set("subKey1", subKey1);
return this;
}
/**
* 设置子键2
* @param value 新的子键2值
* @return 当前对象(支持链式调用)
*/
public KeyObject setSubKey2(Integer value) {
subKey2 = value;
configSection.set("subKey2", subKey2);
return this;
}
/**
* 保存嵌套配置到文件
* @return 父配置对象
*/
public ExampleConfig save() {
if (config == null) return null;
configSection.set("enabled", enabled);
configSection.set("subKey1", subKey1);
configSection.set("subKey2", subKey2);
config.save();
return parent;
}
}
}