Straiker123

Automatic Updater & Downloader of updates for BungeeTheAPI (Proxy version)

Aug 11th, 2021 (edited)
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. # AUTOMATIC UPDATER & DOWNLOADER OF BUNGEE-THEAPI
  2. # Our discord: https://discord.gg/8YtfC234dA
  3.  
  4. public void onLoad() {
  5.     downloadAndUpdate();
  6. }
  7.  
  8. public static void downloadAndUpdate() {
  9.     if(ProxyServer.getInstance().getPluginManager().getPlugin("BungeeTheAPI")==null) {
  10.         try {
  11.             HttpURLConnection conn = (HttpURLConnection)new URL("https://api.spiget.org/v2/resources/87143/download").openConnection();
  12.             conn.setRequestProperty("User-Agent", "DevTec-JavaClient");
  13.             conn.setRequestProperty("Accept-Encoding", "gzip");
  14.             conn.setRequestMethod("GET");
  15.             conn.setConnectTimeout(20000);
  16.             conn.setReadTimeout(20000);
  17.             conn.connect();
  18.             File f = new File("plugins/BungeeTheAPI.jar");
  19.             if(!f.exists()) {
  20.                 f.getParentFile().mkdirs();
  21.                 f.createNewFile();
  22.             }
  23.             ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
  24.             FileOutputStream fos = new FileOutputStream(f);
  25.             fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  26.             fos.close();
  27.             rbc.close();
  28.             //load TheAPI
  29.             File file = new File("plugins/BungeeTheAPI.jar");
  30.             try (JarFile jar = new JarFile(file)) {
  31.               JarEntry pdf = jar.getJarEntry("bungee.yml");
  32.               if (pdf == null)
  33.                 pdf = jar.getJarEntry("plugin.yml");
  34.               try (InputStream in = jar.getInputStream(pdf)) {
  35.                 PluginDescription desc = (PluginDescription)((Yaml)Ref.get(ProxyServer.getInstance().getPluginManager(), "yaml")).loadAs(in, PluginDescription.class);
  36.                 desc.setFile(file);
  37.                 if (!(boolean)Ref.invoke(ProxyServer.getInstance().getPluginManager(), Ref.method(PluginManager.class, "enablePlugin", Map.class, Stack.class, PluginDescription.class), new HashMap<>(), new Stack<>(), desc))
  38.                     ProxyServer.getInstance().getLogger().log(Level.WARNING, "Failed to enable {0}", desc.getName());
  39.                 else {
  40.                     Plugin plugin = ProxyServer.getInstance().getPluginManager().getPlugin("BungeeTheAPI");
  41.                     try {
  42.                         plugin.onEnable();
  43.                         ProxyServer.getInstance().getLogger().log(Level.INFO, "Enabled plugin {0} version {1} by {2}", new Object[] { plugin.getDescription().getName(), plugin.getDescription().getVersion(), plugin.getDescription().getAuthor() });
  44.                       } catch (Throwable t) {
  45.                         ProxyServer.getInstance().getLogger().log(Level.WARNING, "Exception encountered when loading plugin: " + plugin.getDescription().getName(), t);
  46.                       }
  47.                 }
  48.               }
  49.             } catch (Exception ex) {
  50.               ProxyServer.getInstance().getLogger().log(Level.WARNING, "Could not load plugin from file " + file, ex);
  51.             }
  52.         }catch(Exception e) {
  53.            
  54.         }
  55.     } //else download update if needed
  56.     else {
  57.         try {
  58.             if(new SpigotUpdateChecker(ProxyServer.getInstance().getPluginManager().getPlugin("BukkitTheAPI").getDescription().getVersion(), 87143).checkForUpdates()==Version.NEW) {
  59.                 try {
  60.                     HttpURLConnection conn = (HttpURLConnection)new URL("https://api.spiget.org/v2/resources/87143/download").openConnection();
  61.                     conn.setRequestProperty("User-Agent", "DevTec-JavaClient");
  62.                     conn.setRequestProperty("Accept-Encoding", "gzip");
  63.                     conn.setRequestMethod("GET");
  64.                     conn.setConnectTimeout(20000);
  65.                     conn.setReadTimeout(20000);
  66.                     conn.connect();
  67.                     File f = new File("plugins/update/BungeeTheAPI.jar");
  68.                     if(!f.exists()) {
  69.                         f.getParentFile().mkdirs();
  70.                         f.createNewFile();
  71.                     }
  72.                     ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
  73.                     FileOutputStream fos = new FileOutputStream(f);
  74.                     fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  75.                     fos.close();
  76.                     rbc.close();
  77.                 }catch(Exception e) {
  78.                    
  79.                 }
  80.             }
  81.         }catch(Exception e) {
  82.            
  83.         }
  84.     }
  85. }
  86.  
  87. public static String fromStream(InputStream stream) {
  88.     try {
  89.         BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8), 8192);
  90.         StringBuilder sb = new StringBuilder(512);
  91.         String content;
  92.         while ((content = br.readLine()) != null)
  93.             sb.append(content).append(System.lineSeparator());
  94.         br.close();
  95.         return sb.toString();
  96.     } catch (Exception e) {
  97.         return null;
  98.     }
  99. }
Add Comment
Please, Sign In to add comment