├── .github └── FUNDING.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── SpigotMC Description.txt ├── allatori └── allatori.xml ├── pom.xml └── src └── main ├── java └── com │ └── jeff_media │ └── updatechecker │ ├── ArtifactVersion.java │ ├── ComparableVersion.java │ ├── DefaultArtifactVersion.java │ ├── SpigetMapper.java │ ├── ThrowingFunction.java │ ├── UpdateCheckEvent.java │ ├── UpdateCheckListener.java │ ├── UpdateCheckResult.java │ ├── UpdateCheckSource.java │ ├── UpdateCheckSuccess.java │ ├── UpdateChecker.java │ ├── UpdateCheckerMessages.java │ ├── UserAgentBuilder.java │ ├── VersionMapper.java │ ├── VersionSupplier.java │ └── package-info.java └── javadoc └── overview.html /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | other: ['https://paypal.me/mfnalex'] 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Project exclude paths 2 | /target/ 3 | 4 | ### Eclipse ### 5 | .metadata 6 | bin/ 7 | tmp/ 8 | *.tmp 9 | *.bak 10 | *.swp 11 | *~.nib 12 | local.properties 13 | .settings/ 14 | .loadpath 15 | .recommenders 16 | .classpath 17 | .project 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # PyDev specific (Python IDE for Eclipse) 26 | *.pydevproject 27 | 28 | # CDT-specific (C/C++ Development Tooling) 29 | .cproject 30 | 31 | # CDT- autotools 32 | .autotools 33 | 34 | # Java annotation processor (APT) 35 | .factorypath 36 | 37 | # PDT-specific (PHP Development Tools) 38 | .buildpath 39 | 40 | # sbteclipse plugin 41 | .target 42 | 43 | # Tern plugin 44 | .tern-project 45 | 46 | # TeXlipse plugin 47 | .texlipse 48 | 49 | # STS (Spring Tool Suite) 50 | .springBeans 51 | 52 | # Code Recommenders 53 | .recommenders/ 54 | 55 | # Annotation Processing 56 | .apt_generated/ 57 | .apt_generated_test/ 58 | 59 | # Scala IDE specific (Scala & Java development for Eclipse) 60 | .cache-main 61 | .scala_dependencies 62 | .worksheet 63 | 64 | # Uncomment this line if you wish to ignore the project description file. 65 | # Typically, this file would be tracked if it contains build/dependency configurations: 66 | #.project 67 | 68 | ### Eclipse Patch ### 69 | # Spring Boot Tooling 70 | .sts4-cache/ 71 | 72 | ### Java ### 73 | # Compiled class file 74 | *.class 75 | 76 | # Log file 77 | *.log 78 | 79 | # BlueJ files 80 | *.ctxt 81 | 82 | # Mobile Tools for Java (J2ME) 83 | .mtj.tmp/ 84 | 85 | # Package Files # 86 | *.jar 87 | *.war 88 | *.nar 89 | *.ear 90 | *.zip 91 | *.tar.gz 92 | *.rar 93 | 94 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 95 | hs_err_pid* 96 | 97 | ### Maven ### 98 | target/ 99 | pom.xml.tag 100 | pom.xml.releaseBackup 101 | pom.xml.versionsBackup 102 | pom.xml.next 103 | release.properties 104 | dependency-reduced-pom.xml 105 | buildNumber.properties 106 | .mvn/timing.properties 107 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar 108 | .mvn/wrapper/maven-wrapper.jar 109 | /lib/ 110 | 111 | ### IntelliJ### 112 | *.iml 113 | .idea/ 114 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 2.3.1 2 | - Added option to show a support link (thanks @lokka30) 3 | 4 | ## 2.3.0 5 | - Switched to new SpigotMC API 6 | 7 | ## 2.2.0 8 | - Added support for Polymart 9 | - Fixed console not showing the custom plugin name when using setNamePaidVersion and setNameFreeVersion 10 | 11 | ## 2.1.0 12 | 13 | Added support for GitHub Release tags. 14 | 15 | Example: 16 | 17 | ```java 18 | UpdateChecker updateChecker = new UpdateChecker(myPugin, UpdateCheckSource.GITHUB_RELEASE_TAG, "JEFF-Media-GbR/ChestSort"); 19 | ``` 20 | 21 | ## 2.0.0 22 | - Added support for Spiget 23 | - Switched from static init() methods and static getInstance() method to a regular constructor. Since more than one UpdateChecker instances can exist at the same time, you are expected to keep track of the instance(s) you created yourself. 24 | - Changed Maven GroupID to com.jeff_media and Java Package name to com.jeff_media.updatechecker 25 | 26 | This is how to create a new UpdateChecker as of 2.0.0 now: 27 | ```java 28 | // Get version information from SpigotMC.org ("12345" is your SpigotMC resource ID): 29 | UpdateChecker updateChecker = new UpdateChecker(myPlugin, UpdateCheckSource.SPIGOT, "12345"); 30 | 31 | // Get version information from Spiget.org ("12345" is your SpigotMC resource ID): 32 | UpdateChecker updateChecker = new UpdateChecker(myPlugin, UpdateCheckSource.SPIGET, "12345"); 33 | 34 | // Get version information from an HTTP(S) URL: 35 | UpdateChecker updateChecker = new UpdateChecker(myPlugin, UpdateCheckSource.SPIGOT, "https://api.jeff-media.com/chestsort/latest-version.txt"); 36 | ``` 37 | 38 | ## 1.3.2 39 | 40 | Attached javadocs and sources 41 | 42 | ## 1.3.1 43 | 44 | Added UpdateChecker#setUsedVersion(String) 45 | 46 | ## 1.3.0 47 | 48 | Reduced file size from 248KB to 33KB by getting rid of the maven-artifact dependency. 49 | 50 | ## 1.2.4 51 | 52 | Changed relocation of dependencies in pom.xml 53 | 54 | ## 1.2.3 55 | 56 | Changed console output formatting a tiny bit 57 | 58 | ## 1.2.2 59 | 60 | Fixed message "Could not check for updates" being shown to OPs on join when an update check hasn't been done yet 61 | 62 | ## 1.2.1 63 | 64 | Fixed `checkNow()`, `checkNow(CommandSender...)` and `stop()` not returning the instance 65 | 66 | ## 1.2.0 67 | 68 | Detects whether the currently used version is newer than the version found by the UpdateChecker 69 | 70 | ## 1.1.0 71 | 72 | Added support for custom tasks to run after update checks 73 | 74 | ## 1.0.0 75 | 76 | Initial release -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SpigotUpdateChecker 2 | 3 |

4 | 5 | SpigotMC Thread 6 | 7 | 8 | Javadocs 9 | 10 | 11 | Discord 12 | 13 | 14 | Donate 15 | 16 |

17 | 18 | 19 | SpigotUpdateChecker is a simple but powerful library to add a perfectly working update checker to your plugins. **Scroll 20 | all the way to the bottom for maven information, JavaDocs and a complete Example Plugin!** 21 | 22 |

23 | 24 |

