diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d81b23dc..24f6157a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,7 +9,7 @@ on: jobs: build: runs-on: ubuntu-latest - environment: polymart_deploy + environment: updater if: ${{ !github.event.pull_request.head.repo.fork }} steps: - uses: actions/checkout@v2 @@ -47,7 +47,22 @@ jobs: # curl --no-progress-meter -F "file=@build/libs/ajLeaderboards-$VERSION.jar" -F api_key=$POLYMART_TOKEN -F resource_id="2726" -F version="$VERSION" -F title="v$VERSION" -F message="$(cat changelogs.bb)" "https://api.polymart.org/v1/postUpdate" # curl --no-progress-meter -A "AJUPDATER/1.0" -H "Authorization: $MODRINTH_TOKEN" -F data="{\"project_id\": \"C9BKEl8Y\", \"version_number\": \"$VERSION\", \"name\": \"v$VERSION\", \"changelog\": \"$(cat changelogs.html)\", \"file_parts\": [\"file\"], \"version_type\": \"release\", \"loaders\": [\"paper\", \"spigot\"], \"featured\": true, \"game_versions\": $(curl https://ajg0702.us/pl/updater/mc-versions.php), \"dependencies\": [], \"primary_file\": \"file\"}" -F "file=@build/libs/ajLeaderboards-$VERSION.jar" "https://api.modrinth.com/v2/version" # curl -v --no-progress-meter -H "Content-Type: application/json" --request POST -d "{\"content\": \"<@&861713403080999003>\", \"embeds\": [{\"title\": \"${{ github.event.repository.name }} v$VERSION\", \"description\": \"Changelogs\n\n$(curl "https://ajg0702.us/pl/updater/changelogs.php?project=${{ github.repository }}&format=markdown")\n\n[Modrinth](https://modrinth.com/plugin/ajleaderboards/version/$VERSION)\n[Polymart](https://polymart.org/resource/ajleaderboards.2726/updates)\", \"color\": 14845503, \"thumbnail\": {\"url\": \"https://ajg0702.us/pl/icons/${{ github.event.repository.name }}.png\"}}]}" "$DISCORD_WEBHOOK" - - name: Upload to repo + - name: Upload to plugin-updates repo + env: + GH_TOKEN: ${{ secrets.GH_TOKEN }} + run: | + VERSION=`cat build.gradle.kts | grep "version " | awk -F'"' '{print $2}'` + cd build/libs/ + echo -n "{ \"message\": \"Plugin Update: $(echo ajLeaderboards-*.jar) (jar)\", \"content\": \"" > file_upload.json + base64 ajLeaderboards-*.jar >> file_upload.json + echo -n "\" }" >> file_upload.json + curl -X PUT -H "Authorization: Bearer $GH_TOKEN" -H "Content-Type: application/json" -d @file_upload.json "https://api.github.com/repos/ajgeiss0702/plugin-updates/contents/jars/free/$(echo ajLeaderboards*.jar)" + + SHA=`curl -H "Authorization: Bearer $GH_TOKEN" -s "https://api.github.com/repos/ajgeiss0702/plugin-updates/contents/plugins/ajLeaderboards.json" | jq -r '.sha'` + NEW_JSON="{ \"latest\": \"$VERSION\", \"premium\": false }" + + curl -X PUT -H "Authorization: Bearer $GH_TOKEN" -H "Content-Type: application/json" -d "{ \"message\": \"Plugin Update: $(echo ajLeaderboards-*.jar) (json)\", \"content\": \"$(echo $NEW_JSON | base64)\", \"sha\": \"$SHA\" }" "https://api.github.com/repos/ajgeiss0702/plugin-updates/contents/plugins/ajLeaderboards.json" + - name: Publish API to repo env: REPO_TOKEN: ${{ secrets.REPO_TOKEN }} uses: gradle/gradle-build-action@937999e9cc2425eddc7fd62d1053baf041147db7 diff --git a/build.gradle.kts b/build.gradle.kts index 226c1814..7d3586d2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "us.ajg0702" -version = "2.8.0" +version = "2.7.0" repositories { mavenCentral() @@ -39,7 +39,7 @@ dependencies { implementation("net.kyori:adventure-text-minimessage:4.12.0") implementation("net.kyori:adventure-platform-bukkit:4.3.2") - implementation("us.ajg0702:ajUtils:1.2.25") + implementation("us.ajg0702:ajUtils:1.2.27") implementation("us.ajg0702.commands.platforms.bukkit:bukkit:1.0.0") implementation("us.ajg0702.commands.api:api:1.0.0") diff --git a/nms/nms-19/build.gradle.kts b/nms/nms-19/build.gradle.kts index 11beaf2b..051f0fa8 100644 --- a/nms/nms-19/build.gradle.kts +++ b/nms/nms-19/build.gradle.kts @@ -21,7 +21,7 @@ dependencies { testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") compileOnly(project(":nms:nms-legacy")) - compileOnly("us.ajg0702:ajUtils:1.2.25") + compileOnly("us.ajg0702:ajUtils:1.2.27") compileOnly("net.skinsrestorer:skinsrestorer-api:14.1.10") } diff --git a/nms/nms-legacy/build.gradle.kts b/nms/nms-legacy/build.gradle.kts index 56faaa22..66af1985 100644 --- a/nms/nms-legacy/build.gradle.kts +++ b/nms/nms-legacy/build.gradle.kts @@ -18,7 +18,7 @@ dependencies { compileOnly(group = "org.spigotmc", name = "spigot", version = "1.19-R0.1-SNAPSHOT") testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") - compileOnly("us.ajg0702:ajUtils:1.2.25") + compileOnly("us.ajg0702:ajUtils:1.2.27") compileOnly("net.skinsrestorer:skinsrestorer-api:14.1.10") compileOnly("com.squareup.okhttp3:okhttp:4.11.0") } diff --git a/src/main/java/us/ajg0702/leaderboards/LeaderboardPlugin.java b/src/main/java/us/ajg0702/leaderboards/LeaderboardPlugin.java index 33f93feb..9681b889 100644 --- a/src/main/java/us/ajg0702/leaderboards/LeaderboardPlugin.java +++ b/src/main/java/us/ajg0702/leaderboards/LeaderboardPlugin.java @@ -40,6 +40,8 @@ import us.ajg0702.leaderboards.utils.*; import us.ajg0702.utils.common.Config; import us.ajg0702.utils.common.Messages; +import us.ajg0702.utils.common.UpdateManager; +import us.ajg0702.utils.common.UtilsLogger; import us.ajg0702.utils.foliacompat.CompatScheduler; import us.ajg0702.utils.foliacompat.Task; @@ -82,6 +84,8 @@ public class LeaderboardPlugin extends JavaPlugin { private boolean shuttingDown = false; + private UpdateManager updateManager = null; + private final CompatScheduler compatScheduler = new CompatScheduler(this); @Override @@ -207,6 +211,10 @@ public void onEnable() { Bukkit.getPluginManager().registerEvents(new Listeners(this), this); + if(config.getBoolean("enable-updater")) { + updateManager = new UpdateManager(utilsLogger, getDescription().getVersion(), "ajLeaderboards", "ajLeaderboards", null, getDataFolder().getParentFile(), "ajLeaderboards update"); + } + getLogger().info("ajLeaderboards v"+getDescription().getVersion()+" by ajgeiss0702 enabled!"); } @@ -481,6 +489,10 @@ public static MiniMessage getMiniMessage() { return miniMessage; } + public UpdateManager getUpdateManager() { + return updateManager; + } + private static BukkitAudiences adventure; public BukkitAudiences getAdventure() { @@ -551,4 +563,31 @@ public Future safeGetBlockType(Location location) { } return future; } + + private UtilsLogger utilsLogger = new UtilsLogger() { + @Override + public void warn(String s) { + getLogger().warning(s); + } + + @Override + public void warning(String s) { + getLogger().warning(s); + } + + @Override + public void info(String s) { + getLogger().info(s); + } + + @Override + public void error(String s) { + getLogger().severe(s); + } + + @Override + public void severe(String s) { + getLogger().severe(s); + } + }; } diff --git a/src/main/java/us/ajg0702/leaderboards/commands/main/MainCommand.java b/src/main/java/us/ajg0702/leaderboards/commands/main/MainCommand.java index 0920c6e4..f75e4a38 100644 --- a/src/main/java/us/ajg0702/leaderboards/commands/main/MainCommand.java +++ b/src/main/java/us/ajg0702/leaderboards/commands/main/MainCommand.java @@ -35,6 +35,7 @@ public MainCommand(LeaderboardPlugin plugin) { addSubCommand(new Viewer(plugin)); addSubCommand(new Reset(plugin)); addSubCommand(new UpdateAllOffline(plugin)); + addSubCommand(new Update(plugin)); // Debug commands addSubCommand(new Time()); diff --git a/src/main/java/us/ajg0702/leaderboards/commands/main/subcommands/Update.java b/src/main/java/us/ajg0702/leaderboards/commands/main/subcommands/Update.java new file mode 100644 index 00000000..b0d620d2 --- /dev/null +++ b/src/main/java/us/ajg0702/leaderboards/commands/main/subcommands/Update.java @@ -0,0 +1,79 @@ +package us.ajg0702.leaderboards.commands.main.subcommands; + +import us.ajg0702.commands.CommandSender; +import us.ajg0702.commands.SubCommand; +import us.ajg0702.leaderboards.LeaderboardPlugin; +import us.ajg0702.utils.common.UpdateManager; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +public class Update extends SubCommand { + private final LeaderboardPlugin plugin; + public Update(LeaderboardPlugin plugin) { + super("update", Collections.singletonList("updateplugin"), "ajLeaderboards.use", "Download an update for ajLeaderboards (if it is available)"); + this.plugin = plugin; + } + + @Override + public List autoComplete(CommandSender commandSender, String[] strings) { + return Collections.emptyList(); + } + + @Override + public void execute(CommandSender commandSender, String[] strings, String s) { + + UpdateManager updater = plugin.getUpdateManager(); + + if(updater == null) { + commandSender.sendMessage( + plugin.getMessages().getComponent("updater.disabled") + ); + return; + } + + AtomicBoolean done = new AtomicBoolean(false); + plugin.getScheduler().runTaskAsynchronously(() -> { + try { + UpdateManager.DownloadCompleteStatus result = updater.downloadUpdate(); + + done.set(true); + + switch(result) { + case SUCCESS: + commandSender.sendMessage(plugin.getMessages().getComponent("updater.success")); + break; + case WARNING_COULD_NOT_DELETE_OLD_JAR: + commandSender.sendMessage(plugin.getMessages().getComponent("updater.warnings.could-not-delete-old-jar")); + break; + case ERROR_NO_UPDATE_AVAILABLE: + commandSender.sendMessage(plugin.getMessages().getComponent("updater.errors.no-update-available")); + break; + case ERROR_WHILE_CHECKING: + commandSender.sendMessage(plugin.getMessages().getComponent("updater.errors.while-checking")); + break; + case ERROR_ALREADY_DOWNLOADED: + commandSender.sendMessage(plugin.getMessages().getComponent("updater.errors.already-downloaded")); + break; + case ERROR_WHILE_DOWNLOADING: + commandSender.sendMessage(plugin.getMessages().getComponent("updater.errors.while-downloading")); + break; + default: + commandSender.sendMessage(plugin.getMessages().getComponent("updater.errors.unknown", "ERROR:" + result)); + break; + } + } catch(Exception e) { + commandSender.sendMessage(plugin.getMessages().getComponent("updater.errors.uncaught")); + } + }); + plugin.getScheduler().runTaskLaterAsynchronously(() -> { + if(done.get()) return; + + commandSender.sendMessage( + plugin.getMessages().getComponent("updater.slow-feedback") + ); + + }, 20); + } +} diff --git a/src/main/java/us/ajg0702/leaderboards/loaders/MessageLoader.java b/src/main/java/us/ajg0702/leaderboards/loaders/MessageLoader.java index a7adb9fa..5c7be328 100644 --- a/src/main/java/us/ajg0702/leaderboards/loaders/MessageLoader.java +++ b/src/main/java/us/ajg0702/leaderboards/loaders/MessageLoader.java @@ -77,7 +77,18 @@ public static Messages loadMessages(LeaderboardPlugin plugin) { "loading.text", "Loading", "loading.short", "-?-", "loading.position", "..", - "loading.size", "..." + "loading.size", "...", + + "updater.slow-feedback", "&7Checking for update and downloading...", + "updater.disabled", "&cThe updater is disabled! &7Please enable it in the config to download updates.", + "updater.success", "&aUpdate downloaded! &7Restart the server to apply the update.", + "updater.warnings.could-not-delete-old-jar", "&aUpdate downloaded&e but the old jar could not be deleted. &7Please delete the old jar before restarting the server.", + "updater.errors.no-update-available", "&aYou are already up to date!", + "updater.errors.while-checking", "&eAn error occurred while checking for an update. &7See the console for more info.", + "updater.errors.already-downloaded", "&aYou have already downloaded an update! &7Restart the server to apply it.", + "updater.errors.while-downloading", "&eAn error occurred while downloading an update. &7See the console for more info.", + "updater.errors.unknown", "&eAn unknown error occurred: {ERROR}", + "updater.errors.uncaught", "&cAn error occurred while executing this command. &7See the console." )); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b25d5c74..3a87ff7c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -187,6 +187,10 @@ check-cache-on-update: true # Default: true more-fetching: true +# Should we check for updates? +# The updater will also let you update just by doing /ajLeaderboards update +# Changing this requires a restart +enable-updater: true # Should we skip some steps when shutting down the plugin to make it faster? # If you enable this, it can cause warnings after the server shuts down, @@ -230,7 +234,7 @@ fetching-thread-pool-keep-alive: 500 # Don't touch this -config-version: 33 +config-version: 34 # ----- THESE OPTIONS HAVE MOVED ----- # to messages.yml. They will be removed from here in the future.