package io.github.vayleryn.vaylerynessentials;

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

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

public class WarpManager {
	
	private VaylerynEssentials plugin;
	private Map<String, Location> warps = new HashMap<String, Location>();
	
	public WarpManager(VaylerynEssentials plugin) {
		this.plugin = plugin;
	}
	
	public Map<String, Location> getWarps() {
		return warps;
	}
	
	public Location getWarp(String name) {
		return warps.get(name);
	}
	
	public void save() {
		File warpFile = new File(plugin.getDataFolder().getPath() + File.separator + "warps.yml");
		YamlConfiguration warpConfig = new YamlConfiguration();
		for (String warpName : warps.keySet()) {
			warpConfig.set(warpName, new SerialisableLocation(warps.get(warpName)));
		}
		try {
			warpConfig.save(warpFile);
		} catch (IOException exception) {
			exception.printStackTrace();
		}
	}
	
	public void load() {
		File warpFile = new File(plugin.getDataFolder().getPath() + File.separator + "warps.yml");
		YamlConfiguration warpConfig = new YamlConfiguration();
		try {
			warpConfig.load(warpFile);
		} catch (FileNotFoundException exception) {
			exception.printStackTrace();
		} catch (IOException exception) {
			exception.printStackTrace();
		} catch (InvalidConfigurationException exception) {
			exception.printStackTrace();
		}
		for (String warpName : warpConfig.getKeys(false)) {
			warps.put(warpName, ((SerialisableLocation) warpConfig.get(warpName)).toLocation());
		}
	}
	
}

@@ -1195,32 +1195,60 @@
 + %22warps.yml%22);%0A
+%09%09if (warpFile.exists()) %7B%0A%09
 %09%09YamlConfigurat
@@ -1291,22 +1291,24 @@
 on();%0A%09%09
+%09
 try %7B%0A
+%09
 %09%09%09warpC
@@ -1321,32 +1321,33 @@
 load(warpFile);%0A
+%09
 %09%09%7D catch (FileN
@@ -1371,32 +1371,33 @@
 exception) %7B%0A%09%09%09
+%09
 exception.printS
@@ -1401,32 +1401,33 @@
 ntStackTrace();%0A
+%09
 %09%09%7D catch (IOExc
@@ -1441,32 +1441,33 @@
 exception) %7B%0A%09%09%09
+%09
 exception.printS
@@ -1479,16 +1479,17 @@
 race();%0A
+%09
 %09%09%7D catc
@@ -1526,32 +1526,33 @@
 on exception) %7B%0A
+%09
 %09%09%09exception.pri
@@ -1569,18 +1569,20 @@
 ce();%0A%09%09
+%09
 %7D%0A
+%09
 %09%09for (S
@@ -1623,24 +1623,25 @@
 s(false)) %7B%0A
+%09
 %09%09%09warps.put
@@ -1712,23 +1712,28 @@
 ocation());%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%09%0A%7D%0A

Make sure warp file exists before trying to load it