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 |
--------------------------------------------------------------------------------