package com.forgeessentials.core.preloader.classloading;

import net.minecraft.launchwrapper.LaunchClassLoader;

import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class FEClassLoader {
    public void runClassLoad(LaunchClassLoader classloader, File root)
    {
        File lib = new File(root, "lib/");
        if (!lib.exists())
        {
            lib.mkdirs();
        }
        for (File f : lib.listFiles())
        {
            if (f != null)
            {
                try
                {
                    classloader.addURL(f.toURI().toURL());
                    System.out.println("[ForgeEssentials] Loaded library file " + f.getAbsolutePath());
                }
                catch (MalformedURLException e)
                {
                    throw new RuntimeException("Could not add library file " + f.getAbsolutePath() + ", there may be a classloading problem.");
                }
            }
        }

        File module = new File(root, "modules/");
        if (!module.exists())
        {
            module.mkdirs();
        }
        for (File f : module.listFiles())
        {
            if (f != null)
            {
                try
                {
                    classloader.addURL(f.toURI().toURL());
                }
                catch (MalformedURLException e)
                {
                    System.err.println("[ForgeEssentials] Could not add module file " + f.getAbsolutePath() + ", there may be a class loading problem.");
                }
            }
        }
        System.out.println("[ForgeEssentials] Loaded " + module.listFiles().length + " modules");

        checkLibs();
    }

    private static String[] compulsoryLibs = { "com.mysql.jdbc.Driver", "org.pircbotx.PircBotX", "org.h2.Driver" };

    public void checkLibs()
    {
        String prop = System.getProperty("forgeessentials.developermode");
        if (prop != null && prop.equals("true"))
        { // FOR DEVS ONLY! THAT IS WHY IT IS A PROPERTY!!!

            System.out.println("[ForgeEssentials] Running in developer mode. Libraries will not be checked.");
            return;
        }
        List<String> erroredLibs = new ArrayList<String>();
        for (String clazz : compulsoryLibs)
        {
            try
            {
                Class.forName(clazz);
                System.out.println("[ForgeEssentials] Found library " + clazz);
            }
            catch (ClassNotFoundException cnfe)
            {
                erroredLibs.add(clazz);
            }
        }
        if (!erroredLibs.isEmpty())
        {
            for (Object error : erroredLibs.toArray())
            {
                System.err.println(error);
            }
            throw new RuntimeException("[ForgeEssentials] You are missing one or more library files. See your FML log for details.");
        }

    }

}

@@ -127,16 +127,47 @@
 o.File;%0A
+import java.io.FilenameFilter;%0A
 import j
@@ -197,16 +197,16 @@
 eption;%0A
-
 import j
@@ -278,16 +278,225 @@
 oader %7B%0A
+%0A    private static final FilenameFilter jarFilter = new FilenameFilter() %7B%0A        @Override%0A        public boolean accept(File dir, String name) %7B%0A            return name.endsWith(%22.jar%22);%0A        %7D%0A    %7D;%0A%0A
     publ
@@ -672,32 +672,32 @@
 rs();%0A        %7D%0A
-
         for (Fil
@@ -708,32 +708,41 @@
 : lib.listFiles(
+jarFilter
 ))%0A        %7B%0A   
@@ -1419,16 +1419,25 @@
 stFiles(
+jarFilter
 ))%0A     

Add filter to only allow jars to be loaded