├── .gitignore ├── .script └── upload-javadoc.sh ├── .travis.yml ├── Bukkit ├── pom.xml └── src │ └── main │ └── java │ └── org │ └── inventivetalent │ └── update │ └── spiget │ └── SpigetUpdate.java ├── Bungee ├── pom.xml └── src │ └── main │ └── java │ └── org │ └── inventivetalent │ └── update │ └── spiget │ └── SpigetUpdate.java ├── Core ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── inventivetalent │ │ └── update │ │ └── spiget │ │ ├── ResourceFile.java │ │ ├── ResourceInfo.java │ │ ├── ResourceVersion.java │ │ ├── SpigetUpdateAbstract.java │ │ ├── UpdateCallback.java │ │ ├── comparator │ │ └── VersionComparator.java │ │ └── download │ │ ├── DownloadCallback.java │ │ └── UpdateDownloader.java │ └── test │ └── java │ └── org │ └── inventivetalent │ └── update │ └── spiget │ └── test │ ├── ComparatorTest.java │ └── UpdateTest.java ├── LICENSE ├── README.MD ├── pom.xml ├── renovate.json └── settings.xml /.gitignore: -------------------------------------------------------------------------------- 1 | ### Maven template 2 | target/ 3 | pom.xml.tag 4 | pom.xml.releaseBackup 5 | pom.xml.versionsBackup 6 | pom.xml.next 7 | release.properties 8 | dependency-reduced-pom.xml 9 | buildNumber.properties 10 | .mvn/timing.properties 11 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar 12 | .mvn/wrapper/maven-wrapper.jar 13 | 14 | ### JetBrains template 15 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 16 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 17 | 18 | .idea/ 19 | *.iml 20 | *.ipr 21 | 22 | # CMake 23 | cmake-build-*/ 24 | 25 | # File-based project format 26 | *.iws 27 | 28 | # IntelliJ 29 | out/ 30 | 31 | # mpeltonen/sbt-idea plugin 32 | .idea_modules/ 33 | 34 | # JIRA plugin 35 | atlassian-ide-plugin.xml 36 | 37 | # Crashlytics plugin (for Android Studio and IntelliJ) 38 | com_crashlytics_export_strings.xml 39 | crashlytics.properties 40 | crashlytics-build.properties 41 | fabric.properties 42 | -------------------------------------------------------------------------------- /.script/upload-javadoc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo -e "Uploading Javadoc...\n" 3 | 4 | cd $HOME 5 | git config --global user.email "git-bot@inventivetalent.org" 6 | git config --global user.name "InventiveBot" 7 | git clone --quiet --branch=gh-pages https://${GITHUB_TOKEN}@github.com/InventivetalentDev/SpigetUpdater gh-pages > /dev/null 8 | 9 | cd gh-pages 10 | git rm -rf . 11 | cp -Rf $TRAVIS_BUILD_DIR/target/site/apidocs . 12 | git add -f . 13 | git commit -m "Javadoc for #$TRAVIS_BUILD_NUMBER" 14 | git push -fq origin gh-pages > /dev/null 15 | 16 | echo -e "Uploaded Javadoc\n" 17 | 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | 5 | script: "mvn javadoc:aggregate deploy --settings settings.xml" 6 | before_script: 7 | - "chmod +x .script/upload-javadoc.sh" 8 | after_success: ".script/upload-javadoc.sh" 9 | env: 10 | global: 11 | - secure: "KZmkrF5X0P+rVc25Y2P7RUXgTfwHqjPbw8Zfcod/4KQDMzWtAPpsGlE8kulr1ry0ozRgqyAqTrzf1yRR5YxWgJQOy1rEpWsJ0Jfy1zv7nejsAZX4pT1YsiNrZBJNT9fDdOPOYQm9Fm4C2s0tJPamOgCHbbWJs9EscdcKlnHKyU0bKRT7NIlDraCOBMDiw7S1BG8YCZQJ4Au6jhXyJuGYF8+HNg07cPspI9g/VUEFSmBMisDnB/4zLXy55VMMbH7OcMU4c362JGtwYPQjX5HnbR4JsxtxxI0lOBoUDkY77MwsWlQmruwsPn8e00jO1xNkcuFYohzZklUTRZZDENVFtOeA++DXiaeJzRhZBX56eAZopFe0dPWGfYNRkC+dN4g4GY96aInK/Jxpkg9R/DRwBlhTNoJXFoMVkbIPTn/sEXaa7Ea5S475iQpzhuPFR06Tr26JT39e0ycVB37QIprRsL2mQIJ3pMlK8HcZNnDmg6usESLCHPmgSpfkH9KDGrM14fJ6u0Hl6jmAmvV/iqQWynIA0Be3fIjGQ1fyRhBghj+5SUO1Ux28z5TP+B5yXTssf3zK2dCIac6IRHW43math9hY+lS+6bpcsfhyOF7olbKZB1C5PJT7cRByK6PmPC2hQzn9pyYSQbgvHeZaL5KrxCSpNqNPfbYOprake2nobH4=" 12 | - secure: "mnEM2tBdQjDwEBud/3UGpuD6uuma9ytXaL3YWc44ThwjCa5fgs16gh6TXriJ7ZuPJHtwUaTwB93nFJcwPon9/iofGuY5P5aYAbPvSxPvA8qFSwL3hOziQaf7SxePjRPHctlPrBEGYDVEN9NXkdK/nSqRL7ZljYCp6FtiIzEu8QxDSZm0Us6m7QHzGHpAvCaeDpYAoq/6oKtr3Rgf8sQzCDSdvMvFnDGxVc1sM9kULhzSqBubUZkHYg1VrtJ/KjuwQsDFh8iXuXvPqki3hWBtjaXie2vXBlAH1xiKlX1YgycoBeoQMknGrm6WaB3v0IDEGCAzPAlRdgshuWIhXhaNhSVt1YV2LsXCTWSq3MFI0DWrGj134Kw1LVvbTmN8+JRFrzY9a0+zP91Vpi12hKprmf+qWV6zmfTZe47+L8E7zQFI2zCM/VEWKb9HLJZMrZBJqjR13I3rbwA6nrtuv5ZNcrQ3re5xy807U8LkM+2LQzgqNsb6ihkZhPycNqWKEWtjk/TCzsTBPghk6/Krap8XvVOU/MHOpLDIGe3GCfGfG7GFfu3V/iE74TpcO0nbwp3wIfh1qk/JNENpv1/Pc5qx0CZ84ly7gRmmYVtP9ff+9dRk1FzZtQhR/oiMPrqbJxwnP140vrarXQJZIOCJtILjEosi9xSddibTkKRgTu6XGd4=" 13 | - secure: "AhSYOwdwz4hhsopVCYyTyfwni4Hnu4CbLY2BVN6mYnKj/gGLPh+215cv4CQUSGhu8VEBz5AXs7zw/NrbXY0M7D7zCngiqAUSWAG2cvspKIMgwwiKMAn8hheNmwlmdFuiR4kRujoRByoaCp+yvHbYviggTKm6RZXN3jdqbp5kO4dgZD8qZM8JGKk0CA6bein0FrFWVeRNygAISFBn54vEFxeOWZTI3c0XzouaHsYJYfVt0fwOlIaexP0rZXOckXoCku9KzCcmEdeWMov2D5Cm9tJxp02FyK63HD2AuB+fKs62mrRwvmBgQoQx/LLsuKegPbCkj1ym9/y5hS6gLBA8C6q/ZadiRO6mmS7u8QNvM+bZCDeN8p3H81+P5dhUL8LJu91tDMlZo8e+AQyTbOvfQUIqDXCZQMAd20qjuNaucH6P4zXHCaYSTC4E4uPhAvGsZ6op0/Zg0vS6Yg4F+GnGEUMA/JKGLwXU3w7ZE3NXIf2MoM6urXgwU9x5Pt4Twjsc4Q33bs9VAlSvoUSzH0aStBmZ+G0E9NyxNg4LyEDtJtpZ/6te683c3sNCOFft/PfrFh0na0njPpEnSanrHXCrr0IlLfyXimXFkODUtD4BYpH+arlWWCaZgaNGmTsTq8/LQhlo40SRVA+z5dXJ6ckcDfBm7ROwc4NcFndGz0bhcPs=" 14 | before_deploy: 15 | - export BUKKIT_RELASE_FILE=$(find . -name bukkit-*.jar) 16 | - export BUNGEE_RELASE_FILE=$(find . -name bungee-*.jar) 17 | deploy: 18 | provider: releases 19 | api_key: 20 | secure: "eic3KobDkmWVoKQI9KjWgjrhw2aK6xJOnpUP7eKY2C7x5ZGvwZmZZ8PmgT2M07WFuklq/rn87fOJs7FvkpG1jkpF4aGO90wi1zy4LxVKLd4sHSwp1mHBbNAejL3ph+ff7tIKkyTENFKOX5YusXOM7ZRc162ySH4+UpkKLQwtItQ8qKkwcOzI4B9tiiLrq6vAO163/AiIo3lZCFNoeOc5kHnHVoDFRthhcSuFgi+gDk7au4ndA64/mrABuhtfQKJz32/GjybV+8Ah+ZYcGcGm9TuVi2itJ2UiTgnrVkJKWlJNgkuvtgZx6J5lZvHk1gjoiXJl3kojbHgV9rXqrTsKxN+NQPFwaak/S0c6iqRCqu3AzlKL0Xmh3aqA403cxpKI20PiXi5bXUOTBmIwANU/jHyIP9VRZJRpi2/dGLM5p2wPGO9CmsdOlthcreIYvnHXqlL3I/vnyjfxv0eJuRsfJ/hrC2NdI95UjF+AxaCtPsOWbKtdS9STtA0HpQzyCOCNT8Nu2dg/u1Wss6qe0XfzaShG54vBwM9b/bcSi9wh+8ivpEOKEF3RWfZ1dYaiEbeJ/Eu9UYWeVIeFvboUkETz4ZCyB8hoxdJwZ3VJbD5nQbZFqbOlfCa4jmBwGKkOYe/+FBr/iMDhlRwjjmqDC8OCC6k4jT4VDAIeE6ey5YsbLk8=" 21 | file_glob: true 22 | file: 23 | - "Bukkit/target/bukkit-*.jar" 24 | - "Bungee/target/bungee-*.jar" 25 | skip_cleanup: true 26 | on: 27 | tags: true 28 | -------------------------------------------------------------------------------- /Bukkit/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | org.inventivetalent.spiget-update 7 | parent 8 | parent-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | bukkit 13 | ${s-update.version} 14 | 15 | 16 | 17 | org.inventivetalent.spiget-update 18 | core 19 | ${s-update.version} 20 | 21 | 22 | org.bukkit 23 | bukkit 24 | 1.9-R0.1-SNAPSHOT 25 | provided 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Bukkit/src/main/java/org/inventivetalent/update/spiget/SpigetUpdate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 inventivetalent. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, are 5 | * permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this list of 8 | * conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 | * of conditions and the following disclaimer in the documentation and/or other materials 12 | * provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 16 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | * 24 | * The views and conclusions contained in the software and documentation are those of the 25 | * authors and contributors and should not be interpreted as representing official policies, 26 | * either expressed or implied, of anybody else. 27 | */ 28 | 29 | package org.inventivetalent.update.spiget; 30 | 31 | import org.bukkit.Bukkit; 32 | import org.bukkit.plugin.Plugin; 33 | import org.bukkit.plugin.java.JavaPlugin; 34 | import org.inventivetalent.update.spiget.comparator.VersionComparator; 35 | import org.inventivetalent.update.spiget.download.DownloadCallback; 36 | import org.inventivetalent.update.spiget.download.UpdateDownloader; 37 | 38 | import java.io.File; 39 | import java.io.FileReader; 40 | import java.io.FileWriter; 41 | import java.io.IOException; 42 | import java.lang.reflect.Method; 43 | import java.util.Properties; 44 | import java.util.logging.Level; 45 | 46 | public class SpigetUpdate extends SpigetUpdateAbstract { 47 | 48 | protected final Plugin plugin; 49 | protected DownloadFailReason failReason = DownloadFailReason.UNKNOWN; 50 | 51 | public SpigetUpdate(Plugin plugin, int resourceId) { 52 | super(resourceId, plugin.getDescription().getVersion(), plugin.getLogger()); 53 | this.plugin = plugin; 54 | setUserAgent("SpigetResourceUpdater/Bukkit"); 55 | } 56 | 57 | @Override 58 | public SpigetUpdate setUserAgent(String userAgent) { 59 | super.setUserAgent(userAgent); 60 | return this; 61 | } 62 | 63 | @Override 64 | public SpigetUpdate setVersionComparator(VersionComparator comparator) { 65 | super.setVersionComparator(comparator); 66 | return this; 67 | } 68 | 69 | @Override 70 | protected void dispatch(Runnable runnable) { 71 | Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable); 72 | } 73 | 74 | public boolean downloadUpdate() { 75 | if (latestResourceInfo == null) { 76 | failReason = DownloadFailReason.NOT_CHECKED; 77 | return false;// Update not yet checked 78 | } 79 | if (!isVersionNewer(currentVersion, latestResourceInfo.latestVersion.name)) { 80 | failReason = DownloadFailReason.NO_UPDATE; 81 | return false;// Version is no update 82 | } 83 | if (latestResourceInfo.external) { 84 | failReason = DownloadFailReason.NO_DOWNLOAD; 85 | return false;// No download available 86 | } 87 | 88 | File pluginFile = getPluginFile();// /plugins/XXX.jar 89 | if (pluginFile == null) { 90 | failReason = DownloadFailReason.NO_PLUGIN_FILE; 91 | return false; 92 | } 93 | File updateFolder = Bukkit.getUpdateFolderFile(); 94 | if (!updateFolder.exists()) { 95 | if (!updateFolder.mkdirs()) { 96 | failReason = DownloadFailReason.NO_UPDATE_FOLDER; 97 | return false; 98 | } 99 | } 100 | final File updateFile = new File(updateFolder, pluginFile.getName()); 101 | 102 | Properties properties = getUpdaterProperties(); 103 | boolean allowExternalDownload = properties != null && properties.containsKey("externalDownloads") && Boolean.parseBoolean(properties.getProperty("externalDownloads")); 104 | 105 | if (!allowExternalDownload && latestResourceInfo.external) { 106 | failReason = DownloadFailReason.EXTERNAL_DISALLOWED; 107 | return false; 108 | } 109 | 110 | log.info("[SpigetUpdate] Downloading update..."); 111 | dispatch(UpdateDownloader.downloadAsync(latestResourceInfo, updateFile, getUserAgent(), new DownloadCallback() { 112 | @Override 113 | public void finished() { 114 | log.info("[SpigetUpdate] Update saved as " + updateFile.getPath()); 115 | } 116 | 117 | @Override 118 | public void error(Exception exception) { 119 | log.log(Level.WARNING, "[SpigetUpdate] Could not download update", exception); 120 | } 121 | })); 122 | 123 | return true; 124 | } 125 | 126 | public DownloadFailReason getFailReason() { 127 | return failReason; 128 | } 129 | 130 | public Properties getUpdaterProperties() { 131 | File file = new File(Bukkit.getUpdateFolderFile(), "spiget.properties"); 132 | Properties properties = new Properties(); 133 | if (!file.exists()) { 134 | try { 135 | if (!file.createNewFile()) { return null; } 136 | properties.setProperty("externalDownloads", "false"); 137 | properties.store(new FileWriter(file), "Configuration for the Spiget auto-updater. https://spiget.org | https://github.com/InventivetalentDev/SpigetUpdater\n" 138 | + "Use 'externalDownloads' if you want to auto-download resources hosted on external sites\n" 139 | + ""); 140 | } catch (Exception ignored) { 141 | return null; 142 | } 143 | } 144 | try { 145 | properties.load(new FileReader(file)); 146 | } catch (IOException e) { 147 | return null; 148 | } 149 | return properties; 150 | } 151 | 152 | /** 153 | * Get the plugin's file name 154 | * 155 | * @return the plugin file name 156 | */ 157 | private File getPluginFile() { 158 | if (!(this.plugin instanceof JavaPlugin)) { return null; } 159 | try { 160 | Method method = JavaPlugin.class.getDeclaredMethod("getFile"); 161 | method.setAccessible(true); 162 | return (File) method.invoke(this.plugin); 163 | } catch (ReflectiveOperationException e) { 164 | throw new RuntimeException("Could not get plugin file", e); 165 | } 166 | } 167 | 168 | public enum DownloadFailReason { 169 | NOT_CHECKED, 170 | NO_UPDATE, 171 | NO_DOWNLOAD, 172 | NO_PLUGIN_FILE, 173 | NO_UPDATE_FOLDER, 174 | EXTERNAL_DISALLOWED, 175 | UNKNOWN 176 | } 177 | 178 | } 179 | -------------------------------------------------------------------------------- /Bungee/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | parent 7 | org.inventivetalent.spiget-update 8 | parent-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | bungee 13 | ${s-update.version} 14 | 15 | 16 | 17 | org.inventivetalent.spiget-update 18 | core 19 | ${s-update.version} 20 | 21 | 22 | net.md-5 23 | bungeecord-api 24 | 1.16-R0.4 25 | provided 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Bungee/src/main/java/org/inventivetalent/update/spiget/SpigetUpdate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 inventivetalent. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, are 5 | * permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this list of 8 | * conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 | * of conditions and the following disclaimer in the documentation and/or other materials 12 | * provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 16 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | * 24 | * The views and conclusions contained in the software and documentation are those of the 25 | * authors and contributors and should not be interpreted as representing official policies, 26 | * either expressed or implied, of anybody else. 27 | */ 28 | 29 | package org.inventivetalent.update.spiget; 30 | 31 | import net.md_5.bungee.api.ProxyServer; 32 | import net.md_5.bungee.api.plugin.Plugin; 33 | import org.inventivetalent.update.spiget.comparator.VersionComparator; 34 | 35 | public class SpigetUpdate extends SpigetUpdateAbstract { 36 | 37 | protected final Plugin plugin; 38 | 39 | public SpigetUpdate(Plugin plugin, int resourceId) { 40 | super(resourceId, plugin.getDescription().getVersion(), plugin.getLogger()); 41 | this.plugin = plugin; 42 | setUserAgent("SpigetResourceUpdater/Bungee"); 43 | } 44 | 45 | @Override 46 | public SpigetUpdate setUserAgent(String userAgent) { 47 | super.setUserAgent(userAgent); 48 | return this; 49 | } 50 | 51 | @Override 52 | public SpigetUpdate setVersionComparator(VersionComparator comparator) { 53 | super.setVersionComparator(comparator); 54 | return this; 55 | } 56 | 57 | @Override 58 | protected void dispatch(Runnable runnable) { 59 | ProxyServer.getInstance().getScheduler().runAsync(plugin, runnable); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | org.inventivetalent.spiget-update 7 | parent 8 | parent-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | core 13 | ${s-update.version} 14 | 15 | 16 | 17 | com.google.code.gson 18 | gson 19 | 2.9.0 20 | 21 | 22 | 23 | junit 24 | junit 25 | 4.13.2 26 | test 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Core/src/main/java/org/inventivetalent/update/spiget/ResourceFile.java: -------------------------------------------------------------------------------- 1 | package org.inventivetalent.update.spiget; 2 | 3 | public class ResourceFile { 4 | 5 | public String type; 6 | public int ize; 7 | public String sizeUnit; 8 | public String url; 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Core/src/main/java/org/inventivetalent/update/spiget/ResourceInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 inventivetalent. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, are 5 | * permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this list of 8 | * conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 | * of conditions and the following disclaimer in the documentation and/or other materials 12 | * provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 16 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | * 24 | * The views and conclusions contained in the software and documentation are those of the 25 | * authors and contributors and should not be interpreted as representing official policies, 26 | * either expressed or implied, of anybody else. 27 | */ 28 | 29 | package org.inventivetalent.update.spiget; 30 | 31 | public class ResourceInfo { 32 | 33 | public int id; 34 | public boolean external; 35 | public boolean premium; 36 | public ResourceFile file; 37 | 38 | public ResourceVersion latestVersion; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Core/src/main/java/org/inventivetalent/update/spiget/ResourceVersion.java: -------------------------------------------------------------------------------- 1 | package org.inventivetalent.update.spiget; 2 | 3 | public class ResourceVersion { 4 | 5 | public int id; 6 | public String name; 7 | public long releaseDate; 8 | public String url; 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Core/src/main/java/org/inventivetalent/update/spiget/SpigetUpdateAbstract.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 inventivetalent. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, are 5 | * permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this list of 8 | * conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 | * of conditions and the following disclaimer in the documentation and/or other materials 12 | * provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 16 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | * 24 | * The views and conclusions contained in the software and documentation are those of the 25 | * authors and contributors and should not be interpreted as representing official policies, 26 | * either expressed or implied, of anybody else. 27 | */ 28 | 29 | package org.inventivetalent.update.spiget; 30 | 31 | import com.google.gson.Gson; 32 | import com.google.gson.JsonObject; 33 | import com.google.gson.JsonParser; 34 | import org.inventivetalent.update.spiget.comparator.VersionComparator; 35 | 36 | import java.io.InputStreamReader; 37 | import java.net.HttpURLConnection; 38 | import java.net.URL; 39 | import java.util.logging.Level; 40 | import java.util.logging.Logger; 41 | 42 | public abstract class SpigetUpdateAbstract { 43 | 44 | public static final String RESOURCE_INFO = "https://api.spiget.org/v2/resources/%s?ut=%s"; 45 | public static final String RESOURCE_VERSION = "https://api.spiget.org/v2/resources/%s/versions/latest?ut=%s"; 46 | 47 | protected final int resourceId; 48 | protected final String currentVersion; 49 | protected final Logger log; 50 | protected String userAgent = "SpigetResourceUpdater"; 51 | protected VersionComparator versionComparator = VersionComparator.EQUAL; 52 | 53 | protected ResourceInfo latestResourceInfo; 54 | 55 | public SpigetUpdateAbstract(int resourceId, String currentVersion, Logger log) { 56 | this.resourceId = resourceId; 57 | this.currentVersion = currentVersion; 58 | this.log = log; 59 | } 60 | 61 | public SpigetUpdateAbstract setUserAgent(String userAgent) { 62 | this.userAgent = userAgent; 63 | return this; 64 | } 65 | 66 | public String getUserAgent() { 67 | return userAgent; 68 | } 69 | 70 | public SpigetUpdateAbstract setVersionComparator(VersionComparator comparator) { 71 | this.versionComparator = comparator; 72 | return this; 73 | } 74 | 75 | public ResourceInfo getLatestResourceInfo() { 76 | return latestResourceInfo; 77 | } 78 | 79 | protected abstract void dispatch(Runnable runnable); 80 | 81 | public boolean isVersionNewer(String oldVersion, String newVersion) { 82 | return versionComparator.isNewer(oldVersion, newVersion); 83 | } 84 | 85 | public void checkForUpdate(final UpdateCallback callback) { 86 | dispatch(() -> { 87 | try { 88 | HttpURLConnection connection = (HttpURLConnection) new URL(String.format(RESOURCE_INFO, resourceId, System.currentTimeMillis())).openConnection(); 89 | connection.setRequestProperty("User-Agent", getUserAgent()); 90 | JsonObject jsonObject = JsonParser.parseReader(new InputStreamReader(connection.getInputStream())).getAsJsonObject(); 91 | latestResourceInfo = new Gson().fromJson(jsonObject, ResourceInfo.class); 92 | 93 | connection = (HttpURLConnection) new URL(String.format(RESOURCE_VERSION, resourceId, System.currentTimeMillis())).openConnection(); 94 | connection.setRequestProperty("User-Agent", getUserAgent()); 95 | jsonObject = JsonParser.parseReader(new InputStreamReader(connection.getInputStream())).getAsJsonObject(); 96 | latestResourceInfo.latestVersion = new Gson().fromJson(jsonObject, ResourceVersion.class); 97 | 98 | if (isVersionNewer(currentVersion, latestResourceInfo.latestVersion.name)) { 99 | callback.updateAvailable(latestResourceInfo.latestVersion.name, "https://spigotmc.org/" + latestResourceInfo.file.url, !latestResourceInfo.external&&!latestResourceInfo.premium); 100 | } else { 101 | callback.upToDate(); 102 | } 103 | } catch (Exception e) { 104 | log.log(Level.WARNING, "Failed to get resource info from spiget.org", e); 105 | } 106 | }); 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /Core/src/main/java/org/inventivetalent/update/spiget/UpdateCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 inventivetalent. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, are 5 | * permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this list of 8 | * conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 | * of conditions and the following disclaimer in the documentation and/or other materials 12 | * provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 16 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | * 24 | * The views and conclusions contained in the software and documentation are those of the 25 | * authors and contributors and should not be interpreted as representing official policies, 26 | * either expressed or implied, of anybody else. 27 | */ 28 | 29 | package org.inventivetalent.update.spiget; 30 | 31 | public interface UpdateCallback { 32 | 33 | /** 34 | * Called when a new version was found 35 | *

36 | * Use {@link SpigetUpdateAbstract#getLatestResourceInfo()} to get all resource details 37 | * 38 | * @param newVersion the new version's name 39 | * @param downloadUrl URL to download the update 40 | * @param canAutoDownload whether this update can be downloaded automatically 41 | */ 42 | void updateAvailable(String newVersion, String downloadUrl, boolean canAutoDownload); 43 | 44 | /** 45 | * Called when no update was found 46 | */ 47 | void upToDate(); 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Core/src/main/java/org/inventivetalent/update/spiget/comparator/VersionComparator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 inventivetalent. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, are 5 | * permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this list of 8 | * conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 | * of conditions and the following disclaimer in the documentation and/or other materials 12 | * provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 16 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | * 24 | * The views and conclusions contained in the software and documentation are those of the 25 | * authors and contributors and should not be interpreted as representing official policies, 26 | * either expressed or implied, of anybody else. 27 | */ 28 | 29 | package org.inventivetalent.update.spiget.comparator; 30 | 31 | import java.util.Arrays; 32 | 33 | public abstract class VersionComparator { 34 | 35 | /** 36 | * Compares versions by checking if the version strings are equal 37 | */ 38 | public static final VersionComparator EQUAL = new VersionComparator() { 39 | @Override 40 | public boolean isNewer(String currentVersion, String checkVersion) { 41 | return !currentVersion.equals(checkVersion); 42 | } 43 | }; 44 | 45 | /** 46 | * Compares versions by their Semantic Version (Major.Minor.Patch, semver.org). 47 | * Parses major, minor and patch and goes down from major till patch to decide whether a version is newer. 48 | * Also supports longer variations like x.x.x.x or x.x. These cases will be handled properly. 49 | */ 50 | public static final VersionComparator SEM_VER = new VersionComparator() { 51 | @Override 52 | public boolean isNewer(String currentVersion, String checkVersion) { 53 | try { 54 | int[] currentVersionData = Arrays.stream(currentVersion.split("\\.")) 55 | .mapToInt(Integer::parseInt).toArray(); 56 | int[] checkVersionData = Arrays.stream(checkVersion.split("\\.")) 57 | .mapToInt(Integer::parseInt).toArray(); 58 | 59 | int i = 0; 60 | for (int version : checkVersionData) { 61 | if (i == currentVersionData.length) { 62 | return true; 63 | } 64 | 65 | if (version > currentVersionData[i]) { 66 | return true; 67 | } else if (version < currentVersionData[i]) { 68 | return false; 69 | } 70 | 71 | i++; 72 | } 73 | } catch (NumberFormatException e) { 74 | System.err.println("[SpigetUpdate] Invalid SemVer versions specified [" + currentVersion + "] [" + checkVersion + "]"); 75 | } 76 | return false; 77 | } 78 | }; 79 | 80 | /** 81 | * Same as {@link VersionComparator#SEM_VER}, but supports version names with '-SNAPSHOT' suffixes 82 | */ 83 | public static final VersionComparator SEM_VER_SNAPSHOT = new VersionComparator() { 84 | @Override 85 | public boolean isNewer(String currentVersion, String checkVersion) { 86 | currentVersion = currentVersion.replace("-SNAPSHOT", ""); 87 | checkVersion = checkVersion.replace("-SNAPSHOT", ""); 88 | 89 | return SEM_VER.isNewer(currentVersion, checkVersion); 90 | } 91 | }; 92 | 93 | /** 94 | * Called to check if a version is newer 95 | * 96 | * @param currentVersion Current version of the plugin 97 | * @param checkVersion Version to check 98 | * @return true if the checked version is newer 99 | */ 100 | public abstract boolean isNewer(String currentVersion, String checkVersion); 101 | 102 | } 103 | -------------------------------------------------------------------------------- /Core/src/main/java/org/inventivetalent/update/spiget/download/DownloadCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 inventivetalent. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, are 5 | * permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this list of 8 | * conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 | * of conditions and the following disclaimer in the documentation and/or other materials 12 | * provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 16 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | * 24 | * The views and conclusions contained in the software and documentation are those of the 25 | * authors and contributors and should not be interpreted as representing official policies, 26 | * either expressed or implied, of anybody else. 27 | */ 28 | 29 | package org.inventivetalent.update.spiget.download; 30 | 31 | public interface DownloadCallback { 32 | 33 | void finished(); 34 | 35 | void error(Exception exception); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Core/src/main/java/org/inventivetalent/update/spiget/download/UpdateDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 inventivetalent. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, are 5 | * permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this list of 8 | * conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 | * of conditions and the following disclaimer in the documentation and/or other materials 12 | * provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 16 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | * 24 | * The views and conclusions contained in the software and documentation are those of the 25 | * authors and contributors and should not be interpreted as representing official policies, 26 | * either expressed or implied, of anybody else. 27 | */ 28 | 29 | package org.inventivetalent.update.spiget.download; 30 | 31 | import org.inventivetalent.update.spiget.ResourceInfo; 32 | 33 | import java.io.File; 34 | import java.io.FileOutputStream; 35 | import java.io.IOException; 36 | import java.net.HttpURLConnection; 37 | import java.net.URL; 38 | import java.nio.channels.Channels; 39 | import java.nio.channels.ReadableByteChannel; 40 | 41 | public class UpdateDownloader { 42 | 43 | public static final String RESOURCE_DOWNLOAD = "https://api.spiget.org/v2/resources/%s/download"; 44 | 45 | public static Runnable downloadAsync(final ResourceInfo info, final File file, final String userAgent, final DownloadCallback callback) { 46 | return () -> { 47 | try { 48 | download(info, file, userAgent); 49 | callback.finished(); 50 | } catch (Exception e) { 51 | callback.error(e); 52 | } 53 | }; 54 | } 55 | 56 | public static void download(ResourceInfo info, File file, String userAgent) { 57 | if (info.external) { throw new IllegalArgumentException("Cannot download external resource #" + info.id); } 58 | ReadableByteChannel channel; 59 | try { 60 | //https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java 61 | HttpURLConnection connection = (HttpURLConnection) new URL(String.format(RESOURCE_DOWNLOAD, info.id)).openConnection(); 62 | connection.setRequestProperty("User-Agent", userAgent); 63 | connection.setInstanceFollowRedirects(true); 64 | if (connection.getResponseCode() != 200) { 65 | throw new RuntimeException("Download returned status #" + connection.getResponseCode()); 66 | } 67 | channel = Channels.newChannel(connection.getInputStream()); 68 | } catch (IOException e) { 69 | throw new RuntimeException("Download failed", e); 70 | } 71 | try { 72 | FileOutputStream output = new FileOutputStream(file); 73 | output.getChannel().transferFrom(channel, 0, Long.MAX_VALUE); 74 | output.flush(); 75 | output.close(); 76 | } catch (IOException e) { 77 | throw new RuntimeException("Could not save file", e); 78 | } 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /Core/src/test/java/org/inventivetalent/update/spiget/test/ComparatorTest.java: -------------------------------------------------------------------------------- 1 | package org.inventivetalent.update.spiget.test; 2 | 3 | import org.inventivetalent.update.spiget.comparator.VersionComparator; 4 | import org.junit.Test; 5 | import static org.junit.Assert.*; 6 | 7 | public class ComparatorTest { 8 | @Test 9 | public void equalsTest() { 10 | String oldVersion = "1.0.0"; 11 | String newVersion = "2.0.0"; 12 | 13 | assertTrue(VersionComparator.EQUAL.isNewer(oldVersion, newVersion)); 14 | 15 | // This is a case where the simple equals check doesn't shine. 16 | assertTrue(VersionComparator.EQUAL.isNewer(newVersion, oldVersion)); 17 | 18 | assertFalse(VersionComparator.EQUAL.isNewer(oldVersion, oldVersion)); 19 | } 20 | 21 | @Test 22 | public void semverTest() { 23 | String oldVersion = "1.0.0"; 24 | String newVersion = "2.0.0"; 25 | 26 | assertTrue(VersionComparator.SEM_VER.isNewer(oldVersion, newVersion)); 27 | assertFalse(VersionComparator.SEM_VER.isNewer(newVersion, oldVersion)); 28 | assertFalse(VersionComparator.SEM_VER.isNewer(oldVersion, oldVersion)); 29 | } 30 | 31 | @Test 32 | public void longSemverTest() { 33 | String oldVersion = "1.0"; 34 | String newVersion = "2.0.0.0.0.0"; 35 | 36 | assertTrue(VersionComparator.SEM_VER.isNewer(oldVersion, newVersion)); 37 | assertFalse(VersionComparator.SEM_VER.isNewer(newVersion, oldVersion)); 38 | assertFalse(VersionComparator.SEM_VER.isNewer(oldVersion, oldVersion)); 39 | } 40 | 41 | @Test 42 | public void longSemverTest2() { 43 | String oldVersion = "5.0.6.2.7"; 44 | String newVersion = "6.0.7.3"; 45 | 46 | assertTrue(VersionComparator.SEM_VER.isNewer(oldVersion, newVersion)); 47 | assertFalse(VersionComparator.SEM_VER.isNewer(newVersion, oldVersion)); 48 | assertFalse(VersionComparator.SEM_VER.isNewer(oldVersion, oldVersion)); 49 | } 50 | 51 | @Test 52 | public void edgeSemverTest() { 53 | String oldVersion = "1.1.10"; 54 | String newVersion = "1.2.0"; 55 | 56 | assertTrue(VersionComparator.SEM_VER.isNewer(oldVersion, newVersion)); 57 | assertFalse(VersionComparator.SEM_VER.isNewer(newVersion, oldVersion)); 58 | assertFalse(VersionComparator.SEM_VER.isNewer(oldVersion, oldVersion)); 59 | } 60 | 61 | @Test 62 | public void semverSnapshotTest() { 63 | String oldVersion = "1.0.0-SNAPSHOT"; 64 | String newVersion = "2.0.0-SNAPSHOT"; 65 | 66 | assertTrue(VersionComparator.SEM_VER_SNAPSHOT.isNewer(oldVersion, newVersion)); 67 | assertFalse(VersionComparator.SEM_VER_SNAPSHOT.isNewer(newVersion, oldVersion)); 68 | assertFalse(VersionComparator.SEM_VER_SNAPSHOT.isNewer(oldVersion, oldVersion)); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Core/src/test/java/org/inventivetalent/update/spiget/test/UpdateTest.java: -------------------------------------------------------------------------------- 1 | package org.inventivetalent.update.spiget.test; 2 | 3 | import org.inventivetalent.update.spiget.SpigetUpdateAbstract; 4 | import org.inventivetalent.update.spiget.UpdateCallback; 5 | import org.junit.Test; 6 | 7 | import java.util.logging.Logger; 8 | 9 | import static org.junit.Assert.*; 10 | 11 | public class UpdateTest { 12 | 13 | @Test 14 | public void updateTest() { 15 | SpigetUpdateAbstract updateCheck = new SpigetUpdateAbstract(5341/* NickNamer */, "1.0.0", Logger.getLogger("UpdateTest")) { 16 | @Override 17 | protected void dispatch(Runnable runnable) { 18 | runnable.run(); 19 | } 20 | }; 21 | updateCheck.checkForUpdate(new UpdateCallback() { 22 | @Override 23 | public void updateAvailable(String newVersion, String downloadUrl, boolean canAutoDownload) { 24 | System.out.println("newVersion = [" + newVersion + "], downloadUrl = [" + downloadUrl + "], canAutoDownload = [" + canAutoDownload + "]"); 25 | 26 | assertTrue(downloadUrl.startsWith("https://spigotmc.org/resources/nicknamer-integrated-api.5341/download")); 27 | assertFalse(canAutoDownload); 28 | } 29 | 30 | @Override 31 | public void upToDate() { 32 | System.out.println("up-to-date"); 33 | 34 | fail("Resource is up-to-date, but should have an update"); 35 | } 36 | }); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Haylee Schäfer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/InventivetalentDev/SpigetUpdater.svg?branch=master)](https://travis-ci.org/InventivetalentDev/SpigetUpdater) 2 | [![GitHub release](https://img.shields.io/github/release/InventivetalentDev/SpigetUpdater.svg)](https://github.com/InventivetalentDev/SpigetUpdater) 3 | 4 | 5 | Spigot & Bungeecord updater API which uses [spiget.org](https://spiget.org) to check for new versions 6 | 7 | ## Usage 8 | 9 | ### Check for new versions 10 | 11 | ```Java 12 | SpigetUpdate updater = new SpigetUpdate(this, 12345); 13 | 14 | // This compares versions just by checking if they are equal 15 | // This means that older versions will also be treated as updates 16 | updater.setVersionComparator(VersionComparator.EQUAL); 17 | 18 | // This converts a semantic version to an integer and checks if the updated version is greater 19 | updater.setVersionComparator(VersionComparator.SEM_VER); 20 | 21 | updater.checkForUpdate(new UpdateCallback() { 22 | @Override 23 | public void updateAvailable(String newVersion, String downloadUrl, boolean hasDirectDownload) { 24 | //// A new version is available 25 | // newVersion - the latest version 26 | // downloadUrl - URL to the download 27 | // hasDirectDownload - whether the update is available for a direct download on spiget.org 28 | } 29 | 30 | @Override 31 | public void upToDate() { 32 | //// Plugin is up-to-date 33 | } 34 | }); 35 | ``` 36 | 37 | 38 | ### Download updates (Bukkit/Spigot) 39 | ```Java 40 | final SpigetUpdate updater = new SpigetUpdate(this, 12345); 41 | updater.checkForUpdate(new UpdateCallback() { 42 | @Override 43 | public void updateAvailable(String newVersion, String downloadUrl, boolean hasDirectDownload) { 44 | // First check if there is a direct download available 45 | // (Either the resources is hosted on spigotmc.org, or Spiget has a cached version to download) 46 | // external downloads won't work if they are disabled (by default) in spiget.properties 47 | if (hasDirectDownload) { 48 | if (updater.downloadUpdate()) { 49 | // Update downloaded, will be loaded when the server restarts 50 | } else { 51 | // Update failed 52 | getLogger().warning("Update download failed, reason is " + updater.getFailReason()); 53 | } 54 | } 55 | } 56 | 57 | @Override 58 | public void upToDate() { 59 | } 60 | }); 61 | ``` 62 | 63 | ## Maven 64 | ```xml 65 | 66 | 67 | inventive-repo 68 | https://repo.inventivetalent.org/content/groups/public/ 69 | 70 | 71 | 72 | 73 | org.inventivetalent.spiget-update 74 | bukkit|bungee 75 | VERSION 76 | 77 | 78 | ``` 79 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.inventivetalent.spiget-update 8 | parent 9 | pom 10 | parent-SNAPSHOT 11 | 12 | Bukkit 13 | Core 14 | Bungee 15 | 16 | 17 | 18 | UTF-8 19 | 1.4.6-SNAPSHOT 20 | 21 | 22 | 23 | 24 | 25 | maven-compiler-plugin 26 | 3.3 27 | 28 | 8 29 | 8 30 | 31 | 32 | 33 | org.apache.maven.plugins 34 | maven-shade-plugin 35 | 3.2.4 36 | 37 | 38 | package 39 | 40 | shade 41 | 42 | 43 | false 44 | 45 | 46 | org.inventivetalent.spiget-update:** 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | inventive-repo 59 | https://repo.inventivetalent.org/repository/public/ 60 | 61 | 62 | jitpack.io 63 | https://jitpack.io 64 | 65 | 66 | sonatype 67 | https://oss.sonatype.org/content/groups/public/ 68 | 69 | 70 | spigot-repo 71 | https://hub.spigotmc.org/nexus/content/groups/public/ 72 | 73 | 74 | 75 | 76 | 77 | sonatype-nexus-releases 78 | https://repo.inventivetalent.org/repository/maven-releases/ 79 | 80 | 81 | sonatype-nexus-snapshots 82 | https://repo.inventivetalent.org/repository/maven-snapshots/ 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base" 4 | ], 5 | "maven": { 6 | "enabled": true 7 | }, 8 | "ignoreUnstable": false, 9 | "hostRules": [{ 10 | "hostType": "maven", 11 | "endpoint": "https://repo.inventivetalent.org/content/groups/public/" 12 | }] 13 | } 14 | -------------------------------------------------------------------------------- /settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | sonatype-nexus-releases 5 | ${env.CI_DEPLOY_USERNAME} 6 | ${env.CI_DEPLOY_PASSWORD} 7 | 8 | 9 | sonatype-nexus-snapshots 10 | ${env.CI_DEPLOY_USERNAME} 11 | ${env.CI_DEPLOY_PASSWORD} 12 | 13 | 14 | 15 | --------------------------------------------------------------------------------