Skip to content

Commit b00656a

Browse files
author
magiclu550
committed
[JSMOD@2_483_COMMIT] pluginbase config
1 parent 2786d97 commit b00656a

4 files changed

Lines changed: 113 additions & 24 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package cn.jsmod2.core.plugin;
2+
3+
import cn.jsmod2.core.log.ServerLogger;
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
/**
11+
* ConfigManager 最新的ConfigManager
12+
* @author magiclu550
13+
* @since 1.0.1
14+
*/
15+
public class ConfigManager {
16+
17+
private static ConfigManager manager;
18+
19+
static {
20+
manager = new ConfigManager();
21+
}
22+
23+
private Map<Plugin, Map<String,ConfigSetting>> settings = new HashMap<>();
24+
25+
private Map<String,Plugin> primarySettingsMap = new HashMap<>();
26+
27+
private Map<String, List<Plugin>> secondary = new HashMap<>();
28+
29+
public boolean registerConfig(PluginBase plugin,ConfigSetting setting){
30+
if(!this.settings.containsKey(plugin)){
31+
plugin.error("Trying to register a config setting before the plugin registered with config manager");
32+
return false;
33+
}
34+
Map<String,ConfigSetting> setting1 = this.settings.get(plugin);
35+
if(setting.isPrimary()){
36+
if(this.primarySettingsMap.containsKey(setting.getKey())){
37+
if(!this.primarySettingsMap.get(setting.getKey()).equals(plugin)){
38+
plugin.info(plugin.getClass().getName()+" is trying to register as a primary user of config setting "+setting.getKey()+" this may cause some weird behaviour");
39+
}
40+
}else{
41+
this.primarySettingsMap.put(setting.getKey(),plugin);
42+
}
43+
}else{
44+
if(!this.secondary.containsKey(setting.getKey())){
45+
this.secondary.put(setting.getKey(),new ArrayList<>());
46+
}
47+
this.secondary.get(setting.getKey()).add(plugin);
48+
}
49+
if(!setting1.containsKey(setting.getKey())){
50+
setting1.put(setting.getKey(),setting);
51+
}else{
52+
plugin.warn(plugin.getClass().getName()+" is trying to register a duplicate setting: "+setting.getKey());
53+
}
54+
return true;
55+
}
56+
57+
public void unregisterConfig(Plugin plugin,String key){
58+
if(!this.settings.containsKey(plugin)){
59+
return;
60+
}
61+
this.settings.get(plugin).remove(key);
62+
if(!this.secondary.containsKey(key)||!this.secondary.get(key).remove(plugin)){
63+
this.primarySettingsMap.remove(key);
64+
}
65+
}
66+
67+
public <T> T getConfig(Plugin plugin,String key,Class<T> type){
68+
if(!isRegistered(plugin,key)){
69+
ServerLogger.getLogger().multiWarn(getClass(),"Trying to access a config setting that isn't registered to the plugin, this is bad practice.","","");
70+
}
71+
ConfigSetting setting = this.resolvePrimary(key);
72+
return type.cast(setting.getValue());
73+
}
74+
75+
public ConfigSetting resolvePrimary(String key){
76+
Plugin plugin = this.primarySettingsMap.get(key);
77+
return resolveSetting(plugin,key);
78+
}
79+
80+
public ConfigSetting resolveSetting(Plugin plugin,String key){
81+
ConfigSetting setting = null;
82+
Map<String,ConfigSetting> dic = this.settings.get(plugin);
83+
return dic.get(key);
84+
}
85+
86+
public boolean isRegistered(Plugin plugin,String key){
87+
boolean flag = false;
88+
if(this.primarySettingsMap.containsKey(key)){
89+
if(this.secondary.get(key).equals(plugin)){
90+
flag = true;
91+
}else if(this.secondary.containsKey(key) && this.secondary.get(key).contains(plugin)){
92+
flag = true;
93+
}
94+
}
95+
return flag;
96+
}
97+
98+
public static ConfigManager getManager() {
99+
return manager;
100+
}
101+
}

JSMod2Core/src/main/java/cn/jsmod2/core/plugin/ConfigSetting.java renamed to JSMod2DevelopKit/src/main/java/cn/jsmod2/core/plugin/ConfigSetting.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class ConfigSetting {
1010

1111
private boolean primary;
1212

13+
private String description;
14+
1315
public ConfigSetting(String key, String value, String defaultValue,boolean primary) {
1416
this.key = key;
1517
this.value = value;
@@ -32,4 +34,8 @@ public String getDefaultValue() {
3234
public boolean isPrimary() {
3335
return primary;
3436
}
37+
38+
public String getDescription() {
39+
return description;
40+
}
3541
}

JSMod2DevelopKit/src/main/java/cn/jsmod2/core/plugin/PluginBase.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
public abstract class PluginBase implements Plugin {
3434

35-
private Map<String,ConfigSetting> config = new HashMap<>();
3635

3736
private boolean haveInit = false;
3837

@@ -68,7 +67,6 @@ public void init(ILogger logger, IServer server, String pluginName, File serverF
6867
this.classLoader = classLoader;
6968
this.version = version;
7069
this.haveInit = true;
71-
this.getServer().getPluginManager().getSettings().put(this,config);
7270
}
7371
}
7472

@@ -126,31 +124,15 @@ public void initConfig(){
126124
});
127125
}
128126

129-
public Object getConfig(String key){
130-
if(!config.containsKey(key)){
131-
return "";
132-
}
133-
ConfigSetting setting = config.get(key);
134-
return setting.getValue()==null?setting.getDefaultValue():setting.getValue();
135-
}
136-
137-
public Object addConfig(String key,ConfigSetting setting,boolean trySet){
138-
if(trySet){
139-
if(config.containsKey(key)){
140-
return config.get(key);
141-
}
142-
}else{
143-
if(config.get(key)!=null&&config.get(key).isPrimary()){
144-
throw new ServerRuntimeException("primary=true,the key is exists");
145-
}
146-
}
147-
return config.put(key,setting);
127+
public Object addConfig(ConfigSetting setting){
128+
return ConfigManager.getManager().registerConfig(this,setting);
148129
}
149130

150-
public Object addConfig(String key,ConfigSetting value){
151-
return addConfig(key,value,false);
131+
public <T> T getConfig(String key,Class<T> type){
132+
return ConfigManager.getManager().getConfig(this,key,type);
152133
}
153134

135+
154136
public void info(String message){
155137
this.getServer().getLogger().multiInfo(this.getClass(),message,"","");
156138
}

JSMod2Web/src/main/java/cn/jsmod2/web/PanelService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private MethodInvoker invokeMethod(JSONArray values, Object root, String get) th
174174
}
175175
}
176176
if(method == null){
177-
throw new ApiErrorException("no such get method");
177+
throw new ApiErrorException("no such get or set or do method");
178178
}
179179

180180
Class<?>[] paramTypes = method.getParameterTypes();

0 commit comments

Comments
 (0)