1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-12 04:07:19 +01:00
Jobs/com/gamingmesh/jobs/config/YmlMaker.java

98 lines
2.5 KiB
Java
Raw Normal View History

2016-06-25 14:51:21 +02:00
package com.gamingmesh.jobs.config;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import com.gamingmesh.jobs.Jobs;
2016-06-25 14:51:21 +02:00
public class YmlMaker {
Jobs Plugin;
2016-06-25 14:51:21 +02:00
public String fileName;
private JavaPlugin plugin;
public File ConfigFile;
private FileConfiguration Configuration;
public YmlMaker(Jobs Plugin) {
2016-06-25 14:51:21 +02:00
this.Plugin = Plugin;
}
public YmlMaker(JavaPlugin plugin, String fileName) {
if (plugin == null) {
throw new IllegalArgumentException("plugin cannot be null");
}
this.plugin = plugin;
this.fileName = fileName;
File dataFolder = plugin.getDataFolder();
if (dataFolder == null) {
throw new IllegalStateException();
}
this.ConfigFile = new File(dataFolder.toString() + File.separatorChar + this.fileName);
}
public void reloadConfig() {
InputStreamReader f = null;
try {
f = new InputStreamReader(new FileInputStream(this.ConfigFile), "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
this.Configuration = YamlConfiguration.loadConfiguration(f);
InputStream defConfigStream = this.plugin.getResource(this.fileName);
if (defConfigStream != null) {
@SuppressWarnings("deprecation")
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
this.Configuration.setDefaults(defConfig);
}
2016-06-25 15:27:01 +02:00
if (defConfigStream != null)
try {
defConfigStream.close();
} catch (IOException e) {
e.printStackTrace();
}
if (f != null)
try {
f.close();
} catch (Exception e) {
e.printStackTrace();
}
2016-06-25 14:51:21 +02:00
}
public FileConfiguration getConfig() {
if (this.Configuration == null) {
reloadConfig();
}
return this.Configuration;
}
public void saveConfig() {
if ((this.Configuration == null) || (this.ConfigFile == null)) {
return;
}
try {
getConfig().save(this.ConfigFile);
} catch (IOException ex) {
this.plugin.getLogger().log(Level.SEVERE, "Could not save config to " + this.ConfigFile, ex);
}
}
public void saveDefaultConfig() {
if (!this.ConfigFile.exists()) {
this.plugin.saveResource(this.fileName, false);
}
}
}