Updater 2.3- Easy, Safe, and Policy-Compliant auto-updating for your plugins [NEW!]

Discussion in 'Resources' started by Gravity, Aug 28, 2012.

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

    Gravity

    Updater - Version 2.3
    Updater is an easy-to-use but robust and fully policy-compliant plugin updating system. It provides plugin developers with the ability to both check for and download new updates straight from BukkitDev, and requires no web server setup to function.

    Download and Source:
    Updater is a single class file that you need to add to your plugin. Simply create a new class somewhere in your plugin named "Updater", and populate it with Updater's source code, which you can find by clicking the "Get Updater" link below. Then, go to the "How to use it" section to learn how adding one line of code to your plugin will implement Updater.

    Get Updater

    Features:

    • No more hassle! Never worry about configuring your Dropbox text files to the latest build's url, or forgetting to update external files again. Upload once to BukkitDev, and as soon as your file is approved clients will start downloading it, even if the approval comes at 4am and you're fast asleep.
    • Setup is as easy as copying a class and giving it your BukkitDev project slug. Updater will do the rest.
    • Ability to tag certain builds as non-update builds. For instance, on my Jenkins system every build is tagged with -DEV, so that people who are using it do not get switched to the official latest build, and can stay testing the pre-release. Simply edit the "noUpdateTag" array in the class to define what kind of builds should be left alone.
    • Don't hassle your users anymore. Server admins have enough on their hands, don't concern them with updates, because they just will /not/ update. From personal experience, I know that the only time I cared about a plugin update was when something broke. It's far too difficult to worry about a new file every day, but if you let Updater automatically install updates, your users will rejoice!
    • Be safe. EVERY file that Updater downloads has been approved by BukkitDev staff. Real humans go line-by-line through the code of each plugin that is approved on dev.bukkit.org, to verify it is free of any malicious code. Your user's shouldn't have to blindly accept your trust, you can instead show and prove to them that by using updater, you are keeping them secure.
    • Works with both .jar file and .zip file updates.
    -- Get Updater --


    How it works:

    - First, Updater connects to BukkitDev API and requests information about your project.

    - It then searches the information for the latest file, and obtains information about it like its name and version number.

    - Optionally, Updater will run a version check, comparing the newest file with the plugin's current version. NOTE: For this to work, your file titles must be named in this format: 'PluginName vVersionNumber', such as 'AntiCheat v1.0' (or simply 'v1.0', the name is not needed, but suggested). Here's a screenshot of how this should look, if done properly:
    File titles with proper version numbers (open)

    [​IMG]

    - Assuming that an update is needed, Updater will download the file from dev.bukkit.org and store it in the update folder. This is a folder defined in the bukkit.yml file where any stored jars will be switched with its currently-in-use counterpart when the system is reloaded or restarted. This means that the user does not need to worry about replacing the downloaded file with the current file; it's all done in the background.

    How to use it:

    If you are using Maven to manage your project you can use my Maven repository to get the dependency. To do this, edit your pom.xml to add the following repository:
    Code:
        <repositories>
            <repository>
                <id>gravity-repo</id>
                <url>http://repo.gravitydevelopment.net</url>
            </repository>
        </repositories>
    
    Then, add the following dependency:
    Code:
        <dependencies>
            <dependency>
                <groupId>net.gravitydevelopment.updater</groupId>
                <artifactId>updater</artifactId>
                <version>2.1</version>
            </dependency>
        </dependencies>
    
    Otherwise, you can use the traditional way and download the source code for Updater. Simply place this somewhere within your plugin's packages, and then switch over to your main class to get to work.

    As with most of my projects, I boast the fact that you only need one line of code added to your main class (the one that extends JavaPlugin) to make this work (along with my Updater class, of course), so here's what it is:

    Code:
    Updater updater = new Updater(this, id, this.getFile(), Updater.UpdateType.DEFAULT, false);
    That's it! This single line of code will literally keep the user updated for the rest of their life. Here's a breakdown of what all these values are:

    1) "this" - The plugin instance. I suggest using this in your onEnable() method, so that you can properly issue the 'this' keyword. Other methods that are called before onEnable() will not work (but anything after it, or that is called BY onEnable() does work).

    2) "id" - This is how Updater finds your project on BukkitDev. If you don't know what this is, follow the instructions on this wiki article.

    3) "this.getFile()" - The plugin's file, this is so that Updater can properly replace your plugin with the update when it is downloaded. Note that this is a protected value, and so it can only be accessed within your plugin's main class

    4) "Updater.UpdateType.DEFAULT" - This allows you to choose which type of update you would like to take place. Currently there are 3 options:
    - DEFAULT - Typically what you would want. Do an update check, and then if it's out of date download and install the latest update.
    - NO_VERSION_CHECK - In case you know you need (or want) to update, skip version checking and just download the latest file, regardless of any it's details.
    - NO_DOWNLOAD - In case you just want to do a version check. No files will be downloaded, but you still get information about the newest build on DBO, like it's version number and size.

    5) "false" - This is a value declaring whether you want Updater to announce the progress of the download, as it takes place. This is similar to what this output (to the console) will look like:
    Output (open)

    2012-08-29 16:30:56 [INFO] [AntiCheat] Enabling AntiCheat v1.3.6-DEV
    2012-08-29 16:30:57 [INFO] About to download a new update: AntiCheat v1.3.5
    2012-08-29 16:30:57 [INFO] Downloading update: 10% of 93738 bytes.
    2012-08-29 16:30:57 [INFO] Downloading update: 20% of 93738 bytes.
    2012-08-29 16:30:57 [INFO] Downloading update: 30% of 93738 bytes.
    2012-08-29 16:30:57 [INFO] Downloading update: 50% of 93738 bytes.
    2012-08-29 16:30:57 [INFO] Downloading update: 70% of 93738 bytes.
    2012-08-29 16:30:57 [INFO] Downloading update: 80% of 93738 bytes.
    2012-08-29 16:30:57 [INFO] Downloading update: 90% of 93738 bytes.
    2012-08-29 16:30:57 [INFO] Downloading update: 100% of 93738 bytes.
    2012-08-29 16:30:57 [INFO] Finished updating.

    If this option is true, and there is no update, there will be no output to the console.

    You can also see these values documented in JavaDocs here: http://gravitydevelopment.net/docs/updater/

    ------------------------------------------------------------------------------------------------------------------------------------
    NOTICE:
    As of Updater 2.0, a configuration file is created to allow server administrators to globally toggle updating for any plugin using this class. While this option does provide a convenient method for server admins to disable all Updater instances, Bukkit project submission guidelines still require that you make your plugin's Updater instance specifically toggleable with its own configuration option. This gives server administrators the opportunity to only disable the updating capabilities of one plugin in particular, should they choose to do so. You may read more about compliance with this policy here.
    ------------------------------------------------------------------------------------------------------------------------------------


    Expanding updater:

    Note: The following contains more advanced user information on controlling Updater. While Updater is very simple to use, it also gives a great deal of feedback and control to the developer if they want to use it. If you are just starting to develop plugins, it is recommended that you stop here and just use Updater as you have learned to use it so far. If you are an advanced user, you may continue on, but know that all of the following info is optional, and only necessary if you want to customize your experience.

    Now, of course you may want to know what the outcome of the process was, so you can inform the user or update some values in your plugin to reflect that it is now updated. This result can easily be obtained by using the "getResult()" call. This returns an UpdateResult that reflects what happened.​

    Code:
            Updater.UpdateResult result = updater.getResult();
            switch(result)
            {
                case SUCCESS:
                    // Success: The updater found an update, and has readied it to be loaded the next time the server restarts/reloads
                    break;
                case NO_UPDATE:
                    // No Update: The updater did not find an update, and nothing was downloaded.
                    break;
                case DISABLED:
                    // Won't Update: The updater was disabled in its configuration file.
                    break;
                case FAIL_DOWNLOAD:
                    // Download Failed: The updater found an update, but was unable to download it.
                    break;
                case FAIL_DBO:
                    // dev.bukkit.org Failed: For some reason, the updater was unable to contact DBO to download the file.
                    break;
                case FAIL_NOVERSION:
                    // No version found: When running the version check, the file on DBO did not contain the a version in the format 'vVersion' such as 'v1.0'.
                    break;
                case FAIL_BADID:
                    // Bad id: The id provided by the plugin running the updater was invalid and doesn't exist on DBO.
                    break;
                case FAIL_APIKEY:
                    // Bad API key: The user provided an invalid API key for the updater to use.
                    break;
                case UPDATE_AVAILABLE:
                  // There was an update found, but because you had the UpdateType set to NO_DOWNLOAD, it was not downloaded.
            }
    All these values, of course, are documented in easy-to-read HTML here: http://gravitydevelopment.net/docs/updater/

    You also may want to know information about the newest update. Some people prefer to have Updater run a version check ONLY (using UpdateType.NO_DOWNLOAD), then, if there is an update available, start notifying admins as they log in that there is a new version ready, with information like file size and version. An admin would then issue a command, and the developer would run Updater again but this time with UpdateType set to NO_VERSION_CHECK, thus downloading the newest build at the admin's request.

    We have a few methods available for you to use for this information. We already know that we can determine the outcome of the version check by calling getResult(), but here are some more methods you can call to get information about the newest file:

    - getLatestName() - Returns the name of the latest file you have uploaded to BukkitDev (Ex: "AntiCheat v1.5.9")
    - getLatestType() - Returns the type of the latest file you have uploaded to BukkitDev (Alpha, Beta, Release)
    - getLatestGameVersion() - Returns the compatible Game Version of the latest file you have uploaded to BukkitDev (Ex: "CB 1.6.2-R1.0")
    - getLatestFileLink() - Returns the link to the latest file you have uploaded.

    The scenario mentioned about would look something like this (pseudocode):

    Code:
    // In main class
    
    public static boolean update = false;
    public static String name = "";
    public static ReleaseType type = null;
    public static String version = "";
    public static String link = "";
    // You would want to make getter methods in your class, this is just for simplicity.
    
    public void onEnable()
    {
      Updater updater = new Updater(this, YOUR_ID_HERE, this.getFile(), Updater.UpdateType.NO_DOWNLOAD, false); // Start Updater but just do a version check
      update = updater.getResult() == Updater.UpdateResult.UPDATE_AVAILABLE; // Determine if there is an update ready for us
      name = updater.getLatestName(); // Get the latest name
      version = updater.getLatestGameVersion(); // Get the latest game version
      type = updater.getLatestType(); // Get the latest file's type
      link = updater.getLatestFileLink(); // Get the latest link
    }
    
    // In a listener class:
    
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event)
    {
      Player player = event.getPlayer();
      if(player.hasPermission("foo.bar") && Main.update)
      {
        player.sendMessage("An update is available: " + Main.name + ", a " + Main.type + " for " + Main.version + " available at " + Main.link);
        // Will look like - An update is available: AntiCheat v1.5.9, a release for CB 1.6.2-R0.1 available at http://media.curseforge.com/XYZ
        player.sendMessage("Type /update if you would like to automatically update.");
      }
    }
    
    // And then later in a CommandExecutor class, when they type /update:
    
    Updater updater = new Updater(this, YOUR_ID_HERE, this.getFile(), Updater.UpdateType.NO_VERSION_CHECK, true); // Go straight to downloading, and announce progress to console.
    

     
    Last edited: May 6, 2016
    FisheyLP, Nathat23, Eathuis and 36 others like this.
  2. Offline

    doglover129

    I figured it out. Thank you! :)
     
  3. Offline

    ampayne2

    Added this to my plugin :)
    No errors so it looks like it is working fine, although I won't be able to know for sure until I upload v2 of my plugin :p
     
  4. Offline

    Skyshayde

    For testing, I believe you can change the version in your plugin.yml
     
  5. Offline

    ampayne2

    Gravity
    I added the updater in the latest version of my plugin, set to default update type.
    Every time I run my test server with the plugin now, it downloads it again, even though it is already at the latest version.
    My dev-bukkit page is here: http://dev.bukkit.org/server-mods/dropparty/
    Github is here: https://github.com/ampayne2/DropParty
    The version on github is v2.1, 1 version ahead of the released plugin (v2.0), but that shouldn't make a difference because I didn't modify the updater code for v2.1.

    Am I setting something up wrong, or is it supposed to download every time? I was under the impression it only did that if set to NO_VERSION_CHECK.
     
  6. Offline

    Skyshayde

    If I understand this correctly, your test server is running version 2.1, built from source, and it is downloading 2.0 from the dev bukkit each time? Does it actually update?
     
  7. Offline

    ampayne2

    No, the github code is v2.1, just to show what the updater code looks like.
    The test server is version 2.0 (downloaded from the dev bukkit page), and it is downloading 2.0 each time I restart :confused:

    EDIT: Fixed the constant updating.
    In the plugin.yml you have to have the exact same version number as the file on dev.bukkit.
    The Auto-Updater takes the version of bukkit and just removes the periods, so in my case since I had 2 in plugin.yml and 2.0 on dev.bukkit, 20 > 2 and it returns true for out of date.
     
  8. Offline

    C0nsole

    Are we required to give you credit on our plugin page for using this, Gravity ?
     
  9. Offline

    teunie75

    Gravity
    I wanted to use the download part of your class, because I already have a version check that works.
    It downloads a text file which contains the source of the bukkit page, not the most recent jar file
    Code:
        public void update(){
            BufferedInputStream input = null;
            FileOutputStream out = null;
            try{
            URL url = new URL("http://dev.bukkit.org/server-mods/diamond-counter/files.rss");
            int filelength = url.openConnection().getContentLength();
            input = new BufferedInputStream(url.openStream());
            out = new FileOutputStream(plugin.getDataFolder() + "/Diamond Counter.jar");
            byte[] data = new byte[1024];
            int count;
            long downloaded = 0;
            while ((count = input.read(data, 0, 1024)) != -1){
                downloaded += count;
                out.write(data, 0, count);
                int complete = (int) (downloaded * 100 / filelength);
                if(complete % 10 == 0){
                    plugin.logger.info("Download " + complete + "% complete!");
                }
            }
            plugin.logger.info("Done!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
     
  10. Offline

    Gravity

    teunie75
    It downloads a text file because that's what that code does.. You're trying to save an RSS feed to a jar file. The RSS file provides the link to the latest version page, which is in turn scraped to find the latest download link, which is downloaded to the plugins folder.

    It's fine if you want to use my code for that, but you should find out what each part does first. I'd also recommend that you simply do the version check and if it's out of date run Updater with the UpdateType NO_VERSION_CHECK to skip its internal version checking and go straight to file download. I built that in specifically for this purpose.
     
  11. Offline

    teunie75

    Gravity Thanks for the (quick) reply.
    It works now. :)
     
  12. Offline

    Gravity

    Nope. If you feel like it, feel free, but it's absolutely not a requirement.
     
    teunie75 and C0nsole like this.
  13. Offline

    carlgo11

    hm I get a weird error when I startup the plugin "Could not reach BukkitDev file stream for
    update checking. Is dev.bukkit.org offline?" I've followed the instructions and there's no error in the code.
     
  14. Offline

    Awesomeman2

    This is why your staff! :)
     
  15. Love love love
     
  16. Offline

    ToastHelmi

    here is an version of Gravity 's Updater
    it sends a Message to OP's and people who have permission
    you only have to add the permissions to the constructor
    i'm curently working on the /update-command but it seams a little triky...
    http://pastebin.com/u7XMtzbA
     
  17. Offline

    Gravity

    ToastHelmi you don't need to modify the Updater class to accomplish that. Simply start Updater with the UpdateType NO_DOWNLOAD, check the resulting version and do any messaging you need to if the current version is out of date, and then create a command that starts updater with the UpdateType NO_VERSION_CHECK to skip straight to updating. There's even an example in the OP :)
     
  18. Offline

    viper_monster

    Gravity
    I have just copied your Updater.class file and added
    Code:java
    1. Updater updater = new Updater(this, "slug", this.getFile(), Updater.UpdateType.DEFAULT, false);

    to the main class, but every time I run my server Im getting this message
     
  19. Offline

    Ultimate_n00b

    Replace "slug" with your bukkitdev slug.
     
  20. Offline

    viper_monster

    Ultimate_n00b, Gravity
    I have this now:
    Code:java
    1. Updater updater = new Updater(this, "[url]http://dev.bukkit.org/bukkit-plugins/soups[/url]", this.getFile(), Updater.UpdateType.DEFAULT, false);

    but it still sends
     
  21. Offline

    Gravity

    You only use the project's slug, not its entire URL. Please re-read the instructions; it's all explained thoroughly there.
     
    Ultimate_n00b likes this.
  22. Offline

    A5H73Y

    Code:
    03:23:47 [INFO] Starting minecraft server version 1.6.2
    03:23:47 [INFO] Loading properties
    03:23:47 [INFO] Default game type: SURVIVAL
    03:23:47 [INFO] Generating keypair
    03:23:49 [INFO] Starting Minecraft server on *:25565
    03:23:49 [INFO] This server is running CraftBukkit version git-Bukkit-1.5.2-R1.0
    -25-g2a13a5a-b2812jnks (MC: 1.6.2) (Implementing API version 1.6.2-R0.1-SNAPSHOT
    )
    03:23:50 [INFO] [Parkour] Loading Parkour v2.1
    03:23:50 [INFO] Preparing level "world"
    03:23:50 [INFO] Preparing start region for level 0 (Seed: 3034213158742507076)
    03:23:51 [INFO] Preparing spawn area: 62%
    03:23:51 [INFO] Preparing start region for level 1 (Seed: 99281490)
    03:23:52 [INFO] Preparing spawn area: 60%
    03:23:53 [INFO] Preparing start region for level 2 (Seed: 4585429725863546401)
    03:23:54 [INFO] Preparing spawn area: 64%
    03:23:54 [INFO] [Parkour] Enabling Parkour v2.1
    03:23:54 [INFO] [Parkour] 2.1 Enabled!
    03:23:54 [INFO] Server permissions file permissions.yml is empty, ignoring it
    03:23:54 [INFO] Done (4.552s)! For help, type "help" or "?"
    03:23:56 [SEVERE] Exception in thread "Thread-7"
    03:23:56 [SEVERE] java.lang.IndexOutOfBoundsException: index (0) must be less th
    an size (0)
    03:23:56 [SEVERE]      at com.google.common.base.Preconditions.checkElementInde
    x(Preconditions.java:301)
    03:23:56 [SEVERE]      at com.google.common.base.Preconditions.checkElementInde
    x(Preconditions.java:280)
    03:23:56 [SEVERE]      at com.google.common.collect.EmptyImmutableList.get(Empt
    yImmutableList.java:106)
    03:23:56 [SEVERE]      at me.A5H73Y.Parkour.Updater.versionCheck(Updater.java:4
    47)
    03:23:56 [SEVERE]      at me.A5H73Y.Parkour.Updater.access$3(Updater.java:419)
    03:23:56 [SEVERE]      at me.A5H73Y.Parkour.Updater$UpdateRunnable.run(Updater.
    java:577)
    03:23:56 [SEVERE]      at java.lang.Thread.run(Unknown Source)
    >
    This is the error I am getting on startup :/
    Code:
    Updater updater = new Updater(this, "parkour", this.getFile(), Updater.UpdateType.DEFAULT, false);

    This is my code.
    Please help asap as I was planning on updating my plugin tonight :)
    Thank you Gravity
     
  23. Offline

    Gravity

    A5H73Y some of your plugin's files are missing the 'v' in their version number.
     
  24. Offline

    A5H73Y

    [​IMG]
    Is this the correct setup?
     
  25. Offline

    Gravity

    I don't see anything wrong in that regard.
     
  26. Offline

    Awesomeman2

    Gravity Do i have to import it like a listener?
     
  27. Offline

    moose517

    i've been wanting to do auto updater stuff in my plugins but never got around to doing it. This is a great resource that i'll be incorporating into my plugins very soon!
     
  28. Offline

    Skyshayde

    You add that class, you need to import it if it isn't in the same package.
     
    Gravity likes this.
  29. Offline

    Gravity

    I'm not entirely sure what you mean. If you put the Updater class in another package than the class you want to use it from, then yes you'd need to import it like any other. Aside from that, there are no special requirements; just follow the instructions from the OP.
    Glad you find it useful!!
     
  30. Offline

    Awesomeman2

    Gravity Im using 1 package so would that be fine?
     
  31. Offline

    Gravity

    If the two classes are in the same package, you don't need an import statement. However, I'd really recommend that you do some more fiddling with Java basics before you try and dive into more advanced stuff; while I don't consider Updater to be very hard to use, it does rely on some basic prerequisite knowledge.
     
Thread Status:
Not open for further replies.

Share This Page