package vg.civcraft.mc.civmodcore.util;

import static vg.civcraft.mc.civmodcore.CivModCorePlugin.log;

import java.io.IOException;
import java.util.ArrayList;

import com.google.common.reflect.ClassPath;
import vg.civcraft.mc.civmodcore.ACivMod;

public class ClassUtility {

	public static ArrayList<Class<?>> GetClassesForPlugin(ACivMod plugin) {
		return GetClassesForPlugin(plugin, null);
	}

	public static ArrayList<Class<?>> GetClassesForPlugin(ACivMod plugin, Class<?> ofType) {
		ArrayList<Class<?>> classes = new ArrayList<>();
		try {
			Package pack = plugin.getClass().getPackage();
			if (pack == null) {
				log().info("Package name REQUIRED for annotation loading");
			} else {

				String packageName = pack.getName();
				ClassLoader classloader = plugin.classLoader;
				ClassPath path = ClassPath.from(classloader);

				for (ClassPath.ClassInfo info : path.getTopLevelClassesRecursive(packageName)) {
					Class clazz = null;
					try {
						clazz = Class.forName(info.getName(), true, classloader);
					} catch (NoClassDefFoundError e) {
						log().info("CivModCore failed to load class " + info.getName()
								+ ", you could be missing a dependency. This message is more of an info "
								+ "message useful for debugging. If you see this message you can usually "
								+ "ignore it unless the plugin itself throws an error.");
						continue;
					}
					if (ofType == null || ofType.isAssignableFrom(clazz)) {
						classes.add(clazz);
					}
				}
			}
		} catch (ClassNotFoundException | IOException e) {
			e.printStackTrace();
		}
		return classes;
	}

}

@@ -1062,16 +1062,18 @@
  %7B%0A%09%09%09%09%09
+//
 %09log().i
@@ -1129,24 +1129,26 @@
 Name()%0A%09%09%09%09%09
+//
 %09%09%09+ %22, you 
@@ -1213,25 +1213,26 @@
 info %22%0A%09%09%09%09%09
-%09
+//
 %09%09+ %22message
@@ -1301,16 +1301,18 @@
  %22%0A%09%09%09%09%09
+//
 %09%09%09+ %22ig

Comment out failed class loading for now to avoid NPE