Simple AutoUpdating!

Discussion in 'Resources' started by oyasunadev, Sep 29, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    oyasunadev

    Hello, forum, I just finished my auto updating class, and would like to share it to everyone! If you use this, please give me credits in your plugin, thanks!

    getLatestVersion(String site) - Get the latest version from a URL.
    site = The URL to read the contents from.
    getLatestVersion(String site) (open)
    PHP:
    public static String getLatestVersion(String site)
    {
        try {
            
    URL url = new URL(site);

            
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

            
    String str;
            while((
    str in.readLine()) != null)
            {
                return 
    str;
            }

            
    in.close();
        } catch(
    MalformedURLException e) {
            
    e.printStackTrace();
        } catch(
    IOException e) {
            
    e.printStackTrace();
        }

        return 
    null;
    }
    isUpToDate(String version, String latestVersion) - Check if the current version matches the latest.
    version = The current version that is running.
    latestVersion = The latest version.
    isUpToDate(String version
    PHP:
    public static boolean isUpToDate(String versionString latestVersion)
    {
        
    double myVersion Double.parseDouble(version);
        
    double lVersion Double.parseDouble(latestVersion);
        if(
    myVersion == lVersion)
        {
            return 
    true;
        } else {
            return 
    false;
        }
    }
    update(String site, String destination) - Preform the update.
    site = The URL to read the contents from.
    destination = The path to save the update to.
    update(String site
    PHP:
    public static void update(String siteString destination)
    {
        try
        {
            
    URL url = new URL(site);
            
    BufferedInputStream bis = new BufferedInputStream(url.openStream());
            
    FileOutputStream fos = new FileOutputStream(destination);
            
    BufferedOutputStream bos = new BufferedOutputStream(fos1024);
            
    byte data[] = new byte[1024];
            
    int x 0;
            while((
    bis.read(data01024)) >= 0)
            {
                
    bos.write(data0x);
            }
            
    bis.close();
            
    fos.close();
            
    bos.close();
        } catch(
    MalformedURLException e) {
            
    e.printStackTrace();
        } catch(
    FileNotFoundException e) {
            
    e.printStackTrace();
        } catch(
    IOException e) {
            
    e.printStackTrace();
        }
    }

    Usage
    Usage (open)
    PHP:
    if(isUpToDate(pdfFile.getVersion(), getLatestVersion("http://website.com/latest.html")))
    {
        
    System.out.println("[MyPlugin v1.0] Up to date!");
    } else {
        
    System.out.println("[MyPlugin v1.0] Update available! " getLatestVersion("http://website.com/latest.html"));
        
    update("http://website.net/MyPlugin.jar"getDataFolder().getPath() + ".jar");
        
    getServer().reload();
    }[/ 
    PHP ][/SPOILER]
     
  2. Offline

    EdenCampo

    @oyasunadev
    Hello there,
    Well I didn't understand the:
    isUpToDate(String version, String latestVersion) - Check if the current version matches the latest.
    Thingy,I'll add you to skype and can you help me there?
    Also,All that methods are in the main class?

    Eden.
     
  3. Offline

    Lolmewn

    Looks interesting..!

    So what should a "site" look like, just plain HTML which displays 4.0 for example?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  4. Offline

    oyasunadev

    yes that would be the best way to.
     
  5. Offline

    Valrix

    Almost seems like it would be best to have a PHP script that looks in a folder, finds the highest version of the plugin, if you keep older versions around, then shows the most recent version or something. I think this can be done in a more friendly format, but this is pretty cool.
     
  6. This is fine as long as the version sheming only contains X or X.Y, but what if a plugin has the version "3.1.3" or "1.0-alpha1" or ...? Then this will crash with a NumberFormatException (at Double.parseDouble(string)!
     
  7. Offline

    oyasunadev

    I didn't think about that. I will add that in the next release!
     
  8. Offline

    AinSophAur

    Would it be possible to make the plugin update itself to the next version while its running on the server?
     
  9. Offline

    dralletje

    yeah, isn't it possible to let a plugin reload himself using pluginmanager.reloadplugin(this)?
     
  10. Offline

    oyasunadev

    do whatever you want...
     
  11. Offline

    AinSophAur

    Not that I'm aware of..I've noticed there is now disablePlugin(Plugin plugin) now as well as enablePlugin(Plugin plugin) which I guess could be used to reload.
     
  12. Offline

    Rycochet

    Just want to point out that this doesn't actually work for any 0.1 type version string - 0.1 < 0.2, but 0.11 < 0.2 too - which is not what's wanted - you need to split the string by "." and parse each token as an int - then compare them in order of first to last. Any tokens missing are equivalent to a zero (if the other string has a number for that token).

    If not automatically updating you should download to "plugins/update/" (creating if it doesn't exist) then the next time the server starts it will have the more recent version (works for /reload too).

    Nice start though ;-)
     
Thread Status:
Not open for further replies.

Share This Page