Guest User

Untitled

a guest
Nov 1st, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. package com.mikesantos.MsAutoUpdater;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedReader;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.plugin.Plugin;
  13. import org.bukkit.scheduler.BukkitRunnable;
  14.  
  15. public class AutoUpdate {
  16.  
  17.     private Plugin plugin;
  18.     private String urlVersionCheck;
  19.     private String urlDownload;
  20.     private String currentVersion;
  21.  
  22.     private boolean autoDownload;
  23.     private boolean autoShutdown;
  24.     private boolean broadcast;
  25.  
  26.     private String BroadCastMessage = "§3[AutoUpdater] §bServer Reiniciando para Atualizacao.";
  27.    
  28.     public AutoUpdate(Plugin plugin, String UrlVersion, String UrlDownload) {
  29.         this(plugin, UrlVersion, UrlDownload, true, true, true);
  30.     }
  31.  
  32.     public AutoUpdate(Plugin plugin, String UrlVersion, String UrlDownload, boolean AutoDownload,
  33.             boolean AutoShutdown, boolean Broadcast) {
  34.         this.plugin = plugin;
  35.         this.urlVersionCheck = UrlVersion;
  36.         this.urlDownload = UrlDownload;
  37.         this.currentVersion = plugin.getDescription().getVersion();
  38.         this.autoDownload = AutoDownload;
  39.         this.autoShutdown = AutoShutdown;
  40.         this.broadcast = Broadcast;
  41.         start();
  42.     }
  43.  
  44.     private void start() {
  45.         Thread th = new Thread((Runnable) new BukkitRunnable() {
  46.             @Override
  47.             public void run() {
  48.                 String v = null;
  49.                 try {
  50.                     v = getText(urlVersionCheck);
  51.                 } catch (Exception e) {
  52.                     plugin.getLogger().info("Failed to get Version!!!");
  53.                     plugin.getLogger().info("---------- Stack Trace ----------");
  54.                     e.printStackTrace();
  55.                     plugin.getLogger().info("---------- Stack Trace ----------");
  56.                 }
  57.                 if (!v.equalsIgnoreCase(currentVersion)) {
  58.                     plugin.getLogger().info("New update is Avaliable...");
  59.                     if(autoDownload)
  60.                         startDownload();
  61.                 }
  62.             }
  63.         }.runTaskTimerAsynchronously(plugin, 0L, 200L));
  64.         th.setName("Update Check");
  65.         th.start();
  66.     }
  67.  
  68.     private boolean startDownload() {
  69.         try {
  70.             URL download = new URL(this.urlDownload);
  71.             BufferedInputStream in = null;
  72.             FileOutputStream fout = null;
  73.             try {
  74.                 plugin.getLogger().info("Trying to download from: " + download.toString());
  75.                 in = new BufferedInputStream(download.openStream());
  76.                 fout = new FileOutputStream("plugins" + System.getProperty("file.separator") + plugin.getName() + ".jar");
  77.  
  78.                 final byte data[] = new byte[1024];
  79.                 int count;
  80.                 while ((count = in.read(data, 0, 1024)) != -1) {
  81.                     fout.write(data, 0, count);
  82.                 }
  83.             }catch (Exception e){
  84.                 plugin.getLogger().info("Failed to Download new Version!!!");
  85.                 plugin.getLogger().info("---------- Stack Trace ----------");
  86.                 e.printStackTrace();
  87.                 plugin.getLogger().info("---------- Stack Trace ----------");
  88.                 return false;
  89.             } finally {
  90.                 if (in != null) {
  91.                     in.close();
  92.                 }
  93.                 if (fout != null) {
  94.                     fout.close();
  95.                 }
  96.             }
  97.             plugin.getLogger().info("Succesfully downloaded update: " + download.getFile().toString());
  98.             if(this.broadcast){
  99.                 Bukkit.broadcastMessage(this.BroadCastMessage);
  100.             }
  101.             if(this.autoShutdown){
  102.                 Bukkit.shutdown();
  103.             }
  104.             return true;
  105.         } catch (IOException e) {
  106.             plugin.getLogger().info("Failed to Download new Version!!!");
  107.             plugin.getLogger().info("---------- Stack Trace ----------");
  108.             e.printStackTrace();
  109.             plugin.getLogger().info("---------- Stack Trace ----------");
  110.             return false;
  111.         }
  112.     }
  113.  
  114.     private String getText(String url) throws Exception {
  115.         URL website = new URL(url);
  116.         URLConnection connection = website.openConnection();
  117.         connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
  118.         BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  119.         StringBuilder response = new StringBuilder();
  120.         String inputLine;
  121.         while ((inputLine = in.readLine()) != null) {
  122.             response.append(inputLine);
  123.         }
  124.         in.close();
  125.         return response.toString();
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment