package net.wayward_realms.waywardessentials.warp;

import net.wayward_realms.waywardessentials.WaywardEssentials;
import net.wayward_realms.waywardlib.util.file.filter.YamlFileFilter;
import net.wayward_realms.waywardlib.util.serialisation.SerialisableLocation;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;

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

public class WarpManager {

    private WaywardEssentials plugin;

    public WarpManager(WaywardEssentials plugin) {
        this.plugin = plugin;
    }

    public Map<String, Location> getWarps() {
        Map<String, Location> warps = new HashMap<>();
        File warpDirectory = new File(plugin.getDataFolder(), "warps");
        for (File file : warpDirectory.listFiles(new YamlFileFilter())) {
            YamlConfiguration warpConfig = YamlConfiguration.loadConfiguration(file);
            Location warp = ((SerialisableLocation) warpConfig.get("location")).toLocation();
            warps.put(file.getName().replace(".yml", ""), warp);
        }
        return warps;
    }

    public Location getWarp(String name) {
        File warpDirectory = new File(plugin.getDataFolder(), "warps");
        File warpFile = new File(warpDirectory, name.toLowerCase() + ".yml");
        if (warpFile.exists()) {
            YamlConfiguration warpConfig = YamlConfiguration.loadConfiguration(warpFile);
            return ((SerialisableLocation) warpConfig.get("location")).toLocation();
        }
        return null;
    }

    public void addWarp(String name, Location location) {
        File warpDirectory = new File(plugin.getDataFolder(), "warps");
        File warpFile = new File(warpDirectory, name.toLowerCase() + ".yml");
        YamlConfiguration warpConfig = new YamlConfiguration();
        warpConfig.set("location", new SerialisableLocation(location));
        try {
            warpConfig.save(warpFile);
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    public void removeWarp(String name) {
        File warpDirectory = new File(plugin.getDataFolder(), "warps");
        File warpFile = new File(warpDirectory, name.toLowerCase() + ".yml");
        if (warpFile.exists()) {
            warpFile.delete();
        }
    }

}

@@ -759,32 +759,74 @@
 er(), %22warps%22);%0A
+        if (warpDirectory.exists()) %7B%0A    
         for (Fil
@@ -887,32 +887,36 @@
 ) %7B%0A            
+ 
+   
 YamlConfiguratio
@@ -969,24 +969,28 @@
 tion(file);%0A
+    
             
@@ -1079,24 +1079,28 @@
             
+ 
+   
 warps.put(fi
@@ -1140,16 +1140,30 @@
  warp);%0A
+            %7D%0A
         

Check if warp directory exists before trying to find warps