Update the update checker

This commit is contained in:
William committed 2021-09-20 17:25:49 +01:00
1 parent fd0fef0454
commit e025b7bf92
4 files changed
+81 -29

No files matched your search

@@ -16,6 +16,7 @@ import me.william278.huskhomes2.listeners.PluginMessageListener;
import me.william278.huskhomes2.migrators.UpgradeDatabase;
import me.william278.huskhomes2.teleport.SettingHandler;
import me.william278.huskhomes2.util.PlayerList;
import me.william278.huskhomes2.util.UpdateChecker;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.Bukkit;
@@ -185,9 +186,9 @@ public final class HuskHomes extends JavaPlugin {
// Load the messages (in the right language)
MessageManager.loadMessages(HuskHomes.getSettings().getLanguage());
// Check for updates (if enabled)
if (HuskHomes.getSettings().doUpdateChecks()) {
getLogger().info(getVersionCheckString());
// Check for updates via console
if (HuskHomes.getSettings().doStartupUpdateChecks()) {
new UpdateChecker(this).logToConsole();
}
// Initialize the database
@@ -5,7 +5,9 @@ import me.william278.huskhomes2.HuskHomes;
import me.william278.huskhomes2.MessageManager;
import me.william278.huskhomes2.migrators.EssentialsMigrator;
import me.william278.huskhomes2.util.ChatList;
import me.william278.huskhomes2.util.UpdateChecker;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
@@ -24,7 +26,7 @@ public class HuskHomesCommand extends CommandBase implements TabCompleter {
private static final StringBuilder PLUGIN_INFORMATION = new StringBuilder()
.append("[HuskHomes](#00fb9a bold) [| Version ").append(plugin.getDescription().getVersion()).append("](#00fb9a)\n")
.append("[").append(plugin.getDescription().getDescription()).append("](gray)\n")
.append("[• Author:](white) [William278](gray show_text=&7Click to pay a visit open_url=https://youtube.com/William27528)\n")
.append("[• Author:](white) [William278](gray show_text=&7Click to donate open_url=https://www.buymeacoffee.com/william278)\n")
.append("[• Contributors:](white) [imDaniX](gray show_text=&7Code, refactoring), [Log1x](gray show_text=&7Code)\n")
.append("[• Translators:](white) [SnivyJ](gray show_text=&7Simplified Chinese, zh-cn), [TonyPak](gray show_text=&7Traditional Chinese, zh-tw), [Villag3r_](gray show_text=&7Italian, it-it), [ReferTV](gray show_text=&7Polish, pl) \n")
.append("[• Help Wiki:](white) [[Link]](#00fb9a show_text=&7Click to open link open_url=https://github.com/WiIIiam278/HuskHomes2/wiki/)\n")
@@ -73,32 +75,29 @@ public class HuskHomesCommand extends CommandBase implements TabCompleter {
switch (args[0]) {
case "about", "info" -> {
if (sender instanceof Player p) {
if (!p.hasPermission("huskhomes.about")) {
MessageManager.sendMessage(p, "error_no_permission");
return true;
}
if (sender.hasPermission("huskhomes.about")) {
sender.spigot().sendMessage(new MineDown(PLUGIN_INFORMATION.toString()).toComponent());
} else {
sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("error_no_permission")).toComponent());
}
sender.spigot().sendMessage(new MineDown(PLUGIN_INFORMATION.toString()).toComponent());
return true;
}
case "update" -> {
if (sender instanceof Player p) {
if (p.hasPermission("huskhomes.version_checker")) {
if (!HuskHomes.getVersionCheckString().contains("HuskHomes is up to date!")) {
String updateMessage = "[Update:](dark_red) [" +
HuskHomes.getVersionCheckString() + "](red)\n" + "[Get the latest version:](gray) " +
"[[Download]](#00fb9a show_text=&7Click to visit webpage open_url=https://www.spigotmc.org/resources/huskhomes.83767/updates)";
sender.spigot().sendMessage(new MineDown(updateMessage).toComponent());
if (sender.hasPermission("huskhomes.version_checker")) {
sender.spigot().sendMessage(new MineDown("[Checking for HuskHomes updates...](gray)").toComponent());
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
UpdateChecker updateChecker = new UpdateChecker(plugin);
if (updateChecker.isUpToDate()) {
sender.spigot().sendMessage(new MineDown("[HuskHomes](#00fb9a bold) [| HuskHomes is up-to-date, running Version " + updateChecker.getLatestVersion() + "](#00fb9a)").toComponent());
} else {
sender.spigot().sendMessage(new MineDown("[HuskHomes](#00fb9a bold) &#00fb9a&| HuskHomes is up-to-date! (Version " + plugin.getDescription().getVersion() + ")").toComponent());
sender.spigot().sendMessage(
new MineDown("[HuskHomes](#00fb9a bold) [| A new update is available:](#00fb9a) [HuskHomes " + updateChecker.getLatestVersion() + "](#00fb9a bold)" +
"\n[•](white) [Currently running:](#00fb9a) [Version " + updateChecker.getCurrentVersion() + "](gray)" +
"\n[•](white) [Download links:](#00fb9a) [[⏩ Spigot]](gray open_url=https://www.spigotmc.org/resources/huskhomes.83767/updates) [•](#262626) [[⏩ Polymart]](gray open_url=https://polymart.org/resource/huskhomes.284/updates)").toComponent());
}
} else {
MessageManager.sendMessage(p, "error_no_permission");
}
});
} else {
plugin.getLogger().info(HuskHomes.getVersionCheckString());
sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("error_no_permission")).toComponent());
}
return true;
}
@@ -14,8 +14,8 @@ public class Settings {
// Message language setting
private String language;
// Automatically send update HuskHomes reminders
private boolean updateReminders;
// Automatically check for updates on startup
private boolean doStartupUpdateChecks;
// Bungee settings
private boolean doBungee;
@@ -31,7 +31,7 @@ public class Settings {
private String homesDataTable;
private String warpsDataTable;
// MySQL connection settings
// Connection settings for MySQL
private int mySQLport;
private String mySQLhost;
private String mySQLdatabase;
@@ -111,7 +111,7 @@ public class Settings {
try {
this.language = config.getString("language", "en-gb");
this.updateReminders = config.getBoolean("check_for_updates", true);
this.doStartupUpdateChecks = config.getBoolean("check_for_updates", true);
this.doBungee = config.getBoolean("bungee_options.enable_bungee_mode", false);
this.server = config.getString("bungee_options.server_id", "server");
@@ -412,8 +412,8 @@ public class Settings {
return teleportCancelledSound;
}
public boolean doUpdateChecks() {
return updateReminders;
public boolean doStartupUpdateChecks() {
return doStartupUpdateChecks;
}
public boolean doCrossServerTabCompletion() {
@@ -0,0 +1,52 @@
package me.william278.huskhomes2.util;
import me.william278.huskhomes2.HuskHomes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
public class UpdateChecker {
private final static int SPIGOT_PROJECT_ID = 83767;
private final String currentVersion;
private String latestVersion;
private final HuskHomes plugin;
public UpdateChecker(HuskHomes plugin) {
this.plugin = plugin;
this.currentVersion = this.plugin.getDescription().getVersion();
try {
final URL url = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + SPIGOT_PROJECT_ID);
URLConnection urlConnection = url.openConnection();
this.latestVersion = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())).readLine();
} catch (IOException e) {
this.plugin.getLogger().log(Level.WARNING, "An IOException occurred when trying to check for updates.");
} catch (Exception e) {
this.plugin.getLogger().log(Level.WARNING, "An exception occurred when trying to check for updates.");
}
}
public boolean isUpToDate() {
return latestVersion.equals(currentVersion);
}
public String getLatestVersion() {
return latestVersion;
}
public String getCurrentVersion() {
return currentVersion;
}
public void logToConsole() {
if (!isUpToDate()) {
plugin.getLogger().log(Level.WARNING, "A new version of HuskHomes is available: Version "
+ latestVersion + " (Currently running: " + currentVersion + ")");
}
}
}