API Updater

Discussion in 'Resources' started by OMGitzFROST, Apr 26, 2023.

  1. Offline

    OMGitzFROST

    Overview
    I've been working on an updater for a while now and Im close to releasing the first official release, I'm looking for developers who maybe need a new updater or perhaps it would be your first one. Full disclaimer, this updater was inspired by this resource so it may have similarities to how it works. With that being said, I have tried to make the code part of it easier to use, and have adopted what I call our "Provider Framework", which means that our updates are handled by these providers.


    Before we get started please take a look at these resources

    Getting Started
    To get started, decide which build tool you wish to choose and copy the dependency information below.

    Maven
    Code:
    // ADD REPOSITORY
    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
    
    // ADD DEPENDENCY
    <dependencies>
        <dependency>
            <groupId>com.github.OMGitzFROST</groupId>
            <artifactId>MoleculeAPI</artifactId>
            <version>LATEST</version>
        </dependency>
    </dependencies>
    
    Gradle
    Code:
    repositories {
        maven { url 'https://jitpack.io' }
    }
    
    dependencies {
        implementation 'com.github.OMGitzFROST:MoleculeAPI:LATEST'
    }
    
    Once you have picked your preferred build tool and installed the correlating dependency information to its designated file, you should be ready to begin. Using the updater is very simple to setup so let's get started


    Implementation
    For this example we will be using EssentialsX to check for our updates, they have their plugin on most major platforms so it will work perfectly for our example

    Code:
    package your.custom.package;
    
    import com.moleculepowered.api.updater.Updater;
    import com.moleculepowered.api.updater.provider.BukkitProvider;
    import com.moleculepowered.api.updater.provider.GithubProvider;
    import com.moleculepowered.api.updater.provider.SpigetProvider;
    import com.moleculepowered.api.updater.provider.SpigotProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class ExamplePlugin extends JavaPlugin {
    
        @Override
        public void onEnable() {
    
            new Updater(this)
                    .addProvider(new GithubProvider("EssentialsX/Essentials"))
                    .addProvider(new SpigetProvider(9089))
                    .addProvider(new SpigotProvider(9089))
                    .addProvider(new BukkitProvider(93271))
                    .setEnableToggle(getConfig().getBoolean("auto-update"))
                    .setBetaToggle(getConfig().getBoolean("allow-unstable"))
                    .setPermission("your.custom.permission")
                    .setEventHandler(new ExampleHandler())
                    .setInterval("3h")
                    .schedule();
        }
    }
    
    So as you can see with this example, we have a fully customized updater with little to no extra code. And now to explain what each chain method is designed to do.

    Method Breakdown

    The #addProvider method is the bread and butter that makes this setup so powerful, each provider listed is tasked to handle the update check for their assigned platform, Please note that the order in which you add your providers, that will be the order in which we will check for new updates.

    The #setEnableToggle method has a simple function, the boolean value you give it will determine whether the updater is enabled or disabled, and setting it to a false boolean will disable the updater and vice versa.

    The #setBetaToggle method is similar to the enable toggle, except this method is geared more towards allowing beta versions to be detected by the updater and thus sending you notifications of their existence, when you set its boolean value to false, it will only notify you of stable updates.

    The #setPermission method will define the permission that players need to receive update notifications, if you want all players to receive notifications, simply remove this method from the chain or set its value to an empty string.

    The #setEventHandler method is what you can use to add custom messages to the updater, please note that you don't need to define one here as it's not required, if one is not provided by you, we will default the internal handler created by us.

    The #setInterval method should be self-explanatory, it defines the interval in which this updater will fetch new updates, and it uses a simple-to-understand format, in this example, we set the interval to "3h", 3 being the quantity and "h" being the interval type, in this case, hours. So putting it all together this interval is set to 3-hour intervals.

    The #schedule method is the final piece of the puzzle, once all your settings are set to your desired specification you will call this method to schedule the updater, alternatively, there is a #scheduleAsync method that will schedule the updater on an async thread.

    Downloads
    This updater does support downloads, it will create an Updater directory within your plugins folder, and here, you can find all updated jar files that are downloaded. To disable downloading updates, use the #setDownloadToggle within your chain methods and use the boolean parameter to define this.

    Customizing Messages
    Our notification system is built on an event system, once the updater completes its check and configures itself accordingly, the UpdateCompleteEvent will be called, it provides you with a toolbox of methods that should give you everything you need to customize your messages to your liking, but to implement your messages you must register your message handler using our #setEventHandler method (called within the updater chain) Please see below for what an example handler would look like.

    Code:
    package your.custom.package;
    
    import com.moleculepowered.api.event.updater.UpdateCompleteEvent;
    import com.moleculepowered.api.updater.UpdateResult;
    import com.moleculepowered.api.updater.provider.AbstractProvider;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.jetbrains.annotations.NotNull;
    
    public class ExampleHandler implements Listener {
    
        private AbstractProvider provider;
        @EventHandler
        public void onUpdateComplete(@NotNull UpdateCompleteEvent event) {
    
            provider = event.getProvider();
    
            // SEND CONSOLE NOTIFICATIONS WHEN THE UPDATER COMPLETES
            switch (event.getResult()) {
                case LATEST:
                    Console.info("Your custom message here");
                    break;
                case EXISTS:
                    Console.info("Your custom message here");
                    break;
                case DOWNLOADED:
                    Console.info("Your custom message here");
                    break;
                case UPDATE_AVAILABLE:
                    Console.info("Your custom message here");
                    Console.info("Example multi-line message");
                    break;
                case DISABLED:
                    Console.info("Your custom message here");
                    Console.info("Ensure the break is at the bottom when you finish your creating messages");
                    Console.info("So you don't get double messages, or bleeding messages");
                    break;
                default:
                    Console.info("Your custom message here");
            }
    
            // SEND AN NOTIFICATION TO ALL PERMITTED PLAYERS
            event.getAudience().forEach(player -> {
                player.sendMessage("Your custom Message here");
                player.sendMessage("Multi-line messages are also supported here");
              
                if (event.getResult() == UpdateResult.UPDATE_AVAILABLE) {
                    player.sendMessage("Here is an example using the update result");
                }
            });
        }
    }
    
    Hopefully, this tutorial was easy to follow and hopefully, and you'll enjoy using it, please let me know if there are any improvements you'd like to see, and remember, it's still in beta so everything will not be perfect just yet, Thanks again for reading and I hope to hear from yall soon
     
    Last edited: Apr 26, 2023
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    OMGitzFROST

    The developer can specify it if they wish, there are 2 constructors for the BukkitProvider, one only accepts the resource Id while the other also accept the id and has a param to enter their api key, if they decide to use the first constructor shown in the example the api key is simply empty


    Sent from my iPhone using Tapatalk
     

Share This Page