]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/server/ServerRunner.java
Upgrade to 1.13.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.13.2", "https://launcher.mojang.com/v1/objects/3737db93722a9e39eeada7c27e7aca28b144ffa7/server.jar");
33
34         try {
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);
41         }
42     }
43
44     private static void addURLToClasspath(File file) throws MalformedURLException {
45         addURL.accept(file.toURI().toURL());
46     }
47
48     public static void run(String version, List<String> args) throws IOException {
49         String serverJarUrl = VANILLA_SERVER_JARS.get(version);
50
51         addURLToClasspath(getOrDownload(new File("."), serverJarUrl));
52         for (String url : LIBRARIES) {
53             addURLToClasspath(getOrDownload(new File("libs"), url));
54         }
55
56         args = new ArrayList<>(args);
57         args.add("--tweakClass");
58         args.add("com.irtimaled.bbor.launch.ServerTweaker");
59
60         System.out.println("Launching server...");
61         Launch.main(args.toArray(new String[0]));
62     }
63
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()) {
68             return target;
69         }
70         target.getParentFile().mkdirs();
71
72         System.out.println("Downloading library: " + url);
73         new FileOutputStream(target).getChannel()
74                 .transferFrom(Channels.newChannel(new URL(url).openStream()), 0, Long.MAX_VALUE);
75
76         return target;
77     }
78 }