diff --git a/src/main/java/de/xite/scoreboard/main/Config.java b/src/main/java/de/xite/scoreboard/main/Config.java index 3f5b0a1..c647646 100644 --- a/src/main/java/de/xite/scoreboard/main/Config.java +++ b/src/main/java/de/xite/scoreboard/main/Config.java @@ -146,6 +146,7 @@ public static void reloadConfigs(CommandSender s) { // General config sendConfigReloadMessage(s, ChatColor.DARK_AQUA+"Reloading "+ChatColor.YELLOW+"config"+ChatColor.GRAY+"..."); Config.loadConfig(); + PowerBoard.getUpdater().loadConfig(); // Load all external plugin APIs sendConfigReloadMessage(s, ChatColor.DARK_AQUA+"Initializing "+ChatColor.YELLOW+"external plugins"+ChatColor.GRAY+"..."); diff --git a/src/main/java/de/xite/scoreboard/main/PowerBoard.java b/src/main/java/de/xite/scoreboard/main/PowerBoard.java index 691c415..f4994ac 100644 --- a/src/main/java/de/xite/scoreboard/main/PowerBoard.java +++ b/src/main/java/de/xite/scoreboard/main/PowerBoard.java @@ -48,7 +48,6 @@ public void onEnable() { // Initialize variables pl = this; instance = this; - updater = new Updater(spigotMCPluginID); tpsCalc = new TPSCalc(); rateLimitedLogger = new RateLimitedLogger(this); @@ -66,16 +65,7 @@ public void onEnable() { // Load all external plugin APIs ExternalPlugins.initializePlugins(); - - // Check for updates - Bukkit.getScheduler().runTaskAsynchronously(pl, () -> { - if(updater.isUpdateAvailable()) { - pl.getLogger().info("-> A new version (v."+updater.getLatestVersion()+") is available! Your version: "+updater.getCurrentVersion()); - pl.getLogger().info("-> Update me! :)"); - } - }); - - + initializeUpdater(); // ---- Register commands and events ---- // getCommand("pb").setExecutor(new PowerBoardCommand()); @@ -143,6 +133,25 @@ public void onDisable() { updater.downloadFile(true); } + public static void initializeUpdater() { + updater = new Updater(spigotMCPluginID); + + // Check for updates + if(updater.isUpdateCheckEnabled()) { + Bukkit.getScheduler().runTaskAsynchronously(pl, () -> { + if(updater.isUpdateAvailable()) { + pl.getLogger().info("-> A new version (v."+updater.getLatestVersion()+") is available! Your version: "+updater.getCurrentVersion()); + pl.getLogger().info("-> Update me! :)"); + }else { + pl.getLogger().info("-> You are running the latest version! :)"); + } + }); + }else { + pl.getLogger().info("-> You are running PowerBoard version "+updater.getCurrentVersion()+"." + + "You should check if there is a new version available from time to time since you've disabled the automatic update check."); + } + } + public static PowerBoard getInstance() { return instance; } diff --git a/src/main/java/de/xite/scoreboard/utils/Updater.java b/src/main/java/de/xite/scoreboard/utils/Updater.java index a47500f..d59df63 100644 --- a/src/main/java/de/xite/scoreboard/utils/Updater.java +++ b/src/main/java/de/xite/scoreboard/utils/Updater.java @@ -18,23 +18,29 @@ public class Updater { private static final String updaterPrefix = "Updater -> "; private static final PowerBoard instance = PowerBoard.getInstance(); private static final Logger logger = PowerBoard.getInstance().getLogger(); - - final private int pluginID; - private Date lastUpdated; + private static Date lastUpdated; + + private final int pluginID; private String latestVersion; private final String currentVersion; - private final boolean infoMessageEnabled; + private boolean updateCheckEnabled; + private boolean infoMessageEnabled; private boolean updateSuccessful = false; public Updater(int pluginID) { this.pluginID = pluginID; latestVersion = null; currentVersion = instance.getDescription().getVersion(); - infoMessageEnabled = instance.getConfig().getBoolean("update.notification"); + loadConfig(); } private void updateVersion() { - if(latestVersion == null || new Date().getTime() - lastUpdated.getTime() > 1000*60*60*12) { // TODO needs testing + if(lastUpdated == null) + lastUpdated = new Date(); + + if(!updateCheckEnabled) { + latestVersion = getCurrentVersion(); + }else if(latestVersion == null || new Date().getTime() - lastUpdated.getTime() > 1000*60*60*12) { lastUpdated = new Date(); try(InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + pluginID).openStream(); Scanner scanner = new Scanner(inputStream)) { if(scanner.hasNext()) { @@ -65,6 +71,10 @@ public boolean infoMessageEnabled() { return infoMessageEnabled; } + public boolean isUpdateCheckEnabled() { + return updateCheckEnabled; + } + public boolean downloadFile(boolean forceUpdate) { if(this.updateSuccessful) { logger.info("Ignoring update request. Plugin has already been updated."); @@ -156,4 +166,9 @@ public boolean downloadFile(boolean forceUpdate) { logger.info(updaterPrefix+"Update finished! To apply the new update, you have to restart your server."); return true; } + + public void loadConfig() { + updateCheckEnabled = instance.getConfig().getBoolean("update.checkForUpdates"); + infoMessageEnabled = instance.getConfig().getBoolean("update.notification"); + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index fcb8075..5181035 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -104,6 +104,7 @@ placeholder: ### Plugin Updater ### ###################### update: + checkForUpdates: true # Set to false to prevent any version checks notification: true # Send a message to all Admins on join if a new update is available autoupdater: false # With this option, the plugin will be automatically updated (No longer recommended because of some glitches)