Added "CheckForUpdate" configuration option which, when enabled, will check for a new version of the plugin every 24 hours and when one is found, will display a console alert once every hour. Additionally, OPs will see an update alert each time they log in to the server.

This commit is contained in:
David Berdik 2019-12-30 13:26:06 -05:00
parent 8730c32b13
commit 4dbedad121
4 changed files with 70 additions and 0 deletions

View File

@ -82,7 +82,10 @@ public class ConfigDB {
public String HerobrineName = "Herobrine";
public String HerobrineWorldName = "world_herobrine_graveyard";
public boolean ShowInTabList = false;
public boolean CheckForUpdates = true;
public boolean newVersionFound = false;
private boolean isStartupDone = false;
public ConfigDB(Logger l) {
@ -240,6 +243,7 @@ public class ConfigDB {
config.set("config.HerobrineName", "Herobrine");
config.set("config.HerobrineWorldName", "world_herobrine_graveyard");
config.set("config.ShowInTabList", false);
config.set("config.CheckForUpdates", true);
try {
config.save(configF);
@ -331,6 +335,7 @@ public class ConfigDB {
HerobrineName = config.getString("config.HerobrineName");
HerobrineWorldName = config.getString("config.HerobrineWorldName");
ShowInTabList = config.getBoolean("config.ShowInTabList");
CheckForUpdates = this.config.getBoolean("config.CheckForUpdates");
Herobrine.HerobrineMaxHP = HerobrineHP;
Herobrine.getPluginCore().getAICore().Stop_MAIN();

View File

@ -42,6 +42,7 @@ import net.theprogrammersworld.herobrine.listeners.EntityListener;
import net.theprogrammersworld.herobrine.listeners.InventoryListener;
import net.theprogrammersworld.herobrine.listeners.PlayerListener;
import net.theprogrammersworld.herobrine.listeners.WorldListener;
import net.theprogrammersworld.herobrine.misc.UpdateScanner;
public class Herobrine extends JavaPlugin implements Listener {
@ -187,6 +188,10 @@ public class Herobrine extends JavaPlugin implements Listener {
// Support initialize
this.support = new Support();
// If the plugin configuration has updated checking turned on, start the thread responsible for performing the check.
if(configdb.CheckForUpdates)
new Thread(new UpdateScanner()).start();
}
@Override

View File

@ -88,6 +88,11 @@ public class PlayerListener implements Listener {
}
}
}, 20L); // 20L = 1 sec
// If a newer version of Herobrine is available and the player is an OP, display a message to the OP stating that a new version is available.
if(Herobrine.getPluginCore().getConfigDB().newVersionFound && event.getPlayer().isOp())
event.getPlayer().sendMessage(ChatColor.RED + "A new version of Herobrine is available. To "
+ "get it, go to www.theprogrammersworld.net/Herobrine and click \"Download\".");
}
@EventHandler

View File

@ -0,0 +1,55 @@
package net.theprogrammersworld.herobrine.misc;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.ConsoleCommandSender;
import net.theprogrammersworld.herobrine.Herobrine;
public class UpdateScanner implements Runnable {
@Override
public void run() {
// Check for a newer version of the plugin, and put the thread to sleep for 24 hours
// before the check is performed again. If a newer version is found, begin reporting
// the discovery of a newer version once every hour.
final String pluginVersionCount = "25";
ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
while(!Herobrine.getPluginCore().getConfigDB().newVersionFound) {
// Check for updates once every 24 hours.
try {
URL versionCheckURL = new URL("https://www.theprogrammersworld.net/Herobrine/latestVersion.html");
BufferedReader remoteNumberReader = new BufferedReader(new InputStreamReader(versionCheckURL.openStream()));
String remoteVersionNumber = remoteNumberReader.readLine();
if(!remoteVersionNumber.equals(pluginVersionCount)) {
// A newer version was found. Change the value of "newVersionFound", and break
// from this loop in to a loop that will display a "new version" report in the
// console once every hour.
Herobrine.getPluginCore().getConfigDB().newVersionFound = true;
break;
}
} catch (Exception e) {
console.sendMessage(ChatColor.RED + "Herobrine was unable to connect to the internet to check\n" +
"for a new version.");
}
try {
Thread.sleep(86400000);
} catch (InterruptedException e) {}
}
while(true) {
// Display a "new version" message in the console once every hour.
console.sendMessage(ChatColor.RED + "A new version of Herobrine is available.\nTo get it, " +
"go to www.theprogrammersworld.net/Herobrine and click \"Download\".");
try {
Thread.sleep(3600000);
} catch (InterruptedException e) {}
}
}
}