Library Update checking for your Bukkit dev plugin!

Discussion in 'Resources' started by PDKnight, Aug 17, 2015.

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

    PDKnight

    Hey there,
    lots of people want to know how to enable update checking for Bukkit dev plugin, so I've decided to make a library (inspired by betterphp's Update Checking) and a quick tutorial how to use it :) So let's start!

    First, go to your package and create a new class called UpdateChecker and replace auto-generated stuff with this code:
    Code:java
    1. package /*Insert your package name here*/;
    2.  
    3. import java.io.InputStream;
    4. import java.net.MalformedURLException;
    5. import java.net.URL;
    6.  
    7. import javax.xml.parsers.DocumentBuilderFactory;
    8.  
    9. import org.w3c.dom.Document;
    10. import org.w3c.dom.Node;
    11. import org.w3c.dom.NodeList;
    12.  
    13. public class UpdateChecker {
    14. private /*Main*/ plugin;
    15. private URL filedsFeed;
    16.  
    17. private String version;
    18. private String link;
    19.  
    20. public UpdateChecker(/*Main*/ plugin, String url) {
    21. this.plugin = plugin;
    22.  
    23. try {
    24. this.filedsFeed = new URL(url);
    25. } catch (MalformedURLException e) {
    26. e.printStackTrace();
    27. }
    28. }
    29.  
    30. public boolean updateNeeded() {
    31. try {
    32. InputStream input = this.filedsFeed.openConnection()
    33. .getInputStream();
    34. Document document = DocumentBuilderFactory.newInstance()
    35. .newDocumentBuilder().parse(input);
    36.  
    37. Node latestFile = document.getElementsByTagName("item").item(0);
    38. NodeList children = latestFile.getChildNodes();
    39.  
    40. this.version = children.item(1).getTextContent()
    41. .replaceAll("[^0-9.]", "");
    42. this.link = children.item(3).getTextContent();
    43.  
    44. if(!plugin.getDescription().getVersion().equals(version)) {
    45. return true;
    46. }
    47.  
    48. } catch (Exception e) {
    49. e.printStackTrace();
    50. plugin.getLogger().info("Update checker: Something went wrong!");
    51. return false;
    52. }
    53.  
    54. return false;
    55. }
    56.  
    57. public String getVersion() {
    58. return this.version;
    59. }
    60.  
    61. public String getLink() {
    62. return this.link;
    63. }
    64. }

    or copy it from here. Then replace all /*Main*/ with your main class name and change the package in first line.

    Then you need to add this value to your config.yml file*:
    Code:
    checkForUpdates: true

    Add your UpdateChecker before onEnable:
    Code:
    protected UpdateChecker updateChecker;

    and add this code to your onEnable:
    Code:
    boolean checkForUpdates = this.getConfig().getBoolean("checkForUpdates");
    String updateCheckUrl = "http://dev.bukkit.org/bukkit-plugins/your_plugin_name(lower-cased)/files.rss";
    if (checkForUpdates) {
        this.updateChecker = new UpdateChecker(this, updateCheckUrl);
        if (this.updateChecker.updateNeeded()) {
            this.getLogger().info("New version ("
                    + this.updateChecker.getVersion()
                    + ") has been released, get it from "
                    + this.updateChecker.getLink() + "!");
        }
    }
    * As Bukkit dev policy says, we must set update checking to be configurable if we want our plugin be accepted.

    That's all! It should work :)
    Now reload the server and see the magic! :p



    If you've got any problem, just tell me :)
     
    Last edited: Aug 28, 2015
  2. Offline

    FisheyLP

    Nice :D Once I made my own updater that downloads a .txt file from dropbox, check the versions and directly download the update to /plugins/update
     
    0ct0berBkkitPlgins likes this.
  3. Offline

    PDKnight

  4. Offline

    megamichiel

    @PDKnight
    Well done :), I do have one note though:
    Code:
    boolean checkForUpdates = this.getConfig().contains("checkForUpdates") ? (this.getConfig()    .getString("checkForUpdates").equals("true") ? true : false) : false;
    can just be
    Code:
    boolean checkForUpdates = this.getConfig().getBoolean("checkForUpdates");
    Because when the config doesn't contain the value it returns false, so you don't have to make the check yourself or use a string.
     
  5. Offline

    PDKnight

    @megamichiel I know about it, but what will be in checkForUpdates when checkForUpdates in config is set to "blah"? :(
     
  6. Offline

    FisheyLP

    Then even .getBoolean() won't work because it can't cast "blah" to boolean
     
  7. Offline

    MCnumi

    Am I the only one getting errors from this?
    I am getting 2 errors, one from: if(!(plugin).getDescription().getVersion().equals(version)) {
    The error is on plugin.getDescription. where it is saying, "The method getDescription() is undefined for the type Main"
    The second one is: plugin.getLogger().info("Update checker: Something went wrong!");
    The error is on plugin.getLogger where it is saying, "The method getLogger() is undefined for the type Main"
    I have followed the directions and I still can't fix it. It is telling me that I can add the cast (Object) to plugin (the one before getLogger and getDescription
     
  8. Offline

    megamichiel

    @PDKnight
    Code:
    public boolean getBoolean(String path) {
      Object def = getDefault(path);
      return getBoolean(path, (def instanceof Boolean) ? (Boolean) def : false);
    }
    This is the code from MemorySection, if it's null or not a boolean it's always false.
     
  9. Offline

    PDKnight

    @MCnumi @megamichiel I've repaired my code, change all /*Main*/s to your main class name :)

    Cheers,
    PDKnight
     
    Last edited: Aug 18, 2015
  10. Offline

    MCnumi

    Thanks!!

    Also:

    public class /*FILENAMEHERE*\ extends JavaPlugin implements Listener {
    protected UpdateChecker updateChecker;
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
    // send them a message saying the latest update!!!
    boolean checkForUpdates = this.getConfig().getBoolean("checkForUpdates");
    String updateCheckUrl = "http://dev.bukkit.org/bukkit-plugins/your_plugin_name(lower-cased)/files.rss";
    if (checkForUpdates) {
    this.updateChecker = new UpdateChecker(this, updateCheckUrl);
    if (this.updateChecker.updateNeeded()) {
    this.getLogger().info("New version ("
    + this.updateChecker.getVersion()
    + " has been released, get it from "
    + this.updateChecker.getLink() + "!");
    make sure to make the second updateCheckUrl Spelled EXACTLY like that.
    you made URL all caps on the second one. I had to fix it.
     
    Last edited: Aug 18, 2015
  11. Offline

    PDKnight

    @MCnumi Thanks. Anyway, if you're making your plugin in Eclipse, it will tell you you that variable does not exist, but thanks though :)
     
Thread Status:
Not open for further replies.

Share This Page