From 0a5f8556fe65331ce2e4bf75b7eb680ca57d414f Mon Sep 17 00:00:00 2001 From: md_5 Date: Tue, 24 Sep 2013 12:14:47 +1000 Subject: [PATCH] Add config saving --- .../net/md_5/bungee/config/Configuration.java | 2 +- .../bungee/config/ConfigurationProvider.java | 8 ++++++- .../md_5/bungee/config/YamlConfiguration.java | 22 +++++++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/config/src/main/java/net/md_5/bungee/config/Configuration.java b/config/src/main/java/net/md_5/bungee/config/Configuration.java index eee2af6c..85651786 100644 --- a/config/src/main/java/net/md_5/bungee/config/Configuration.java +++ b/config/src/main/java/net/md_5/bungee/config/Configuration.java @@ -13,7 +13,7 @@ public final class Configuration { private static final char SEPARATOR = '.'; - private final Map self; + final Map self; private final Configuration defaults; private Configuration getSectionFor(String path) diff --git a/config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java b/config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java index 5329c579..9fcda3fe 100644 --- a/config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java +++ b/config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java @@ -1,7 +1,9 @@ package net.md_5.bungee.config; import java.io.File; +import java.io.IOException; import java.io.Reader; +import java.io.Writer; import java.util.HashMap; import java.util.Map; @@ -21,7 +23,11 @@ public abstract class ConfigurationProvider } /*------------------------------------------------------------------------*/ - public abstract Configuration load(File file); + public abstract void save(Configuration config, File file) throws IOException; + + public abstract void save(Configuration config, Writer writer); + + public abstract Configuration load(File file) throws IOException; public abstract Configuration load(Reader reader); diff --git a/config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java b/config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java index 3cec6865..c903016b 100644 --- a/config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java +++ b/config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java @@ -2,8 +2,10 @@ package net.md_5.bungee.config; import java.io.File; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.io.Reader; +import java.io.Writer; import java.util.Map; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -26,14 +28,26 @@ public class YamlConfiguration extends ConfigurationProvider }; @Override - public Configuration load(File file) + public void save(Configuration config, File file) throws IOException + { + try ( FileWriter writer = new FileWriter( file ) ) + { + save( config, writer ); + } + } + + @Override + public void save(Configuration config, Writer writer) + { + yaml.get().dump( config.self, writer ); + } + + @Override + public Configuration load(File file) throws IOException { try ( FileReader reader = new FileReader( file ) ) { return load( reader ); - } catch ( IOException ex ) - { - return null; } }