From d9405a9f49d5c9ab6b4982d67187d942ac168403 Mon Sep 17 00:00:00 2001 From: Thijs Wiefferink Date: Mon, 23 Feb 2015 18:11:51 +0100 Subject: [PATCH] Make the updatechecker check async If the updatechecker cannot reach BukkitDev it would slow down the startup of the server up to 30 seconds (and also if you use /reload), this will not happen anymore. --- src/nl/evolutioncoding/areashop/AreaShop.java | 37 +++++++++++++------ .../listeners/PlayerLoginListener.java | 8 ++-- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/nl/evolutioncoding/areashop/AreaShop.java b/src/nl/evolutioncoding/areashop/AreaShop.java index 12a6515..9084344 100644 --- a/src/nl/evolutioncoding/areashop/AreaShop.java +++ b/src/nl/evolutioncoding/areashop/AreaShop.java @@ -172,18 +172,33 @@ public final class AreaShop extends JavaPlugin { // Dont initialize the updatechecker if disabled in the config if(this.getConfig().getBoolean("checkForUpdates")) { - try { - updater = new Updater(this, 76518, null, UpdateType.NO_DOWNLOAD, false); - AreaShop.debug("Result=" + updater.getResult().toString() + ", Latest=" + updater.getLatestName() + ", Type=" + updater.getLatestType()); - updateAvailable = updater.getResult() == UpdateResult.UPDATE_AVAILABLE; - if(updateAvailable) { - this.getLogger().info("Update from AreaShop V" + this.getDescription().getVersion() + " to " + updater.getLatestName() + " available, get the latest version at http://dev.bukkit.org/bukkit-plugins/regionbuyandrent/"); + new BukkitRunnable() { + @Override + public void run() { + try { + updater = new Updater(AreaShop.getInstance(), 76518, null, UpdateType.NO_DOWNLOAD, false); + AreaShop.debug("Result=" + updater.getResult().toString() + ", Latest=" + updater.getLatestName() + ", Type=" + updater.getLatestType()); + updateAvailable = updater.getResult() == UpdateResult.UPDATE_AVAILABLE; + if(updateAvailable) { + AreaShop.getInstance().getLogger().info("Update from AreaShop V" + AreaShop.getInstance().getDescription().getVersion() + " to " + updater.getLatestName() + " available, get the latest version at http://dev.bukkit.org/bukkit-plugins/regionbuyandrent/"); + new BukkitRunnable() { + @Override + public void run() { + for(Player player : Bukkit.getOnlinePlayers()) { + if(player.hasPermission("areashop.notifyupdate")) { + AreaShop.getInstance().message(player, "update-playerNotify", AreaShop.getInstance().getDescription().getVersion(), AreaShop.getInstance().getUpdater().getLatestName()); + } + } + } + }.runTask(AreaShop.getInstance()); + } + } catch(Exception e) { + AreaShop.debug("Something went wrong with the Updater:"); + AreaShop.debug(e.getMessage()); + updateAvailable = false; + } } - } catch(Exception e) { - AreaShop.debug("Something went wrong with the Updater:"); - AreaShop.debug(e.getMessage()); - updateAvailable = false; - } + }.runTaskAsynchronously(this); } } } diff --git a/src/nl/evolutioncoding/areashop/listeners/PlayerLoginListener.java b/src/nl/evolutioncoding/areashop/listeners/PlayerLoginListener.java index 872ac62..9979032 100644 --- a/src/nl/evolutioncoding/areashop/listeners/PlayerLoginListener.java +++ b/src/nl/evolutioncoding/areashop/listeners/PlayerLoginListener.java @@ -40,6 +40,10 @@ public final class PlayerLoginListener implements Listener { return; } final Player player = event.getPlayer(); + // Notify admins for plugin updates + if(plugin.updateAvailable() && player.hasPermission("areashop.notifyupdate")) { + AreaShop.getInstance().message(player, "update-playerNotify", AreaShop.getInstance().getDescription().getVersion(), AreaShop.getInstance().getUpdater().getLatestName()); + } // Schedule task to check for notifications, prevents a lag spike at login new BukkitRunnable() { @Override @@ -66,10 +70,6 @@ public final class PlayerLoginListener implements Listener { } } } - // Notify admins for plugin updates - if(plugin.updateAvailable() && player.hasPermission("areashop.notifyupdate")) { - AreaShop.getInstance().message(player, "update-playerNotify", AreaShop.getInstance().getDescription().getVersion(), AreaShop.getInstance().getUpdater().getLatestName()); - } this.cancel(); } }.runTaskTimer(plugin, 25, 25);