25 | 26 | Author: mfnalex 27 | 28 | Contributors: MrNemo64, lokka30, GamerCoder215, PauLem 29 | 30 | [Related SpigotMC thread](https://www.spigotmc.org/threads/powerful-update-checker-with-only-one-line-of-code.500010/) 31 | 32 | ## Features 33 | 34 | You can issue manual and repeated update checks and send the result as ingame message to specific players and/or have 35 | them printed to the console. 36 | 37 | All checks are done asynchronously. When the check is done, a custom event is called. The update checker itself listens 38 | to it and can automatically notify Operators on Join or players with a specific permission. 39 | 40 | Of course, you can also just listen to the UpdateCheckEvent yourself to do whatever you like once a new version is 41 | detected. 42 | 43 | It is also possible to define two download links if your plugin is available as both, a free and paid version, and you 44 | can add links to your donation page and changelog. 45 | 46 | You can either provide all those links, including to the API endpoint where the latest version is checked yourself, or 47 | just provide the SpigotMC Resource ID of your plugin for the Update Checker to get those links automatically. 48 | 49 | **Supported API endpoints** to retrieve version information: 50 | 51 | - SpigotMC API 52 | - Spiget API 53 | - Polymart API 54 | - GitHub Release Tags 55 | - Hangar 56 | - Your own custom URL pointing to a text file 57 | - Everything else, if you write your own tiny parser 58 | 59 | ## Maven 60 | 61 | The UpdateChecker is available in my public repository: 62 | 63 | ```xml 64 | 65 | 66 | jeff-media-public 67 | https://repo.jeff-media.com/public/ 68 | 69 | 70 | 71 | 72 | com.jeff_media 73 | SpigotUpdateChecker 74 | 3.0.4 75 | compile 76 | 77 | 78 | ``` 79 | 80 | Please note that you will also have to shade and relocate the UpdateChecker into your .jar file: 81 | 82 | ```xml 83 | 84 | ... 85 | 86 | ... 87 | 88 | org.apache.maven.plugins 89 | maven-shade-plugin 90 | 3.3.0 91 | 92 | 93 | 94 | 95 | 96 | com.jeff_media.updatechecker 97 | your.package.updatechecker 98 | 99 | 100 | 101 | 102 | 103 | package 104 | 105 | shade 106 | 107 | 108 | 109 | 110 | 111 | 112 | ``` 113 | 114 | ## Gradle (Kotlin) 115 | ```kotlin 116 | plugins { 117 | id("com.github.johnrengelman.shadow") version "x.x.x" // Used for shading 118 | } 119 | 120 | repositories { 121 | maven(url = "https://repo.jeff-media.com/public/") 122 | } 123 | 124 | dependencies { 125 | implementation("com.jeff_media:SpigotUpdateChecker:3.0.4") 126 | } 127 | 128 | tasks.named("shadowJar") { 129 | dependencies { 130 | relocate("com.jeff_media", "your.package.lib") { 131 | include(dependency("com.jeff_media:")) 132 | } 133 | } 134 | } 135 | ``` 136 | 137 | 138 | **Failing to relocate the package will make the UpdateChecker throw an exception, so RELOCATE IT!** 139 | 140 | ## Example 141 | 142 | To get a working UpdateChecker, this is already enough: 143 | 144 | ```java 145 | public class MyPlugin extends JavaPlugin { 146 | // To get the Resource ID, look at the number at the end of the URL of your plugin's SpigotMC page 147 | private static final String SPIGOT_RESOURCE_ID = "59773"; 148 | 149 | @Override 150 | public void onEnable() { 151 | new UpdateChecker(this, UpdateCheckSource.SPIGOT, SPIGOT_RESOURCE_ID) // You can also use Spiget instead of Spigot - Spiget's API is usually much faster up to date. 152 | .checkEveryXHours(24) // Check every 24 hours 153 | .checkNow(); // And check right now 154 | } 155 | } 156 | ``` 157 | 158 | The code above will print a message to the console once a new version is available and also send a message to every OP 159 | joining the server. If no new version is found, no message will be sent. 160 | 161 | Of course, there are many more options you can use. For example: 162 | 163 | ```java 164 | import com.jeff_media.updatechecker.UpdateCheckSource; 165 | 166 | public class MyPlugin extends JavaPlugin { 167 | private static final String SPIGOT_RESOURCE_ID = "59773"; 168 | 169 | @Override 170 | public void onEnable() { 171 | new UpdateChecker(this, UpdateCheckSource.CUSTOM_URL, "https://api.jeff-media.de/chestsort/latest-version.txt") // A link to a URL that contains the latest version as String 172 | .setDownloadLink("https://www.chestsort.de") // You can either use a custom URL or the Spigot Resource ID 173 | .setDonationLink("https://paypal.me/mfnalex") 174 | .setChangelogLink(SPIGOT_RESOURCE_ID) // Same as for the Download link: URL or Spigot Resource ID 175 | .setNotifyOpsOnJoin(true) // Notify OPs on Join when a new version is found (default) 176 | .setNotifyByPermissionOnJoin("myplugin.updatechecker") // Also notify people on join with this permission 177 | .setUserAgent(new UserAgentBuilder().addPluginNameAndVersion()) 178 | .checkEveryXHours(0.5) // Check every 30 minutes 179 | .checkNow(); // And check right now 180 | } 181 | } 182 | ``` 183 | 184 | ## Differentiating between free and paid versions 185 | 186 | Now imagine you have two versions of your plugin. One free version and a paid version with extra features, like my 187 | AngelChest Free and AngelChest Plus plugin. If both plugins share the same codebase and only decide on runtime whether 188 | to unlock the premium features, you can easily get something like this working: 189 | 190 | Users of the free version will get links to both versions, so they can see the advantages of your paid version, while we 191 | don't want to send the free version link to users who already bought the paid version. The Update Checker uses 192 | SpigotMC's [Premium Resource Placeholders](https://www.spigotmc.org/wiki/premium-resource-placeholders-identifiers/) 193 | to detect whether a server is using the paid version, but you can also override this detection using 194 | *UpdateChecker#setUsingPaidVersion(boolean)*. 195 | 196 | To achieve this, you can just do this: 197 | 198 | ```java 199 | import com.jeff_media.updatechecker.UpdateCheckSource; 200 | 201 | public class MyPlugin extends JavaPlugin { 202 | private static final int ANGELCHEST_FREE = 60383; 203 | private static final int ANGELCHEST_PLUS = 88214; 204 | private final boolean usingPaidVersion = howEverYouDetectIt(); 205 | 206 | @Override 207 | public void onEnable() { 208 | new UpdateChecker(this, UpdateCheckSource.CUSTOM_URL, "https://api.jeff-media.de/angelchest/latest-version.txt") 209 | .setFreeDownloadLink(ANGELCHEST_FREE) 210 | .setPaidDownloadLink(ANGELCHEST_PLUS) 211 | .setNameFreeVersion("Free") // Optional. It's the suffix for the download links 212 | .setNamePaidVersion("Plus") // when both links are shown. 213 | .checkNow(); 214 | } 215 | } 216 | ``` 217 | 218 | Users of the free version will now see both links: 219 | 220 |

221 | 222 |

223 | 224 | Users of the paid version will however only get the paid version's download link, just like in the screenshots at the 225 | top. 226 | 227 | ## Using Consumers 228 | 229 | You can use Consumers to change the behaviour of the Update Checker. 230 | 231 | ```java 232 | import com.jeff_media.updatechecker.UpdateCheckSource; 233 | 234 | public class MyPlugin extends JavaPlugin { 235 | 236 | @Override 237 | public void onEnable() { 238 | new UpdateChecker(this, UpdateCheckSource.CUSTOM_URL, "https://api.jeff-media.de/angelchest/latest-version.txt") 239 | .setDownloadLink("https://www.chestsort.de") 240 | .onSuccess((commandSenders, latestVersion) -> { 241 | for (CommandSender sender : commandSenders) { 242 | sender.sendMessage("This code will run after the update check was successfull."); 243 | } 244 | }) 245 | .onFail((commandSenders, exception) -> { 246 | for (CommandSender sender : commandSenders) { 247 | sender.sendMessage("This code will run after the update check failed."); 248 | } 249 | }) 250 | .setNotifyRequesters(false) // Do not show the default messages, instead only run our custom consumers 251 | .checkNow(); 252 | } 253 | } 254 | ``` 255 | 256 | ## Building & Obfuscation 257 | 258 | The .jar published in my public repository has been run through allatori to decrease the file size by about 30%. 259 | It does not affect performance in any negative way. 260 | The used obfuscation settings are allowed on SpigotMC for both free and paid plugins. 261 | 262 | **If you like to build it yourself**, just comment out the maven-exec-plugin part in your pom.xml 263 | (currently lines 167 to 192). 264 | 265 | ## JavaDocs and Example plugin 266 | 267 | JavaDocs are available here: https://hub.jeff-media.com/javadocs/spigotupdatechecker/ 268 | 269 | Example plugin: https://github.com/JEFF-Media-GbR/Spigot-UpdateChecker-Example 270 | 271 | ## Other libraries by me 272 | 273 | ### [CustomBlockData](https://github.com/JEFF-Media-GbR/CustomBlockData) 274 | My **CustomBlockData** library provides a PersistentDataContainer for every block in your world - easily save EVERY information you like inside blocks, without any external storage needed! 275 | 276 | ### [MorePersistentDataTypes](https://github.com/JEFF-Media-GbR/MorePersistentDataTypes) 277 | Adds a ton of new **PersistentDataTypes** to use with Bukkit's PersistentDataContainer. 278 | 279 | ## Discord 280 | 281 | Feel free to join my Discord for help. 282 | 283 | 284 | -------------------------------------------------------------------------------- /SpigotMC Description.txt: -------------------------------------------------------------------------------- 1 | [B]Hi everyone :)[/B] 2 | 3 | Today I wanna show you how to get an awesome and fancy Update Checker for your plugins [B]in basically one line of code[/B] (really!), but it's also extremely powerful and customizable! 4 | 5 | [SIZE=2]I once posted something similar as a resource in this forum, but the old resource is basically useless compared to this, so I made another thread.[/SIZE] 6 | 7 | [B]Updates:[/B] 8 | [LIST] 9 | [*][B]1.2.1:[/B] Fixed checkNow(), checkNow(CommandSender...) and stop() not returning the instance 10 | [*][B]1.2.0:[/B] Detects whether the currently used version is newer than the version found by the UpdateChecker 11 | [*][B]1.1.0:[/B] Added Consumers (onSucces, onFail) for custom behaviour without Event Listeners - Thanks [USER=228867]@Nemo_64[/USER] 12 | [/LIST] 13 | 14 | [SIZE=6][B]Overview[/B][/SIZE] 15 | [B]GitHub[/B]: [URL]https://github.com/JEFF-Media-GbR/Spigot-UpdateChecker[/URL] 16 | 17 | The SpigotUpdateChecker is a very easy to use library for you to add a perfectly working update checker to your plugins. [B]Scroll all the way to the bottom for maven information, JavaDocs and a complete Example Plugin![/B] 18 | 19 | [CENTER][IMG]https://api.jeff-media.de/img/updatechecker2.png[/IMG][/CENTER] 20 | 21 | [SIZE=6][B]Features[/B][/SIZE] 22 | You can issue manual and repeated update checks and send the result as ingame message to specific players and/or have them printed to the console. 23 | 24 | All checks are done asynchronously. When the check is done, a custom event is called. The update checker itself listens to it and can automatically notify Operators on Join or players with a specific permission. 25 | 26 | Of course, you can also just listen to the UpdateCheckEvent yourself to do whatever you like once a new version is detected. 27 | 28 | It is also possible to define two download links if your plugin is available as both, a free and paid version, and you can add links to your donation page and changelog. 29 | 30 | You can either provide all those links, including to the API endpoint where the latest version is checked yourself, or just provide the SpigotMC Resource ID of your plugin for the Update Checker to get those links automatically. 31 | 32 | [SIZE=6][B]Example[/B][/SIZE] 33 | To get a working UpdateChecker, this is already enough: 34 | 35 | [CODE=Java]public class MyPlugin extends JavaPlugin { 36 | // To get the Resource ID, look at the number at the end of the URL of your plugin's SpigotMC page 37 | private static final int SPIGOT_RESOURCE_ID = 59773; 38 | 39 | @Override 40 | public void onEnable() { 41 | UpdateChecker.init(this, SPIGOT_RESOURCE_ID) 42 | .checkEveryXHours(24) // Check every 24 hours 43 | .checkNow(); // And check right now 44 | } 45 | }[/CODE] 46 | 47 | The code above will print a message to the console once a new version is available and also send a message to every OP joining the server. If no new version is found, no message will be sent. The whole UpdateChecker initialization would easily fit in one line, anything more is not needed. 48 | 49 | But, of course, there are many more options you can use. For example: 50 | 51 | [CODE=Java]public class MyPlugin extends JavaPlugin { 52 | private static final int SPIGOT_RESOURCE_ID = 59773; 53 | 54 | @Override 55 | public void onEnable() { 56 | UpdateChecker.init(this, "https://api.jeff-media.de/chestsort/latest-version.txt") // A link to a URL that contains the latest version as String 57 | .setDownloadLink("https://www.chestsort.de") // You can either use a custom URL or the Spigot Resource ID 58 | .setDonationLink("https://paypal.me/mfnalex") 59 | .setChangelogLink(SPIGOT_RESOURCE_ID) // Same as for the Download link: URL or Spigot Resource ID 60 | .setNotifyOpsOnJoin(true) // Notify OPs on Join when a new version is found (default) 61 | .setNotifyByPermissionOnJoin("myplugin.updatechecker") // Also notify people on join with this permission 62 | .setUserAgent(new UserAgentBuilder().addPluginNameAndVersion()) 63 | .checkEveryXHours(0.5) // Check every 30 minutes 64 | .checkNow(); // And check right now 65 | } 66 | }[/CODE] 67 | 68 | [SIZE=6][B]Differentiating between free and paid versions[/B][/SIZE] 69 | Now imagine you have two versions of your plugin. One free version and a paid version with extra features, like my AngelChest Free and AngelChest Plus plugin. If both plugins share the same codebase and only decide on runtime whether to unlock the premium features, you can easily get something like this working: 70 | 71 | Users of the free version will get links to both versions, so they can see the advantages of your paid version, while we don't want to send the free version link to users who already bought the paid version. The Update Checker uses SpigotMC's Premium Resource Placeholders to detect whether a server is using the paid version, but you can also override this detection using [I]UpdateChecker#setUsingPaidVersion(boolean)[/I]. 72 | 73 | To achieve this, you can just do this: 74 | 75 | [CODE=Java]public class MyPlugin extends JavaPlugin { 76 | private static final int ANGELCHEST_FREE = 60383; 77 | private static final int ANGELCHEST_PLUS = 88214; 78 | 79 | @Override 80 | public void onEnable() { 81 | UpdateChecker.init(this, "https://api.jeff-media.de/angelchest/latest-version.txt") 82 | .setFreeDownloadLink(ANGELCHEST_FREE) 83 | .setPaidDownloadLink(ANGELCHEST_PLUS) 84 | .setNameFreeVersion("Free") // Optional. It's the suffix for the download links 85 | .setNamePaidVersion("Plus") // when both links are shown. 86 | .checkNow(); 87 | } 88 | }[/CODE] 89 | 90 | Users of the free version will now see both links: 91 | 92 | [CENTER][IMG]https://api.jeff-media.de/img/updatechecker1.png[/IMG][/CENTER] 93 | 94 | Users of the paid version will however only get the paid version's download link, just like in the screenshots at the top. 95 | 96 | [SIZE=6][B]Using Consumers[/B][/SIZE] 97 | 98 | You can use Consumers to change the behaviour of the Update Checker without registering an Event Listener. Thanks [USER=228867]@Nemo_64[/USER] for the Pull request! Example: 99 | 100 | [CODE=Java]public class MyPlugin extends JavaPlugin { 101 | 102 | @Override 103 | public void onEnable() { 104 | UpdateChecker.init(this, "https://api.jeff-media.de/angelchest/latest-version.txt") 105 | .setDownloadLink("https://www.chestsort.de") 106 | .onSuccess((commandSenders, latestVersion) -> { 107 | for(CommandSender sender : commandSenders) { 108 | sender.sendMessage("This code will run after the update check was successfull."); 109 | } 110 | }) 111 | .onFail((commandSenders, exception) -> { 112 | for(CommandSender sender : commandSenders) { 113 | sender.sendMessage("This code will run after the update check failed."); 114 | } 115 | }) 116 | .setNotifyRequesters(false) // Do not show the default messages, instead only run our custom consumers 117 | .checkNow(); 118 | } 119 | }[/CODE] 120 | 121 | [SIZE=6][B]Maven[/B][/SIZE] 122 | The UpdateChecker is available in my public repository: 123 | 124 | [CODE=HTML] 125 | 126 | jeff-media-gbr 127 | https://repo.jeff-media.de/maven2/ 128 | 129 | 130 | 131 | 132 | de.jeff_media 133 | SpigotUpdateChecker 134 | 1.2.0 135 | compile 136 | 137 | [/CODE] 138 | 139 | Please note that you will also have to shade and relocate the UpdateChecker into your .jar file: 140 | 141 | [CODE=HTML] 142 | ... 143 | 144 | ... 145 | 146 | org.apache.maven.plugins 147 | maven-shade-plugin 148 | 3.2.1 149 | 150 | 151 | 152 | 153 | 154 | de.jeff_media.updatechecker 155 | your.package.updatechecker 156 | 157 | 158 | 159 | 160 | 161 | package 162 | 163 | shade 164 | 165 | 166 | 167 | 168 | 169 | [/CODE] 170 | 171 | [B]Failing to relocate the package will make the UpdateChecker throw an exception, so CHANGE IT![/B] 172 | 173 | [SIZE=6][B]JavaDocs and Example plugin[/B][/SIZE] 174 | JavaDocs are available here: [URL]https://repo.jeff-media.de/javadocs/SpigotUpdateChecker/[/URL] 175 | Example plugin: [URL]https://github.com/JEFF-Media-GbR/Spigot-UpdateChecker-Example[/URL] 176 | GitHub: [URL]https://github.com/JEFF-Media-GbR/Spigot-UpdateChecker[/URL] 177 | 178 | [SIZE=6][B]Thanks for reading![/B][/SIZE] 179 | That's all! You can check the JavaDocs and the linked example plugins to learn about this thing. I hope someone finds it useful, I spent at least 5 hours making this today so hopefully I am not the only one who would find it useful :) 180 | 181 | Have a nice day everyone! -------------------------------------------------------------------------------- /allatori/allatori.xml: -------------------------------------------------------------------------------- 1 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.jeff_media 8 | SpigotUpdateChecker 9 | 3.0.4 10 | jar 11 | 12 | 13 | UTF-8 14 | 1.8 15 | 8 16 | 8 17 | 18 | 19 | Powerful update checker for Spigot plugins. 20 | 2021 21 | https://github.com/JEFF-Media-GbR/Spigot-UpdateChecker 22 | 23 | 24 | JEFF Media GbR 25 | https://www.jeff-media.com 26 | 27 | 28 | 29 | 30 | doclint-java8-disable 31 | 32 | [1.8,) 33 | 34 | 35 | 36 | 37 | 38 | org.apache.maven.plugins 39 | maven-javadoc-plugin 40 | 3.3.1 41 | 42 | 43 | -Xdoclint:none 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.apache.maven.plugins 56 | maven-compiler-plugin 57 | 3.8.1 58 | 59 | ${java.version} 60 | ${java.version} 61 | 62 | 63 | 64 | org.apache.maven.plugins 65 | maven-shade-plugin 66 | 3.5.0 67 | 68 | true 69 | 70 | 71 | 72 | package 73 | 74 | shade 75 | 76 | 77 | 78 | 79 | *:* 80 | 81 | META-INF/** 82 | 83 | 84 | 85 | 86 | 87 | com.github.Anon8281:UniversalScheduler 88 | 89 | 90 | 91 | 92 | com.github.Anon8281.universalScheduler 93 | com.jeff_media.updatechecker.universalScheduler 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | org.apache.maven.plugins 102 | maven-jar-plugin 103 | 3.2.0 104 | 105 | ${project.name} 106 | 107 | 108 | 109 | org.apache.maven.plugins 110 | maven-javadoc-plugin 111 | 112 | 113 | attach-javadocs 114 | 115 | jar 116 | 117 | 118 | 119 | 120 | 121 | org.apache.maven.plugins 122 | maven-source-plugin 123 | 124 | 125 | attach-sources 126 | 127 | jar 128 | 129 | 130 | 131 | 132 | 133 | org.apache.maven.plugins 134 | maven-enforcer-plugin 135 | 3.0.0 136 | 137 | 138 | enforce-maven 139 | 140 | enforce 141 | 142 | 143 | 144 | 145 | 3.1.1 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | spigot-repo 158 | https://hub.spigotmc.org/nexus/content/repositories/snapshots/ 159 | 160 | 161 | jitpack.io 162 | https://jitpack.io 163 | 164 | 165 | 166 | 167 | 168 | org.spigotmc 169 | spigot-api 170 | 1.18.2-R0.1-SNAPSHOT 171 | provided 172 | 173 | 174 | org.projectlombok 175 | lombok 176 | 1.18.24 177 | provided 178 | 179 | 180 | org.jetbrains 181 | annotations-java5 182 | 23.0.0 183 | provided 184 | 185 | 186 | com.github.Anon8281 187 | UniversalScheduler 188 | 0.1.6 189 | compile 190 | 191 | 192 | 193 | 194 | 195 | jeff-media-public 196 | https://repo.jeff-media.com/public/ 197 | 198 | 199 | 200 | 201 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/ArtifactVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | interface ArtifactVersion extends Comparable { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/ComparableVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | import java.math.BigInteger; 22 | import java.util.*; 23 | 24 | class ComparableVersion implements Comparable { 25 | private String value; 26 | private ListItem items; 27 | 28 | public ComparableVersion(final String version) { 29 | this.parseVersion(version); 30 | } 31 | 32 | public final void parseVersion(String version) { 33 | this.value = version; 34 | this.items = new ListItem(); 35 | version = version.toLowerCase(Locale.ENGLISH); 36 | ListItem list = this.items; 37 | final Deque stack = new ArrayDeque<>(); 38 | stack.push(list); 39 | boolean isDigit = false; 40 | int startIndex = 0; 41 | for (int i = 0; i < version.length(); ++i) { 42 | final char c = version.charAt(i); 43 | if (c == '.') { 44 | if (i == startIndex) { 45 | (list).add(IntItem.ZERO); 46 | } else { 47 | list.add(parseItem(isDigit, version.substring(startIndex, i))); 48 | } 49 | startIndex = i + 1; 50 | } else if (c == '-') { 51 | if (i == startIndex) { 52 | (list).add(IntItem.ZERO); 53 | } else { 54 | list.add(parseItem(isDigit, version.substring(startIndex, i))); 55 | } 56 | startIndex = i + 1; 57 | (list).add(list = new ListItem()); 58 | stack.push(list); 59 | } else if (Character.isDigit(c)) { 60 | if (!isDigit && i > startIndex) { 61 | (list).add(new StringItem(version.substring(startIndex, i), true)); 62 | startIndex = i; 63 | (list).add(list = new ListItem()); 64 | stack.push(list); 65 | } 66 | isDigit = true; 67 | } else { 68 | if (isDigit && i > startIndex) { 69 | list.add(parseItem(true, version.substring(startIndex, i))); 70 | startIndex = i; 71 | (list).add(list = new ListItem()); 72 | stack.push(list); 73 | } 74 | isDigit = false; 75 | } 76 | } 77 | if (version.length() > startIndex) { 78 | list.add(parseItem(isDigit, version.substring(startIndex))); 79 | } 80 | while (!stack.isEmpty()) { 81 | list = (ListItem) stack.pop(); 82 | list.normalize(); 83 | } 84 | } 85 | 86 | private static Item parseItem(final boolean isDigit, String buf) { 87 | if (!isDigit) { 88 | return new StringItem(buf, false); 89 | } 90 | buf = stripLeadingZeroes(buf); 91 | if (buf.length() <= 9) { 92 | return new IntItem(buf); 93 | } 94 | if (buf.length() <= 18) { 95 | return new LongItem(buf); 96 | } 97 | return new BigIntegerItem(buf); 98 | } 99 | 100 | private static String stripLeadingZeroes(final String buf) { 101 | if (buf == null || buf.isEmpty()) { 102 | return "0"; 103 | } 104 | for (int i = 0; i < buf.length(); ++i) { 105 | final char c = buf.charAt(i); 106 | if (c != '0') { 107 | return buf.substring(i); 108 | } 109 | } 110 | return buf; 111 | } 112 | 113 | @Override 114 | public int compareTo(final ComparableVersion o) { 115 | return this.items.compareTo(o.items); 116 | } 117 | 118 | @Override 119 | public int hashCode() { 120 | return this.items.hashCode(); 121 | } 122 | 123 | @Override 124 | public boolean equals(final Object o) { 125 | return o instanceof ComparableVersion && this.items.equals(((ComparableVersion) o).items); 126 | } 127 | 128 | @Override 129 | public String toString() { 130 | return this.value; 131 | } 132 | 133 | private interface Item { 134 | 135 | int compareTo(final Item p0); 136 | 137 | int getType(); 138 | 139 | boolean isNull(); 140 | } 141 | 142 | private static class IntItem implements Item { 143 | public static final IntItem ZERO; 144 | 145 | static { 146 | ZERO = new IntItem(); 147 | } 148 | 149 | private final int value; 150 | 151 | private IntItem() { 152 | this.value = 0; 153 | } 154 | 155 | IntItem(final String str) { 156 | this.value = Integer.parseInt(str); 157 | } 158 | 159 | @Override 160 | public int compareTo(final Item item) { 161 | if (item == null) { 162 | return (this.value != 0) ? 1 : 0; 163 | } 164 | switch (item.getType()) { 165 | case 3: { 166 | final int itemValue = ((IntItem) item).value; 167 | return Integer.compare(this.value, itemValue); 168 | } 169 | case 0: 170 | case 4: { 171 | return -1; 172 | } 173 | case 1: 174 | case 2: { 175 | return 1; 176 | } 177 | default: { 178 | throw new IllegalStateException("invalid item: " + item.getClass()); 179 | } 180 | } 181 | } 182 | 183 | @Override 184 | public int getType() { 185 | return 3; 186 | } 187 | 188 | @Override 189 | public boolean isNull() { 190 | return this.value == 0; 191 | } 192 | 193 | @Override 194 | public int hashCode() { 195 | return this.value; 196 | } 197 | 198 | @Override 199 | public boolean equals(final Object o) { 200 | if (this == o) { 201 | return true; 202 | } 203 | if (o == null || this.getClass() != o.getClass()) { 204 | return false; 205 | } 206 | final IntItem intItem = (IntItem) o; 207 | return this.value == intItem.value; 208 | } 209 | 210 | @Override 211 | public String toString() { 212 | return Integer.toString(this.value); 213 | } 214 | } 215 | 216 | private static class LongItem implements Item { 217 | private final long value; 218 | 219 | LongItem(final String str) { 220 | this.value = Long.parseLong(str); 221 | } 222 | 223 | @Override 224 | public int compareTo(final Item item) { 225 | if (item == null) { 226 | return (this.value != 0L) ? 1 : 0; 227 | } 228 | switch (item.getType()) { 229 | case 3: 230 | case 1: 231 | case 2: { 232 | return 1; 233 | } 234 | case 4: { 235 | final long itemValue = ((LongItem) item).value; 236 | return Long.compare(this.value, itemValue); 237 | } 238 | case 0: { 239 | return -1; 240 | } 241 | default: { 242 | throw new IllegalStateException("invalid item: " + item.getClass()); 243 | } 244 | } 245 | } 246 | 247 | @Override 248 | public int getType() { 249 | return 4; 250 | } 251 | 252 | @Override 253 | public boolean isNull() { 254 | return this.value == 0L; 255 | } 256 | 257 | @Override 258 | public int hashCode() { 259 | return (int) (this.value ^ this.value >>> 32); 260 | } 261 | 262 | @Override 263 | public boolean equals(final Object o) { 264 | if (this == o) { 265 | return true; 266 | } 267 | if (o == null || this.getClass() != o.getClass()) { 268 | return false; 269 | } 270 | final LongItem longItem = (LongItem) o; 271 | return this.value == longItem.value; 272 | } 273 | 274 | @Override 275 | public String toString() { 276 | return Long.toString(this.value); 277 | } 278 | } 279 | 280 | private static class BigIntegerItem implements Item { 281 | private final BigInteger value; 282 | 283 | BigIntegerItem(final String str) { 284 | this.value = new BigInteger(str); 285 | } 286 | 287 | @Override 288 | public int compareTo(final Item item) { 289 | if (item == null) { 290 | return BigInteger.ZERO.equals(this.value) ? 0 : 1; 291 | } 292 | switch (item.getType()) { 293 | case 3: 294 | case 4: 295 | case 1: 296 | case 2: { 297 | return 1; 298 | } 299 | case 0: { 300 | return this.value.compareTo(((BigIntegerItem) item).value); 301 | } 302 | default: { 303 | throw new IllegalStateException("invalid item: " + item.getClass()); 304 | } 305 | } 306 | } 307 | 308 | @Override 309 | public int getType() { 310 | return 0; 311 | } 312 | 313 | @Override 314 | public boolean isNull() { 315 | return BigInteger.ZERO.equals(this.value); 316 | } 317 | 318 | @Override 319 | public int hashCode() { 320 | return this.value.hashCode(); 321 | } 322 | 323 | @Override 324 | public boolean equals(final Object o) { 325 | if (this == o) { 326 | return true; 327 | } 328 | if (o == null || this.getClass() != o.getClass()) { 329 | return false; 330 | } 331 | final BigIntegerItem that = (BigIntegerItem) o; 332 | return this.value.equals(that.value); 333 | } 334 | 335 | @Override 336 | public String toString() { 337 | return this.value.toString(); 338 | } 339 | } 340 | 341 | private static class StringItem implements Item { 342 | private static final List QUALIFIERS; 343 | private static final Properties ALIASES; 344 | private static final String RELEASE_VERSION_INDEX; 345 | 346 | static { 347 | QUALIFIERS = Arrays.asList("alpha", "beta", "milestone", "rc", "snapshot", "", "sp"); 348 | ((ALIASES = new Properties())).put("ga", ""); 349 | (StringItem.ALIASES).put("final", ""); 350 | (StringItem.ALIASES).put("release", ""); 351 | (StringItem.ALIASES).put("cr", "rc"); 352 | RELEASE_VERSION_INDEX = String.valueOf(StringItem.QUALIFIERS.indexOf("")); 353 | } 354 | 355 | private final String value; 356 | 357 | StringItem(String value, final boolean followedByDigit) { 358 | if (followedByDigit && value.length() == 1) { 359 | switch (value.charAt(0)) { 360 | case 'a': { 361 | value = "alpha"; 362 | break; 363 | } 364 | case 'b': { 365 | value = "beta"; 366 | break; 367 | } 368 | case 'm': { 369 | value = "milestone"; 370 | break; 371 | } 372 | } 373 | } 374 | this.value = StringItem.ALIASES.getProperty(value, value); 375 | } 376 | 377 | @Override 378 | public int compareTo(final Item item) { 379 | if (item == null) { 380 | return comparableQualifier(this.value).compareTo(StringItem.RELEASE_VERSION_INDEX); 381 | } 382 | switch (item.getType()) { 383 | case 0: 384 | case 3: 385 | case 4: 386 | case 2: { 387 | return -1; 388 | } 389 | case 1: { 390 | return comparableQualifier(this.value).compareTo(comparableQualifier(((StringItem) item).value)); 391 | } 392 | default: { 393 | throw new IllegalStateException("invalid item: " + item.getClass()); 394 | } 395 | } 396 | } 397 | 398 | @Override 399 | public int getType() { 400 | return 1; 401 | } 402 | 403 | @Override 404 | public boolean isNull() { 405 | return comparableQualifier(this.value).compareTo(StringItem.RELEASE_VERSION_INDEX) == 0; 406 | } 407 | 408 | public static String comparableQualifier(final String qualifier) { 409 | final int i = StringItem.QUALIFIERS.indexOf(qualifier); 410 | return (i == -1) ? (StringItem.QUALIFIERS.size() + "-" + qualifier) : String.valueOf(i); 411 | } 412 | 413 | @Override 414 | public int hashCode() { 415 | return this.value.hashCode(); 416 | } 417 | 418 | @Override 419 | public boolean equals(final Object o) { 420 | if (this == o) { 421 | return true; 422 | } 423 | if (o == null || this.getClass() != o.getClass()) { 424 | return false; 425 | } 426 | final StringItem that = (StringItem) o; 427 | return this.value.equals(that.value); 428 | } 429 | 430 | @Override 431 | public String toString() { 432 | return this.value; 433 | } 434 | } 435 | 436 | private static class ListItem extends ArrayList implements Item { 437 | void normalize() { 438 | for (int i = this.size() - 1; i >= 0; --i) { 439 | final Item lastItem = this.get(i); 440 | if (lastItem.isNull()) { 441 | this.remove(i); 442 | } else if (!(lastItem instanceof ListItem)) { 443 | break; 444 | } 445 | } 446 | } 447 | 448 | @Override 449 | public int compareTo(final Item item) { 450 | if (item == null) { 451 | if (this.size() == 0) { 452 | return 0; 453 | } 454 | final Item first = this.get(0); 455 | return first.compareTo(null); 456 | } else { 457 | switch (item.getType()) { 458 | case 0: 459 | case 3: 460 | case 4: { 461 | return -1; 462 | } 463 | case 1: { 464 | return 1; 465 | } 466 | case 2: { 467 | final Iterator left = this.iterator(); 468 | final Iterator right = ((ListItem) item).iterator(); 469 | while (left.hasNext() || right.hasNext()) { 470 | final Item l = left.hasNext() ? left.next() : null; 471 | final Item r = right.hasNext() ? right.next() : null; 472 | //noinspection ConstantConditions 473 | final int result = (l == null) ? ((r == null) ? 0 : (-1 * r.compareTo(l))) : l.compareTo(r); 474 | if (result != 0) { 475 | return result; 476 | } 477 | } 478 | return 0; 479 | } 480 | default: { 481 | throw new IllegalStateException("invalid item: " + item.getClass()); 482 | } 483 | } 484 | } 485 | } 486 | 487 | @Override 488 | public int getType() { 489 | return 2; 490 | } 491 | 492 | @Override 493 | public boolean isNull() { 494 | return this.size() == 0; 495 | } 496 | 497 | @Override 498 | public String toString() { 499 | final StringBuilder buffer = new StringBuilder(); 500 | for (final Item item : this) { 501 | if (buffer.length() > 0) { 502 | buffer.append((item instanceof ListItem) ? '-' : '.'); 503 | } 504 | buffer.append(item); 505 | } 506 | return buffer.toString(); 507 | } 508 | } 509 | } 510 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/DefaultArtifactVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | import org.jetbrains.annotations.NotNull; 22 | 23 | class DefaultArtifactVersion implements ArtifactVersion { 24 | private ComparableVersion comparable; 25 | 26 | public DefaultArtifactVersion(final String version) { 27 | this.parseVersion(version); 28 | } 29 | 30 | @Override 31 | public int hashCode() { 32 | return 11 + this.comparable.hashCode(); 33 | } 34 | 35 | @Override 36 | public boolean equals(final Object other) { 37 | return this == other || (other instanceof ArtifactVersion && this.compareTo((ArtifactVersion)other) == 0); 38 | } 39 | 40 | @Override 41 | public int compareTo(final @NotNull ArtifactVersion otherVersion) { 42 | if (otherVersion instanceof DefaultArtifactVersion) { 43 | return this.comparable.compareTo(((DefaultArtifactVersion)otherVersion).comparable); 44 | } 45 | return this.compareTo(new DefaultArtifactVersion(otherVersion.toString())); 46 | } 47 | 48 | public final void parseVersion(final String version) { 49 | this.comparable = new ComparableVersion(version); 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return this.comparable.toString(); 55 | } 56 | } -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/SpigetMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | import com.google.gson.Gson; 22 | import com.google.gson.JsonObject; 23 | 24 | import java.io.BufferedReader; 25 | import java.io.IOException; 26 | 27 | class SpigetMapper implements ThrowingFunction { 28 | @Override 29 | public String apply(BufferedReader bufferedReader) { 30 | return new Gson().fromJson(bufferedReader, JsonObject.class).get("name").getAsString(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/ThrowingFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | @FunctionalInterface 22 | interface ThrowingFunction { 23 | R apply(T t) throws E; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/UpdateCheckEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | import org.bukkit.command.CommandSender; 22 | import org.bukkit.event.Event; 23 | import org.bukkit.event.HandlerList; 24 | import org.jetbrains.annotations.NotNull; 25 | import org.jetbrains.annotations.Nullable; 26 | 27 | /** 28 | * This event is called whenever an update check is finished. 29 | */ 30 | public class UpdateCheckEvent extends Event { 31 | 32 | private static final HandlerList HANDLERS = new HandlerList(); 33 | private final UpdateChecker instance; 34 | private final UpdateCheckResult result; 35 | private final UpdateCheckSuccess success; 36 | private @Nullable CommandSender[] requesters = null; 37 | 38 | protected UpdateCheckEvent(UpdateCheckSuccess success) { 39 | instance = UpdateChecker.getInstance(); 40 | this.success = success; 41 | if (success == UpdateCheckSuccess.FAIL && instance.getLatestVersion() == null) { 42 | result = UpdateCheckResult.UNKNOWN; 43 | } else { 44 | if (instance.isUsingLatestVersion()) { 45 | result = UpdateCheckResult.RUNNING_LATEST_VERSION; 46 | } else { 47 | result = UpdateCheckResult.NEW_VERSION_AVAILABLE; 48 | } 49 | } 50 | } 51 | 52 | @SuppressWarnings("unused") 53 | public static HandlerList getHandlerList() { 54 | return HANDLERS; 55 | } 56 | 57 | @Override 58 | public @NotNull HandlerList getHandlers() { 59 | return HANDLERS; 60 | } 61 | 62 | /** 63 | * Returns the latest version string found by the UpdateChecker, or null if all previous checks have failed. 64 | * 65 | * @return Latest version string found by the UpdateChecker, or null if all previous checks have failed 66 | */ 67 | public @Nullable String getLatestVersion() { 68 | return instance.getLatestVersion(); 69 | } 70 | 71 | /** 72 | * Gets an array of all CommandSenders who have requested this update check. Normally this will either be the ConsoleCommandSender or a player. 73 | * 74 | * @return Array of all CommandSenders who have requested this update check 75 | */ 76 | public @Nullable CommandSender[] getRequesters() { 77 | if (requesters == null || requesters.length == 0) return null; 78 | return requesters; 79 | } 80 | 81 | /** 82 | * Sets the CommandSenders who requested this update check. 83 | * 84 | * @param requesters CommandSenders who requested this update check 85 | * @return UpdateCheckEvent instance 86 | */ 87 | protected UpdateCheckEvent setRequesters(@Nullable CommandSender... requesters) { 88 | this.requesters = requesters; 89 | return this; 90 | } 91 | 92 | /** 93 | * Gets the result, i.e. whether a new version is available or not. 94 | * 95 | * @return UpdateCheckResult of this update check 96 | */ 97 | public UpdateCheckResult getResult() { 98 | return result; 99 | } 100 | 101 | /** 102 | * Checks whether the update checking attempt was successful or failed. 103 | * 104 | * @return UpdateCheckSuccess of this update check 105 | */ 106 | public UpdateCheckSuccess getSuccess() { 107 | return success; 108 | } 109 | 110 | /** 111 | * Gets the version string of the currently used plugin version. 112 | * 113 | * @return Version string of the currently used plugin version 114 | */ 115 | public @NotNull String getUsedVersion() { 116 | return instance.getUsedVersion(); 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/UpdateCheckListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | import org.bukkit.command.CommandSender; 22 | import org.bukkit.entity.Player; 23 | import org.bukkit.event.EventHandler; 24 | import org.bukkit.event.Listener; 25 | import org.bukkit.event.player.PlayerJoinEvent; 26 | 27 | class UpdateCheckListener implements Listener { 28 | 29 | private final UpdateChecker instance; 30 | 31 | UpdateCheckListener() { 32 | instance = UpdateChecker.getInstance(); 33 | } 34 | 35 | @SuppressWarnings("unused") 36 | @EventHandler 37 | public void notifyOnJoin(PlayerJoinEvent playerJoinEvent) { 38 | if (!instance.isCheckedAtLeastOnce()) return; 39 | Player player = playerJoinEvent.getPlayer(); 40 | if ((player.isOp() && instance.isNotifyOpsOnJoin()) || (instance.getNotifyPermission() != null && player.hasPermission(instance.getNotifyPermission()))) { 41 | UpdateCheckerMessages.printCheckResultToPlayer(player, false); 42 | } 43 | } 44 | 45 | @SuppressWarnings("unused") 46 | @EventHandler 47 | public void onUpdateCheck(UpdateCheckEvent event) { 48 | if (!instance.isCheckedAtLeastOnce()) return; 49 | if (!instance.isNotifyRequesters()) return; 50 | if (event.getRequesters() == null) return; 51 | for (CommandSender commandSender : event.getRequesters()) { 52 | if (commandSender instanceof Player) { 53 | UpdateCheckerMessages.printCheckResultToPlayer((Player) commandSender, true); 54 | } else { 55 | UpdateCheckerMessages.printCheckResultToConsole(event); 56 | } 57 | 58 | } 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/UpdateCheckResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | /** 22 | * Represents whether a new version is available or not 23 | */ 24 | public enum UpdateCheckResult { 25 | /** 26 | * Result when a new version is available 27 | */ 28 | NEW_VERSION_AVAILABLE, 29 | /** 30 | * Result when the plugin is already the latest version 31 | */ 32 | RUNNING_LATEST_VERSION, 33 | /** 34 | * Result when the update check failed, or the version could not be compared 35 | */ 36 | UNKNOWN 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/UpdateCheckSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | import org.bukkit.plugin.java.JavaPlugin; 22 | 23 | /** 24 | * Represents the source from where to fetch update information. 25 | */ 26 | public enum UpdateCheckSource { 27 | /** 28 | * SpigotMC API. Trustworthy, but slow. Requires the SpigotMC resource ID (the number at the end of your plugin's SpigotMC URL) as parameter in {@link UpdateChecker#UpdateChecker(JavaPlugin, UpdateCheckSource, String)}. 29 | */ 30 | SPIGOT, 31 | /** 32 | * Polymart API. Trustworthy, but slow. Requires the Polymart resource ID (the number at the end of your plugin's Polymart URL) as parameter in {@link UpdateChecker#UpdateChecker(JavaPlugin, UpdateCheckSource, String)}. 33 | */ 34 | POLYMART, 35 | /** 36 | * Spiget API. Not official, but faster than SpigotMC API. Requires the SpigotMC resource ID (the number at the end of your plugin's SpigotMC URL) as parameter in {@link UpdateChecker#UpdateChecker(JavaPlugin, UpdateCheckSource, String)}. 37 | */ 38 | SPIGET, 39 | /** 40 | * GitHub Releases API. Requires your repository in the format "UserName/RepositoryName" (for example: "JEFF-Media-GbR/ChestSort") as parameter in {@link UpdateChecker#UpdateChecker(JavaPlugin, UpdateCheckSource, String)}. It will use the latest release's tag string. 41 | */ 42 | GITHUB_RELEASE_TAG, 43 | /** 44 | * Hangar API. Requires your resource in the format "UserName/ProjectName/ReleaseChannel" (for example: "JEFF-Media-GbR/ChestSort/Release") as parameter in {@link UpdateChecker#UpdateChecker(JavaPlugin, UpdateCheckSource, String)}. It will use the latest release's version string according to that release channel. 45 | */ 46 | HANGAR, 47 | /** 48 | * Custom link on where to fetch update checking information. Requires an HTTP or HTTPS URL as parameter in {@link UpdateChecker#UpdateChecker(JavaPlugin, UpdateCheckSource, String)}. The linked file must be a plaintext file containing only for the latest version string. 49 | */ 50 | CUSTOM_URL 51 | } -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/UpdateCheckSuccess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | /** 22 | * Represents whether the update check was successful 23 | */ 24 | public enum UpdateCheckSuccess { 25 | SUCCESS, 26 | FAIL 27 | } -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/UpdateChecker.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | import com.github.Anon8281.universalScheduler.UniversalScheduler; 22 | import com.github.Anon8281.universalScheduler.scheduling.schedulers.TaskScheduler; 23 | import com.github.Anon8281.universalScheduler.scheduling.tasks.MyScheduledTask; 24 | import org.bukkit.Bukkit; 25 | import org.bukkit.command.CommandSender; 26 | import org.bukkit.plugin.Plugin; 27 | import org.bukkit.plugin.java.JavaPlugin; 28 | import org.jetbrains.annotations.NotNull; 29 | import org.jetbrains.annotations.Nullable; 30 | 31 | import java.io.BufferedReader; 32 | import java.io.IOException; 33 | import java.io.InputStreamReader; 34 | import java.net.HttpURLConnection; 35 | import java.net.URL; 36 | import java.util.ArrayList; 37 | import java.util.List; 38 | import java.util.Objects; 39 | import java.util.function.BiConsumer; 40 | 41 | /** 42 | * Main class. Automatically checks for updates. 43 | */ 44 | @SuppressWarnings("UnusedReturnValue") 45 | public class UpdateChecker { 46 | 47 | static final String VERSION = "3.0.4"; 48 | private static final String SPIGOT_CHANGELOG_SUFFIX = "/history"; 49 | private static final String SPIGOT_DOWNLOAD_LINK = "https://www.spigotmc.org/resources/"; 50 | private static final String SPIGOT_UPDATE_API = "https://api.spigotmc.org/simple/0.2/index.php?action=getResource&id=%s"; 51 | private static final String POLYMART_CHANGELOG_SUFFIX = "/updates"; 52 | private static final String POLYMART_DOWNLOAD_LINK = "https://polymart.org/resource/"; 53 | private static final String POLYMART_UPDATE_API = "https://api.polymart.org/v1/getResourceInfoSimple/?resource_id=%s&key=version"; 54 | private static final String SPIGET_UPDATE_API = "https://api.spiget.org/v2/resources/%s/versions/latest"; 55 | private static final String GITHUB_RELEASE_API = "https://api.github.com/repos/%s/%s/releases"; 56 | private static final String HANGAR_RELEASE_API = "https://hangar.papermc.io/api/v1/projects/%s/%s/latest?channel=%s"; 57 | private static UpdateChecker instance = null; 58 | private static boolean listenerAlreadyRegistered = false; 59 | 60 | static { 61 | checkRelocation(); 62 | } 63 | 64 | private final String spigotUserId = "%%__USER__%%"; 65 | private final String apiLink; 66 | private final ThrowingFunction mapper; 67 | private final UpdateCheckSource updateCheckSource; 68 | private final VersionSupplier supplier; 69 | private final Plugin plugin; 70 | private String changelogLink = null; 71 | private boolean checkedAtLeastOnce = false; 72 | private boolean coloredConsoleOutput = false; 73 | private String donationLink = null; 74 | private String freeDownloadLink = null; 75 | private String latestVersion = null; 76 | private String nameFreeVersion = "Free"; 77 | private String namePaidVersion = "Paid"; 78 | private boolean notifyOpsOnJoin = true; 79 | private String notifyPermission = null; 80 | private boolean notifyRequesters = true; 81 | private String supportLink = null; 82 | private boolean suppressUpToDateMessage = true; 83 | private BiConsumer onFail = (requesters, ex) -> ex.printStackTrace(); 84 | private BiConsumer onSuccess = (requesters, latestVersion) -> { 85 | }; 86 | private String paidDownloadLink = null; 87 | private static TaskScheduler scheduler; 88 | @Nullable 89 | private MyScheduledTask updaterTask = null; 90 | private int timeout = 0; 91 | private String usedVersion; 92 | private String userAgentString = null; 93 | private boolean usingPaidVersion = false; 94 | 95 | { 96 | instance = this; 97 | } 98 | 99 | /** 100 | * Initializes an UpdateChecker instance with a custom {@link VersionSupplier}. 101 | * 102 | * @param plugin Instance of your plugin 103 | * @param supplier VersionSupplier that supplies the latest version of your plugin 104 | */ 105 | public UpdateChecker(@NotNull JavaPlugin plugin, @NotNull VersionSupplier supplier) { 106 | this.plugin = plugin; 107 | this.apiLink = null; 108 | this.supplier = supplier; 109 | this.updateCheckSource = null; 110 | this.mapper = null; 111 | init(); 112 | } 113 | 114 | private void init() { 115 | Objects.requireNonNull(plugin, "Plugin cannot be null."); 116 | 117 | this.usedVersion = plugin.getDescription().getVersion().trim(); 118 | 119 | if (detectPaidVersion()) { 120 | usingPaidVersion = true; 121 | } 122 | 123 | scheduler = UniversalScheduler.getScheduler(plugin); 124 | 125 | if (!listenerAlreadyRegistered) { 126 | Bukkit.getPluginManager().registerEvents(new UpdateCheckListener(), plugin); 127 | listenerAlreadyRegistered = true; 128 | } 129 | } 130 | 131 | /** 132 | * Detects whether the Spigot User ID placeholder has been properly replaced by a numeric string 133 | * 134 | * @return true if the Spigot User ID placeholder has been properly replaced by a numeric string 135 | */ 136 | private boolean detectPaidVersion() { 137 | return spigotUserId.matches("^[0-9]+$"); 138 | } 139 | 140 | /** 141 | * Initializes an UpdateChecker instance. 142 | * 143 | * @param plugin Instance of your plugin 144 | * @param updateCheckSource Source where to check for updates. To use a custom source, see {@link UpdateChecker#UpdateChecker(JavaPlugin, VersionSupplier)} 145 | * @param parameter Parameter for the update checker source. See {@link UpdateCheckSource} for more informatino 146 | */ 147 | public UpdateChecker(@NotNull JavaPlugin plugin, @NotNull UpdateCheckSource updateCheckSource, @NotNull String parameter) { 148 | 149 | this.plugin = plugin; 150 | 151 | this.supplier = null; 152 | 153 | final String apiLink; 154 | final ThrowingFunction mapper; 155 | 156 | switch (this.updateCheckSource = updateCheckSource) { 157 | case CUSTOM_URL: { 158 | apiLink = parameter; 159 | mapper = VersionMapper.TRIM_FIRST_LINE; 160 | break; 161 | } 162 | case SPIGOT: { 163 | apiLink = String.format(SPIGOT_UPDATE_API, parameter); 164 | mapper = VersionMapper.SPIGOT; 165 | break; 166 | } 167 | case POLYMART: { 168 | apiLink = String.format(POLYMART_UPDATE_API, parameter); 169 | mapper = VersionMapper.TRIM_FIRST_LINE; 170 | break; 171 | } 172 | case SPIGET: { 173 | apiLink = String.format(SPIGET_UPDATE_API, parameter); 174 | mapper = VersionMapper.SPIGET; 175 | break; 176 | } 177 | case GITHUB_RELEASE_TAG: { 178 | String[] split = parameter.split("/"); 179 | if (split.length < 2) { 180 | throw new IllegalArgumentException("Given GitHub repository must be in the format \"/\""); 181 | } 182 | apiLink = String.format(GITHUB_RELEASE_API, split[0], split[1]); 183 | mapper = VersionMapper.GITHUB_RELEASE_TAG; 184 | break; 185 | } 186 | case HANGAR: { 187 | String[] split = parameter.split("/"); 188 | if (split.length <3) { 189 | throw new IllegalArgumentException("Given HangarMC project must be in the format \"//\""); 190 | } 191 | 192 | apiLink = String.format(HANGAR_RELEASE_API, split[0], split[1], split[2]); 193 | mapper = VersionMapper.TRIM_FIRST_LINE; 194 | break; 195 | } 196 | default: 197 | throw new UnsupportedOperationException(); 198 | } 199 | 200 | Objects.requireNonNull(apiLink, "API Link cannot be null."); 201 | 202 | this.apiLink = apiLink; 203 | this.mapper = mapper; 204 | 205 | init(); 206 | 207 | } 208 | 209 | /** 210 | * Gets the current UpdateChecker singleton if it has been created, otherwise null. 211 | * 212 | * @return UpdateChecker instance being ran, or null if {@link #UpdateChecker(JavaPlugin, UpdateCheckSource, String)} wasn't called yet. 213 | * @deprecated As of SpigotUpdateChecker 1.4.0, more than one instance can exist at the same time. Keep track of the instances you created yourself. 214 | */ 215 | @Deprecated 216 | public static UpdateChecker getInstance() { 217 | return instance; 218 | } 219 | 220 | /** 221 | * Initializes the UpdateChecker instance. HAS to be called before the 222 | * UpdateChecker can run. 223 | * 224 | * @param plugin Main class of your plugin 225 | * @param spigotResourceId SpigotMC Resource ID to get the latest version String 226 | * from the SpigotMC Web API 227 | * @return UpdateChecker instance being ran 228 | * @deprecated Use {@link #UpdateChecker(JavaPlugin, UpdateCheckSource, String)} instead. 229 | */ 230 | @Deprecated 231 | public static UpdateChecker init(@NotNull JavaPlugin plugin, int spigotResourceId) { 232 | return new UpdateChecker(plugin, UpdateCheckSource.SPIGOT, String.valueOf(spigotResourceId)); 233 | } 234 | 235 | /** 236 | * Initializes the UpdateChecker instance. HAS to be called before the 237 | * UpdateChecker can run. 238 | * 239 | * @param plugin Main class of your plugin 240 | * @param apiLink HTTP(S) link to a file containing a string with the latest 241 | * version of your plugin. 242 | * @return UpdateChecker instance being ran 243 | * @deprecated Use {@link #UpdateChecker(JavaPlugin, UpdateCheckSource, String)} instead. 244 | */ 245 | @Deprecated 246 | public static UpdateChecker init(@NotNull JavaPlugin plugin, @NotNull String apiLink) { 247 | return new UpdateChecker(plugin, UpdateCheckSource.CUSTOM_URL, apiLink); 248 | } 249 | 250 | /** 251 | * Checks that the class was properly relocated. Proudly stolen from bStats.org 252 | */ 253 | private static void checkRelocation() { 254 | if (Bukkit.getServer().getClass().getName().equals("be.seeseemelk.mockbukkit.ServerMock")) return; 255 | final String defaultPackageDe = new String(new byte[]{'d', 'e', '.', 'j', 'e', 'f', 'f', '_', 'm', 'e', 'd', 'i', 'a', '.', 'u', 'p', 'd', 'a', 't', 'e', 'c', 'h', 'e', 'c', 'k', 'e', 'r'}); 256 | final String defaultPackageCom = new String(new byte[]{'c', 'o', 'm', '.', 'j', 'e', 'f', 'f', '_', 'm', 'e', 'd', 'i', 'a', '.', 'u', 'p', 'd', 'a', 't', 'e', 'c', 'h', 'e', 'c', 'k', 'e', 'r'}); 257 | final String examplePackage = new String(new byte[]{'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'}); 258 | String packageName = UpdateChecker.class.getPackage().getName(); 259 | if (packageName.startsWith(defaultPackageDe) || packageName.startsWith(defaultPackageCom) || packageName.startsWith(examplePackage)) { 260 | throw new IllegalStateException("SpigotUpdateChecker class has not been relocated correctly! Check the GitHub's README.md for instructions."); 261 | } 262 | } 263 | 264 | /** 265 | * Returns whether the message "You are using the latest version of " will be suppressed. 266 | * 267 | * @return True when the message will be suppressed, otherwise false 268 | */ 269 | public boolean isSuppressUpToDateMessage() { 270 | return suppressUpToDateMessage; 271 | } 272 | 273 | /** 274 | * Starts to check every X hours for updates. If a task is already running, it 275 | * gets cancelled and replaced with the new one, so don't be afraid to use this 276 | * in your reload function. The first check will also happen after X hours so 277 | * you might want to call checkNow() too. When you set notifyRequesters to true 278 | * (default), the Console will get a notification about the check result. 279 | * 280 | * @param hours Amount of hours in between checks 281 | * @return UpdateChecker instance being ran 282 | */ 283 | public UpdateChecker checkEveryXHours(double hours) { 284 | double minutes = hours * 60; 285 | double seconds = minutes * 60; 286 | long ticks = ((int) seconds) * 20L; 287 | stop(); 288 | if (ticks > 0) { 289 | updaterTask = getScheduler().runTaskTimer(() -> checkNow(Bukkit.getConsoleSender()), ticks, ticks); 290 | } else { 291 | updaterTask = null; 292 | } 293 | return this; 294 | } 295 | 296 | /** 297 | * Stops the scheduled update checks. THIS IS NOT NEEDED when calling 298 | * checkEveryXHours(double) again, as the UpdateChecker will automatically stop 299 | * its previous task. 300 | */ 301 | public UpdateChecker stop() { 302 | if (updaterTask != null) { 303 | updaterTask.cancel(); 304 | } 305 | updaterTask = null; 306 | return this; 307 | } 308 | 309 | /** 310 | * Checks for updates now and sends the result to the given list of 311 | * CommandSenders. Can be null to silently check for updates. 312 | * 313 | * @param requesters CommandSenders to send the result to, or null 314 | */ 315 | public UpdateChecker checkNow(@Nullable CommandSender... requesters) { 316 | if (plugin == null) { 317 | throw new IllegalStateException("Plugin has not been set."); 318 | } 319 | if (apiLink == null && supplier == null) { 320 | throw new IllegalStateException("API Link has not been set and no supplier was provided."); 321 | } 322 | 323 | checkedAtLeastOnce = true; 324 | 325 | if (userAgentString == null) { 326 | userAgentString = UserAgentBuilder.getDefaultUserAgent().build(); 327 | } 328 | 329 | getScheduler().runTaskAsynchronously(() -> { 330 | 331 | UpdateCheckEvent updateCheckEvent; 332 | 333 | try { 334 | if (supplier != null) { 335 | latestVersion = supplier.getLatestVersionString(); 336 | } else { 337 | final HttpURLConnection httpConnection = (HttpURLConnection) new URL(apiLink).openConnection(); 338 | httpConnection.addRequestProperty("User-Agent", userAgentString); 339 | if (timeout > 0) { 340 | httpConnection.setConnectTimeout(timeout); 341 | } 342 | try (final InputStreamReader input = new InputStreamReader(httpConnection.getInputStream()); final BufferedReader reader = new BufferedReader(input)) { 343 | latestVersion = mapper.apply(reader); 344 | } 345 | } 346 | 347 | if (!isUsingLatestVersion() && !isOtherVersionNewer(usedVersion, latestVersion)) { 348 | latestVersion = usedVersion; 349 | } 350 | 351 | updateCheckEvent = new UpdateCheckEvent(UpdateCheckSuccess.SUCCESS); 352 | } catch (final IOException exception) { 353 | updateCheckEvent = new UpdateCheckEvent(UpdateCheckSuccess.FAIL); 354 | getScheduler().runTask(() -> getOnFail().accept(requesters, exception)); 355 | } 356 | 357 | UpdateCheckEvent finalUpdateCheckEvent = updateCheckEvent.setRequesters(requesters); 358 | 359 | getScheduler().runTask(() -> { 360 | 361 | if (finalUpdateCheckEvent.getSuccess() == UpdateCheckSuccess.SUCCESS) { 362 | getOnSuccess().accept(requesters, latestVersion); 363 | } 364 | 365 | Bukkit.getPluginManager().callEvent(finalUpdateCheckEvent); 366 | }); 367 | 368 | }); 369 | return this; 370 | } 371 | 372 | /** 373 | * Checks whether the latest found version of the plugin is being used. 374 | * 375 | * @return true if the latest found version is the one currently in use, otherwise false 376 | */ 377 | public boolean isUsingLatestVersion() { 378 | return usedVersion.equals(instance.latestVersion); 379 | } 380 | 381 | /** 382 | * Checks whether one version is really newer than another according to the semantic versioning scheme, including letters. 383 | * 384 | * @param myVersion One version string 385 | * @param otherVersion Another version string 386 | * @return true if the other version is indeed newer, otherwise false 387 | */ 388 | public static boolean isOtherVersionNewer(String myVersion, String otherVersion) { 389 | DefaultArtifactVersion used = new DefaultArtifactVersion(myVersion); 390 | DefaultArtifactVersion latest = new DefaultArtifactVersion(otherVersion); 391 | return used.compareTo(latest) < 0; 392 | } 393 | 394 | /** 395 | * Gets the task that will run when/after the update check fails. 396 | * 397 | * @return Task that will run when/after the update check fails. 398 | */ 399 | public BiConsumer getOnFail() { 400 | return onFail; 401 | } 402 | 403 | /** 404 | * Gets the task that will run when/after the update check succeeds. 405 | * 406 | * @return Task that will run when/after the update check succeeds. 407 | */ 408 | public BiConsumer getOnSuccess() { 409 | return onSuccess; 410 | } 411 | 412 | /** 413 | * Checks for updates now and sends the result to the console when 414 | * notifyRequesters is set to true (default) 415 | */ 416 | public UpdateChecker checkNow() { 417 | checkNow(Bukkit.getConsoleSender()); 418 | return this; 419 | } 420 | 421 | /** 422 | * Returns a list of applicable Download links. 423 | *

424 | * If using the free version and there are links for the free and paid version, 425 | * element 0 will be the link to the paid version and element will be the link 426 | * to the free version 427 | *

428 | * If using the paid version, there will be only one element containing the link 429 | * to the paid version, or, if that is not set, the link to the free version. 430 | *

431 | * If there is no paid version, there will be only one element containing the 432 | * link to the free version, or, if that is not set, the link to the plus 433 | * version. 434 | *

435 | * If no download links are set, returns an empty list. 436 | * 437 | * @return List of zero to two download links. If the list contains two links, 438 | * the first element is the paid download link. 439 | */ 440 | public List getAppropriateDownloadLinks() { 441 | List list = new ArrayList<>(); 442 | 443 | if (usingPaidVersion) { 444 | if (paidDownloadLink != null) { 445 | list.add(paidDownloadLink); 446 | } else if (freeDownloadLink != null) { 447 | list.add(freeDownloadLink); 448 | } 449 | } else { 450 | if (paidDownloadLink != null) { 451 | list.add(paidDownloadLink); 452 | } 453 | if (freeDownloadLink != null) { 454 | list.add(freeDownloadLink); 455 | } 456 | } 457 | return list; 458 | } 459 | 460 | /** 461 | * Returns the changelog link 462 | * 463 | * @return Changelog Link 464 | */ 465 | public String getChangelogLink() { 466 | return changelogLink; 467 | } 468 | 469 | /** 470 | * Sets a link to your plugin's changelog generated from your plugin's SpigotMC/Polymart 471 | * Resource ID 472 | * 473 | * @param resourceId Spigot/Polymart Resource ID 474 | * @return UpdateChecker instance being ran 475 | */ 476 | public UpdateChecker setChangelogLink(int resourceId) { 477 | if (updateCheckSource == UpdateCheckSource.SPIGOT) 478 | return setChangelogLink(SPIGOT_DOWNLOAD_LINK + resourceId + SPIGOT_CHANGELOG_SUFFIX); 479 | if (updateCheckSource == UpdateCheckSource.POLYMART) 480 | return setChangelogLink(POLYMART_DOWNLOAD_LINK + resourceId + POLYMART_CHANGELOG_SUFFIX); 481 | return this; 482 | } 483 | 484 | /** 485 | * Sets a link to your plugin's changelog. 486 | * 487 | * @param link Changelog link 488 | * @return UpdateChecker instance being ran 489 | */ 490 | public UpdateChecker setChangelogLink(@Nullable String link) { 491 | changelogLink = link; 492 | return this; 493 | } 494 | 495 | /** 496 | * Returns the support link 497 | * 498 | * @return Support Link 499 | */ 500 | @Nullable 501 | public String getSupportLink() { 502 | return supportLink; 503 | } 504 | 505 | /** 506 | * Sets a link to your plugin's support channel. 507 | * 508 | * @param link Support link 509 | * @return UpdateChecker instance being ran 510 | */ 511 | @NotNull 512 | public UpdateChecker setSupportLink(@Nullable String link) { 513 | this.supportLink = link; 514 | return this; 515 | } 516 | 517 | /** 518 | * Returns the donation link 519 | * 520 | * @return Donation link 521 | */ 522 | public String getDonationLink() { 523 | return donationLink; 524 | } 525 | 526 | /** 527 | * Sets a link to your plugin's donation website 528 | * 529 | * @param donationLink Donation link 530 | * @return UpdateChecker instance being ran 531 | */ 532 | public UpdateChecker setDonationLink(@Nullable String donationLink) { 533 | this.donationLink = donationLink; 534 | return this; 535 | } 536 | 537 | /** 538 | * Returns the last successful UpdateCheckResult 539 | * 540 | * @return Last successful UpdateCheckResult 541 | */ 542 | public UpdateCheckResult getLastCheckResult() { 543 | if (latestVersion == null) { 544 | return UpdateCheckResult.UNKNOWN; 545 | } 546 | if (latestVersion.equals(usedVersion)) { 547 | return UpdateCheckResult.RUNNING_LATEST_VERSION; 548 | } 549 | return UpdateCheckResult.NEW_VERSION_AVAILABLE; 550 | } 551 | 552 | /** 553 | * Returns the latest version string found by the UpdateChecker, or null if all 554 | * checks until yet have failed. 555 | * 556 | * @return Latest version string found by the UpdateChecker 557 | */ 558 | public String getLatestVersion() { 559 | return latestVersion; 560 | } 561 | 562 | /** 563 | * Returns the name/suffix of the free plugin version 564 | * 565 | * @return Name/suffix of the free plugin version 566 | */ 567 | public String getNameFreeVersion() { 568 | return nameFreeVersion; 569 | } 570 | 571 | /** 572 | * Sets the name/suffix for the free version's name. E.g. when you set this to 573 | * "Free", the Download link for the free version will be shown as "Download 574 | * (Free): [Link]" 575 | * 576 | * @param nameFreeVersion Name/suffix of the free plugin version 577 | * @return UpdateChecker instance being ran 578 | */ 579 | public UpdateChecker setNameFreeVersion(String nameFreeVersion) { 580 | this.nameFreeVersion = nameFreeVersion; 581 | return this; 582 | } 583 | 584 | /** 585 | * Returns the name/suffix of the paid plugin version 586 | * 587 | * @return Name/suffix of the paid plugin version 588 | */ 589 | public String getNamePaidVersion() { 590 | return namePaidVersion; 591 | } 592 | 593 | /** 594 | * Sets the name/suffix for the paid version's name. E.g. when you set this to 595 | * "Platinum version", the Download link for the paid version will be shown as 596 | * "Download (Platinum version): [Link]" 597 | * 598 | * @param namePaidVersion Name/suffix of the paid plugin version 599 | * @return UpdateChecker instance being ran 600 | */ 601 | public UpdateChecker setNamePaidVersion(String namePaidVersion) { 602 | this.namePaidVersion = namePaidVersion; 603 | return this; 604 | } 605 | 606 | /** 607 | * Returns the permission required to receive UpdateChecker messages on join 608 | * 609 | * @return Permission required to receive UpdateChecker messages on join, or null if not set 610 | */ 611 | public @Nullable 612 | String getNotifyPermission() { 613 | return notifyPermission; 614 | } 615 | 616 | /** 617 | * Gets the plugin that instantiated this UpdateChecker instance 618 | * 619 | * @return Plugin that instantiated this UpdateChecker instance 620 | */ 621 | protected Plugin getPlugin() { 622 | return plugin; 623 | } 624 | 625 | /** 626 | * Gets the TaskScheduler instance used by the UpdateChecker 627 | * 628 | * @return TaskScheduler instance used by the UpdateChecker 629 | */ 630 | public static TaskScheduler getScheduler() { 631 | return scheduler; 632 | } 633 | 634 | /** 635 | * Gets the Spigot User ID of the user who downloaded the plugin if it's a premium plugin, otherwise "%%__USER__%%" 636 | * 637 | * @return Spigot User ID of the user who downloaded the plugin if it's a premium plugin, otherwise "%%__USER__%%" 638 | */ 639 | public String getSpigotUserId() { 640 | return spigotUserId; 641 | } 642 | 643 | /** 644 | * Gets the version string of the currently used plugin version 645 | * 646 | * @return Version string of the currently used plugin version 647 | */ 648 | public String getUsedVersion() { 649 | return usedVersion; 650 | } 651 | 652 | /** 653 | * Sets the version string of the currently used plugin version. 654 | * By default, this is the version defined in the plugin.yml file. 655 | * 656 | * @param usedVersion new version string 657 | */ 658 | public UpdateChecker setUsedVersion(String usedVersion) { 659 | this.usedVersion = usedVersion; 660 | return this; 661 | } 662 | 663 | /** 664 | * Checks whether the update checker already ran. 665 | * 666 | * @return True when the update checker already ran, otherwise false 667 | */ 668 | @SuppressWarnings("BooleanMethodIsAlwaysInverted") 669 | public boolean isCheckedAtLeastOnce() { 670 | return checkedAtLeastOnce; 671 | } 672 | 673 | /** 674 | * Returns whether colored console output is enabled 675 | * 676 | * @return true when colored console output is enabled, otherwise false 677 | */ 678 | public boolean isColoredConsoleOutput() { 679 | return coloredConsoleOutput; 680 | } 681 | 682 | /** 683 | * Sets whether or not the used and latest version will be displayed in color in the console 684 | * 685 | * @param coloredConsoleOutput Whether to use color in the console output 686 | * @return UpdateChecker instance being ran 687 | */ 688 | public UpdateChecker setColoredConsoleOutput(boolean coloredConsoleOutput) { 689 | this.coloredConsoleOutput = coloredConsoleOutput; 690 | return this; 691 | } 692 | 693 | /** 694 | * Returns whether OPs will be notified on join when a new version is available 695 | * 696 | * @return true when OPs will be notified on join when a new version is available, otherwise false 697 | */ 698 | public boolean isNotifyOpsOnJoin() { 699 | return notifyOpsOnJoin; 700 | } 701 | 702 | /** 703 | * Whether or not to inform OPs on join when there is a new version available. 704 | * 705 | * @param notifyOpsOnJoin Whether to inform on OPs on join when there is a new version available 706 | * @return UpdateChecker instance being ran 707 | */ 708 | public UpdateChecker setNotifyOpsOnJoin(boolean notifyOpsOnJoin) { 709 | this.notifyOpsOnJoin = notifyOpsOnJoin; 710 | return this; 711 | } 712 | 713 | /** 714 | * Gets whether the given CommandSenders will be informed about UpdateChecker results 715 | * 716 | * @return Whether or not to inform given CommandSenders about UpdateChecker results 717 | */ 718 | public boolean isNotifyRequesters() { 719 | return notifyRequesters; 720 | } 721 | 722 | /** 723 | * Whether or not CommandSenders who request an update check will be notified of the result. 724 | * When you use your own tasks using onSuccess and onFail, consider setting this to false. 725 | * 726 | * @param notify Whether or not to notify given CommandSenders about UpdateChecker results 727 | * @return true when CommandSenders will be notified, otherwise false 728 | */ 729 | public UpdateChecker setNotifyRequesters(boolean notify) { 730 | notifyRequesters = notify; 731 | return this; 732 | } 733 | 734 | /** 735 | * Returns whether the paid version of the plugin is installed. 736 | * 737 | * @return True if the paid version is used, otherwise false 738 | */ 739 | public boolean isUsingPaidVersion() { 740 | return usingPaidVersion; 741 | } 742 | 743 | /** 744 | * Tells the UpdateChecker whether the server already uses the paid version of 745 | * your plugin. If yes, the downloads to the free version are not shown. You can 746 | * ignore this if you only offer one version of your plugin. When this value is 747 | * not set, the Update Checker automatically sets this to true by checking the 748 | * %%__USER__%% placeholder, see 749 | * https://www.spigotmc.org/wiki/premium-resource-placeholders-identifiers/ 750 | * 751 | * @param paidVersion Whether or not the user is using the paid version of your plugin 752 | * @return UpdateChecker instance being ran 753 | */ 754 | public UpdateChecker setUsingPaidVersion(boolean paidVersion) { 755 | usingPaidVersion = paidVersion; 756 | return this; 757 | } 758 | 759 | /** 760 | * Sets a task that will run when/after the update check has failed. 761 | * 762 | * @param onFail Task that will run when/after the update check has failed. 763 | * @return UpdateChecker instance being ran 764 | */ 765 | public UpdateChecker onFail(BiConsumer onFail) { 766 | this.onFail = onFail == null ? (requesters, ex) -> ex.printStackTrace() : onFail; 767 | return this; 768 | } 769 | 770 | /** 771 | * Sets a task that will run when/after the update check has succeeded. 772 | * 773 | * @param onSuccess Task that will run when/after the update check has succeeded. 774 | * @return UpdateChecker instance being ran 775 | */ 776 | public UpdateChecker onSuccess(BiConsumer onSuccess) { 777 | this.onSuccess = onSuccess == null ? (requesters, latestVersion) -> { 778 | } : onSuccess; 779 | return this; 780 | } 781 | 782 | /** 783 | * Sets the download link for your plugin generated from your plugin's SpigotMC/Polymart 784 | * Resource ID. Use this if there is only one version of your plugin, either 785 | * only a free or only a paid version. 786 | * 787 | * @param resourceId Spigot/Polymart Resource ID 788 | * @return UpdateChecker instance being ran 789 | */ 790 | public UpdateChecker setDownloadLink(int resourceId) { 791 | if (updateCheckSource == UpdateCheckSource.SPIGOT) return setDownloadLink(SPIGOT_DOWNLOAD_LINK + resourceId); 792 | if (updateCheckSource == UpdateCheckSource.POLYMART) 793 | return setDownloadLink(POLYMART_DOWNLOAD_LINK + resourceId); 794 | return this; 795 | } 796 | 797 | /** 798 | * Sets the download link for your plugin. Use this if there is only one version 799 | * of your plugin, either only a free or only a paid version. 800 | * 801 | * @param downloadLink Download link 802 | * @return UpdateChecker instance being ran 803 | */ 804 | public UpdateChecker setDownloadLink(@Nullable String downloadLink) { 805 | this.paidDownloadLink = null; 806 | this.freeDownloadLink = downloadLink; 807 | return this; 808 | } 809 | 810 | /** 811 | * Sets whether the message "You are using the latest version of " should be suppressed. 812 | * Defaults to false 813 | * 814 | * @param suppress Whether to suppress the message "You are using the latest version of " 815 | * @return UpdateChecker instance being ran 816 | */ 817 | public UpdateChecker suppressUpToDateMessage(boolean suppress) { 818 | this.suppressUpToDateMessage = suppress; 819 | return this; 820 | } 821 | 822 | /** 823 | * Sets the download link for the free version of your plugin generated from 824 | * your plugin's SpigotMC/Polymart Resource ID. Use this if there is both, a free and a 825 | * paid version of your plugin available. 826 | * 827 | * @param resourceId Spigot/Polymart Resource ID of the free version 828 | * @return UpdateChecker instance being ran 829 | */ 830 | public UpdateChecker setFreeDownloadLink(int resourceId) { 831 | if (updateCheckSource == UpdateCheckSource.SPIGOT) 832 | return setFreeDownloadLink(SPIGOT_DOWNLOAD_LINK + resourceId); 833 | if (updateCheckSource == UpdateCheckSource.POLYMART) 834 | return setFreeDownloadLink(POLYMART_DOWNLOAD_LINK + resourceId); 835 | return this; 836 | } 837 | 838 | /** 839 | * Sets the download link for the free version of your plugin. Use this if there 840 | * is both, a free and a paid version of your plugin available. 841 | * 842 | * @param freeDownloadLink Download link of the free version 843 | * @return UpdateChecker instance being ran 844 | */ 845 | public UpdateChecker setFreeDownloadLink(@Nullable String freeDownloadLink) { 846 | this.freeDownloadLink = freeDownloadLink; 847 | return this; 848 | } 849 | 850 | /** 851 | * Sets the permission needed to be informed about UpdateChecker results on join. 852 | * 853 | * @param permission Permission needed to be informed about UpdateChecker results on join 854 | * @return UpdateChecker instance being ran 855 | */ 856 | public UpdateChecker setNotifyByPermissionOnJoin(@Nullable String permission) { 857 | notifyPermission = permission; 858 | return this; 859 | } 860 | 861 | /** 862 | * Sets the download link for the paid version of your plugin generated from 863 | * your plugin's SpigotMC/Polymart Resource ID. Use this if there is both, a free and a 864 | * paid version of your plugin available. 865 | * 866 | * @param resourceId Spigot/Polymart Resource ID of the paid version 867 | * @return UpdateChecker instance being ran 868 | */ 869 | public UpdateChecker setPaidDownloadLink(int resourceId) { 870 | if (updateCheckSource == UpdateCheckSource.SPIGOT) 871 | return setPaidDownloadLink(SPIGOT_DOWNLOAD_LINK + resourceId); 872 | if (updateCheckSource == UpdateCheckSource.POLYMART) 873 | return setPaidDownloadLink(POLYMART_DOWNLOAD_LINK + resourceId); 874 | return this; 875 | } 876 | 877 | /** 878 | * Sets the download link for the paid version of your plugin. Use this if there 879 | * is both, a free and a paid version of your plugin available. 880 | * 881 | * @param link Download link of the paid version 882 | * @return UpdateChecker instance being ran 883 | */ 884 | public UpdateChecker setPaidDownloadLink(@NotNull String link) { 885 | paidDownloadLink = link; 886 | return this; 887 | } 888 | 889 | /** 890 | * Sets the timeout for the HTTP(S) connection in milliseconds. 0 = use Java's 891 | * default value 892 | * 893 | * @param timeout Timeout in milliseconds, or 0 to use Java's default value 894 | */ 895 | public UpdateChecker setTimeout(int timeout) { 896 | this.timeout = timeout; 897 | return this; 898 | } 899 | 900 | /** 901 | * Sets the UserAgent string using a UserAgentBuilder 902 | * 903 | * @param userAgentBuilder UserAgentBuilder instance 904 | * @return UpdateChecker instance being ran 905 | */ 906 | public UpdateChecker setUserAgent(@NotNull UserAgentBuilder userAgentBuilder) { 907 | userAgentString = userAgentBuilder.build(); 908 | return this; 909 | } 910 | 911 | /** 912 | * Sets the UserAgent string using plain text 913 | * 914 | * @param userAgent UserAgent string 915 | * @return UpdateChecker instance being ran 916 | */ 917 | public UpdateChecker setUserAgent(@Nullable String userAgent) { 918 | userAgentString = userAgent; 919 | return this; 920 | } 921 | 922 | } 923 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | import net.md_5.bungee.api.chat.ClickEvent; 22 | import net.md_5.bungee.api.chat.ComponentBuilder; 23 | import net.md_5.bungee.api.chat.HoverEvent; 24 | import net.md_5.bungee.api.chat.TextComponent; 25 | import org.bukkit.ChatColor; 26 | import org.bukkit.entity.Player; 27 | import org.bukkit.plugin.Plugin; 28 | import org.jetbrains.annotations.NotNull; 29 | 30 | import java.util.ArrayList; 31 | import java.util.Iterator; 32 | import java.util.List; 33 | import java.util.logging.Level; 34 | import java.util.logging.Logger; 35 | import java.util.stream.Stream; 36 | 37 | class UpdateCheckerMessages { 38 | 39 | @NotNull 40 | private static TextComponent createLink(@NotNull final String text, @NotNull final String link) { 41 | final ComponentBuilder lore = new ComponentBuilder("Link: ") 42 | .bold(true) 43 | .append(link) 44 | .bold(false); 45 | final TextComponent component = new TextComponent(text); 46 | component.setBold(true); 47 | // TODO: Make color configurable 48 | component.setColor(net.md_5.bungee.api.ChatColor.GOLD); 49 | component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, link)); 50 | //noinspection deprecation 51 | component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, lore.create())); 52 | return component; 53 | } 54 | 55 | protected static void printCheckResultToConsole(UpdateCheckEvent event) { 56 | 57 | final UpdateChecker instance = UpdateChecker.getInstance(); 58 | final Plugin plugin = instance.getPlugin(); 59 | 60 | if (event.getSuccess() == UpdateCheckSuccess.FAIL || event.getResult() == UpdateCheckResult.UNKNOWN) { 61 | plugin.getLogger().warning("Could not check for updates."); 62 | return; 63 | } 64 | 65 | if (event.getResult() == UpdateCheckResult.RUNNING_LATEST_VERSION) { 66 | if (UpdateChecker.getInstance().isSuppressUpToDateMessage()) return; 67 | plugin.getLogger().info(String.format("You are using the latest version of %s.", plugin.getName())); 68 | return; 69 | } 70 | 71 | List lines = new ArrayList<>(); 72 | 73 | lines.add(String.format("There is a new version of %s available!", plugin.getName())); 74 | lines.add(" "); 75 | lines.add(String.format("Your version: %s%s", instance.isColoredConsoleOutput() ? ChatColor.RED : "", event.getUsedVersion())); 76 | lines.add(String.format("Latest version: %s%s", instance.isColoredConsoleOutput() ? ChatColor.GREEN : "", event.getLatestVersion())); 77 | 78 | List downloadLinks = instance.getAppropriateDownloadLinks(); 79 | 80 | if (downloadLinks.size() > 0) { 81 | lines.add(" "); 82 | lines.add("Please update to the newest version."); 83 | lines.add(" "); 84 | if (downloadLinks.size() == 1) { 85 | lines.add("Download:"); 86 | lines.add(" " + downloadLinks.get(0)); 87 | } else if (downloadLinks.size() == 2) { 88 | lines.add(String.format("Download (%s)", instance.getNamePaidVersion())); 89 | lines.add(" " + downloadLinks.get(0)); 90 | lines.add(" "); 91 | lines.add(String.format("Download (%s)", instance.getNameFreeVersion())); 92 | lines.add(" " + downloadLinks.get(1)); 93 | } 94 | } 95 | 96 | if(instance.getSupportLink() != null) { 97 | lines.add(" "); 98 | lines.add("Support:"); 99 | lines.add(" " + instance.getSupportLink()); 100 | } 101 | 102 | if(instance.getDonationLink() != null) { 103 | lines.add(" "); 104 | lines.add("Donate:"); 105 | lines.add(" " + instance.getDonationLink()); 106 | } 107 | 108 | printNiceBoxToConsole(plugin.getLogger(), lines); 109 | } 110 | 111 | protected static void printCheckResultToPlayer(Player player, boolean showMessageWhenLatestVersion) { 112 | UpdateChecker instance = UpdateChecker.getInstance(); 113 | if (instance.getLastCheckResult() == UpdateCheckResult.NEW_VERSION_AVAILABLE) { 114 | player.sendMessage(ChatColor.GRAY + "There is a new version of " + ChatColor.GOLD + instance.getPlugin().getName() + ChatColor.GRAY + " available."); 115 | sendLinks(player); 116 | player.sendMessage(ChatColor.DARK_GRAY + "Latest version: " + ChatColor.GREEN + instance.getLatestVersion() + ChatColor.DARK_GRAY + " | Your version: " + ChatColor.RED + instance.getUsedVersion()); 117 | player.sendMessage(""); 118 | } else if (instance.getLastCheckResult() == UpdateCheckResult.UNKNOWN) { 119 | player.sendMessage(ChatColor.GOLD + instance.getPlugin().getName() + ChatColor.RED + " could not check for updates."); 120 | } else { 121 | if (showMessageWhenLatestVersion) { 122 | player.sendMessage(ChatColor.GREEN + "You are running the latest version of " + ChatColor.GOLD + instance.getPlugin().getName()); 123 | } 124 | } 125 | } 126 | 127 | private static void printNiceBoxToConsole(Logger logger, List lines) { 128 | int longestLine = 0; 129 | for (String line : lines) { 130 | longestLine = Math.max(line.length(), longestLine); 131 | } 132 | longestLine += 2; 133 | if (longestLine > 120) longestLine = 120; 134 | longestLine += 2; 135 | StringBuilder dash = new StringBuilder(longestLine); 136 | Stream.generate(() -> "*").limit(longestLine).forEach(dash::append); 137 | 138 | logger.log(Level.WARNING, dash.toString()); 139 | for (String line : lines) { 140 | logger.log(Level.WARNING, ("*" + " ") + line); 141 | } 142 | logger.log(Level.WARNING, dash.toString()); 143 | } 144 | 145 | private static void sendLinks(@NotNull final Player... players) { 146 | 147 | UpdateChecker instance = UpdateChecker.getInstance(); 148 | 149 | List links = new ArrayList<>(); 150 | 151 | List downloadLinks = instance.getAppropriateDownloadLinks(); 152 | 153 | if (downloadLinks.size() == 2) { 154 | links.add(createLink(String.format("Download (%s)", instance.getNamePaidVersion()), downloadLinks.get(0))); 155 | links.add(createLink(String.format("Download (%s)", instance.getNameFreeVersion()), downloadLinks.get(1))); 156 | } else if (downloadLinks.size() == 1) { 157 | links.add(createLink("Download", downloadLinks.get(0))); 158 | } 159 | if (instance.getDonationLink() != null) { 160 | links.add(createLink("Donate", instance.getDonationLink())); 161 | } 162 | if (instance.getChangelogLink() != null) { 163 | links.add(createLink("Changelog", instance.getChangelogLink())); 164 | } 165 | if (instance.getSupportLink() != null) { 166 | links.add(createLink("Support", instance.getSupportLink())); 167 | } 168 | 169 | final TextComponent placeholder = new TextComponent(" | "); 170 | placeholder.setColor(net.md_5.bungee.api.ChatColor.GRAY); 171 | 172 | TextComponent text = new TextComponent(""); 173 | 174 | Iterator iterator = links.iterator(); 175 | while (iterator.hasNext()) { 176 | TextComponent next = iterator.next(); 177 | text.addExtra(next); 178 | if (iterator.hasNext()) { 179 | text.addExtra(placeholder); 180 | } 181 | } 182 | 183 | for (Player player : players) { 184 | player.spigot().sendMessage(text); 185 | } 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/UserAgentBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | import org.bukkit.Bukkit; 22 | import org.bukkit.plugin.Plugin; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Iterator; 26 | import java.util.List; 27 | 28 | /** 29 | * Creates a User-Agent string. Always starts with "JEFF-Media-GbR-SpigotUpdateChecker/[version]" followed by all added parameters. 30 | */ 31 | @SuppressWarnings("unused") 32 | public class UserAgentBuilder { 33 | 34 | private final StringBuilder builder = new StringBuilder("JEFF-Media-GbR-SpigotUpdateChecker/").append(UpdateChecker.VERSION); 35 | private final UpdateChecker instance = UpdateChecker.getInstance(); 36 | private final List list = new ArrayList<>(); 37 | private final Plugin plugin = instance.getPlugin(); 38 | 39 | /** 40 | * Returns the default User-Agent, consisting of Plugin name and version, Server version and Bukkit version 41 | * 42 | * @return UserAgentBuilder instance 43 | */ 44 | public static UserAgentBuilder getDefaultUserAgent() { 45 | return new UserAgentBuilder().addPluginNameAndVersion().addServerVersion().addBukkitVersion(); 46 | } 47 | 48 | /** 49 | * Adds the Bukkit version. For example "BukkitVersion/1.16.5-R0.1-SNAPSHOT" 50 | * 51 | * @return UserAgentBuilder instance 52 | */ 53 | public UserAgentBuilder addBukkitVersion() { 54 | list.add("BukkitVersion/" + Bukkit.getBukkitVersion()); 55 | return this; 56 | } 57 | 58 | /** 59 | * Adds a custom Key/Value string. For example "foo/bar" 60 | * 61 | * @param key Key 62 | * @param value Value 63 | * @return UserAgentBuilder instance 64 | */ 65 | public UserAgentBuilder addKeyValue(String key, String value) { 66 | list.add(key + "/" + value); 67 | return this; 68 | } 69 | 70 | /** 71 | * Adds a custom string. For example "foo" 72 | * 73 | * @param text Custom string 74 | * @return UserAgentBuilder instance 75 | */ 76 | public UserAgentBuilder addPlaintext(String text) { 77 | list.add(text); 78 | return this; 79 | } 80 | 81 | /** 82 | * Adds the plugin and version. For example "AngelChest/3.11.0" 83 | * 84 | * @return UserAgentBuilder instance 85 | */ 86 | public UserAgentBuilder addPluginNameAndVersion() { 87 | list.add(plugin.getName() + "/" + plugin.getDescription().getVersion()); 88 | return this; 89 | } 90 | 91 | /** 92 | * Adds the Server version. For example "ServerVersion/git-Paper-584 (MC: 1.16.5)" 93 | * 94 | * @return UserAgentBuilder instance 95 | */ 96 | public UserAgentBuilder addServerVersion() { 97 | list.add("ServerVersion/" + Bukkit.getVersion()); 98 | return this; 99 | } 100 | 101 | /** 102 | * Returns the Spigot User ID of the user who downloaded the plugin. Only works for paid plugins from SpigotMC.org. For example "SpigotUID/175238" 103 | * 104 | * @return UserAgentBuilder instance 105 | */ 106 | public UserAgentBuilder addSpigotUserId() { 107 | String uid = instance.isUsingPaidVersion() ? instance.getSpigotUserId() : "none"; 108 | list.add("SpigotUID/" + uid); 109 | return this; 110 | } 111 | 112 | /** 113 | * Returns whether this copy of the .jar is a paid plugin downloaded from SpigotMC.org. For example "Paid/true" 114 | * 115 | * @return UserAgentBuilder instance 116 | */ 117 | public UserAgentBuilder addUsingPaidVersion() { 118 | list.add("Paid/" + instance.isUsingPaidVersion()); 119 | return this; 120 | } 121 | 122 | /** 123 | * Converts this UserAgentBuilder instance to a UserAgent string 124 | * 125 | * @return UserAgent string 126 | */ 127 | protected String build() { 128 | if (list.size() > 0) { 129 | builder.append(" ("); 130 | Iterator it = list.iterator(); 131 | while (it.hasNext()) { 132 | builder.append(it.next()); 133 | if (it.hasNext()) { 134 | builder.append(", "); 135 | } 136 | } 137 | builder.append(")"); 138 | } 139 | return builder.toString(); 140 | } 141 | 142 | } 143 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/VersionMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | import com.google.gson.Gson; 22 | import com.google.gson.JsonArray; 23 | import com.google.gson.JsonObject; 24 | 25 | import java.io.BufferedReader; 26 | import java.io.IOException; 27 | 28 | interface VersionMapper { 29 | ThrowingFunction TRIM_FIRST_LINE = reader -> reader.readLine().trim(); 30 | 31 | ThrowingFunction SPIGET = reader -> new Gson().fromJson(reader, JsonObject.class).get("name").getAsString(); 32 | 33 | ThrowingFunction GITHUB_RELEASE_TAG = reader -> { 34 | JsonArray array = new Gson().fromJson(reader, JsonArray.class); 35 | if(array.size()==0) { 36 | throw new IOException("Could not check for updates: no GitHub release found."); 37 | } 38 | JsonObject release = array.get(0).getAsJsonObject(); 39 | return release.get("tag_name").getAsString(); 40 | }; 41 | ThrowingFunction SPIGOT = reader -> new Gson().fromJson(reader, JsonObject.class).get("current_version").getAsString(); 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/VersionSupplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package com.jeff_media.updatechecker; 20 | 21 | import org.bukkit.plugin.java.JavaPlugin; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Used to supply the latest version of your plugin in conjunction with {@link UpdateChecker#UpdateChecker(JavaPlugin, VersionSupplier)} 27 | */ 28 | @FunctionalInterface 29 | public interface VersionSupplier { 30 | 31 | /** 32 | * Returns the latest version of your plugin. Gets called async so do not access any Bukkit API. 33 | */ 34 | String getLatestVersionString() throws IOException; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/jeff_media/updatechecker/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Alexander Majka (mfnalex), JEFF Media GbR 3 | * Website: https://www.jeff-media.com 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | /** 20 | * Main package. Must be relocated when shading! 21 | */ 22 | package com.jeff_media.updatechecker; -------------------------------------------------------------------------------- /src/main/javadoc/overview.html: -------------------------------------------------------------------------------- 1 | 2 |

3 | The SpigotUpdateChecker is a simple library for you to add a perfectly working update checker to your plugins. 4 |

5 |

6 | If you need help, feel free to join my discord at discord.jeff-media.com. Source code for this library is available on GitHub. 7 |

8 | --------------------------------------------------------------------------------