Creating an update checker that checks for updates

Jul 23, 2019
Creating an update checker that checks for updates
  • Creating an update checker, that checks for updates
    This is a simple tutorial on how to create an update checker for your plugin.
    There is only one requirement: Your resource must be uploaded on SpigotMC.org.

    Step 1 - Getting your resource's Id
    At first, you need your resource's Id which can you find in your resource's URL.
    This is a resource URL - Your resources URL should look similar.

    Step 2 - Getting the spigot API Link
    This is very easy. This is the Spigot version API link
    Just replace the Id at the end with your resource's Id!

    Step 3 - Creating the update checker
    In order to do this, create a new Thread with a name like updateChecker
    that it looks like that:
    Code (Java):
    Thread updateChecker = new Thread() {
        @Override
        public void run() {

        }
    };
    Then create a new URL and use the new URL constructor. Use your API URL as an argument.
    Code (Java):
    URL url = null;
    try {
        url = new URL("https://api.spigotmc.org/legacy/update.php?resource=65264");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    Now, open a new URLConnection with url.openConnection();
    Code (Java):
    URLConnection conn = null;
    try {
        conn = url.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Now you create a new BufferedReader that reads the URLConnection's inputstream.
    Also check if reader.readline(); equals your current version. If that's the case the plugin will output:

    "[Plugin] No updates available."
    if reader.readline(); NOT equals the current version it will output :
    "[Plugin] There is an update! Download it at https://www.spigotmc.org/resources/<resource's name>.<resource's id>/".
    Code (Java):
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        if (reader.readLine().equals(currentVersionString)) {
            System.out.println("[Plugin] No updates available.");
        } else {
            System.out.println("[Plugin] There is an update! Download it at https://www.spigotmc.org/resources/<resource's name>.<resource's id>/");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    Now you have to start the Thread in your onEnable method.
    Code (Java):
    updateChecker.start();
    I hope this is helpful.
  • Loading...
  • Loading...