增加github更新支持

This commit is contained in:
zhangyuheng 2024-08-25 12:10:44 +08:00
parent 216886e88c
commit 4a7e0dfbdf
2 changed files with 45 additions and 10 deletions

10
pom.xml
View File

@ -35,6 +35,10 @@
<id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public</url>
</repository>
<repository>
<id>opencollab-snapshot</id>
<url>https://repo.opencollab.dev/main/</url>
</repository>
</repositories>
<dependencies>
@ -81,6 +85,12 @@
<version>2.2</version><!-- Validate this is the most recent version from the CI -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.geysermc.geyser</groupId>
<artifactId>api</artifactId>
<version>2.4.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

View File

@ -20,17 +20,17 @@ import java.net.URL;
import java.nio.file.Files;
import java.util.Random;
public class GiteaReleaseCheck implements Listener {
private static class GiteaRelease {
public class RepoReleaseCheck implements Listener {
private static class ReleaseInfo {
public String tag_name;
public String message;
public String html_url;
public String download_url;
}
private static GiteaReleaseCheck instance = null;
private static RepoReleaseCheck instance = null;
public GiteaReleaseCheck(JavaPlugin plugin, String giteaServer, String owner, String repo) {
public RepoReleaseCheck(JavaPlugin plugin, String giteaServer, String owner, String repo) {
instance = this;
this.gitea_server = giteaServer;
this.owner = owner;
@ -47,16 +47,40 @@ public class GiteaReleaseCheck implements Listener {
}, (10 + new Random().nextInt(10)) * 20, (60 * 60 * 12 + new Random().nextInt(60)) * 20);
}
public RepoReleaseCheck(JavaPlugin plugin, String owner, String repo) {
instance = this;
this.owner = owner;
this.repo = repo;
this.plugin = plugin;
this.current_version = plugin.getDescription().getVersion();
this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
// 异步每12小时检查一次更新
Scheduler.runTaskRepeatAsync(() -> {
getLatestRelease();
if (auto_update) {
downloadUpdate();
}
}, (10 + new Random().nextInt(10)) * 20, (60 * 60 * 12 + new Random().nextInt(60)) * 20);
}
public void enableAutoUpdate() {
auto_update = true;
}
private String repoReleases() {
return gitea_server + "/api/v1/repos/" + owner + "/" + repo + "/releases";
if (gitea_server != null) {
return gitea_server + "/api/v1/repos/" + owner + "/" + repo + "/releases";
} else {
return github_base_url + "/repos/" + owner + "/" + repo + "/releases";
}
}
private String tag(String tagName) {
return gitea_server + "/api/v1/repos/" + owner + "/" + repo + "/tags/" + tagName;
if (gitea_server != null) {
return gitea_server + "/api/v1/repos/" + owner + "/" + repo + "/tags/" + tagName;
} else {
return github_base_url + "/repos/" + owner + "/" + repo + "/git/refs/tags/" + tagName;
}
}
private void getLatestRelease() {
@ -81,12 +105,12 @@ public class GiteaReleaseCheck implements Listener {
}
JSONArray releaseList = (JSONArray) new JSONParser().parse(builder.toString());
JSONObject latestRelease = (JSONObject) releaseList.get(0);
GiteaRelease release = new GiteaRelease();
ReleaseInfo release = new ReleaseInfo();
release.tag_name = (String) latestRelease.get("tag_name");
release.message = (String) latestRelease.get("body");
release.html_url = (String) latestRelease.get("html_url");
JSONArray assets = (JSONArray) latestRelease.get("assets");
if (assets.size() > 0) {
if (!assets.isEmpty()) {
JSONObject asset = (JSONObject) assets.get(0);
release.download_url = (String) asset.get("browser_download_url");
}
@ -192,12 +216,13 @@ public class GiteaReleaseCheck implements Listener {
}
}
private final String gitea_server;
private String gitea_server = null;
private final String github_base_url = "https://api.github.com";
private final String owner;
private final String repo;
private final JavaPlugin plugin;
private final String current_version;
private GiteaRelease latest_release = null;
private ReleaseInfo latest_release = null;
private boolean auto_update = false;
private boolean isNewVersion(String current, String in_coming) {