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");
33 VANILLA_SERVER_JARS.put("1.13.1", "https://launcher.mojang.com/v1/objects/fe123682e9cb30031eae351764f653500b7396c9/server.jar");
34 VANILLA_SERVER_JARS.put("1.13", "https://launcher.mojang.com/v1/objects/d0caafb8438ebd206f99930cfaecfa6c9a13dca0/server.jar");
37 Method method = URLClassLoader.class
38 .getDeclaredMethod("addURL", URL.class);
39 method.setAccessible(true);
40 addURL = url -> method.invoke(ClassLoader.getSystemClassLoader(), url);
41 } catch (ReflectiveOperationException e) {
42 throw new AssertionError(e);
46 private static void addURLToClasspath(File file) throws MalformedURLException {
47 addURL.accept(file.toURI().toURL());
50 public static void run(String version, List<String> args) throws IOException {
51 String serverJarUrl = VANILLA_SERVER_JARS.get(version);
53 addURLToClasspath(getOrDownload(new File("."), serverJarUrl));
54 for (String url : LIBRARIES) {
55 addURLToClasspath(getOrDownload(new File("libs"), url));
58 args = new ArrayList<>(args);
59 args.add("--tweakClass");
60 args.add("com.irtimaled.bbor.launch.ServerTweaker");
62 System.out.println("Launching server...");
63 Launch.main(args.toArray(new String[0]));
66 private static File getOrDownload(File directory, String url) throws IOException {
67 String fileName = url.substring(url.lastIndexOf('/') + 1);
68 File target = new File(directory, fileName);
69 if (target.isFile()) {
72 target.getParentFile().mkdirs();
74 System.out.println("Downloading library: " + url);
75 new FileOutputStream(target).getChannel()
76 .transferFrom(Channels.newChannel(new URL(url).openStream()), 0, Long.MAX_VALUE);