Skip to content

Commit

Permalink
Add updater
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgeiss0702 committed Jun 7, 2024
1 parent c92fd70 commit d5f6d48
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 8 deletions.
19 changes: 17 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "us.ajg0702"
version = "2.8.0"
version = "2.7.0"

repositories {
mavenCentral()
Expand Down Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion nms/nms-19/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion nms/nms-legacy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/us/ajg0702/leaderboards/LeaderboardPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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!");
}

Expand Down Expand Up @@ -481,6 +489,10 @@ public static MiniMessage getMiniMessage() {
return miniMessage;
}

public UpdateManager getUpdateManager() {
return updateManager;
}

private static BukkitAudiences adventure;

public BukkitAudiences getAdventure() {
Expand Down Expand Up @@ -551,4 +563,31 @@ public Future<Material> 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);
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}
13 changes: 12 additions & 1 deletion src/main/java/us/ajg0702/leaderboards/loaders/MessageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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."

));
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d5f6d48

Please sign in to comment.