Update Checker

Added update checker and config to enable/disable it.
This commit is contained in:
jameslfc19 2020-04-16 12:40:40 +01:00
parent 81410994cc
commit 4a33fae7b5
4 changed files with 153 additions and 0 deletions

View File

@ -6,8 +6,11 @@ import com.jamesdpeters.minecraft.chests.listeners.HopperListener;
import com.jamesdpeters.minecraft.chests.listeners.InventoryListener;
import com.jamesdpeters.minecraft.chests.serialize.InventoryStorage;
import com.jamesdpeters.minecraft.chests.serialize.LinkedChest;
import com.jamesdpeters.minecraft.chests.versionchecker.UpdateCheck;
import fr.minuskube.inv.InventoryManager;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.java.JavaPlugin;
@ -28,6 +31,8 @@ public class ChestsPlusPlus extends JavaPlugin {
int pluginId = 7166;
Metrics metrics = new Metrics(this, pluginId);
Settings.initConfig(this);
PLUGIN = this;
new RemoteChestCommand().register(this);
getServer().getPluginManager().registerEvents(new ChestLinkListener(),this);
@ -39,6 +44,29 @@ public class ChestsPlusPlus extends JavaPlugin {
INVENTORY_MANAGER = new InventoryManager(this);
INVENTORY_MANAGER.init();
if(Settings.isUpdateCheckEnabled()) {
String SPIGOT_URL = "https://www.spigotmc.org/resources/chests-chest-linking-hopper-filtering-remote-chests-menus.71355/";
UpdateCheck
.of(this)
.resourceId(71355)
.currentVersion("1.15 v1.2.2")
.handleResponse((versionResponse, version) -> {
switch (versionResponse) {
case FOUND_NEW:
getLogger().warning("New version of the plugin has been found: " + version);
getLogger().warning("Download at: https://www.spigotmc.org/resources/chests-chest-linking-hopper-filtering-remote-chests-menus.71355/");
Bukkit.broadcastMessage(ChatColor.RED + "[Chests++] New version of the plugin was found: " + version);
break;
case LATEST:
getLogger().info("Plugin is up to date! Thank you for supporting Chests++!");
break;
case UNAVAILABLE:
Bukkit.broadcastMessage("Unable to perform an update check.");
}
})
.check();
}
getLogger().info("Chests++ enabled!");
}

View File

@ -0,0 +1,44 @@
package com.jamesdpeters.minecraft.chests;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.plugin.Plugin;
import java.util.concurrent.TimeUnit;
public class Settings {
private static String CHECK_UPDATE = "update-checker";
private static Settings cf;
private FileConfiguration configuration;
private Plugin plugin;
public static void initConfig(Plugin plugin){
cf = new Settings();
cf.plugin = plugin;
cf.configuration = plugin.getConfig();
//DEFAULT VALUES
cf.configuration.addDefault(CHECK_UPDATE,true);
cf.configuration.options().copyDefaults(true);
cf.plugin.saveConfig();
}
private static void save(){
cf.plugin.saveConfig();;
}
public static void reloadConfig(){
cf.configuration = cf.plugin.getConfig();
}
/**
* GETTERS AND SETTERS
*/
public static boolean isUpdateCheckEnabled() {
return cf.configuration.getBoolean(CHECK_UPDATE);
}
}

View File

@ -0,0 +1,74 @@
package com.jamesdpeters.minecraft.chests.versionchecker;
import com.google.common.base.Preconditions;
import com.google.common.io.Resources;
import com.google.common.net.HttpHeaders;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.function.BiConsumer;
public class UpdateCheck {
private static final String SPIGOT_URL = "https://api.spigotmc.org/legacy/update.php?resource=%d";
private final JavaPlugin javaPlugin;
private String currentVersion;
private int resourceId = -1;
private BiConsumer<VersionResponse, String> versionResponse;
private UpdateCheck(JavaPlugin javaPlugin) {
this.javaPlugin = Objects.requireNonNull(javaPlugin, "javaPlugin");
this.currentVersion = javaPlugin.getDescription().getVersion();
}
public static UpdateCheck of(JavaPlugin javaPlugin) {
return new UpdateCheck(javaPlugin);
}
public UpdateCheck currentVersion(String currentVersion) {
this.currentVersion = currentVersion;
return this;
}
public UpdateCheck resourceId(int resourceId) {
this.resourceId = resourceId;
return this;
}
public UpdateCheck handleResponse(BiConsumer<VersionResponse, String> versionResponse) {
this.versionResponse = versionResponse;
return this;
}
public void check() {
Objects.requireNonNull(this.javaPlugin, "javaPlugin");
Objects.requireNonNull(this.currentVersion, "currentVersion");
Preconditions.checkState(this.resourceId != -1, "resource id not set");
Objects.requireNonNull(this.versionResponse, "versionResponse");
Bukkit.getScheduler().runTaskAsynchronously(this.javaPlugin, () -> {
try {
HttpURLConnection httpURLConnection = (HttpsURLConnection) new URL(String.format(SPIGOT_URL, this.resourceId)).openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT, "Mozilla/5.0");
String fetchedVersion = Resources.toString(httpURLConnection.getURL(), Charset.defaultCharset());
boolean latestVersion = fetchedVersion.equalsIgnoreCase(this.currentVersion);
Bukkit.getScheduler().runTask(this.javaPlugin, () -> this.versionResponse.accept(latestVersion ? VersionResponse.LATEST : VersionResponse.FOUND_NEW, latestVersion ? this.currentVersion : fetchedVersion));
} catch (IOException exception) {
exception.printStackTrace();
Bukkit.getScheduler().runTask(this.javaPlugin, () -> this.versionResponse.accept(VersionResponse.UNAVAILABLE, null));
}
});
}
}

View File

@ -0,0 +1,7 @@
package com.jamesdpeters.minecraft.chests.versionchecker;
public enum VersionResponse {
LATEST,
FOUND_NEW,
UNAVAILABLE
}