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