From c7e10a88b684666f694bf578dcdf98e295b64c6d Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 26 Apr 2016 20:33:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0SpigotMC=E5=92=8CBukk?= =?UTF-8?q?itDev=E6=9B=B4=E6=96=B0API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/pw/yumc/Yum/ext/BukkitUpdater.java | 428 ++++++++++++++++++ .../java/pw/yumc/Yum/ext/SpigotUpdater.java | 119 +++++ src/main/java/pw/yumc/Yum/ext/Updater.java | 298 ++++++++++++ 3 files changed, 845 insertions(+) create mode 100644 src/main/java/pw/yumc/Yum/ext/BukkitUpdater.java create mode 100644 src/main/java/pw/yumc/Yum/ext/SpigotUpdater.java create mode 100644 src/main/java/pw/yumc/Yum/ext/Updater.java diff --git a/src/main/java/pw/yumc/Yum/ext/BukkitUpdater.java b/src/main/java/pw/yumc/Yum/ext/BukkitUpdater.java new file mode 100644 index 0000000..dd2eedd --- /dev/null +++ b/src/main/java/pw/yumc/Yum/ext/BukkitUpdater.java @@ -0,0 +1,428 @@ +/* + * Updater for Bukkit. + * + * This class provides the means to safely and easily update a plugin, or check to see if it is updated using dev.bukkit.org + */ + +// Somewhat modified by aadnk. +package pw.yumc.Yum.ext; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.error.Report; + +/** + * Check dev.bukkit.org to find updates for a given plugin, and download the updates if needed. + *

+ * VERY, VERY IMPORTANT: Because there are no standards for adding auto-update toggles in your plugin's config, this system provides NO CHECK WITH YOUR CONFIG to make sure the user has allowed + * auto-updating. + *
+ * It is a BUKKIT POLICY that you include a boolean value in your config that prevents the auto-updater from running AT ALL. + *
+ * If you fail to include this option in your config, your plugin will be REJECTED when you attempt to submit it to dev.bukkit.org. + *

+ * An example of a good configuration option would be something similar to 'auto-update: true' - if this value is set to false you may NOT run the auto-updater. + *
+ * If you are unsure about these rules, please read the plugin submission guidelines: http://goo.gl/8iU5l + * + * @author Gravity + * @version 2.0 + */ + +public class BukkitUpdater extends Updater { + private static final String TITLE_VALUE = "name"; // Gets remote file's title + private static final String LINK_VALUE = "downloadUrl"; // Gets remote file's download link + private static final String TYPE_VALUE = "releaseType"; // Gets remote file's release type + + private static final String VERSION_VALUE = "gameVersion"; // Gets remote file's build version + private static final Object FILE_NAME = "fileName"; // Gets remote file's name + private static final String QUERY = "/servermods/files?projectIds="; // Path to GET + private static final String HOST = "https://api.curseforge.com"; // Slugs will be appended to this to get to the project's RSS feed + + private static final int BYTE_SIZE = 1024; // Used for downloading files + + private URL url; // Connecting to RSS + private File file; // The plugin's file + private Thread thread; // Updater thread + private int id = -1; // Project's Curse ID + + private String apiKey = null; // BukkitDev ServerMods API key + + private YamlConfiguration config; // Config file + private String updateFolder;// The folder that downloads will be placed in + + /** + * Initialize the updater. + *

+ * Call {@link #start()} to actually start looking (and downloading) updates. + * + * @param plugin + * The plugin that is checking for an update. + * @param id + * The dev.bukkit.org id of the project + * @param file + * The file that the plugin is running from, get this by doing this.getFile() from within your main class. + * @param type + * Specify the type of update this will be. See {@link UpdateType} + * @param announce + * True if the program should announce the progress of new updates in console + */ + public BukkitUpdater(final Plugin plugin, final int id, final File file, final UpdateType type, final boolean announce) { + super(plugin, type, announce); + + this.file = file; + this.id = id; + this.updateFolder = plugin.getServer().getUpdateFolder(); + + final File dataFolder = plugin.getDataFolder(); + if (dataFolder != null) { + final File pluginFile = plugin.getDataFolder().getParentFile(); + final File updaterFile = new File(pluginFile, "Updater"); + final File updaterConfigFile = new File(updaterFile, "config.yml"); + + if (!updaterFile.exists()) { + updaterFile.mkdir(); + } + if (!updaterConfigFile.exists()) { + try { + updaterConfigFile.createNewFile(); + } catch (final IOException e) { + plugin.getLogger().severe("The updater could not create a configuration in " + updaterFile.getAbsolutePath()); + e.printStackTrace(); + } + } + this.config = YamlConfiguration.loadConfiguration(updaterConfigFile); + + this.config + .options() + .header("This configuration file affects all plugins using the Updater system (version 2+ - http://forums.bukkit.org/threads/96681/ )" + + '\n' + + "If you wish to use your API key, read http://wiki.bukkit.org/ServerMods_API and place it below." + + '\n' + + "Some updating systems will not adhere to the disabled value, but these may be turned off in their plugin's configuration."); + this.config.addDefault("api-key", "PUT_API_KEY_HERE"); + this.config.addDefault("disable", false); + + if (this.config.get("api-key", null) == null) { + this.config.options().copyDefaults(true); + try { + this.config.save(updaterConfigFile); + } catch (final IOException e) { + plugin.getLogger().severe("The updater could not save the configuration in " + updaterFile.getAbsolutePath()); + e.printStackTrace(); + } + } + + if (this.config.getBoolean("disable")) { + this.result = UpdateResult.DISABLED; + return; + } + + String key = this.config.getString("api-key"); + if (key.equalsIgnoreCase("PUT_API_KEY_HERE") || key.equals("")) { + key = null; + } + + this.apiKey = key; + } + + try { + this.url = new URL(BukkitUpdater.HOST + BukkitUpdater.QUERY + id); + } catch (final MalformedURLException e) { + plugin.getLogger().severe("The project ID provided for updating, " + id + " is invalid."); + this.result = UpdateResult.FAIL_BADID; + e.printStackTrace(); + } + } + + @Override + public String getRemoteVersion() { + return getLatestName(); + } + + /** + * Evaluate whether the version number is marked showing that it should not be updated by this program + */ + /* + * private boolean hasTag(String version) { + * for (final String string : BukkitUpdater.NO_UPDATE_TAG) { + * if (version.contains(string)) { + * return true; + * } + * } + * return false; + * } + */ + + public boolean read() { + try { + final URLConnection conn = this.url.openConnection(); + conn.setConnectTimeout(5000); + + if (this.apiKey != null) { + conn.addRequestProperty("X-API-Key", this.apiKey); + } + conn.addRequestProperty("User-Agent", "Updater (by Gravity)"); + conn.setDoOutput(true); + + final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); + final String response = reader.readLine(); + + final JSONArray array = (JSONArray) JSONValue.parse(response); + + if (array.size() == 0) { + this.plugin.getLogger().warning("The updater could not find any files for the project id " + this.id); + this.result = UpdateResult.FAIL_BADID; + return false; + } + + final JSONObject jsonObject = (JSONObject) array.get(array.size() - 1); + this.versionFileName = (String) jsonObject.get(BukkitUpdater.FILE_NAME); + this.versionName = (String) jsonObject.get(BukkitUpdater.TITLE_VALUE); + this.versionLink = (String) jsonObject.get(BukkitUpdater.LINK_VALUE); + this.versionType = (String) jsonObject.get(BukkitUpdater.TYPE_VALUE); + this.versionGameVersion = (String) jsonObject.get(BukkitUpdater.VERSION_VALUE); + + return true; + } catch (final IOException e) { + if (e.getMessage().contains("HTTP response code: 403")) { + this.plugin.getLogger().warning("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml"); + this.plugin.getLogger().warning("Please double-check your configuration to ensure it is correct."); + this.result = UpdateResult.FAIL_APIKEY; + } else { + this.plugin.getLogger().warning("The updater could not contact dev.bukkit.org for updating."); + this.plugin.getLogger().warning("If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime."); + this.result = UpdateResult.FAIL_DBO; + } + e.printStackTrace(); + return false; + } + } + + // aadnk - decouple the thread start and the constructor. + /** + * Begin looking for updates. + * + * @param type + * - the update type. + */ + @Override + public void start(final UpdateType type) { + waitForThread(); + + this.type = type; + this.thread = new Thread(new UpdateRunnable()); + this.thread.start(); + } + + /** + * Check if the name of a jar is one of the plugins currently installed, used for extracting the correct files out of a zip. + */ + private boolean pluginFile(final String name) { + for (final File file : new File("plugins").listFiles()) { + if (file.getName().equals(name)) { + return true; + } + } + return false; + } + + /** + * Save an update from dev.bukkit.org into the server's update folder. + */ + private void saveFile(final File folder, final String file, final String u) { + if (!folder.exists()) { + folder.mkdir(); + } + BufferedInputStream in = null; + FileOutputStream fout = null; + try { + // Download the file + final URL url = new URL(u); + final int fileLength = url.openConnection().getContentLength(); + in = new BufferedInputStream(url.openStream()); + fout = new FileOutputStream(folder.getAbsolutePath() + "/" + file); + + final byte[] data = new byte[BukkitUpdater.BYTE_SIZE]; + int count; + if (this.announce) { + this.plugin.getLogger().info("About to download a new update: " + this.versionName); + } + long downloaded = 0; + while ((count = in.read(data, 0, BukkitUpdater.BYTE_SIZE)) != -1) { + downloaded += count; + fout.write(data, 0, count); + final int percent = (int) ((downloaded * 100) / fileLength); + if (this.announce && ((percent % 10) == 0)) { + this.plugin.getLogger().info("Downloading update: " + percent + "% of " + fileLength + " bytes."); + } + } + // Just a quick check to make sure we didn't leave any files from last time... + for (final File xFile : new File(this.plugin.getDataFolder().getParent(), this.updateFolder).listFiles()) { + if (xFile.getName().endsWith(".zip")) { + xFile.delete(); + } + } + // Check to see if it's a zip file, if it is, unzip it. + final File dFile = new File(folder.getAbsolutePath() + "/" + file); + if (dFile.getName().endsWith(".zip")) { + // Unzip + this.unzip(dFile.getCanonicalPath()); + } + if (this.announce) { + this.plugin.getLogger().info("Finished updating."); + } + } catch (final Exception ex) { + this.plugin.getLogger().warning("The auto-updater tried to download a new update, but was unsuccessful."); + this.result = BukkitUpdater.UpdateResult.FAIL_DOWNLOAD; + } finally { + try { + if (in != null) { + in.close(); + } + if (fout != null) { + fout.close(); + } + } catch (final Exception ex) { + } + } + } + + /** + * Part of Zip-File-Extractor, modified by Gravity for use with Bukkit + */ + private void unzip(final String file) { + try { + final File fSourceZip = new File(file); + final String zipPath = file.substring(0, file.length() - 4); + ZipFile zipFile = new ZipFile(fSourceZip); + Enumeration e = zipFile.entries(); + while (e.hasMoreElements()) { + ZipEntry entry = e.nextElement(); + File destinationFilePath = new File(zipPath, entry.getName()); + destinationFilePath.getParentFile().mkdirs(); + if (entry.isDirectory()) { + continue; + } else { + final BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); + int b; + final byte buffer[] = new byte[BukkitUpdater.BYTE_SIZE]; + final FileOutputStream fos = new FileOutputStream(destinationFilePath); + final BufferedOutputStream bos = new BufferedOutputStream(fos, BukkitUpdater.BYTE_SIZE); + while ((b = bis.read(buffer, 0, BukkitUpdater.BYTE_SIZE)) != -1) { + bos.write(buffer, 0, b); + } + bos.flush(); + bos.close(); + bis.close(); + final String name = destinationFilePath.getName(); + if (name.endsWith(".jar") && this.pluginFile(name)) { + destinationFilePath.renameTo(new File(this.plugin.getDataFolder().getParent(), this.updateFolder + "/" + name)); + } + } + entry = null; + destinationFilePath = null; + } + e = null; + zipFile.close(); + zipFile = null; + + // Move any plugin data folders that were included to the right place, Bukkit won't do this for us. + for (final File dFile : new File(zipPath).listFiles()) { + if (dFile.isDirectory()) { + if (this.pluginFile(dFile.getName())) { + final File oFile = new File(this.plugin.getDataFolder().getParent(), dFile.getName()); // Get current dir + final File[] contents = oFile.listFiles(); // List of existing files in the current dir + for (final File cFile : dFile.listFiles()) // Loop through all the files in the new dir + { + boolean found = false; + for (final File xFile : contents) // Loop through contents to see if it exists + { + if (xFile.getName().equals(cFile.getName())) { + found = true; + break; + } + } + if (!found) { + // Move the new file into the current dir + cFile.renameTo(new File(oFile.getCanonicalFile() + "/" + cFile.getName())); + } else { + // This file already exists, so we don't need it anymore. + cFile.delete(); + } + } + } + } + dFile.delete(); + } + new File(zipPath).delete(); + fSourceZip.delete(); + } catch (final IOException ex) { + this.plugin.getLogger().warning("The auto-updater tried to unzip a new update file, but was unsuccessful."); + this.result = BukkitUpdater.UpdateResult.FAIL_DOWNLOAD; + ex.printStackTrace(); + } + new File(file).delete(); + } + + // aadnk - added listeners + private class UpdateRunnable implements Runnable { + @Override + public void run() { + try { + if (BukkitUpdater.this.url != null) { + // Obtain the results of the project's file feed + if (BukkitUpdater.this.read()) { + if (BukkitUpdater.this.versionCheck(BukkitUpdater.this.versionName)) { + performUpdate(); + } + } + } + } catch (final Exception e) { + // Any generic error will be handled here + ProtocolLibrary.getErrorReporter().reportDetailed(BukkitUpdater.this, Report.newBuilder(REPORT_CANNOT_UPDATE_PLUGIN).error(e).callerParam(this)); + + } finally { + // Invoke the listeners on the main thread + for (final Runnable listener : listeners) { + plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, listener); + } + } + } + + private void performUpdate() { + if ((BukkitUpdater.this.versionLink != null) && (BukkitUpdater.this.type != UpdateType.NO_DOWNLOAD)) { + final File pluginFolder = plugin.getDataFolder().getParentFile(); + final File destinationFolder = new File(pluginFolder, updateFolder); + String name = BukkitUpdater.this.file.getName(); + + // If it's a zip file, it shouldn't be downloaded as the plugin's name + if (BukkitUpdater.this.versionLink.endsWith(".zip")) { + name = versionFileName; + } + BukkitUpdater.this.saveFile(destinationFolder, name, BukkitUpdater.this.versionLink); + } else { + BukkitUpdater.this.result = UpdateResult.UPDATE_AVAILABLE; + } + } + } +} \ No newline at end of file diff --git a/src/main/java/pw/yumc/Yum/ext/SpigotUpdater.java b/src/main/java/pw/yumc/Yum/ext/SpigotUpdater.java new file mode 100644 index 0000000..83c9b5b --- /dev/null +++ b/src/main/java/pw/yumc/Yum/ext/SpigotUpdater.java @@ -0,0 +1,119 @@ +/** + * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. + * Copyright (C) 2015 dmulloy2 + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + */ +package pw.yumc.Yum.ext; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.bukkit.plugin.Plugin; + +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.error.Report; +import com.google.common.base.Charsets; +import com.google.common.io.Closer; + +/** + * Adapted version of the Bukkit updater for use with Spigot resources + * + * @author dmulloy2 + */ + +public final class SpigotUpdater extends Updater { + private static final String PROTOCOL = "https://"; + + private static final String RESOURCE_URL = PROTOCOL + "www.spigotmc.org/resources/protocollib.1997/"; + + private static final String API_URL = PROTOCOL + "www.spigotmc.org/api/general.php"; + + private static final String ACTION = "POST"; + + private static final int ID = 1997; + + private static final byte[] API_KEY = ("key=98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4&resource=" + ID).getBytes(Charsets.UTF_8); + private String remoteVersion; + + public SpigotUpdater(final Plugin plugin, final UpdateType type, final boolean announce) { + super(plugin, type, announce); + } + + @Override + public String getRemoteVersion() { + return remoteVersion; + } + + @Override + public String getResult() { + waitForThread(); + return String.format(result.toString(), remoteVersion, plugin.getDescription().getVersion(), RESOURCE_URL); + } + + public String getSpigotVersion() throws IOException { + final Closer closer = Closer.create(); + try { + final HttpURLConnection con = (HttpURLConnection) new URL(API_URL).openConnection(); + con.setDoOutput(true); + con.setRequestMethod(ACTION); + con.getOutputStream().write(API_KEY); + + final InputStreamReader isr = closer.register(new InputStreamReader(con.getInputStream())); + final BufferedReader br = closer.register(new BufferedReader(isr)); + return br.readLine(); + } finally { + closer.close(); + } + } + + @Override + public void start(final UpdateType type) { + waitForThread(); + + this.type = type; + this.thread = new Thread(new SpigotUpdateRunnable()); + this.thread.start(); + } + + private class SpigotUpdateRunnable implements Runnable { + @Override + public void run() { + try { + final String version = getSpigotVersion(); + remoteVersion = version; + + if (versionCheck(version)) { + result = UpdateResult.SPIGOT_UPDATE_AVAILABLE; + } else { + result = UpdateResult.NO_UPDATE; + } + } catch (final Throwable ex) { + // if (ProtocolLibrary.getConfig().isDebug()) { + ProtocolLibrary.getErrorReporter().reportDetailed(SpigotUpdater.this, Report.newBuilder(REPORT_CANNOT_UPDATE_PLUGIN).error(ex).callerParam(this)); + // } else { + // plugin.getLogger().log(Level.WARNING, "Failed to check for updates: " + ex); + // } + // ProtocolLibrary.disableUpdates(); + } finally { + // Invoke the listeners on the main thread + for (final Runnable listener : listeners) { + plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, listener); + } + } + } + } +} \ No newline at end of file diff --git a/src/main/java/pw/yumc/Yum/ext/Updater.java b/src/main/java/pw/yumc/Yum/ext/Updater.java new file mode 100644 index 0000000..2713bbe --- /dev/null +++ b/src/main/java/pw/yumc/Yum/ext/Updater.java @@ -0,0 +1,298 @@ +/** + * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. + * Copyright (C) 2015 dmulloy2 + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + */ +package pw.yumc.Yum.ext; + +import java.io.File; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import org.bukkit.plugin.Plugin; + +import com.comphenix.protocol.error.ReportType; +import com.comphenix.protocol.utility.MinecraftVersion; +import com.google.common.base.Preconditions; + +/** + * @author dmulloy2 + */ + +public abstract class Updater { + public static final ReportType REPORT_CANNOT_UPDATE_PLUGIN = new ReportType("Cannot update ProtocolLib."); + + protected Plugin plugin; + protected String versionName; + protected String versionLink; + protected String versionType; + protected String versionGameVersion; + + protected String versionFileName; + protected UpdateType type; + + protected boolean announce; + protected Thread thread; + protected UpdateResult result = UpdateResult.SUCCESS; + + protected List listeners = new CopyOnWriteArrayList(); + + public static Updater create(final Plugin protocolLib, final int id, final File file, final UpdateType type, final boolean announce) { + // if (Util.isUsingSpigot()) { + return new SpigotUpdater(protocolLib, type, announce); + // } else { + // return new BukkitUpdater(protocolLib, id, file, type, announce); + // } + } + + protected Updater(final Plugin plugin, final UpdateType type, final boolean announce) { + this.plugin = plugin; + this.type = type; + this.announce = announce; + } + + /** + * Add a listener to be executed when we have determined if an update is available. + *

+ * The listener will be executed on the main thread. + * + * @param listener + * - the listener to add. + */ + public void addListener(final Runnable listener) { + listeners.add(Preconditions.checkNotNull(listener, "listener cannot be NULL")); + } + + /** + * Get the latest version's file link. + */ + public String getLatestFileLink() { + this.waitForThread(); + return this.versionLink; + } + + /** + * Get the latest version's game version. + */ + public String getLatestGameVersion() { + this.waitForThread(); + return this.versionGameVersion; + } + + /** + * Get the latest version's name. + */ + public String getLatestName() { + this.waitForThread(); + return this.versionName; + } + + /** + * Get the latest version's release type (release, beta, or alpha). + */ + public String getLatestType() { + this.waitForThread(); + return this.versionType; + } + + public abstract String getRemoteVersion(); + + /** + * Get the result of the update process. + */ + public String getResult() { + this.waitForThread(); + return this.result.toString(); + } + + /** + * Determine if we are already checking for an update. + * + * @return TRUE if we are, FALSE otherwise. + */ + public boolean isChecking() { + return this.thread != null && this.thread.isAlive(); + } + + /** + * Remove a listener. + * + * @param listener + * - the listener to remove. + * @return TRUE if the listener was removed, FALSE otherwise. + */ + public boolean removeListener(final Runnable listener) { + return listeners.remove(listener); + } + + public boolean shouldNotify() { + switch (result) { + case SPIGOT_UPDATE_AVAILABLE: + case SUCCESS: + case UPDATE_AVAILABLE: + return true; + default: + return false; + } + } + + public abstract void start(UpdateType type); + + public boolean versionCheck(final String title) { + if (this.type != UpdateType.NO_VERSION_CHECK) { + String version = this.plugin.getDescription().getVersion(); + + boolean devBuild = false; + if (version.contains("-SNAPSHOT") || version.contains("-BETA")) { + devBuild = true; + version = version.substring(0, version.indexOf("-")); + } + + final String[] splitTitle = title.split(" "); + String remoteVersion; + + if (splitTitle.length == 2) { + remoteVersion = splitTitle[1].split("-")[0]; + } else if (this instanceof SpigotUpdater) { + remoteVersion = splitTitle[0]; + } else { + // The file's name did not contain the string 'vVersion' + final String authorInfo = this.plugin.getDescription().getAuthors().size() == 0 ? "" : " (" + this.plugin.getDescription().getAuthors().get(0) + ")"; + this.plugin.getLogger().warning("The author of this plugin " + authorInfo + " has misconfigured their Auto Update system"); + this.plugin.getLogger().warning("File versions should follow the format 'PluginName VERSION[-SNAPSHOT]'"); + this.plugin.getLogger().warning("Please notify the author of this error."); + this.result = BukkitUpdater.UpdateResult.FAIL_NOVERSION; + return false; + } + + // Parse the version + if (remoteVersion.startsWith("v")) { + remoteVersion = remoteVersion.substring(1); + } + + final MinecraftVersion parsedRemote = new MinecraftVersion(remoteVersion); + final MinecraftVersion parsedCurrent = new MinecraftVersion(plugin.getDescription().getVersion()); + + if (devBuild && parsedRemote.equals(parsedCurrent)) { + // They're using a dev build and this version has been released + return !remoteVersion.contains("-BETA") && !remoteVersion.contains("-SNAPSHOT"); + } + + // The remote version has to be greater + if (parsedRemote.compareTo(parsedCurrent) <= 0) { + // We already have the latest version, or this build is tagged for no-update + this.result = BukkitUpdater.UpdateResult.NO_UPDATE; + return false; + } + } + return true; + } + + /** + * As the result of Updater output depends on the thread's completion, it is necessary to wait for the thread to finish + * before allowing anyone to check the result. + */ + protected void waitForThread() { + if (thread != null && thread.isAlive()) { + try { + thread.join(); + } catch (final InterruptedException ex) { + ex.printStackTrace(); + } + } + } + + /** + * Gives the dev the result of the update process. Can be obtained by called getResult(). + */ + public enum UpdateResult { + /** + * The updater found an update, and has readied it to be loaded the next time the server restarts/reloads. + */ + SUCCESS("The updater found an update, and has readied it to be loaded the next time the server restarts/reloads."), + + /** + * The updater did not find an update, and nothing was downloaded. + */ + NO_UPDATE("The updater did not find an update, and nothing was downloaded."), + + /** + * The server administrator has disabled the updating system + */ + DISABLED("The server administrator has disabled the updating system"), + + /** + * The updater found an update, but was unable to download it. + */ + FAIL_DOWNLOAD("The updater found an update, but was unable to download it."), + + /** + * For some reason, the updater was unable to contact dev.bukkit.org to download the file. + */ + FAIL_DBO("For some reason, the updater was unable to contact dev.bukkit.org to download the file."), + /** + * When running the version check, the file on DBO did not contain the a version in the format 'vVersion' such as 'v1.0'. + */ + FAIL_NOVERSION("When running the version check, the file on DBO did not contain the a version in the format 'vVersion' such as 'v1.0'."), + + /** + * The id provided by the plugin running the updater was invalid and doesn't exist on DBO. + */ + FAIL_BADID("The id provided by the plugin running the updater was invalid and doesn't exist on DBO."), + + /** + * The server administrator has improperly configured their API key in the configuration + */ + FAIL_APIKEY("The server administrator has improperly configured their API key in the configuration"), + + /** + * The updater found an update, but because of the UpdateType being set to NO_DOWNLOAD, it wasn't downloaded. + */ + UPDATE_AVAILABLE("The updater found an update, but because of the UpdateType being set to NO_DOWNLOAD, it wasn't downloaded."), + + /** + * The updater found an update at Spigot + */ + SPIGOT_UPDATE_AVAILABLE("The updater found an update: %s (Running %s). Download at %s"); + + private final String description; + + private UpdateResult(final String description) { + this.description = description; + } + + @Override + public String toString() { + return description; + } + } + + /** + * Allows the dev to specify the type of update that will be run. + */ + public enum UpdateType { + /** + * Run a version check, and then if the file is out of date, download the newest version. + */ + DEFAULT, + /** + * Don't run a version check, just find the latest update and download it. + */ + NO_VERSION_CHECK, + /** + * Get information about the version and the download size, but don't actually download anything. + */ + NO_DOWNLOAD + } +}