diff --git a/pom.xml b/pom.xml index 1427f29..f311154 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.noodles.antibot AntiBotMain - 1.2 + 1.3 jar AntiBot diff --git a/src/main/java/net/noodles/antibot/antibotmain/AntiBot.java b/src/main/java/net/noodles/antibot/antibotmain/AntiBot.java index 946e9e1..4e41d40 100644 --- a/src/main/java/net/noodles/antibot/antibotmain/AntiBot.java +++ b/src/main/java/net/noodles/antibot/antibotmain/AntiBot.java @@ -40,20 +40,17 @@ public void onEnable() { Logger.log(Logger.LogLevel.OUTLINE, "********************"); this.setEnabled(true); Logger.log(Logger.LogLevel.INFO, "Checking for updates..."); - this.checker = new UpdateChecker(this); - if (this.checker.isConnected()) { - if (this.checker.hasUpdate()) { - Logger.log(Logger.LogLevel.OUTLINE, "********************"); - Logger.log(Logger.LogLevel.WARNING, ("AntiBot is outdated!")); - Logger.log(Logger.LogLevel.WARNING, ("Newest version: " + this.checker.getLatestVersion())); - Logger.log(Logger.LogLevel.WARNING, ("Your version: " + Settings.VERSION)); - Logger.log(Logger.LogLevel.WARNING, ("Please Update Here: " + Settings.PLUGIN_URL)); - Logger.log(Logger.LogLevel.OUTLINE, "********************"); + new UpdateChecker(this, 45325).getLatestVersion(version -> { + if (this.getDescription().getVersion().equalsIgnoreCase(version)) { + Logger.log(Logger.LogLevel.SUCCESS,("AntiBot is up to date!")); } else { - Logger.log(Logger.LogLevel.SUCCESS, "AntiBot is up to date!"); - } - } - + Logger.log(Logger.LogLevel.OUTLINE, "*********************************************************************"); + Logger.log(Logger.LogLevel.WARNING,("AntiBot is outdated!")); + Logger.log(Logger.LogLevel.WARNING,("Newest version: " + version)); + Logger.log(Logger.LogLevel.WARNING,("Your version: " + Settings.VERSION)); + Logger.log(Logger.LogLevel.WARNING,("Please Update Here: " + Settings.PLUGIN_URL)); + Logger.log(Logger.LogLevel.OUTLINE, "*********************************************************************"); } + }); } @Override diff --git a/src/main/java/net/noodles/antibot/antibotmain/Utils/Settings.java b/src/main/java/net/noodles/antibot/antibotmain/Utils/Settings.java index da02320..e8f17eb 100644 --- a/src/main/java/net/noodles/antibot/antibotmain/Utils/Settings.java +++ b/src/main/java/net/noodles/antibot/antibotmain/Utils/Settings.java @@ -7,5 +7,7 @@ public class Settings { public static String DEVELOPER_URL = "https://bghddevelopment.com"; public static String PLUGIN_URL = "https://spigotmc.org/resources/45325"; public static String SUPPORT_DISCORD_URL = "https://bghddevelopment.com/discord"; - public static String VERSION = "1.1"; + public static String VERSION = "1.3"; + public static String NAME = "AntiBot"; + } diff --git a/src/main/java/net/noodles/antibot/antibotmain/Utils/UpdateChecker.java b/src/main/java/net/noodles/antibot/antibotmain/Utils/UpdateChecker.java index e3216d9..3c30e8f 100644 --- a/src/main/java/net/noodles/antibot/antibotmain/Utils/UpdateChecker.java +++ b/src/main/java/net/noodles/antibot/antibotmain/Utils/UpdateChecker.java @@ -1,49 +1,37 @@ package net.noodles.antibot.antibotmain.Utils; import net.noodles.antibot.antibotmain.AntiBot; +import org.bukkit.Bukkit; import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.util.Scanner; +import java.util.function.Consumer; public class UpdateChecker { - public AntiBot plugin; - public String version; - - - public UpdateChecker(AntiBot plugin) { + + private AntiBot plugin; + private int resourceId; + + public UpdateChecker(AntiBot plugin, int resourceId) { this.plugin = plugin; - this.version = this.getLatestVersion(); + this.resourceId = resourceId; } - - @SuppressWarnings("unused") - public String getLatestVersion() { - try { - final int resource = 45325; - final HttpURLConnection con = (HttpURLConnection)new URL("https://api.spigotmc.org/legacy/update.php?resource=45325").openConnection(); - con.setDoOutput(true); - con.setRequestMethod("POST"); - con.getOutputStream().write("key=98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4&resource=45325".getBytes("UTF-8")); - final String version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); - if (version.length() <= 7) { - return version; + + public void getLatestVersion(Consumer consumer) { + Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> { + try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); Scanner scanner = new Scanner(inputStream)) { + if (scanner.hasNext()) { + consumer.accept(scanner.next()); + } + } catch (IOException exception) { + this.plugin.getLogger().info("Cannot look for updates: " + exception.getMessage()); } - } - catch (Exception ex) { - System.out.println("---------------------------------"); - this.plugin.getLogger().info("Failed to check for a update!"); - System.out.println("---------------------------------"); - } - return null; - } - - public boolean isConnected() { - return this.version != null; - } - - public boolean hasUpdate() { - return !this.version.equals(this.plugin.getDescription().getVersion()); + }); } } diff --git a/src/main/java/net/noodles/antibot/antibotmain/Utils/UpdateJoin.java b/src/main/java/net/noodles/antibot/antibotmain/Utils/UpdateJoin.java index 6e2bf86..51602d2 100644 --- a/src/main/java/net/noodles/antibot/antibotmain/Utils/UpdateJoin.java +++ b/src/main/java/net/noodles/antibot/antibotmain/Utils/UpdateJoin.java @@ -10,12 +10,32 @@ public class UpdateJoin implements Listener { + + public UpdateJoin(AntiBot plugin) { + this.plugin = plugin; + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + public UpdateChecker checker; - private AntiBot main; + private AntiBot plugin; - public UpdateJoin(AntiBot main) { - this.main = main; - main.getServer().getPluginManager().registerEvents(this, main); + @EventHandler + public void onJoin(final PlayerJoinEvent e) { + Player p = e.getPlayer(); + if (AntiBot.plugin.getConfig().getBoolean("Update.Enabled") == true) { + if (p.hasPermission("antibot.update")) { + new UpdateChecker(plugin, 45325).getLatestVersion(version -> { + if (!AntiBot.getInstance().getDescription().getVersion().equalsIgnoreCase(version)) { + p.sendMessage(ChatColor.GRAY + "****************************************************************"); + p.sendMessage(ChatColor.RED + "AntiBot is outdated!"); + p.sendMessage(ChatColor.RED + "Newest version: " + version); + p.sendMessage(ChatColor.RED + "Your version: " + ChatColor.BOLD + Settings.VERSION); + p.sendMessage(ChatColor.GOLD + "Please Update Here: " + ChatColor.ITALIC + Settings.PLUGIN_URL); + p.sendMessage(ChatColor.GRAY + "****************************************************************"); } + }); + } + } } @@ -25,32 +45,10 @@ public void onDevJoin(PlayerJoinEvent e) { //THIS EVENT IS USED FOR DEBUG REASON if (p.getName().equals("Noodles_YT")) { p.sendMessage(ChatColor.RED + "BGHDDevelopment Debug Message"); p.sendMessage(" "); - p.sendMessage(ChatColor.GREEN + "This server is using AntiBot" + " version " + Settings.VERSION); - p.sendMessage(ChatColor.GREEN + "The newest version is " + this.checker.getLatestVersion()); + p.sendMessage(ChatColor.GREEN + "This server is using " + Settings.NAME + " version " + Settings.VERSION); p.sendMessage(" "); - - } else { - return; } } - @EventHandler - public void onJoin(PlayerJoinEvent e) { - Player p = e.getPlayer(); - if (p.hasPermission("antibot.update")) { - if (AntiBot.getPlugin().getConfig().getBoolean("Update.Enabled") == true){ - this.checker = new UpdateChecker(AntiBot.plugin); - if (this.checker.isConnected()) { - if (this.checker.hasUpdate()) { - p.sendMessage(ChatColor.GRAY + "========================="); - p.sendMessage(ChatColor.RED + "AntiBot is outdated!"); - p.sendMessage(ChatColor.GREEN + "Newest version: " + this.checker.getLatestVersion()); - p.sendMessage(ChatColor.RED + "Your version: " + Settings.VERSION); - p.sendMessage(ChatColor.GRAY + "========================="); - } - } - } - } -} } \ No newline at end of file