Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class UpdateChecker {
- private final JavaPlugin javaPlugin;
- private final String localPluginVersion;
- private String spigotPluginVersion;
- //Constants. Customize to your liking.
- private final int ID = 44876; //The ID of your resource. Can be found in the
- private final String ERR_MSG = "&cUpdate checker failed!";
- private final String UPDATE_MSG = "&fA new update is available at:&b https://www.spigotmc.org/resources/" + this.ID + "/updates";
- private final Permission UPDATE_PERM = new Permission("yourplugin.update", PermissionDefault.FALSE);
- private final boolean PERM_ONLY = true; //Whether or not to ignore notifying OPs without the above permission.
- public UpdateChecker(final JavaPlugin javaPlugin) {
- this.javaPlugin = javaPlugin;
- this.localPluginVersion = javaPlugin.getDescription().getVersion();
- }
- public void checkForUpdate() {
- //The request is executed asynchronously as to not block the main thread.
- Bukkit.getScheduler().runTaskAsynchronously(this.javaPlugin, () -> {
- //Request the current version of your plugin on SpigotMC.
- try {
- HttpsURLConnection connection = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.ID).openConnection();
- connection.setRequestMethod("GET");
- this.spigotPluginVersion = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();
- } catch (IOException e) {
- Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', this.ERR_MSG));
- e.printStackTrace();
- return;
- }
- //Check if the requested version is the same as the one in your plugin.yml.
- if (this.localPluginVersion.equals(this.spigotPluginVersion)) return;
- Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', this.UPDATE_MSG));
- //Register the PlayerJoinEvent
- Bukkit.getScheduler().runTask(this.javaPlugin, () -> Bukkit.getPluginManager().registerEvents(new Listener() {
- @EventHandler(priority = EventPriority.MONITOR)
- public void onPlayerJoin(PlayerJoinEvent event) {
- Player player = event.getPlayer();
- if (player.hasPermission(UpdateChecker.this.UPDATE_PERM) || (player.isOp() && !PERM_ONLY)) {
- player.sendMessage(ChatColor.translateAlternateColorCodes('&', UpdateChecker.this.UPDATE_MSG));
- }
- }
- }, this.javaPlugin));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement