package com.btk5h.skriptmirror;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

import ch.njol.skript.Skript;

public class LibraryLoader {
  private static ClassLoader classLoader = LibraryLoader.class.getClassLoader();

  private static final PathMatcher MATCHER =
      FileSystems.getDefault().getPathMatcher("glob:**/*.jar");

  private static class LibraryVisitor extends SimpleFileVisitor<Path> {
    private List<URL> urls = new ArrayList<>();

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
      if (MATCHER.matches(file)) {
        Skript.info("Loaded external library " + file.getFileName());
        urls.add(file.toUri().toURL());
      }
      return super.visitFile(file, attrs);
    }

    public URL[] getUrls() {
      return urls.toArray(new URL[urls.size()]);
    }
  }

  public static void loadLibraries() {
    try {
      LibraryVisitor visitor = new LibraryVisitor();
      Files.walkFileTree(SkriptMirror.getInstance().getDataFolder().toPath(), visitor);
      classLoader = new URLClassLoader(visitor.getUrls(), LibraryLoader.class.getClassLoader());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static ClassLoader getClassLoader() {
    return classLoader;
  }
}

@@ -1222,24 +1222,142 @@
 braries() %7B%0A
+    Path dataFolder = SkriptMirror.getInstance().getDataFolder().toPath();%0A    if (Files.isDirectory(dataFolder)) %7B%0A  
     try %7B%0A  
@@ -1354,16 +1354,18 @@
   try %7B%0A
+  
       Li
@@ -1411,24 +1411,26 @@
 or();%0A      
+  
 Files.walkFi
@@ -1440,59 +1440,18 @@
 ree(
-SkriptMirror.getInstance().getDataFolder().toPath()
+dataFolder
 , vi
@@ -1458,16 +1458,18 @@
 sitor);%0A
+  
       cl
@@ -1561,16 +1561,18 @@
 ));%0A    
+  
 %7D catch 
@@ -1595,16 +1595,18 @@
 %7B%0A      
+  
 e.printS
@@ -1614,24 +1614,32 @@
 ackTrace();%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A 

Check if directory exists before loading libraries (fixes #7)