package net.minecraft.launchwrapper.nallar.cachingclassloader;

import lombok.val;

import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.*;

public class Main {
	private static final String serverJarArgument = "--serverjar=";
	private static final Method addUrlMethod = getAddURLMethod();
	private static final boolean ADD_LIBRARIES = Boolean.getBoolean(System.getProperty("cachingClassLoader.addLibraries", "false"));
	@SuppressWarnings("WeakerAccess")
	public static String[] startupArgs;

	public static void main(String[] args) {
		try {
			run(args);
		} catch (Throwable t) {
			t.printStackTrace(System.err);
		}
	}

	private static void run(String[] args) throws Exception {
		ClassLoader classLoader = Main.class.getClassLoader();
		String loc = null;
		ArrayList<String> argsList = new ArrayList<>(Arrays.asList(args));
		for (Iterator<String> i$ = argsList.iterator(); i$.hasNext(); ) {
			String arg = i$.next();
			if (arg.toLowerCase().startsWith(serverJarArgument)) {
				loc = arg.substring(serverJarArgument.length());
				i$.remove();
				break;
			}
		}
		startupArgs = args = argsList.toArray(new String[argsList.size()]);
		if (loc == null) {
			loc = System.getProperty("serverJar");
		}
		loc = loc == null ? null : loc.trim();
		addLibraries((URLClassLoader) classLoader, loc);

		try {
			Class<?> launchwrapper = Class.forName("net.minecraft.launchwrapper.Launch", true, classLoader);
			System.out.println(String.valueOf(launchwrapper.getClassLoader()));
			Class.forName("org.objectweb.asm.Type", true, classLoader);
			Method main = launchwrapper.getMethod("main", String[].class);
			String[] allArgs = new String[args.length + 2];
			allArgs[0] = "--tweakClass";
			allArgs[1] = "net.minecraftforge.fml.common.launcher.FMLServerTweaker";
			System.arraycopy(args, 0, allArgs, 2, args.length);
			main.invoke(null, (Object) allArgs);
		} catch (ClassNotFoundException e) {
			System.err.println(e.toString());
			System.exit(1);
		} catch (Throwable t) {
			System.err.println("A problem occurred running the Server launcher.");
			t.printStackTrace(System.err);
			System.exit(1);
		}

	}

	private static void addLibraries(URLClassLoader classLoader, String loc) {
		if (loc == null) {
			System.err.println("You have not specified a server jar");
			System.err.println("Please add --serverJar=<minecraft forge jar name here> at the end of your java arguments.");
			System.err.println("Example: java -Xmx=2G -XX:MaxPermSize=256m -XX:+AgressiveOpts -jar cachingClassLoader.jar --serverJar=minecraft_forge.jar");
			System.exit(1);
		}
		File locFile = new File(loc);
		try {
			locFile = locFile.getCanonicalFile();
		} catch (IOException e) {
			e.printStackTrace(System.err);
		}
		if (locFile.exists()) {
			System.out.println("Adding server jar: " + loc + " @ " + locFile + " to libraries.");
			addPathToClassLoader(locFile, classLoader);
		} else {
			System.err.println("Could not find specified server jar: " + loc + " @ " + locFile);
			System.exit(1);
		}
		if (ADD_LIBRARIES) {
			File libraries = new File("libraries");
			addLibraries(classLoader, libraries);
		}
	}

	private static void addLibraries(URLClassLoader classLoader, File file) {
		File[] files = file.listFiles();
		if (files != null) {
			for (File inner : files) {
				if (inner.isDirectory())
					addLibraries(classLoader, inner);
				else if (inner.isFile()) {
					val lName = inner.getName().toLowerCase();
					if (lName.endsWith(".jar") || lName.endsWith(".zip"))
						addPathToClassLoader(inner, classLoader);
				}
			}
		}

	}

	private static Method getAddURLMethod() {
		Method method;
		try {
			method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
		} catch (NoSuchMethodException e) {
			throw new RuntimeException(e);
		}
		method.setAccessible(true);
		return method;
	}

	private static void addPathToClassLoader(File path, URLClassLoader classLoader) {
		try {
			URL u = path.toURI().toURL();
			System.out.println("Added " + u + " to " + classLoader);
			addUrlMethod.invoke(classLoader, u);
		} catch (Throwable t) {
			throw new RuntimeException(t);
		}
	}
}

@@ -61,28 +61,8 @@
 r;%0A%0A
-import lombok.val;%0A%0A
 impo
@@ -169,525 +169,45 @@
 %7B%0A%09p
-rivate static final String serverJarArgument = %22--serverjar=%22;%0A%09private static final Method addUrlMethod = getAddURLMethod();%0A%09private static final boolean ADD_LIBRARIES = Boolean.getBoolean(System.getProperty(%22cachingClassLoader.addLibraries%22, %22false%22));%0A%09@SuppressWarnings(%22WeakerAccess%22)%0A%09public static String%5B%5D startupArgs;%0A%0A%09public static void main(String%5B%5D args) %7B%0A%09%09try %7B%0A%09%09%09run(args);%0A%09%09%7D catch (Throwable t) %7B%0A%09%09%09t.printStackTrace(System.err);%0A%09%09%7D%0A%09%7D%0A%0A%09private static void run(String%5B%5D args) throws Exception
+ublic static void main(String%5B%5D args)
  %7B%0A%09
@@ -447,16 +447,68 @@
 next();%0A
+%09%09%09final String serverJarArgument = %22--serverjar=%22;%0A
 %09%09%09if (a
@@ -653,22 +653,8 @@
 %7D%0A%09%09
-startupArgs = 
 args
@@ -1997,56 +1997,8 @@
 ava 
--Xmx=2G -XX:MaxPermSize=256m -XX:+AgressiveOpts 
 -jar
@@ -2514,556 +2514,8 @@
 %09%09%7D%0A
-%09%09if (ADD_LIBRARIES) %7B%0A%09%09%09File libraries = new File(%22libraries%22);%0A%09%09%09addLibraries(classLoader, libraries);%0A%09%09%7D%0A%09%7D%0A%0A%09private static void addLibraries(URLClassLoader classLoader, File file) %7B%0A%09%09File%5B%5D files = file.listFiles();%0A%09%09if (files != null) %7B%0A%09%09%09for (File inner : files) %7B%0A%09%09%09%09if (inner.isDirectory())%0A%09%09%09%09%09addLibraries(classLoader, inner);%0A%09%09%09%09else if (inner.isFile()) %7B%0A%09%09%09%09%09val lName = inner.getName().toLowerCase();%0A%09%09%09%09%09if (lName.endsWith(%22.jar%22) %7C%7C lName.endsWith(%22.zip%22))%0A%09%09%09%09%09%09addPathToClassLoader(inner, classLoader);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%0A
 %09%7D%0A%0A
@@ -2973,20 +2973,25 @@
 %0A%09%09%09
-addUrl
+getAddURL
 Method
+()
 .inv

Simplify Main.