]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/server/ServerRunner.java
Upgrade to 1.14.2
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / server / ServerRunner.java
1 package com.irtimaled.bbor.server;
2
3 import net.minecraft.launchwrapper.Launch;
4
5 import java.io.File;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.lang.reflect.Method;
9 import java.net.MalformedURLException;
10 import java.net.URL;
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;
16 import java.util.Map;
17
18 public class ServerRunner {
19     private static final Map<String, String> VANILLA_SERVER_JARS = new HashMap<>();
20
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"
27     };
28
29     private static final ThrowableConsumer<URL> addURL;
30
31     static {
32         VANILLA_SERVER_JARS.put("1.14.2", "https://launcher.mojang.com/v1/objects/808be3869e2ca6b62378f9f4b33c946621620019/server.jar");
33         VANILLA_SERVER_JARS.put("1.13.2", "https://launcher.mojang.com/v1/objects/3737db93722a9e39eeada7c27e7aca28b144ffa7/server.jar");
34
35         try {
36             Method method = URLClassLoader.class
37                     .getDeclaredMethod("addURL", URL.class);
38             method.setAccessible(true);
39             addURL = url -> method.invoke(ClassLoader.getSystemClassLoader(), url);
40         } catch (ReflectiveOperationException e) {
41             throw new AssertionError(e);
42         }
43     }
44
45     private static void addURLToClasspath(File file) throws MalformedURLException {
46         addURL.accept(file.toURI().toURL());
47     }
48
49     public static void run(String version, List<String> args) throws IOException {
50         String serverJarUrl = VANILLA_SERVER_JARS.get(version);
51
52         addURLToClasspath(getOrDownload(new File("."), serverJarUrl));
53         for (String url : LIBRARIES) {
54             addURLToClasspath(getOrDownload(new File("libs"), url));
55         }
56
57         args = new ArrayList<>(args);
58         args.add("--tweakClass");
59         args.add("com.irtimaled.bbor.launch.ServerTweaker");
60
61         System.out.println("Launching server...");
62         Launch.main(args.toArray(new String[0]));
63     }
64
65     private static File getOrDownload(File directory, String url) throws IOException {
66         String fileName = url.substring(url.lastIndexOf('/') + 1);
67         File target = new File(directory, fileName);
68         if (target.isFile()) {
69             return target;
70         }
71         target.getParentFile().mkdirs();
72
73         System.out.println("Downloading library: " + url);
74         new FileOutputStream(target).getChannel()
75                 .transferFrom(Channels.newChannel(new URL(url).openStream()), 0, Long.MAX_VALUE);
76
77         return target;
78     }
79 }