Vinetos

Updater | Bukkit Dev' (#60)

Aug 10th, 2016
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.74 KB | None | 0 0
  1. package fr.vinetos.test;
  2.  
  3. import org.bukkit.configuration.InvalidConfigurationException;
  4. import org.bukkit.configuration.file.YamlConfiguration;
  5. import org.bukkit.plugin.Plugin;
  6. import org.json.simple.JSONObject;
  7. import org.json.simple.parser.JSONParser;
  8. import org.json.simple.parser.ParseException;
  9.  
  10. import java.io.BufferedReader;
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.io.InputStreamReader;
  14. import java.net.HttpURLConnection;
  15. import java.net.URL;
  16. /**
  17.  * @author inventivetalent ( https://inventivetalent.org/ )
  18.  */
  19. public class SpigotUpdater extends Thread {
  20.     private final Plugin plugin;
  21.     private final int id;
  22.     private final boolean log;
  23.     private boolean enabled;
  24.     private URL url;
  25.  
  26.     public SpigotUpdater(Plugin plugin, int resourceID) throws IOException {
  27.         this(plugin, resourceID, true);
  28.     }
  29.  
  30.     public SpigotUpdater(Plugin plugin, int resourceID, boolean log) throws IOException {
  31.         this.enabled = true;
  32.         if(plugin == null) {
  33.             throw new IllegalArgumentException("Plugin cannot be null");
  34.         } else if(resourceID == 0) {
  35.             throw new IllegalArgumentException("Resource ID cannot be null (0)");
  36.         } else {
  37.             this.plugin = plugin;
  38.             this.id = resourceID;
  39.             this.log = log;
  40.             this.url = new URL("https://api.inventivetalent.org/spigot/resource-simple/" + resourceID);
  41.             File configDir = new File(plugin.getDataFolder().getParentFile(), "SpigotUpdates");
  42.             File config = new File(configDir, "config.yml");
  43.             YamlConfiguration yamlConfig = new YamlConfiguration();
  44.             if(!configDir.exists()) {
  45.                 configDir.mkdirs();
  46.             }
  47.  
  48.             if(!config.exists()) {
  49.                 config.createNewFile();
  50.                 yamlConfig.options().header("Configuration for the SpigotUpdate system\nit will inform you about new versions of all plugins which use this updater\n\'enabled\' specifies whether the system is enabled (affects all plugins)");
  51.                 yamlConfig.options().copyDefaults(true);
  52.                 yamlConfig.addDefault("enabled", Boolean.valueOf(true));
  53.                 yamlConfig.save(config);
  54.             }
  55.  
  56.             try {
  57.                 yamlConfig.load(config);
  58.             } catch (InvalidConfigurationException var8) {
  59.                 var8.printStackTrace();
  60.                 return;
  61.             }
  62.  
  63.             this.enabled = yamlConfig.getBoolean("enabled");
  64.             super.start();
  65.         }
  66.     }
  67.  
  68.     public synchronized void start() {
  69.     }
  70.  
  71.     public void run() {
  72.         if(this.plugin.isEnabled()) {
  73.             if(this.enabled) {
  74.                 if(this.log) {
  75.                     this.plugin.getLogger().info("[Updater] Searching for updates.");
  76.                 }
  77.  
  78.                 HttpURLConnection connection = null;
  79.  
  80.                 try {
  81.                     connection = (HttpURLConnection)this.url.openConnection();
  82.                     connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
  83.                     connection.setRequestMethod("GET");
  84.                     BufferedReader e = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  85.                     String e11 = "";
  86.  
  87.                     for(String line = null; (line = e.readLine()) != null; e11 = e11 + line) {
  88.                         ;
  89.                     }
  90.  
  91.                     e.close();
  92.                     JSONObject json = null;
  93.  
  94.                     try {
  95.                         json = (JSONObject)(new JSONParser()).parse(e11);
  96.                     } catch (ParseException var9) {
  97.                         ;
  98.                     }
  99.  
  100.                     String currentVersion = null;
  101.                     if(json != null && json.containsKey("version")) {
  102.                         String version = (String)json.get("version");
  103.                         if(version != null && !version.isEmpty()) {
  104.                             currentVersion = version;
  105.                         }
  106.                     }
  107.  
  108.                     if(currentVersion == null) {
  109.                         if(this.log) {
  110.                             this.plugin.getLogger().warning("[Updater] Invalid response received.");
  111.                             this.plugin.getLogger().warning("[updater] Either the author of this plugin has configured the updater wrong, or the API is experiencing some issues.");
  112.                         }
  113.  
  114.                         return;
  115.                     }
  116.  
  117.                     if(!currentVersion.equals(this.plugin.getDescription().getVersion())) {
  118.                         this.plugin.getLogger().info("[Updater] Found new version: " + currentVersion + "! (Your version is " + this.plugin.getDescription().getVersion() + ")");
  119.                         this.plugin.getLogger().info("[Updater] Download here: http://www.spigotmc.org/resources/" + this.id);
  120.                     } else if(this.log) {
  121.                         this.plugin.getLogger().info("[Updater] Plugin is up-to-date.");
  122.                     }
  123.                 } catch (IOException var10) {
  124.                     if(this.log) {
  125.                         if(connection != null) {
  126.                             try {
  127.                                 int e1 = connection.getResponseCode();
  128.                                 this.plugin.getLogger().warning("[Updater] API connection returned response code " + e1);
  129.                             } catch (IOException var8) {
  130.                                 ;
  131.                             }
  132.                         }
  133.  
  134.                         var10.printStackTrace();
  135.                     }
  136.                 }
  137.  
  138.             }
  139.         }
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment