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