Advertisement
Straiker123

Automatic Updater & Downloader of updates for TheAPI (Bukkit version)

Aug 11th, 2021
1,772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. # AUTOMATIC UPDATER & DOWNLOADER OF THEAPI
  2. # Our discord: https://discord.gg/8YtfC234dA
  3.  
  4. public static void downloadAndUpdate() {
  5.     if(Bukkit.getPluginManager().getPlugin("TheAPI")==null) {
  6.         try {
  7.             HttpURLConnection conn = (HttpURLConnection)new URL("https://api.spiget.org/v2/resources/72679/download").openConnection();
  8.             conn.setRequestProperty("User-Agent", "DevTec-JavaClient");
  9.             conn.setRequestProperty("Accept-Encoding", "gzip");
  10.             conn.setRequestMethod("GET");
  11.             conn.setConnectTimeout(20000);
  12.             conn.setReadTimeout(20000);
  13.             conn.connect();
  14.             File f = new File("plugins/TheAPI.jar");
  15.             if(!f.exists()) {
  16.                 f.getParentFile().mkdirs();
  17.                 f.createNewFile();
  18.             }
  19.             ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
  20.             FileOutputStream fos = new FileOutputStream(f);
  21.             fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  22.             fos.close();
  23.             rbc.close();
  24.             //load TheAPI
  25.             File file = new File("plugins/TheAPI.jar");
  26.             Bukkit.getPluginManager().enablePlugin(Bukkit.getPluginManager().loadPlugin(file));
  27.         }catch(Exception e) {
  28.            
  29.         }
  30.     } //else download update if needed
  31.     else {
  32.         try {
  33.             if(new SpigotUpdateChecker(Bukkit.getPluginManager().getPlugin("TheAPI").getDescription().getVersion(), 72679).checkForUpdates()==Version.NEW) {
  34.                 try {
  35.                     HttpURLConnection conn = (HttpURLConnection)new URL("https://api.spiget.org/v2/resources/72679/download").openConnection();
  36.                     conn.setRequestProperty("User-Agent", "DevTec-JavaClient");
  37.                     conn.setRequestProperty("Accept-Encoding", "gzip");
  38.                     conn.setRequestMethod("GET");
  39.                     conn.setConnectTimeout(20000);
  40.                     conn.setReadTimeout(20000);
  41.                     conn.connect();
  42.                     File f = new File("plugins/update/TheAPI.jar");
  43.                     if(!f.exists()) {
  44.                         f.getParentFile().mkdirs();
  45.                         f.createNewFile();
  46.                     }
  47.                     ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
  48.                     FileOutputStream fos = new FileOutputStream(f);
  49.                     fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  50.                     fos.close();
  51.                     rbc.close();
  52.                 }catch(Exception e) {
  53.                    
  54.                 }
  55.             }
  56.         }catch(Exception e) {
  57.            
  58.         }
  59.     }
  60. }
  61.  
  62. public static String fromStream(InputStream stream) {
  63.     try {
  64.         BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8), 8192);
  65.         StringBuilder sb = new StringBuilder(512);
  66.         String content;
  67.         while ((content = br.readLine()) != null)
  68.             sb.append(content).append(System.lineSeparator());
  69.         br.close();
  70.         return sb.toString();
  71.     } catch (Exception e) {
  72.         return null;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement