From bb7dfde0f052587849f7a5086ba90de5936592b5 Mon Sep 17 00:00:00 2001 From: Mykyta Date: Mon, 11 Nov 2019 10:01:37 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Async=20update=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- src/main/java/xyz/nkomarn/Harbor/Harbor.java | 2 +- .../Harbor/listener/PlayerListener.java | 38 ++++++++++--------- .../java/xyz/nkomarn/Harbor/util/Updater.java | 6 +-- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index c1793be..ec8cb32 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 xyz.nkomarn Harbor - 1.6 + 1.6.1 UTF-8 diff --git a/src/main/java/xyz/nkomarn/Harbor/Harbor.java b/src/main/java/xyz/nkomarn/Harbor/Harbor.java index 938722c..c25dd0f 100644 --- a/src/main/java/xyz/nkomarn/Harbor/Harbor.java +++ b/src/main/java/xyz/nkomarn/Harbor/Harbor.java @@ -12,7 +12,7 @@ import xyz.nkomarn.Harbor.util.Metrics; public class Harbor extends JavaPlugin { public static Harbor instance; - public static String version = "1.6"; + public static String version = "1.6.1"; public static Essentials essentials; public void onEnable() { diff --git a/src/main/java/xyz/nkomarn/Harbor/listener/PlayerListener.java b/src/main/java/xyz/nkomarn/Harbor/listener/PlayerListener.java index 5b6e1f1..e36e13d 100644 --- a/src/main/java/xyz/nkomarn/Harbor/listener/PlayerListener.java +++ b/src/main/java/xyz/nkomarn/Harbor/listener/PlayerListener.java @@ -5,9 +5,11 @@ import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; +import xyz.nkomarn.Harbor.Harbor; import xyz.nkomarn.Harbor.util.Config; import xyz.nkomarn.Harbor.util.Updater; import java.util.concurrent.ExecutionException; @@ -19,23 +21,25 @@ public class PlayerListener implements Listener { if (!e.getPlayer().hasPermission("harbor.admin") || !Config.getBoolean("features.notifier")) return; // Check for updates - boolean updateAvailable = false; - try { - updateAvailable = Updater.check().get(); - } catch (InterruptedException | ExecutionException ex) { - ex.printStackTrace(); - } + Bukkit.getScheduler().runTaskAsynchronously(Harbor.instance, () -> { + boolean updateAvailable = false; + try { + updateAvailable = Updater.check().get(); + } catch (InterruptedException | ExecutionException ex) { + ex.printStackTrace(); + } - if (updateAvailable) { - TextComponent updateMessage = new TextComponent(ChatColor.translateAlternateColorCodes('&', - Config.getString("messages.miscellaneous.prefix") - + "&7Hey there, Harbor " + Updater.latest + " is now out!" - + " Click this message to upgrade automatically.")); - updateMessage.setColor(ChatColor.GRAY); - updateMessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, - new ComponentBuilder("§a§l↑ §7Click to update Harbor now!").create())); - updateMessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/harbor update")); - e.getPlayer().spigot().sendMessage(updateMessage); - } + if (updateAvailable) { + TextComponent updateMessage = new TextComponent(ChatColor.translateAlternateColorCodes('&', + Config.getString("messages.miscellaneous.prefix") + + "&7Hey there, Harbor " + Updater.latest + " is now out!" + + " Click this message to upgrade automatically.")); + updateMessage.setColor(ChatColor.GRAY); + updateMessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, + new ComponentBuilder("§a§l↑ §7Click to update Harbor now!").create())); + updateMessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/harbor update")); + e.getPlayer().spigot().sendMessage(updateMessage); + } + }); } } diff --git a/src/main/java/xyz/nkomarn/Harbor/util/Updater.java b/src/main/java/xyz/nkomarn/Harbor/util/Updater.java index 81c5783..a8fce7a 100644 --- a/src/main/java/xyz/nkomarn/Harbor/util/Updater.java +++ b/src/main/java/xyz/nkomarn/Harbor/util/Updater.java @@ -8,7 +8,7 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executors; +import java.util.concurrent.ForkJoinPool; import java.util.concurrent.Future; import java.util.stream.Collectors; @@ -20,7 +20,7 @@ public class Updater { public static Future check() { CompletableFuture future = new CompletableFuture<>(); - Executors.newCachedThreadPool().submit(() -> { + ForkJoinPool.commonPool().submit(() -> { try { URL latestVersion = new URL("https://api.spigotmc.org/legacy/update.php?resource=60088"); URLConnection request = latestVersion.openConnection(); @@ -43,7 +43,7 @@ public class Updater { public static Future upgrade() { CompletableFuture future = new CompletableFuture<>(); - Executors.newCachedThreadPool().submit(() -> { + ForkJoinPool.commonPool().submit(() -> { String jarName = new File(Updater.class.getProtectionDomain().getCodeSource().getLocation() .getPath()).getName();