Solved Making an update notification

Discussion in 'Spigot Plugin Development' started by StereoN, Jul 20, 2018.

  1. Hello, I was wondering how I would do an update notification for my plugin. After some searches I've seen some people using the spigotmc API, but I don't quite know how to do it. My current try is a bit of a modified method found, but it runs into a null pointer exception when trying to fetch the version from spigot.

    Code (Text):

    public void checkUpdate() {
           
            Bukkit.getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable()
            {
                public void run()
                {
                try {
                    HttpURLConnection con = (HttpURLConnection) new URL("http://www.spigotmc.org/api/general.php").openConnection();
                    con.setDoOutput(true);
                    con.setRequestMethod("POST");
                    try (OutputStream outputStream = con.getOutputStream())
                    {
                        outputStream.write(("key=98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4&resource=" + id).getBytes("UTF-8"));
                    }
                   
                    String version;
                   
                    try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())))
                    {
                        version = bufferedReader.readLine();
                    }
               
                    String v = plugin.getConfig().getString("version");
                   
                    if (version.equals(v))
                    {
                        Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&cUpdate: &fYour version is up to date."));
                        return;
                    }
                   
                    else
                    {
                        Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&cUpdate: &fAn update is ready for you:"));
                        Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&chttps://www.spigotmc.org/resources/simple-advertising.40414/updates"));
                        return;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&cUpdate: &fThere was a problem checking the updates."));
                    return;
                }
                }
            }, 0L);
        }
     
    The part that turns null is the "version" string.
    Any ideeas for another method or how I could fix this one? Thanks :3
     
  2. Use this:

    Code (Java):
    /**
    * Created by EncryptDev
    */

    public class UpdateChecker implements Listener {

        private String url = "https://api.spigotmc.org/legacy/update.php?resource=";
        private String id = "resourceId";

        private boolean isAvailable;

        public UpdateChecker() {

        }

        public boolean isAvailable() {
            return isAvailable;
        }

        @EventHandler
        public void on(PlayerJoinEvent event) {
            if(event.getPlayer().isOp())
                if(isAvailable)
                    event.getPlayer().sendMessage("Update available message");
        }

        public void check() {
            isAvailable = checkUpdate();
        }

        private boolean checkUpdate() {
            Log.info("Check for updates...");
            try {
                String localVersion = MoreCrafting.getInstance().getDescription().getVersion();
                HttpsURLConnection connection = (HttpsURLConnection) new URL(url + id).openConnection();
                connection.setRequestMethod("GET");
                String raw = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();

                String remoteVersion;
                if(raw.contains("-")) {
                    remoteVersion = raw.split("-")[0].trim();
                } else {
                    remoteVersion = raw;
                }

                if(!localVersion.equalsIgnoreCase(remoteVersion))
                    return true;

            } catch (IOException e) {
                return false;
            }
            return false;
        }

    }
    by id set your resource id.

    Your resource id find you here:
    https://www.spigotmc.org/resources/morecrafting-5x5-crafting-system-create-your-recipes.58367/

    It's the underlined line.

    EDIT: And call in the onEnable method:
    Code (Java):
    UpdateChecker updateChecker = new UpdateChecker();
            updateChecker.check();
            getServer().getPluginManager().registerEvents(updateChecker, this);
     
    • Agree Agree x 1
  3. This is my API which checks whether someone is using the latest, outdated, or test version of your plugin.

    Code (Java):
    UpdateChecker updateChecker = new UpdateChecker(resourceId);
    Code (Java):
    private int resourceId;
    private URL resourceURL;
    private String currentVersionString;
    private String latestVersionString;
    private UpdateCheckResult updateCheckResult;

    UpdateChecker(int resourceId) {
        try {
            this.resourceId = resourceId;
            this.resourceURL = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + resourceId);
        } catch (Exception exception) {
            return;
        }

        currentVersionString = Main.getPlugin().getDescription().getVersion();
        latestVersionString = getLatestVersion();

        if (latestVersionString == null) {
            updateCheckResult = UpdateCheckResult.NO_RESULT;
            return;
        }

        int currentVersion = Integer.parseInt(currentVersionString.replace("v", "").replace(".", ""));
        int latestVersion = Integer.parseInt(getLatestVersion().replace("v", "").replace(".", ""));

        if (currentVersion < latestVersion) updateCheckResult = UpdateCheckResult.OUT_DATED;
        else if (currentVersion == latestVersion) updateCheckResult = UpdateCheckResult.UP_TO_DATE;
        else updateCheckResult = UpdateCheckResult.UNRELEASED;
    }

    public enum UpdateCheckResult {
        NO_RESULT, OUT_DATED, UP_TO_DATE, UNRELEASED,
    }

    public int getResourceId() {
        return resourceId;
    }

    public String getResourceURL() {
        return "https://www.spigotmc.org/resources/" + resourceId;
    }

    public String getCurrentVersionString() {
        return currentVersionString;
    }

    public String getLatestVersionString() {
        return latestVersionString;
    }

    public UpdateCheckResult getUpdateCheckResult() {
        return updateCheckResult;
    }

    public String getLatestVersion() {
        try {
            URLConnection urlConnection = resourceURL.openConnection();
            return new BufferedReader(new InputStreamReader(urlConnection.getInputStream())).readLine();
        } catch (Exception exception) {
            return null;
        }
    }
     
    • Agree Agree x 1
  4. Thank you, I'll try them both :3
     
    • Optimistic Optimistic x 1
  5. I know this is an old post.. but what would I replace
    String localVersion = SparkPlug.getInstance().getDescription().getVersion();
    with?