Skip to content

Instantly share code, notes, and snippets.

@DevSrSouza
Created May 31, 2018 06:33
Show Gist options
  • Save DevSrSouza/1f0f6cb7a26f758dd7daef8f83cd845b to your computer and use it in GitHub Desktop.
Save DevSrSouza/1f0f6cb7a26f758dd7daef8f83cd845b to your computer and use it in GitHub Desktop.
Class deserializer and serialiazer to YAML
import org.bukkit.configuration.file.FileConfiguration;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Deserialize a public static fields of a classe
*
* Deserialize public static fields de uma classe
*
* @author DevSrSouza
* @version 1.0
*
* https://github.com/DevSrSouza/
* You can find updates here https://gist.github.com/DevSrSouza
*/
public class ClassYamlConfiguration {
/**
* If you want to translate & to § on load and § to & on save let this true
*
* Se voce querer converter as cores em String deixe true
*/
public static boolean AUTO_CONVERT_COLORS = true;
/**
* If on config have the field name he loads to the field the value
* else he set to the config the default value of field
*
* @param clazz your class model
* @param fileConfiguration your config
* @return true if set something on the config(save the configuration if is true)
*/
public static boolean loadAndSetDefault(Class clazz, FileConfiguration fileConfiguration) {
return loadAndSetDefault(clazz,
(path) -> fileConfiguration.get(path),
(path, value) -> fileConfiguration.set(path, value));
}
public static boolean loadAndSetDefault(Class clazz, Function<String, Object> get, BiConsumer<String, Object> set) {
boolean anyModification = false;
for(Field f : clazz.getFields()) {
if(Modifier.isStatic(f.getModifiers())) {
Object template = null;
try{
template = f.get(null);
}catch (Exception ex) {
continue;
}
if(f.getType().equals(Class.class)) {
boolean modificou = loadAndSetDefault((Class)template, (path) ->
get.apply(f.getName() + "." + path),
(path, value) -> set.accept(f.getName() + "." + path, value));
if(modificou) anyModification=true;
}else{
Object got = get.apply(f.getName());
if(template != null && got == null) {
anyModification = true;
if (f.getType().equals(String.class)) {
Object fixColor = AUTO_CONVERT_COLORS ? ((String) template).replace('§', '&') : template;
set.accept(f.getName(), fixColor);
got = fixColor;
} else if (f.getType().equals(List.class)
&& ((Class<?>) ((ParameterizedType) f.getGenericType()).getActualTypeArguments()[0]).equals(String.class)) {
Object fixColor = AUTO_CONVERT_COLORS ?
((List<String>) template).stream()
.map(s->s.replace('§', '&')).collect(Collectors.toList())
: template;
set.accept(f.getName(), fixColor);
got = fixColor;
} else {
set.accept(f.getName(), template);
}
}
if(got != null) {
try {
if(f.getType().equals(String.class)) {
f.set(null, AUTO_CONVERT_COLORS ? ((String)got).replace('&', '§') : got);
}else if (f.getType().equals(List.class)
&& ((Class<?>) ((ParameterizedType) f.getGenericType()).getActualTypeArguments()[0]).equals(String.class)) {
f.set(null, AUTO_CONVERT_COLORS ?
((List<String>) got).stream()
.map(s->s.replace('&', '§')).collect(Collectors.toList())
: got);
}else {
f.set(null, got);
}
} catch (Exception e) {
continue;
}
}
}
}
}
return anyModification;
}
}
# Target default
# Isso foi como ficou com os padroes
server_name: SamplePlugin
default_int_value: 50
messages:
on_server_start: '&cServidor iniciando...'
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
public class Sample extends JavaPlugin {
@Override
public void onEnable() {
getDataFolder().mkdirs();
File file = new File(getDataFolder(), "config.yml");
FileConfiguration config = null;
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) { e.printStackTrace(); }
}
config = YamlConfiguration.loadConfiguration(file);
if(ClassYamlConfiguration.loadAndSetDefault(MyConfig.class, config)) {
try {
config.save(file);
} catch (IOException e) { e.printStackTrace(); }
}
}
public static class MyConfig {
public static String server_name = "SamplePlugin";
public static int default_int_value = 50;
public static Class messages = MessagesConfig.class;
}
public static class MessagesConfig {
public static String on_server_start = "§cServidor iniciando...";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment