Update-notifications - Simple, fast, and no requirements

Discussion in 'Resources' started by garbagemule, Jul 7, 2011.

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

    garbagemule

    Alright, the auto-updaters are all nice and dandy, but the whole overwriting a .jar that's in use freaks me out. Besides, they're extremely troublesome to set up, and until there's something more generic and streamlined available, I present this extremely simple, hassle-free, no-requirements alternative. Does it auto-update? No, but it notifies on new updates, and that's really all I personally need, because my changelogs are too big to fit inside of a chat window, and I want people to visit the thread and read up on the changes. Anyway...

    This method exploits the way the Bukkit forum threads work. All threads have a dynamic name-part, and a static id-part. The name changes whenever you update the thread title, but the id stays the same. The key thing to note here is that forums.bukkit.org/threads/<id> will redirect to forums.bukkit.org/threads/<name>.<id> - meaning you have a static reference that will always redirect to the updated thread.

    Basically, this link http://forums.bukkit.org/threads/19144/ will redirect to this link http://forums.bukkit.org/threads/fun-mobarena-v0-92-1-become-a-mob-fighting-gladiator-953.19144/. Let's put this to good use.

    PHP:
    public static void checkForUpdates(JavaPlugin plugin)
    {
        
    // Link to forum thread.
        
    String site "http://forums.bukkit.org/threads/19144/";
        
        try
        {
            
    // Make a URI of the site address.
            
    URI baseURI = new URI(site);
            
            
    // Open the connection and don't redirect.
            
    HttpURLConnection con = (HttpURLConnectionbaseURI.toURL().openConnection();
            
    con.setInstanceFollowRedirects(false);
            
            
    // Get the redirect link
            
    String header con.getHeaderField("Location");
            
            
    // If something's wrong with the connection...
            
    if (header == null)
            {
                
    System.out.println("Couldn't connect to the plugin thread.");
                return;
            }
            
            
    // Otherwise, grab the location header to get the real URI.
            
    String url = new URI(con.getHeaderField("Location")).toString();
            
            
    // If the current version is the same as the thread version.
            
    if (url.contains(plugin.getDescription().getVersion().replace(".""-")))
            {
                
    // Optional - can be used with /<command>.
                
    System.out.println("Plugin is up-to-date.");
                return;
            }
            
            
    // Otherwise, notify of new version.
            
    System.out.println("There is a new version of the plugin available!");
        }
        catch (
    Exception e)
        {
            
    e.printStackTrace();
        }
    }
    What this method does, is that it opens a connection to the forum thread link provided in the top. It cancels the redirect, but grabs the value of the "Location" header, which is the actual thread with the <name>.<id> URI. Then it compares the version in the thread title with the version in plugin.yml, and if there is a mismatch, an update notification is sent (in this case to the console).

    I use this method (slightly modified) in MobArena on player join events, if the player is an op. The feature can of course be disabled, if the notifications aren't appreciated.

    Why is it better than the auto-updaters? It's not. It doesn't auto-update, but it is fast, simple, and it doesn't require you to have a webhost with a database or anything like that. All you need is the link to your plugin thread.
     
    rmsy, dakoslug, vanZeben and 2 others like this.
Thread Status:
Not open for further replies.

Share This Page