package io.github.vayleryn.vaylerynessentials;

import io.github.vayleryn.vaylerynlib.plugin.essentials.Kit;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;

public class KitManager {
	
	private Map<String, Kit> kits = new HashMap<String, Kit>();
	private VaylerynEssentials plugin;
	
	public KitManager(VaylerynEssentials plugin) {
		this.plugin = plugin;
	}
	
	public Map<String, Kit> getKits() {
		return kits;
	}
	
	public Kit getKit(String name) {
		return kits.get(name);
	}
	
	public void save() {
		File kitFile = new File(plugin.getDataFolder().getPath() + File.separator + "kits.yml");
		YamlConfiguration kitConfig = new YamlConfiguration();
		for (String kitName : kits.keySet()) {
			kitConfig.set(kitName, kits.get(kitName));
		}
		try {
			kitConfig.save(kitFile);
		} catch (IOException exception) {
			exception.printStackTrace();
		}
	}
	
	public void load() {
		File kitFile = new File(plugin.getDataFolder().getPath() + File.separator + "kits.yml");
		YamlConfiguration kitConfig = new YamlConfiguration();
		try {
			kitConfig.load(kitFile);
		} catch (FileNotFoundException exception) {
			exception.printStackTrace();
		} catch (IOException exception) {
			exception.printStackTrace();
		} catch (InvalidConfigurationException exception) {
			exception.printStackTrace();
		}
		for (String kitName : kitConfig.getKeys(false)) {
			kits.put(kitName, (Kit) kitConfig.get(kitName));
		}
	}
	
}

@@ -1163,32 +1163,59 @@
  + %22kits.yml%22);%0A
+%09%09if (kitFile.exists()) %7B%0A%09
 %09%09YamlConfigurat
@@ -1255,16 +1255,17 @@
 tion();%0A
+%09
 %09%09try %7B%0A
@@ -1259,32 +1259,33 @@
 ();%0A%09%09%09try %7B%0A%09%09%09
+%09
 kitConfig.load(k
@@ -1285,32 +1285,33 @@
 .load(kitFile);%0A
+%09
 %09%09%7D catch (FileN
@@ -1332,32 +1332,33 @@
 on exception) %7B%0A
+%09
 %09%09%09exception.pri
@@ -1367,32 +1367,33 @@
 StackTrace();%0A%09%09
+%09
 %7D catch (IOExcep
@@ -1402,32 +1402,33 @@
 on exception) %7B%0A
+%09
 %09%09%09exception.pri
@@ -1441,24 +1441,25 @@
 kTrace();%0A%09%09
+%09
 %7D catch (Inv
@@ -1493,32 +1493,33 @@
 exception) %7B%0A%09%09%09
+%09
 exception.printS
@@ -1531,22 +1531,24 @@
 race();%0A
+%09
 %09%09%7D%0A%09%09
+%09
 for (Str
@@ -1585,24 +1585,25 @@
 s(false)) %7B%0A
+%09
 %09%09%09kits.put(
@@ -1638,23 +1638,28 @@
 (kitName));%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%09%0A%7D%0A

Make sure kit file exists before attempting to load it