SHsuperCM

ExcludeAPIFromNMSJar.java

Aug 20th, 2021
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.InputStream;
  4. import java.util.Enumeration;
  5. import java.util.Iterator;
  6. import java.util.Objects;
  7. import java.util.Set;
  8. import java.util.stream.Collectors;
  9. import java.util.zip.ZipEntry;
  10. import java.util.zip.ZipFile;
  11. import java.util.zip.ZipOutputStream;
  12.  
  13. public class ExcludeAPIFromNMSJar {
  14.     public static void main(String[] args) throws Exception {
  15.         if (args.length != 1) return;
  16.  
  17.         File cache = new File(args[0]);
  18.         //noinspection ConstantConditions
  19.         File patchedFile = cache.listFiles((dir, name) -> name.startsWith("patched_") && name.endsWith(".jar"))[0];
  20.         //noinspection ConstantConditions
  21.         File apiFile = cache.listFiles((dir, name) -> name.startsWith("paper-api-") && name.endsWith(".jar"))[0];
  22.         File nmsFile = new File(cache, "nms.jar");
  23.  
  24.         System.out.println("Excluding " + apiFile.getName() + " files from " + patchedFile.getName() + "..");
  25.  
  26.         int apiEntriesCount = 0, patchedEntriesCount = 0;
  27.         try (ZipFile patchedZip = new ZipFile(patchedFile);
  28.              ZipFile apiZip = new ZipFile(apiFile);
  29.              ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(nmsFile))) {
  30.             Set<String> apiEntries = apiZip.stream().map(ZipEntry::getName).collect(Collectors.toSet());
  31.             apiEntriesCount = apiEntries.size();
  32.             Iterator<? extends ZipEntry> iterator = patchedZip.entries().asIterator();
  33.  
  34.             while (iterator.hasNext()) {
  35.                 ZipEntry zipEntry = iterator.next();
  36.                 patchedEntriesCount++;
  37.                 if (apiEntries.contains(zipEntry.getName()))
  38.                     continue;
  39.  
  40.                 zos.putNextEntry(zipEntry);
  41.                 InputStream is = patchedZip.getInputStream(zipEntry);
  42.                 byte[] buf = new byte[1024];
  43.                 int len;
  44.                 while((len = is.read(buf)) > 0)
  45.                     zos.write(buf, 0, len);
  46.  
  47.                 is.close();
  48.             }
  49.         }
  50.  
  51.         System.out.println("Excluded " + (patchedEntriesCount - apiEntriesCount)  + " files from final nms jar.");
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment