fix: 修复检查版本更新时的问题

This commit is contained in:
2024-03-18 16:49:54 +08:00
parent aab082500a
commit fb75b4d95f
4 changed files with 60 additions and 57 deletions

View File

@@ -26,9 +26,9 @@ repositories {
dependencies { dependencies {
// 对于 Bukkit 插件 // 对于 Bukkit 插件
compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.3.2") compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.3.3-SNAPSHOT")
// 对于 BungeeCord 插件 // 对于 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> <dependency>
<groupId>cn.hamster3.mc.plugin</groupId> <groupId>cn.hamster3.mc.plugin</groupId>
<artifactId>core-bukkit</artifactId> <artifactId>core-bukkit</artifactId>
<version>1.3.2</version> <version>1.3.3-SNAPSHOT</version>
</dependency> </dependency>
<!--对于 BungeeCord 插件--> <!--对于 BungeeCord 插件-->
<dependency> <dependency>
<groupId>cn.hamster3.mc.plugin</groupId> <groupId>cn.hamster3.mc.plugin</groupId>
<artifactId>core-bungee</artifactId> <artifactId>core-bungee</artifactId>
<version>1.3.2</version> <version>1.3.3-SNAPSHOT</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -5,7 +5,7 @@ plugins {
} }
group = "cn.hamster3.mc.plugin" group = "cn.hamster3.mc.plugin"
version = "1.3.2" version = "1.3.3-SNAPSHOT"
subprojects { subprojects {
apply { apply {

View File

@@ -27,7 +27,6 @@ import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@@ -127,7 +126,8 @@ public class HamsterCorePlugin extends JavaPlugin {
YamlConfig config = YamlConfig.load(reader); YamlConfig config = YamlConfig.load(reader);
UpdateCheckUtils.showUpdate(plugin.getName(), config, Bukkit.getConsoleSender()::sendMessage); UpdateCheckUtils.showUpdate(plugin.getName(), config, Bukkit.getConsoleSender()::sendMessage);
} }
} catch (IOException ignored) { } catch (Exception e) {
e.printStackTrace();
} }
} }
}); });

View File

@@ -14,6 +14,9 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public final class UpdateCheckUtils { public final class UpdateCheckUtils {
private static final JsonParser JSON_PARSER = new JsonParser(); private static final JsonParser JSON_PARSER = new JsonParser();
@@ -21,59 +24,59 @@ public final class UpdateCheckUtils {
private 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"); String version = config.getString("version");
if (version == null) { if (version == null) {
return; return;
} }
String checkType = config.getString("CHECK_TYPE", ""); String checkType = config.getString("CHECK_TYPE", "");
try { String baseUrl = config.getString("GIT_BASE_URL");
switch (checkType) { String gitRepo = config.getString("GIT_REPO");
case "GITEA_RELEASES": { String downloadUrl = config.getString("DOWNLOAD_URL");
String baseUrl = config.getString("GIT_BASE_URL"); if (baseUrl == null || gitRepo == null || downloadUrl == null) {
String gitRepo = config.getString("GIT_REPO"); return;
String downloadUrl = config.getString("DOWNLOAD_URL");
if (baseUrl == null || gitRepo == null || downloadUrl == null) {
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));
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) {
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));
break;
}
}
} catch (Exception ignored) {
} }
String gitToken = config.getString("GIT_TOKEN");
String lastRelease = null;
switch (checkType) {
case "GITEA_RELEASES": {
lastRelease = getGiteaLastRelease(baseUrl, gitRepo, gitToken);
break;
}
case "GITLAB_RELEASES": {
int projectID = getGitlabProjectID(baseUrl, gitRepo, gitToken);
if (projectID < 0) {
break;
}
lastRelease = getGitlabLastRelease(baseUrl, projectID, gitToken);
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));
}
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;
}
}
return 0;
} }
@Nullable @Nullable
@@ -103,12 +106,12 @@ public final class UpdateCheckUtils {
} }
public static int getGitlabProjectID(@NotNull String baseUrl, @NotNull String repo, @Nullable String token) throws IOException { 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(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true); connection.setDoInput(true);
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
if (token != null) { if (token != null) {
connection.setRequestProperty("PRIVATE-TOKEN", "token " + token); connection.setRequestProperty("PRIVATE-TOKEN", token);
} }
connection.connect(); connection.connect();
try (InputStream stream = connection.getInputStream()) { try (InputStream stream = connection.getInputStream()) {
@@ -130,7 +133,7 @@ public final class UpdateCheckUtils {
connection.setDoInput(true); connection.setDoInput(true);
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
if (token != null) { if (token != null) {
connection.setRequestProperty("Authorization", "token " + token); connection.setRequestProperty("PRIVATE-TOKEN", token);
} }
connection.connect(); connection.connect();
try (InputStream stream = connection.getInputStream()) { try (InputStream stream = connection.getInputStream()) {