Improve version checker and make plugin YAML permissions description better

This commit is contained in:
AppleDash 2016-10-02 12:36:53 -04:00
parent 230d50d81b
commit ee79c639bc
4 changed files with 41 additions and 9 deletions

View File

@ -25,6 +25,7 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
private EconomyManager economyManager;
private VaultHook vaultHook;
private TransactionLogger transactionLogger;
private GithubVersionChecker versionChecker;
private final Map<String, SaneEconomyCommand> COMMANDS = new HashMap<String, SaneEconomyCommand>() {{
put("balance", new BalanceCommand(SaneEconomy.this));
@ -56,7 +57,8 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
getLogger().info("Not hooking into Vault because it isn't loaded.");
}
getServer().getScheduler().scheduleAsyncDelayedTask(this, GithubVersionChecker::checkUpdateAvailable);
versionChecker = new GithubVersionChecker("SaneEconomyCore", this.getDescription().getVersion());
getServer().getScheduler().scheduleAsyncDelayedTask(this, versionChecker::checkUpdateAvailable);
getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
economyManager.getBackend().reloadTopPlayerBalances();
@ -118,6 +120,10 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
getServer().getPluginManager().disablePlugin(this);
}
public GithubVersionChecker getVersionChecker() {
return versionChecker;
}
/**
* Get the active EconomyManager.
* @return EconomyManager

View File

@ -38,8 +38,8 @@ public class JoinQuitListener implements Listener {
}
/* Update notification */
if (player.hasPermission("saneeconomy.update-notify") && GithubVersionChecker.isUpdateAvailable()) {
MessageUtils.sendMessage(player, "An update is available! The currently-installed version is %s, but the newest available is %s. Please go to %s to update!", plugin.getDescription().getVersion(), GithubVersionChecker.getNewestVersion(), GithubVersionChecker.DOWNLOAD_URL);
if (player.hasPermission("saneeconomy.update-notify") && plugin.getVersionChecker().isUpdateAvailable()) {
MessageUtils.sendMessage(player, "An update is available! The currently-installed version is %s, but the newest available is %s. Please go to %s to update!", plugin.getDescription().getVersion(), plugin.getVersionChecker().getNewestVersion(), GithubVersionChecker.DOWNLOAD_URL);
}
}
}

View File

@ -14,11 +14,18 @@ import org.appledash.saneeconomy.utils.WebUtils;
public class GithubVersionChecker {
public static final String DOWNLOAD_URL = "https://github.com/AppleDash/SaneEconomy/releases";
private static final String RELEASES_URL = "https://api.github.com/repos/AppleDash/SaneEconomy/releases";
private static boolean updateChecked;
private static boolean updateAvailable;
private boolean updateChecked;
private boolean updateAvailable;
private static String newestVersion;
private final String pluginName;
private final String currentVersion;
public static void checkUpdateAvailable() {
public GithubVersionChecker(String pluginName, String currentVersion) {
this.pluginName = pluginName;
this.currentVersion = currentVersion;
}
public void checkUpdateAvailable() {
String jsonContent = WebUtils.getContents(RELEASES_URL);
JsonArray array = (JsonArray)new JsonParser().parse(jsonContent);
@ -36,6 +43,12 @@ public class GithubVersionChecker {
continue;
}
String releaseName = releaseObj.get("name").getAsString().split(" ")[0];
if (!releaseName.equalsIgnoreCase(pluginName)) { // Not for this plugin.
continue;
}
String versionStr = releaseObj.get("tag_name").getAsString();
int version = releaseToInt(versionStr);
@ -51,15 +64,15 @@ public class GithubVersionChecker {
updateAvailable = newestVersion > currentVersion;
}
private static int releaseToInt(String release) {
private int releaseToInt(String release) {
return Integer.valueOf(release.trim().replace(".", ""));
}
public static boolean isUpdateAvailable() {
public boolean isUpdateAvailable() {
return updateChecked && updateAvailable;
}
public static String getNewestVersion() {
public String getNewestVersion() {
return newestVersion;
}
}

View File

@ -49,3 +49,16 @@ permissions:
saneeconomy.balancetop:
description: Allows you to view the players on the server who have the most money.
default: true
saneeconomy.update-notify:
description: Allows you to be notified of updates to the plugin on join.
default: op
saneeconomy.*:
children:
saneeconomy.balance: true
saneeconomy.balance.other: true
saneeconomy.ecoadmin: true
saneeconomy.pay: true
saneeconomy.admin: true
saneeconomy.balancetop: true
saneeconomy.update-notify: true
default: op