Skip to content

Instantly share code, notes, and snippets.

@DiscowZombie
Created January 18, 2018 18:26
Show Gist options
  • Select an option

  • Save DiscowZombie/41308e89f224311e6f377123523d7b96 to your computer and use it in GitHub Desktop.

Select an option

Save DiscowZombie/41308e89f224311e6f377123523d7b96 to your computer and use it in GitHub Desktop.
Gestion des configs - Minecraft Bukkit
package fr.discowzombie.ultimatets.spigot.utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
public class Config extends YamlConfiguration {
private File file;
private String defaults;
private JavaPlugin plugin;
/**
* Creates new PluginFile, with defaults
* @param plugin - Your plugin
* @param fileName - Name of the file
* @param defaultsName - Name of the defaults
*/
public Config(JavaPlugin plugin, String fileName, String defaultsName) {
this.plugin = plugin;
this.defaults = defaultsName;
this.file = new File(plugin.getDataFolder(), fileName);
reload();
}
/**
* Reload configuration
*/
public void reload() {
if (!file.exists()) {
try {
file.getParentFile().mkdirs();
file.createNewFile();
} catch (IOException exception) {
exception.printStackTrace();
plugin.getLogger().severe("Error while creating file " + file.getName());
}
}
try {
load(file);
if (defaults != null) {
InputStreamReader reader = new InputStreamReader(plugin.getResource(defaults));
FileConfiguration defaultsConfig = YamlConfiguration.loadConfiguration(reader);
setDefaults(defaultsConfig);
options().copyDefaults(true);
reader.close();
save();
}
} catch (IOException exception) {
exception.printStackTrace();
plugin.getLogger().severe("Error while loading file " + file.getName());
} catch (InvalidConfigurationException exception) {
exception.printStackTrace();
plugin.getLogger().severe("Error while loading file " + file.getName());
}
}
/**
* Save configuration
*/
public void save() {
try {
options().indent(2);
save(file);
} catch (IOException exception) {
exception.printStackTrace();
plugin.getLogger().severe("Error while saving file " + file.getName());
}
}
}
package fr.discowzombie.ultimatets.spigot.utils;
import fr.discowzombie.ultimatets.spigot.UltimateTs;
public abstract class ConfigManager {
private static Config conf, messages;
public static void generateConf(UltimateTs main){
conf = new Config(main, "config.yml", "config.yml");
conf.save();
messages = new Config(main, "messages.yml", "messages.yml");
messages.save();
}
public static Config getConf(){
return conf;
}
public static void saveConf(){
conf.save();
}
public static Config getMes(){
return messages;
}
public static void saveMes(){
messages.save();
}
}
@Override
public void onEnable() {
ConfigManager.generateConf(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment