Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.Reader;
- import java.io.UnsupportedEncodingException;
- import java.nio.file.DirectoryNotEmptyException;
- import java.nio.file.Files;
- import java.nio.file.NoSuchFileException;
- import java.nio.file.Path;
- import java.util.logging.Level;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import net.frosted.Skywars.Skywars;
- public class Configuration {
- private FileConfiguration customConfig = null;
- private File customConfigFile = null;
- private JavaPlugin plugin = null;
- public Configuration(JavaPlugin plugin, String fileName) {
- this.plugin = plugin;
- this.name = name;
- }
- public void reloadConfig() {
- if (customConfigFile == null) {
- customConfigFile = new File(Skywars.getInstance().getDataFolder(), name);
- }
- customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
- // Look for defaults in the jar
- Reader defConfigStream;
- try {
- defConfigStream = new InputStreamReader(plugin.getResource(name), "UTF8");
- if (defConfigStream != null) {
- YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
- customConfig.setDefaults(defConfig);
- }
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public FileConfiguration getConfig() {
- if (customConfig == null) {
- reloadConfig();
- }
- return customConfig;
- }
- public boolean deleteConfig() {
- customConfigFile = new File(plugin.getDataFolder(), name);
- Path path = customConfigFile.toPath();
- try {
- Files.delete(path);
- return true;
- } catch (NoSuchFileException x) {
- System.err.format("%s: no such" + " file or directory%n", path);
- return false;
- } catch (DirectoryNotEmptyException x) {
- System.err.format("%s not empty%n", path);
- return false;
- } catch (IOException x) {
- // File permission problems are caught here.
- System.err.println(x);
- return false;
- }
- }
- public void saveConfig() {
- if (customConfig == null || customConfigFile == null) {
- return;
- }
- try {
- getConfig().save(customConfigFile);
- } catch (IOException ex) {
- plugin.getServer().getLogger().log(Level.SEVERE, "Could not save config to " + customConfigFile, ex);
- }
- }
- public void saveDefaultConfig() {
- if (customConfigFile == null) {
- customConfigFile = new File(plugin.getDataFolder(), name);
- }
- if (!customConfigFile.exists()) {
- plugin.saveResource(name, false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement