mfnalex / Spigot-UpdateChecker

UpdateChecker for your Spigot plugins with only one line of code needed in your plugin!
GNU General Public License v3.0
53 stars 14 forks source link

Add lambda support to avoid having to register a listener for the event #1

Closed MrNemo64 closed 3 years ago

MrNemo64 commented 3 years ago

Some parts of the code have been changed to follow java guidelines. Mainly using getters instead of directly accesing the fields of the UpdateChecker class. The main focus of this pull request if to add a way to listen to version checks without having to register a new event listener and allowing the user to decide what to do in case of exception. The last change is making the Messages#sendLinks(Player) accept a Player[] instead of Player. This way we can send the link message to several players without having to call the method every time and avoid creating the message every time

mfnalex commented 3 years ago

Thanks! Can you maybe give an exampe on how to use it? I really have no idea about consumers :D

MrNemo64 commented 3 years ago

Here is an example of how to use it

UpdateChecker.init(this, "https://api.jeff-media.de/chestsort/latest-version.txt") // A link to a URL that contains the latest version as String
    .setDownloadLink("https://www.chestsort.de") // You can either use a custom URL or the Spigot Resource ID
    .setDonationLink("https://paypal.me/mfnalex")
    .setChangelogLink(SPIGOT_RESOURCE_ID) // Same as for the Download link: URL or Spigot Resource ID
    .setNotifyOpsOnJoin(true) // Notify OPs on Join when a new version is found (default)
    .setNotifyByPermissionOnJoin("myplugin.updatechecker") // Also notify people on join with this permission
    .setUserAgent(new UserAgentBuilder().addPluginNameAndVersion())
    .checkEveryXHours(0.5) // Check every 30 minutes
    .setOnSuccessfulCachedLatestVersion((requesters, readedVersion) -> {
        // a version checking process happend and was successful
        if(readedVersion.endsWith("b")) {
            // version ends with "b" so its a beta
            for(CommandSender requester : requesters)
                requester.sendMessage("The lastes version is a beta");
        } else {
            // has no "b" at the end so its a full release
            for(CommandSender requester : requesters)
                requester.sendMessage("The lastes version is a full release");
        }
    })
    .setOnUnsuccessfulCachedLatestVersion((requesters, ex) -> {
      // a version checking process happend and an exception was thrown
        // we need to handle this
        String message = "Somthing went wrong checking for a version\n";
        if(ex instanceof IOException) {
            message += "The error ocurred with the connection";
        } else if (ex instanceof MalformedURLException) {
            message += "The URL has an incorrect format";
        }
        for(CommandSender requester : requesters)
            requester.sendMessage(message);
    })
    .checkNow(); // And check right now