/*
 * Copyright  2014 - 2017 | Wurst-Imperium | All rights reserved.
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package tk.wurst_client.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import com.google.gson.JsonElement;

import tk.wurst_client.utils.JsonUtils;

public abstract class Config
{
	private final Path path;
	
	public Config(String name)
	{
		this.path = WurstFolders.MAIN.resolve(name);
	}
	
	public final void initialize()
	{
		if(Files.exists(path))
			load();
		else
			save();
	}
	
	public final void load()
	{
		try
		{
			loadFromJson(readFile(path));
		}catch(Exception e)
		{
			System.out.println("Failed to load " + path.getFileName());
			e.printStackTrace();
		}
	}
	
	public final void save()
	{
		try
		{
			writeFile(path, saveToJson());
		}catch(Exception e)
		{
			System.out.println("Failed to save " + path.getFileName());
			e.printStackTrace();
		}
	}
	
	protected JsonElement readFile(Path path) throws IOException
	{
		return JsonUtils.jsonParser.parse(Files.newBufferedReader(path));
	}
	
	protected void writeFile(Path path, JsonElement json) throws IOException
	{
		JsonUtils.prettyGson.toJson(json, Files.newBufferedWriter(path));
	}
	
	protected abstract void loadFromJson(JsonElement json);
	
	protected abstract JsonElement saveToJson();
}

@@ -304,16 +304,78 @@
 files;%0A%0A
+import java.io.BufferedReader;%0Aimport java.io.BufferedWriter;%0A
 import j
@@ -1218,16 +1218,82 @@
 tion%0A%09%7B%0A
+%09%09try(BufferedReader reader = Files.newBufferedReader(path))%0A%09%09%7B%0A%09
 %09%09return
@@ -1324,39 +1324,20 @@
 rse(
-Files.newBufferedReader(path));
+reader);%0A%09%09%7D
 %0A%09%7D%0A
@@ -1415,16 +1415,82 @@
 tion%0A%09%7B%0A
+%09%09try(BufferedWriter writer = Files.newBufferedWriter(path))%0A%09%09%7B%0A%09
 %09%09JsonUt
@@ -1521,39 +1521,20 @@
 on, 
-Files.newBufferedWriter(path));
+writer);%0A%09%09%7D
 %0A%09%7D%0A

Fix default readFile & writeFile() methods