fix: 修复检查版本更新时的问题
This commit is contained in:
@@ -26,9 +26,9 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
// 对于 Bukkit 插件
|
||||
compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.3.2")
|
||||
compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.3.3-SNAPSHOT")
|
||||
// 对于 BungeeCord 插件
|
||||
compileOnly("cn.hamster3.mc.plugin:core-bungee:1.3.2")
|
||||
compileOnly("cn.hamster3.mc.plugin:core-bungee:1.3.3-SNAPSHOT")
|
||||
}
|
||||
```
|
||||
|
||||
@@ -54,13 +54,13 @@ dependencies {
|
||||
<dependency>
|
||||
<groupId>cn.hamster3.mc.plugin</groupId>
|
||||
<artifactId>core-bukkit</artifactId>
|
||||
<version>1.3.2</version>
|
||||
<version>1.3.3-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!--对于 BungeeCord 插件-->
|
||||
<dependency>
|
||||
<groupId>cn.hamster3.mc.plugin</groupId>
|
||||
<artifactId>core-bungee</artifactId>
|
||||
<version>1.3.2</version>
|
||||
<version>1.3.3-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@@ -5,7 +5,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "cn.hamster3.mc.plugin"
|
||||
version = "1.3.2"
|
||||
version = "1.3.3-SNAPSHOT"
|
||||
|
||||
subprojects {
|
||||
apply {
|
||||
|
@@ -27,7 +27,6 @@ import org.bukkit.scheduler.BukkitTask;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -127,7 +126,8 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
YamlConfig config = YamlConfig.load(reader);
|
||||
UpdateCheckUtils.showUpdate(plugin.getName(), config, Bukkit.getConsoleSender()::sendMessage);
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@@ -14,6 +14,9 @@ import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class UpdateCheckUtils {
|
||||
private static final JsonParser JSON_PARSER = new JsonParser();
|
||||
@@ -21,15 +24,12 @@ public final class UpdateCheckUtils {
|
||||
private UpdateCheckUtils() {
|
||||
}
|
||||
|
||||
public static void showUpdate(@NotNull String pluginName, @NotNull ConfigSection config, @NotNull UpdateReceiver sender) {
|
||||
public static void showUpdate(@NotNull String pluginName, @NotNull ConfigSection config, @NotNull UpdateReceiver sender) throws IOException {
|
||||
String version = config.getString("version");
|
||||
if (version == null) {
|
||||
return;
|
||||
}
|
||||
String checkType = config.getString("CHECK_TYPE", "");
|
||||
try {
|
||||
switch (checkType) {
|
||||
case "GITEA_RELEASES": {
|
||||
String baseUrl = config.getString("GIT_BASE_URL");
|
||||
String gitRepo = config.getString("GIT_REPO");
|
||||
String downloadUrl = config.getString("DOWNLOAD_URL");
|
||||
@@ -37,43 +37,46 @@ public final class UpdateCheckUtils {
|
||||
return;
|
||||
}
|
||||
String gitToken = config.getString("GIT_TOKEN");
|
||||
String lastRelease = getGiteaLastRelease(baseUrl, gitRepo, gitToken);
|
||||
if (lastRelease == null) {
|
||||
break;
|
||||
}
|
||||
if (lastRelease.compareToIgnoreCase(version) <= 0) {
|
||||
break;
|
||||
}
|
||||
sender.sendMessage(String.format("§a插件 §l%s§a 发布了新版本 %s", pluginName, lastRelease));
|
||||
sender.sendMessage(String.format("§b下载链接: §n%s", downloadUrl));
|
||||
String lastRelease = null;
|
||||
switch (checkType) {
|
||||
case "GITEA_RELEASES": {
|
||||
lastRelease = getGiteaLastRelease(baseUrl, gitRepo, gitToken);
|
||||
break;
|
||||
}
|
||||
case "GITLAB_RELEASES": {
|
||||
String baseUrl = config.getString("GIT_BASE_URL");
|
||||
String gitRepo = config.getString("GIT_REPO");
|
||||
String downloadUrl = config.getString("DOWNLOAD_URL");
|
||||
if (baseUrl == null || gitRepo == null || downloadUrl == null) {
|
||||
return;
|
||||
}
|
||||
String gitToken = config.getString("GIT_TOKEN");
|
||||
int projectID = getGitlabProjectID(baseUrl, gitRepo, gitToken);
|
||||
if (projectID < 0) {
|
||||
break;
|
||||
}
|
||||
String lastRelease = getGitlabLastRelease(baseUrl, projectID, gitToken);
|
||||
if (lastRelease == null) {
|
||||
lastRelease = getGitlabLastRelease(baseUrl, projectID, gitToken);
|
||||
break;
|
||||
}
|
||||
if (lastRelease.compareToIgnoreCase(version) <= 0) {
|
||||
break;
|
||||
}
|
||||
if (lastRelease == null) {
|
||||
return;
|
||||
}
|
||||
if (compareVersion(version, lastRelease) <= 0) {
|
||||
return;
|
||||
}
|
||||
sender.sendMessage(String.format("§a插件 §l%s§a 发布了新版本 %s", pluginName, lastRelease));
|
||||
sender.sendMessage(String.format("§b下载链接: §n%s", downloadUrl));
|
||||
break;
|
||||
}
|
||||
|
||||
public static int compareVersion(@NotNull String version1, String version2) {
|
||||
List<Integer> collect1 = Arrays.stream(version1.split("[+-]")[0].split("\\.")).map(Integer::parseInt).collect(Collectors.toList());
|
||||
List<Integer> collect2 = Arrays.stream(version2.split("[+-]")[0].split("\\.")).map(Integer::parseInt).collect(Collectors.toList());
|
||||
int max = Math.max(collect1.size(), collect2.size());
|
||||
for (int i = 0; i < max; i++) {
|
||||
int v1 = i < collect1.size() ? collect1.get(i) : 0;
|
||||
int v2 = i < collect2.size() ? collect2.get(i) : 0;
|
||||
if (v1 > v2) {
|
||||
return 1;
|
||||
}
|
||||
if (v1 < v2) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -103,12 +106,12 @@ public final class UpdateCheckUtils {
|
||||
}
|
||||
|
||||
public static int getGitlabProjectID(@NotNull String baseUrl, @NotNull String repo, @Nullable String token) throws IOException {
|
||||
URL url = new URL("https://" + baseUrl + "/api/v4/projects?search=" + repo);
|
||||
URL url = new URL(baseUrl + "/api/v4/projects?search=" + repo);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setDoInput(true);
|
||||
connection.setRequestMethod("GET");
|
||||
if (token != null) {
|
||||
connection.setRequestProperty("PRIVATE-TOKEN", "token " + token);
|
||||
connection.setRequestProperty("PRIVATE-TOKEN", token);
|
||||
}
|
||||
connection.connect();
|
||||
try (InputStream stream = connection.getInputStream()) {
|
||||
@@ -130,7 +133,7 @@ public final class UpdateCheckUtils {
|
||||
connection.setDoInput(true);
|
||||
connection.setRequestMethod("GET");
|
||||
if (token != null) {
|
||||
connection.setRequestProperty("Authorization", "token " + token);
|
||||
connection.setRequestProperty("PRIVATE-TOKEN", token);
|
||||
}
|
||||
connection.connect();
|
||||
try (InputStream stream = connection.getInputStream()) {
|
||||
|
Reference in New Issue
Block a user