Creating an update checker that checks for updates

Nov 4, 2021
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 are only two requirements to use this update checker:

    1. Your resource must be uploaded on SpigotMC.org.

    Step 1 - Getting your resource's id
    At first, you need your resource's id which you can find in your resource's URL.
    Your resources URL should look similar like this:
    https://www.spigotmc.org/resources/<resource's name>.<resource's id>

    Step 2 - Getting the Spigot API URL
    This is very easy. This is the Spigot API URL:
    https://api.spigotmc.org/legacy/update.php?resource=<resource's id>
    Just replace the id at the end with your resource's id!

    For example, the id of EssentialsX's resource page is 9089, meaning its API URL would be this:
    https://api.spigotmc.org/legacy/update.php?resource=9089


    Step 3 - Creating the update checker
    Add a new class to your project:
    Code (Java):

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.Plugin;

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.util.Scanner;
    import java.util.function.Consumer;

    // From: https://www.spigotmc.org/wiki/creating-an-update-checker-that-checks-for-updates
    public class UpdateChecker {

        private final JavaPlugin plugin;
        private final int resourceId;

        public UpdateChecker(JavaPlugin plugin, int resourceId) {
            this.plugin = plugin;
            this.resourceId = resourceId;
        }

        public void getVersion(final Consumer<String> consumer) {
            Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
                try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); Scanner scanner = new Scanner(inputStream)) {
                    if (scanner.hasNext()) {
                        consumer.accept(scanner.next());
                    }
                } catch (IOException exception) {
                    plugin.getLogger().info("Unable to check for updates: " + exception.getMessage());
                }
            });
        }
    }
     
    You have to pass an instance of your class which extends JavaPlugin through its constructor to use a BukkitScheduler. The BukkitScheduler is used to run the update check asynchronously, otherwise the whole server will freeze while it runs. You also have to pass the resource's id through the constructor.

    Then you can create a new instance of the update checker in your onEnable method, like this:

    Code (Java):
        @Override
        public void onEnable() {
            new UpdateChecker(this, 12345).getVersion(version -> {
                if (this.getDescription().getVersion().equals(version)) {
                    getLogger().info("There is not a new update available.");
                } else {
                    getLogger().info("There is a new update available.");
                }
            });
        }
    It looks for a resource with the id 12345 and compares its version with the version registered in the plugin.yml file. Of course, you will need to change the Resource ID in the method above.

    Make sure you are using the Java import for the Consumer class, not Bukkit's: import java.util.function.Consumer;
  • Loading...
  • Loading...