Advertisement
Guest User

UpdateChecker

a guest
Jul 9th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. public class UpdateChecker {
  2.  
  3.     private final JavaPlugin javaPlugin;
  4.     private final String localPluginVersion;
  5.     private String spigotPluginVersion;
  6.  
  7.     //Constants. Customize to your liking.
  8.     private final int ID = 44876; //The ID of your resource. Can be found in the
  9.     private final String ERR_MSG = "&cUpdate checker failed!";
  10.     private final String UPDATE_MSG = "&fA new update is available at:&b https://www.spigotmc.org/resources/" + this.ID + "/updates";
  11.     private final Permission UPDATE_PERM = new Permission("yourplugin.update", PermissionDefault.FALSE);
  12.     private final boolean PERM_ONLY = true; //Whether or not to ignore notifying OPs without the above permission.
  13.  
  14.     public UpdateChecker(final JavaPlugin javaPlugin) {
  15.         this.javaPlugin = javaPlugin;
  16.         this.localPluginVersion = javaPlugin.getDescription().getVersion();
  17.     }
  18.  
  19.     public void checkForUpdate() {
  20.         //The request is executed asynchronously as to not block the main thread.
  21.         Bukkit.getScheduler().runTaskAsynchronously(this.javaPlugin, () -> {
  22.             //Request the current version of your plugin on SpigotMC.
  23.             try {
  24.                 HttpsURLConnection connection = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.ID).openConnection();
  25.                 connection.setRequestMethod("GET");
  26.                 this.spigotPluginVersion = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();
  27.             } catch (IOException e) {
  28.                 Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', this.ERR_MSG));
  29.                 e.printStackTrace();
  30.                 return;
  31.             }
  32.  
  33.             //Check if the requested version is the same as the one in your plugin.yml.
  34.             if (this.localPluginVersion.equals(this.spigotPluginVersion)) return;
  35.  
  36.             Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', this.UPDATE_MSG));
  37.  
  38.             //Register the PlayerJoinEvent
  39.             Bukkit.getScheduler().runTask(this.javaPlugin, () -> Bukkit.getPluginManager().registerEvents(new Listener() {
  40.                 @EventHandler(priority = EventPriority.MONITOR)
  41.                 public void onPlayerJoin(PlayerJoinEvent event) {
  42.                     Player player = event.getPlayer();
  43.                     if (player.hasPermission(UpdateChecker.this.UPDATE_PERM) || (player.isOp() && !PERM_ONLY)) {
  44.                         player.sendMessage(ChatColor.translateAlternateColorCodes('&', UpdateChecker.this.UPDATE_MSG));
  45.                     }
  46.                 }
  47.             }, this.javaPlugin));
  48.         });
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement