Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.talexframe.frame.core.modules.plugins.adapt.config.json;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.talexframe.frame.core.talex.FrameConfig;
import lombok.SneakyThrows;

/**
* {@link com.talexframe.frame.core.modules.plugins.adapt.config Package }
*
* @author TalexDreamSoul 22/04/17 上午 08:59 Project: TalexFrame
*/
public class JSONConfig extends FrameConfig {

private JSONObject json = new JSONObject();

public JSONConfig(String provider, String path) {

super(provider, path);
}

@Override
public String serialize() {

return json.toString();
}

@SneakyThrows
@Override
public FrameConfigWrapper<?> deserialize(String data) {

this.json = JSONUtil.parseObj(data);

return new FrameConfigWrapper<>(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package com.talexframe.frame.core.modules.plugins.adapt.config.json;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.HashMultimap;
import com.talexframe.frame.core.modules.plugins.adapt.PluginCompAdapter;
import com.talexframe.frame.core.modules.plugins.addon.PluginScanner;
import com.talexframe.frame.core.modules.plugins.core.WebPlugin;
import com.talexframe.frame.core.pojo.annotations.TVConfig;
import com.talexframe.frame.core.talex.FrameCreator;
import lombok.SneakyThrows;

import java.io.File;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.Set;

/**
* {@link com.talexframe.frame.core.modules.plugins.adapt.config Package }
*
* @author TalexDreamSoul 22/04/17 上午 08:59 Project: TalexFrame
*/
public class JSONConfigAdapter extends PluginCompAdapter<JSONConfig> {

private static final HashMultimap<Class<?>, JSONConfig> map = HashMultimap.create();

@SneakyThrows
@Override
protected boolean injectWithInstance(PluginScanner scanner, WebPlugin webPlugin, JSONConfig instance) {

map.get(instance.getClass()).forEach(jsonConfig -> {

if( jsonConfig.getPath().equalsIgnoreCase(instance.getPath()) ){

tframe.crash(new RuntimeException(jsonConfig.getPath() + " is already exist"));

}

});

File file = new File(webPlugin.getPluginDataFolder(), instance.getPath());

if( !file.exists() ) {

if( instance.isRequired() ) {

tframe.getFrameSender().warnConsoleMessage("[JSONConfigAdapter] " + instance.getPath() + " not found - The plugin will not work without it!");

tframe.crash(new RuntimeException(instance.getPath() + " not found - The plugin will not work without it!"));

} else {

tframe.getFrameSender().sendConsoleMessage("[JSONConfigAdapter] " + instance.getPath() + " not found!");

}

return false;

}

JSONObject jsonObject = JSONUtil.readJSONObject(file, StandardCharsets.UTF_8);

Class<?> clz = instance.getClass();

TVConfig classAnno = clz.getAnnotation(TVConfig.class);

if( classAnno != null ) {

for( Field field : clz.getDeclaredFields() ) {

String name = StrUtil.isBlankIfStr(classAnno.name()) ? field.getName() : classAnno.name();

field.setAccessible( true );

if( jsonObject.containsKey(name) ) {

field.set( instance, jsonObject.get( name, field.getType() ) );

} else if( classAnno.ignore() ) {

tframe.getFrameSender().warnConsoleMessage("[JSONConfigAdapter] " + instance.getPath() + " not found - The plugin allow ignore it.");

} else {

tframe.crash(new RuntimeException("[JSONConfigAdapter] " + instance.getPath() + "/@JSON " + name + " - The plugin will not work without it! For class: " + clz.getName()));

return false;

}

}

} else {

for( Field field : clz.getDeclaredFields() ) {

TVConfig fieldAnno = field.getAnnotation(TVConfig.class);
String name = StrUtil.isBlankIfStr(fieldAnno.name()) ? field.getName() : fieldAnno.name();

field.setAccessible(true);

if( jsonObject.containsKey(name) ) {

field.set( instance, jsonObject.get( name, field.getType() ) );

} else if( fieldAnno.ignore() ) {

tframe.getFrameSender().warnConsoleMessage("[JSONConfigAdapter] " + instance.getPath() + "/@JSON " + name + " not found - The plugin allow ignore it.");

} else {

tframe.crash(new RuntimeException("[JSONConfigAdapter] " + instance.getPath() + "/@JSON " + name + " - The plugin will not work without it! For class: " + clz.getName()));

return false;

}

}

}

map.put( clz, instance );

return true;
}

@SneakyThrows
@Override
public boolean logoutInstance(PluginScanner scanner, WebPlugin webPlugin, Class<? extends FrameCreator> clazz) {

Set<JSONConfig> configs = map.get(clazz);

for( JSONConfig config : configs ) {

JSONObject json = new JSONObject();
TVConfig classAnno = clazz.getAnnotation(TVConfig.class);

if( classAnno != null ) {

for( Field field : clazz.getDeclaredFields() ) {

String name = StrUtil.isBlankIfStr(classAnno.name()) ? field.getName() : classAnno.name();

field.setAccessible( true );

Object value = field.get( config );

if( value != null ) {

json.set( name, value );

} else if( classAnno.ignore() ) {

tframe.getFrameSender().warnConsoleMessage("[JSONConfigAdapter] " + config.getPath() + "/@json " + name + " not found - The plugin allow ignore it.");

} else {

tframe.crash(new RuntimeException("[JSONConfigAdapter] " + config.getPath() + "/@json " + name + " - The plugin will not work without it! For class: " + clazz.getName()));

return false;

}

}

} else {

for( Field field : clazz.getDeclaredFields() ) {

TVConfig fieldAnno = field.getAnnotation(TVConfig.class);
String name = StrUtil.isBlankIfStr(fieldAnno.name()) ? field.getName() : fieldAnno.name();

Object value = field.get( config );
field.setAccessible( true );

if( value != null ) {

json.set( name, value );

} else if( fieldAnno.ignore() ) {

tframe.getFrameSender().warnConsoleMessage("[JSONConfigAdapter] " + config.getPath() + "/@json " + name + " not found - The plugin allow ignore it.");

} else {

tframe.crash(new RuntimeException("[JSONConfigAdapter] " + config.getPath() + "/@json " + name + " - The plugin will not work without it! For class: " + clazz.getName()));

return false;

}

}

}

File file = new File(webPlugin.getPluginDataFolder(), config.getPath());

FileUtil.writeString(json.toString(), file, StandardCharsets.UTF_8);

}

return false;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.talexframe.frame.core.modules.plugins.adapt.config.yaml;

import com.talexframe.frame.core.talex.FrameConfig;
import lombok.SneakyThrows;
import org.bukkit.configuration.file.YamlConfiguration;

/**
* {@link com.talexframe.frame.core.modules.plugins.adapt.config Package }
*
* @author TalexDreamSoul 22/04/17 上午 08:31 Project: TalexFrame
*/
public class YamlConfig extends FrameConfig {

private final YamlConfiguration yaml = new YamlConfiguration();

public YamlConfig(String provider, String path) {

super(provider, path);
}

@Override
public String serialize() {

return yaml.saveToString();
}

@SneakyThrows
@Override
public FrameConfigWrapper<?> deserialize(String data) {

yaml.loadFromString(data);

return new FrameConfigWrapper<>(this);
}

}
Loading