Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Properties;
- import java.util.logging.Logger;
- public class PluginChecker {
- /** Minecraft's logger. */
- private static final Logger _logMC = Logger.getLogger("Minecraft");
- protected String[] getPlugins() {
- String strPlugins = etc.getLoader().getPluginList();
- if(strPlugins.length() > 0) {
- return strPlugins.split("(?i)\\s\\(.\\),?");
- } else {
- _logMC.info("No plugins currently enabled.");
- return null;
- }
- }
- public void CheckPlugins() {
- String[] plugins = getPlugins();
- if(plugins == null) return;
- for(String plugin : plugins) {
- if(plugin.length() > 0) {
- Plugin objPlugin = etc.getLoader().getPlugin(plugin);
- InputStream ioManifest = objPlugin.getClass().getResourceAsStream("/plugit.properties");
- if(ioManifest == null) continue;
- Properties manifest = new Properties();
- String remoteManifest = "";
- String version = "";
- try {
- manifest.load(ioManifest);
- version = manifest.getProperty("version");
- remoteManifest = manifest.getProperty("manifest");
- ioManifest.close();
- _logMC.info("Checking " + plugin + "(Version: " + version + ") for updates..");
- } catch(IOException e) {
- continue;
- }
- try {
- URL urlManifest = new URL(remoteManifest);
- manifest = new Properties();
- manifest.load(urlManifest.openStream());
- if(!version.equalsIgnoreCase(manifest.getProperty("version"))) {
- _logMC.info("Newer version of " + plugin + " available. Now updating..");
- URL urlDownload = new URL(manifest.getProperty("binary"));
- InputStream is = urlDownload.openStream();
- // Create temporary file.
- String tmpPath = System.getProperty("user.dir") + File.separator + "plugins" + File.separator + objPlugin.getName() + ".tmp";
- String pluginPath = System.getProperty("user.dir") + File.separator + "plugins" + File.separator + objPlugin.getName() + ".jar";
- _logMC.info("Writing new file to " + tmpPath);
- File outFile = new File(tmpPath);
- if(outFile.exists()) outFile.delete();
- outFile.createNewFile();
- FileOutputStream fos= new FileOutputStream(outFile);
- int oneChar, count=0;
- while ((oneChar=is.read()) != -1)
- {
- fos.write(oneChar);
- count++;
- }
- is.close();
- fos.close();
- _logMC.info("New file downloaded.");
- // Disable plugin.
- if(objPlugin.isEnabled() == true) objPlugin.disable();
- File pluginFile = new File(pluginPath);
- pluginFile.delete();
- outFile.renameTo(pluginFile);
- etc.getLoader().reloadPlugin(objPlugin.getName());
- } else {
- _logMC.info(plugin + " is up to date.");
- }
- } catch(MalformedURLException e) {
- e.printStackTrace();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement