perf: 简化代码

This commit is contained in:
2024-03-18 17:13:14 +08:00
parent e6cb7efe77
commit 9bcf8a28dd
4 changed files with 23 additions and 23 deletions

View File

@@ -1,10 +1,13 @@
package cn.hamster3.mc.plugin.core.common.util;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -24,19 +27,23 @@ public final class UpdateCheckUtils {
private UpdateCheckUtils() {
}
public static void showUpdate(@NotNull String pluginName, @NotNull ConfigSection config, @NotNull UpdateReceiver sender) throws IOException {
String version = config.getString("version");
public static void checkUpdate(@NotNull String pluginName, @NotNull ConfigSection updateConfig) throws IOException {
checkUpdate(pluginName, updateConfig, CoreAPI.getInstance().getAudienceProvider().console());
}
public static void checkUpdate(@NotNull String pluginName, @NotNull ConfigSection updateConfig, @NotNull Audience sender) throws IOException {
String version = updateConfig.getString("version");
if (version == null) {
return;
}
String checkType = config.getString("CHECK_TYPE", "");
String baseUrl = config.getString("GIT_BASE_URL");
String gitRepo = config.getString("GIT_REPO");
String downloadUrl = config.getString("DOWNLOAD_URL");
String checkType = updateConfig.getString("CHECK_TYPE", "");
String baseUrl = updateConfig.getString("GIT_BASE_URL");
String gitRepo = updateConfig.getString("GIT_REPO");
String downloadUrl = updateConfig.getString("DOWNLOAD_URL");
if (baseUrl == null || gitRepo == null || downloadUrl == null) {
return;
}
String gitToken = config.getString("GIT_TOKEN");
String gitToken = updateConfig.getString("GIT_TOKEN");
String lastRelease = null;
switch (checkType) {
case "GITEA_RELEASES": {
@@ -58,13 +65,15 @@ public final class UpdateCheckUtils {
if (compareVersion(lastRelease, version) <= 0) {
return;
}
sender.sendMessage(String.format("§a插件 §l%s§a 发布了新版本 %s", pluginName, lastRelease));
sender.sendMessage(String.format("§b下载链接: §n%s", downloadUrl));
sender.sendMessage(Component.text(String.format("§a插件 §l%s§a 发布了新版本 %s", pluginName, lastRelease)));
sender.sendMessage(Component.text(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());
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;
@@ -143,8 +152,4 @@ public final class UpdateCheckUtils {
}
}
}
public interface UpdateReceiver {
void sendMessage(@NotNull String message);
}
}