1 package com.irtimaled.bbor.server;
3 import net.minecraft.launchwrapper.Launch;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.lang.reflect.Method;
9 import java.net.MalformedURLException;
11 import java.net.URLClassLoader;
12 import java.nio.channels.Channels;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
18 public class ServerRunner {
19 private static final Map<String, String> VANILLA_SERVER_JARS = new HashMap<>();
21 private static final String[] LIBRARIES = {
22 "https://github.com/irtimaled/Mixin/releases/download/org/spongepowered/mixin/0.7.11-SNAPSHOT/mixin-0.7.11-SNAPSHOT.jar",
23 "https://repo1.maven.org/maven2/org/ow2/asm/asm/6.2/asm-6.2.jar",
24 "https://repo1.maven.org/maven2/org/ow2/asm/asm-commons/6.2/asm-commons-6.2.jar",
25 "https://repo1.maven.org/maven2/org/ow2/asm/asm-tree/6.2/asm-tree-6.2.jar",
26 "https://libraries.minecraft.net/net/minecraft/launchwrapper/1.12/launchwrapper-1.12.jar"
29 private static final ThrowableConsumer<URL> addURL;
32 VANILLA_SERVER_JARS.put("1.13.2", "https://launcher.mojang.com/v1/objects/3737db93722a9e39eeada7c27e7aca28b144ffa7/server.jar");
35 Method method = URLClassLoader.class
36 .getDeclaredMethod("addURL", URL.class);
37 method.setAccessible(true);
38 addURL = url -> method.invoke(ClassLoader.getSystemClassLoader(), url);
39 } catch (ReflectiveOperationException e) {
40 throw new AssertionError(e);
44 private static void addURLToClasspath(File file) throws MalformedURLException {
45 addURL.accept(file.toURI().toURL());
48 public static void run(String version, List<String> args) throws IOException {
49 String serverJarUrl = VANILLA_SERVER_JARS.get(version);
51 addURLToClasspath(getOrDownload(new File("."), serverJarUrl));
52 for (String url : LIBRARIES) {
53 addURLToClasspath(getOrDownload(new File("libs"), url));
56 args = new ArrayList<>(args);
57 args.add("--tweakClass");
58 args.add("com.irtimaled.bbor.launch.ServerTweaker");
60 System.out.println("Launching server...");
61 Launch.main(args.toArray(new String[0]));
64 private static File getOrDownload(File directory, String url) throws IOException {
65 String fileName = url.substring(url.lastIndexOf('/') + 1);
66 File target = new File(directory, fileName);
67 if (target.isFile()) {
70 target.getParentFile().mkdirs();
72 System.out.println("Downloading library: " + url);
73 new FileOutputStream(target).getChannel()
74 .transferFrom(Channels.newChannel(new URL(url).openStream()), 0, Long.MAX_VALUE);