Advertisement
SHsuperCM

SetupPaperMC

Jun 22nd, 2021
1,537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.InputStream;
  4. import java.net.URL;
  5. import java.nio.channels.Channels;
  6. import java.nio.channels.ReadableByteChannel;
  7. import java.nio.file.Files;
  8. import java.util.Enumeration;
  9. import java.util.zip.ZipEntry;
  10. import java.util.zip.ZipFile;
  11. import java.util.zip.ZipOutputStream;
  12.  
  13. public class SetupPaperMC {
  14.     public static void main(String[] args) {
  15.         try {
  16.             System.out.println("Setting up PaperMC versions...");
  17.             File cache = new File("cache");
  18.             if (cache.exists()) {
  19.                 File[] files = cache.listFiles();
  20.                 if (files != null)
  21.                     for (File file : files)
  22.                         file.delete();
  23.             }
  24.  
  25.             for (int i = 0; i < args.length; i++) {
  26.                 String[] v = args[i].split(":");
  27.                 File paperclip = new File("paper.jar");
  28.  
  29.                 paperclip.delete();
  30.  
  31.                 System.out.println("Downloading PaperMC " + args[i] + "...");
  32.  
  33.                 ReadableByteChannel rbc = Channels.newChannel(new URL("https://papermc.io/api/v2/projects/paper/versions/" + v[0] + "/builds/" + v[1] + "/downloads/paper-" + v[0] + "-" + v[1] + ".jar").openStream());
  34.                 FileOutputStream fos = new FileOutputStream(paperclip);
  35.                 fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  36.                 fos.close();
  37.                 rbc.close();
  38.  
  39.                 new ProcessBuilder("java", "-Dpaperclip.patchonly=true", "-jar", paperclip.getAbsolutePath())
  40.                         .inheritIO()
  41.                         .start()
  42.                         .waitFor();
  43.  
  44.                 System.out.println("Generating NMS...");
  45.  
  46.                 File[] jars = cache.listFiles();
  47.                 if (jars != null)
  48.                     for (File jarFile : jars) {
  49.                         if (jarFile.getName().startsWith("patched_")) {
  50.                             File nmsFile = new File(jarFile.getParentFile(), jarFile.getName().replace("patched_", "nms_"));
  51.                             if (!nmsFile.exists()) {
  52.                                 if (i == args.length - 1) {
  53.                                     Files.copy(jarFile.toPath(), nmsFile.toPath());
  54.                                 } else {
  55.                                     ZipFile jarZip = new ZipFile(jarFile);
  56.  
  57.                                     try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(nmsFile))) {
  58.                                         Enumeration<? extends ZipEntry> entries = jarZip.entries();
  59.                                         while (entries.hasMoreElements()) {
  60.                                             ZipEntry entry = entries.nextElement();
  61.  
  62.                                             if (!entry.getName().startsWith("net/minecraft/server/v") && !entry.getName().startsWith("org/bukkit/craftbukkit/v"))
  63.                                                 continue;
  64.  
  65.                                             zos.putNextEntry(entry);
  66.                                             InputStream is = jarZip.getInputStream(entry);
  67.                                             byte[] buf = new byte[1024];
  68.                                             int len;
  69.                                             while((len = is.read(buf)) > 0)
  70.                                                 zos.write(buf, 0, len);
  71.  
  72.                                             is.close();
  73.                                         }
  74.                                     }
  75.                                     jarZip.close();
  76.                                 }
  77.                                 break;
  78.                             }
  79.                         }
  80.                     }
  81.             }
  82.         } catch (Exception e) {
  83.             e.printStackTrace();
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement