Skip to content

Commit 832870c

Browse files
Merge pull request #4 from Talexs/develop
@initial 7.0.0-preview.417.1 ConfigAdapters
2 parents 8b5c36f + 452456c commit 832870c

29 files changed

Lines changed: 3831 additions & 0 deletions

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@
230230
<artifactId>druid</artifactId>
231231
<version>1.2.8</version>
232232
</dependency>
233+
<dependency>
234+
<groupId>org.jetbrains</groupId>
235+
<artifactId>annotations</artifactId>
236+
<version>13.0</version>
237+
</dependency>
238+
<dependency>
239+
<groupId>org.apache.commons</groupId>
240+
<artifactId>commons-lang3</artifactId>
241+
<version>3.12.0</version>
242+
</dependency>
233243
</dependencies>
234244

235245
<dependencyManagement>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.talexframe.frame.core.modules.plugins.adapt.config.json;
2+
3+
import cn.hutool.json.JSONObject;
4+
import cn.hutool.json.JSONUtil;
5+
import com.talexframe.frame.core.talex.FrameConfig;
6+
import lombok.SneakyThrows;
7+
8+
/**
9+
* {@link com.talexframe.frame.core.modules.plugins.adapt.config Package }
10+
*
11+
* @author TalexDreamSoul 22/04/17 上午 08:59 Project: TalexFrame
12+
*/
13+
public class JSONConfig extends FrameConfig {
14+
15+
private JSONObject json = new JSONObject();
16+
17+
public JSONConfig(String provider, String path) {
18+
19+
super(provider, path);
20+
}
21+
22+
@Override
23+
public String serialize() {
24+
25+
return json.toString();
26+
}
27+
28+
@SneakyThrows
29+
@Override
30+
public FrameConfigWrapper<?> deserialize(String data) {
31+
32+
this.json = JSONUtil.parseObj(data);
33+
34+
return new FrameConfigWrapper<>(this);
35+
}
36+
37+
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package com.talexframe.frame.core.modules.plugins.adapt.config.json;
2+
3+
import cn.hutool.core.io.FileUtil;
4+
import cn.hutool.core.util.StrUtil;
5+
import cn.hutool.json.JSONObject;
6+
import cn.hutool.json.JSONUtil;
7+
import com.google.common.collect.HashMultimap;
8+
import com.talexframe.frame.core.modules.plugins.adapt.PluginCompAdapter;
9+
import com.talexframe.frame.core.modules.plugins.addon.PluginScanner;
10+
import com.talexframe.frame.core.modules.plugins.core.WebPlugin;
11+
import com.talexframe.frame.core.pojo.annotations.TVConfig;
12+
import com.talexframe.frame.core.talex.FrameCreator;
13+
import lombok.SneakyThrows;
14+
15+
import java.io.File;
16+
import java.lang.reflect.Field;
17+
import java.nio.charset.StandardCharsets;
18+
import java.util.Set;
19+
20+
/**
21+
* {@link com.talexframe.frame.core.modules.plugins.adapt.config Package }
22+
*
23+
* @author TalexDreamSoul 22/04/17 上午 08:59 Project: TalexFrame
24+
*/
25+
public class JSONConfigAdapter extends PluginCompAdapter<JSONConfig> {
26+
27+
private static final HashMultimap<Class<?>, JSONConfig> map = HashMultimap.create();
28+
29+
@SneakyThrows
30+
@Override
31+
protected boolean injectWithInstance(PluginScanner scanner, WebPlugin webPlugin, JSONConfig instance) {
32+
33+
map.get(instance.getClass()).forEach(jsonConfig -> {
34+
35+
if( jsonConfig.getPath().equalsIgnoreCase(instance.getPath()) ){
36+
37+
tframe.crash(new RuntimeException(jsonConfig.getPath() + " is already exist"));
38+
39+
}
40+
41+
});
42+
43+
File file = new File(webPlugin.getPluginDataFolder(), instance.getPath());
44+
45+
if( !file.exists() ) {
46+
47+
if( instance.isRequired() ) {
48+
49+
tframe.getFrameSender().warnConsoleMessage("[JSONConfigAdapter] " + instance.getPath() + " not found - The plugin will not work without it!");
50+
51+
tframe.crash(new RuntimeException(instance.getPath() + " not found - The plugin will not work without it!"));
52+
53+
} else {
54+
55+
tframe.getFrameSender().sendConsoleMessage("[JSONConfigAdapter] " + instance.getPath() + " not found!");
56+
57+
}
58+
59+
return false;
60+
61+
}
62+
63+
JSONObject jsonObject = JSONUtil.readJSONObject(file, StandardCharsets.UTF_8);
64+
65+
Class<?> clz = instance.getClass();
66+
67+
TVConfig classAnno = clz.getAnnotation(TVConfig.class);
68+
69+
if( classAnno != null ) {
70+
71+
for( Field field : clz.getDeclaredFields() ) {
72+
73+
String name = StrUtil.isBlankIfStr(classAnno.name()) ? field.getName() : classAnno.name();
74+
75+
field.setAccessible( true );
76+
77+
if( jsonObject.containsKey(name) ) {
78+
79+
field.set( instance, jsonObject.get( name, field.getType() ) );
80+
81+
} else if( classAnno.ignore() ) {
82+
83+
tframe.getFrameSender().warnConsoleMessage("[JSONConfigAdapter] " + instance.getPath() + " not found - The plugin allow ignore it.");
84+
85+
} else {
86+
87+
tframe.crash(new RuntimeException("[JSONConfigAdapter] " + instance.getPath() + "/@JSON " + name + " - The plugin will not work without it! For class: " + clz.getName()));
88+
89+
return false;
90+
91+
}
92+
93+
}
94+
95+
} else {
96+
97+
for( Field field : clz.getDeclaredFields() ) {
98+
99+
TVConfig fieldAnno = field.getAnnotation(TVConfig.class);
100+
String name = StrUtil.isBlankIfStr(fieldAnno.name()) ? field.getName() : fieldAnno.name();
101+
102+
field.setAccessible(true);
103+
104+
if( jsonObject.containsKey(name) ) {
105+
106+
field.set( instance, jsonObject.get( name, field.getType() ) );
107+
108+
} else if( fieldAnno.ignore() ) {
109+
110+
tframe.getFrameSender().warnConsoleMessage("[JSONConfigAdapter] " + instance.getPath() + "/@JSON " + name + " not found - The plugin allow ignore it.");
111+
112+
} else {
113+
114+
tframe.crash(new RuntimeException("[JSONConfigAdapter] " + instance.getPath() + "/@JSON " + name + " - The plugin will not work without it! For class: " + clz.getName()));
115+
116+
return false;
117+
118+
}
119+
120+
}
121+
122+
}
123+
124+
map.put( clz, instance );
125+
126+
return true;
127+
}
128+
129+
@SneakyThrows
130+
@Override
131+
public boolean logoutInstance(PluginScanner scanner, WebPlugin webPlugin, Class<? extends FrameCreator> clazz) {
132+
133+
Set<JSONConfig> configs = map.get(clazz);
134+
135+
for( JSONConfig config : configs ) {
136+
137+
JSONObject json = new JSONObject();
138+
TVConfig classAnno = clazz.getAnnotation(TVConfig.class);
139+
140+
if( classAnno != null ) {
141+
142+
for( Field field : clazz.getDeclaredFields() ) {
143+
144+
String name = StrUtil.isBlankIfStr(classAnno.name()) ? field.getName() : classAnno.name();
145+
146+
field.setAccessible( true );
147+
148+
Object value = field.get( config );
149+
150+
if( value != null ) {
151+
152+
json.set( name, value );
153+
154+
} else if( classAnno.ignore() ) {
155+
156+
tframe.getFrameSender().warnConsoleMessage("[JSONConfigAdapter] " + config.getPath() + "/@json " + name + " not found - The plugin allow ignore it.");
157+
158+
} else {
159+
160+
tframe.crash(new RuntimeException("[JSONConfigAdapter] " + config.getPath() + "/@json " + name + " - The plugin will not work without it! For class: " + clazz.getName()));
161+
162+
return false;
163+
164+
}
165+
166+
}
167+
168+
} else {
169+
170+
for( Field field : clazz.getDeclaredFields() ) {
171+
172+
TVConfig fieldAnno = field.getAnnotation(TVConfig.class);
173+
String name = StrUtil.isBlankIfStr(fieldAnno.name()) ? field.getName() : fieldAnno.name();
174+
175+
Object value = field.get( config );
176+
field.setAccessible( true );
177+
178+
if( value != null ) {
179+
180+
json.set( name, value );
181+
182+
} else if( fieldAnno.ignore() ) {
183+
184+
tframe.getFrameSender().warnConsoleMessage("[JSONConfigAdapter] " + config.getPath() + "/@json " + name + " not found - The plugin allow ignore it.");
185+
186+
} else {
187+
188+
tframe.crash(new RuntimeException("[JSONConfigAdapter] " + config.getPath() + "/@json " + name + " - The plugin will not work without it! For class: " + clazz.getName()));
189+
190+
return false;
191+
192+
}
193+
194+
}
195+
196+
}
197+
198+
File file = new File(webPlugin.getPluginDataFolder(), config.getPath());
199+
200+
FileUtil.writeString(json.toString(), file, StandardCharsets.UTF_8);
201+
202+
}
203+
204+
return false;
205+
206+
}
207+
208+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.talexframe.frame.core.modules.plugins.adapt.config.yaml;
2+
3+
import com.talexframe.frame.core.talex.FrameConfig;
4+
import lombok.SneakyThrows;
5+
import org.bukkit.configuration.file.YamlConfiguration;
6+
7+
/**
8+
* {@link com.talexframe.frame.core.modules.plugins.adapt.config Package }
9+
*
10+
* @author TalexDreamSoul 22/04/17 上午 08:31 Project: TalexFrame
11+
*/
12+
public class YamlConfig extends FrameConfig {
13+
14+
private final YamlConfiguration yaml = new YamlConfiguration();
15+
16+
public YamlConfig(String provider, String path) {
17+
18+
super(provider, path);
19+
}
20+
21+
@Override
22+
public String serialize() {
23+
24+
return yaml.saveToString();
25+
}
26+
27+
@SneakyThrows
28+
@Override
29+
public FrameConfigWrapper<?> deserialize(String data) {
30+
31+
yaml.loadFromString(data);
32+
33+
return new FrameConfigWrapper<>(this);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)