Simple Update Checker

Discussion in 'Resources' started by KeybordPiano459, Feb 7, 2013.

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

    KeybordPiano459

    In one of my more recent plugins, I added an update checker to easily let my players know when there's an update for my plugin. I really like this because it gives me LOTS of flexibility. What I do here is that depending on the version of the plugin, it gets a message from a URL, but this is per version. You will need to store a .txt file somewhere, and I find it easy to store on github. Also- remember that somewhere (I dunno where) terms are stated that you must have a way to disable any update checker or auto updater of some sort, which is implemented as a config option here. Here's the class that I would use if this plugin's version is 1.3:
    http://pastebin.com/c76Jmhch
    Code:java
    1. package ;
    2.  
    3. import java.io.BufferedReader;
    4. import java.io.IOException;
    5. import java.io.InputStreamReader;
    6. import java.net.URL;
    7. import java.util.logging.Logger;
    8.  
    9. public class UpdateChecker {
    10. <MAINCLASS> plugin;
    11. public UpdateChecker(<MAINCLASS> plugin) {
    12. this.plugin = plugin;
    13. currentVersion = plugin.version;
    14. }
    15.  
    16. private String currentVersion;
    17. private String readurl = "[url]https://raw.github.com/keybordpiano459/Newspaper/master/version.txt[/url]";
    18.  
    19. public void startUpdateCheck() {
    20. if (plugin.getConfig().getBoolean("update-checker")) {
    21. Logger log = plugin.getLogger();
    22. try {
    23. log.info("Checking for a new version...");
    24. URL url = new URL(readurl);
    25. BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    26. String str;
    27. while ((str = br.readLine()) != null) {
    28. String line = str;
    29. if (line.charAt(0) == '1' && line.charAt(2) == '3') {
    30. plugin.updatemsg = line.substring(5);
    31. log.info(plugin.updatemsg);
    32. }
    33. }
    34. br.close();
    35. } catch (IOException e) {
    36. log.severe("The UpdateChecker URL is invalid! Please let me know!");
    37. }
    38. }
    39. }
    40. }

    Then, you would just put a few lines in your main class:
    Code:java
    1. private UpdateChecker updatechecker;
    2. public void onEnable() {
    3. updatechecker = new UpdateChecker(this);
    4. updatechecker.startUpdateCheck();
    5. }

    Please comment if you see any bugs or... a suggestion... or... something =/ Whatever!
     
  2. Offline

    Ugleh

    You just post somewhere that the variable readurl should not have the url bbcode and that it is the forums posting that is doing it, unless of course you can edit it and fix it.
     
  3. Offline

    KeybordPiano459

    I think that people should know that you wouldn't be putting a URL BBCode tag in java, as I can't fix it, it just comes back up, but in case anyone didn't know, there aren't supposed to be the url tags.
     
  4. Offline

    teunie75

    Also for version, I used
    Code:
    currentVersion = plugin.getDescription().getVersion();
     
  5. Offline

    KeybordPiano459

    Meh, I've been getting a few bugs with that, so I just define it myself in most cases.
     
  6. is
    Code:
    [url] required to URL?
     
  7. Offline

    zack6849

     
  8. Offline

    gomeow

    You should do the update checking from DBO, not somewhere else
     
  9. Offline

    KeybordPiano459

    I believe I have another update checker using that method floating around the resources forum somewhere... But I prefer this method because it's more easily customizable.
     
  10. Offline

    gomeow

    and less likely to be approved...
     
  11. Offline

    KeybordPiano459

    How come? I've had plugins approved with this class...
     
  12. Offline

    drtshock

    gomeow this method would be approved as long as it has a config toggle.
     
  13. Offline

    TnT

    Offtopic posts removed.
     
    DreTaX and KeybordPiano459 like this.
  14. Offline

    breezeyboy

    Im sure you only need a ConfigToggle if it self-updates, Not check version?
     
  15. Offline

    drtshock

    breezeyboy likes this.
  16. Offline

    breezeyboy

    drtshock Alright, Thanks... Can I have the update checker enabled by default?
     
  17. Offline

    drtshock

    breezeyboy You can have it default to true as long as it's possible to toggle it off.
     
    breezeyboy likes this.
  18. Offline

    Minnymin3

    This should also be a bukkitrunnable so that it does not completely stop the server when it is run. Opening URLs can be very laggy with slower internet connections.
     
  19. Offline

    mickedplay

    @KeyboardPiano459 You never use currentVersion in your UpdateChecker...
     
  20. Offline

    AoH_Ruthless

    Minnymin3
    A while block would be better than a BukkitRunnable in that scenario.
     
  21. Offline

    Minnymin3

  22. Offline

    AoH_Ruthless

    Minnymin3
    I didn't notice the dates, I just saw it appear in the recent threads (I was not the original bumper), but my bad.
     
  23. Offline

    mickedplay

    Minnymin3 No, I didn't. It doesn't bother me too.
     
Thread Status:
Not open for further replies.

Share This Page