Creating an Update Checker that checks for updates

Mar 20, 2019
Creating an Update Checker that checks for updates
  • Creating an UpdateChecker 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
    As first, you need your resource's ID which can be found in your resource's URL.
    This is a resource URL - Your resources URL should look similar.
    Code (url (Unknown Language)):
    https://www.spigotmc.org/resources/betteradmin.65264
    In this case the ID is 65264.


    STEP 2 - Getting the spigot API Link
    This is very easy. This is an Spigot version API link
    Code (url (Unknown Language)):
    https://api.spigotmc.org/legacy/update.php?resource=65264
    Just replace the ID at the end with your resources 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 checkupdates = new Thread(){

               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) {
                       
                       
                    }
    Now, open a new URLConnection with url.openConnection();
    Code (Java):
    URLConnection conn = null;
                    try {
                        conn = url.openConnection();
                    } catch (IOException e) {
                       
                       
                    }
    Now you create a new BufferedReader that reads the URLConnection's inputstream. Also check if br.readline(); equals your current version. If that's the case the plugin will output "[Plugin]No updates aviable"
    if br.readline() NOT equals the currentversion it will output : "[Plugin]There is an update! Download it at https://www.spigotmc.org/resources/betteradmin.65264/".
    Code (Java):
                    try {
                        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                        if(br.readLine().equals(currentversionstring)) {
                            System.out.println("[Plugin]No Updates aviable");
                            update = false;
                        }else {
                            System.out.println("[Plugin]There is an update! Download it at https://www.spigotmc.org/resources/betteradmin.65264/");
                           
                        }
                    } catch (IOException e) {
                       
                       
                    }
                }
    Now you have to start the Thread in your onEnable method.
    Code (Java):
    checkupdates.start();
    I hope this is helpful.
  • Loading...
  • Loading...