Advertisement
cgrunwald

Untitled

Nov 6th, 2010
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.util.Properties;
  8. import java.util.logging.Logger;
  9.  
  10. public class PluginChecker {
  11.     /** Minecraft's logger. */
  12.     private static final Logger _logMC = Logger.getLogger("Minecraft");
  13.    
  14.     protected String[] getPlugins() {
  15.         String strPlugins = etc.getLoader().getPluginList();
  16.  
  17.         if(strPlugins.length() > 0) {
  18.             return strPlugins.split("(?i)\\s\\(.\\),?");
  19.         } else {
  20.             _logMC.info("No plugins currently enabled.");
  21.             return null;
  22.         }
  23.  
  24.     }
  25.  
  26.     public void CheckPlugins() {
  27.         String[] plugins = getPlugins();
  28.         if(plugins == null) return;
  29.  
  30.         for(String plugin : plugins) {
  31.             if(plugin.length() > 0) {
  32.                 Plugin objPlugin = etc.getLoader().getPlugin(plugin);
  33.                 InputStream ioManifest = objPlugin.getClass().getResourceAsStream("/plugit.properties");
  34.                 if(ioManifest == null) continue;
  35.                 Properties manifest = new Properties();
  36.                 String remoteManifest = "";
  37.                 String version = "";
  38.                 try {
  39.                     manifest.load(ioManifest);
  40.                     version = manifest.getProperty("version");
  41.                     remoteManifest = manifest.getProperty("manifest");
  42.                     ioManifest.close();
  43.                     _logMC.info("Checking " + plugin + "(Version: " + version + ") for updates..");
  44.                 } catch(IOException e) {
  45.                     continue;
  46.                 }
  47.  
  48.                 try {
  49.                     URL urlManifest = new URL(remoteManifest);
  50.                     manifest = new Properties();
  51.                     manifest.load(urlManifest.openStream());
  52.                     if(!version.equalsIgnoreCase(manifest.getProperty("version"))) {
  53.                         _logMC.info("Newer version of " + plugin + " available. Now updating..");
  54.                         URL urlDownload = new URL(manifest.getProperty("binary"));
  55.                         InputStream is = urlDownload.openStream();
  56.  
  57.                         // Create temporary file.
  58.                         String tmpPath = System.getProperty("user.dir") + File.separator + "plugins" + File.separator + objPlugin.getName() + ".tmp";
  59.                         String pluginPath = System.getProperty("user.dir") + File.separator + "plugins" + File.separator + objPlugin.getName() + ".jar";
  60.                         _logMC.info("Writing new file to " + tmpPath);
  61.                         File outFile = new File(tmpPath);
  62.                         if(outFile.exists()) outFile.delete();
  63.                         outFile.createNewFile();
  64.  
  65.                         FileOutputStream fos= new FileOutputStream(outFile);
  66.                         int oneChar, count=0;
  67.                         while ((oneChar=is.read()) != -1)
  68.                         {
  69.                             fos.write(oneChar);
  70.                             count++;
  71.                         }
  72.                         is.close();
  73.                         fos.close();
  74.                         _logMC.info("New file downloaded.");
  75.                         // Disable plugin.
  76.                         if(objPlugin.isEnabled() == true) objPlugin.disable();
  77.                         File pluginFile = new File(pluginPath);
  78.                         pluginFile.delete();
  79.                         outFile.renameTo(pluginFile);
  80.                         etc.getLoader().reloadPlugin(objPlugin.getName());
  81.                     } else {
  82.                         _logMC.info(plugin + " is up to date.");
  83.                     }
  84.                 } catch(MalformedURLException e) {
  85.                     e.printStackTrace();
  86.                 } catch(IOException e) {
  87.                     e.printStackTrace();
  88.                 }
  89.             }
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement