Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add disable update check option + small updater improvements #48

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/de/xite/scoreboard/main/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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+"...");
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/de/xite/scoreboard/main/PowerBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public void onEnable() {
// Initialize variables
pl = this;
instance = this;
updater = new Updater(spigotMCPluginID);
tpsCalc = new TPSCalc();
rateLimitedLogger = new RateLimitedLogger(this);

Expand All @@ -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());
Expand Down Expand Up @@ -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;
}
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/de/xite/scoreboard/utils/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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");
}
}
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down