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

@@ -124,7 +124,7 @@ public class HamsterCorePlugin extends JavaPlugin {
} }
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
YamlConfig config = YamlConfig.load(reader); YamlConfig config = YamlConfig.load(reader);
UpdateCheckUtils.showUpdate(plugin.getName(), config, Bukkit.getConsoleSender()::sendMessage); UpdateCheckUtils.checkUpdate(plugin.getName(), config);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@@ -7,7 +7,6 @@ import cn.hamster3.mc.plugin.core.common.util.UpdateCheckUtils;
import lombok.Getter; import lombok.Getter;
import net.kyori.adventure.platform.bungeecord.BungeeAudiences; import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
import java.io.File; import java.io.File;
@@ -73,10 +72,7 @@ public class HamsterCorePlugin extends Plugin {
} }
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
YamlConfig config = YamlConfig.load(reader); YamlConfig config = YamlConfig.load(reader);
UpdateCheckUtils.showUpdate( UpdateCheckUtils.checkUpdate(plugin.getDescription().getName(), config);
plugin.getDescription().getName(), config,
(s) -> ProxyServer.getInstance().getConsole().sendMessage(TextComponent.fromLegacyText(s))
);
} }
} catch (IOException ignored) { } catch (IOException ignored) {
} }

View File

@@ -1,10 +1,13 @@
package cn.hamster3.mc.plugin.core.common.util; 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 cn.hamster3.mc.plugin.core.common.config.ConfigSection;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; 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.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -24,19 +27,23 @@ public final class UpdateCheckUtils {
private UpdateCheckUtils() { private UpdateCheckUtils() {
} }
public static void showUpdate(@NotNull String pluginName, @NotNull ConfigSection config, @NotNull UpdateReceiver sender) throws IOException { public static void checkUpdate(@NotNull String pluginName, @NotNull ConfigSection updateConfig) throws IOException {
String version = config.getString("version"); 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) { if (version == null) {
return; return;
} }
String checkType = config.getString("CHECK_TYPE", ""); String checkType = updateConfig.getString("CHECK_TYPE", "");
String baseUrl = config.getString("GIT_BASE_URL"); String baseUrl = updateConfig.getString("GIT_BASE_URL");
String gitRepo = config.getString("GIT_REPO"); String gitRepo = updateConfig.getString("GIT_REPO");
String downloadUrl = config.getString("DOWNLOAD_URL"); String downloadUrl = updateConfig.getString("DOWNLOAD_URL");
if (baseUrl == null || gitRepo == null || downloadUrl == null) { if (baseUrl == null || gitRepo == null || downloadUrl == null) {
return; return;
} }
String gitToken = config.getString("GIT_TOKEN"); String gitToken = updateConfig.getString("GIT_TOKEN");
String lastRelease = null; String lastRelease = null;
switch (checkType) { switch (checkType) {
case "GITEA_RELEASES": { case "GITEA_RELEASES": {
@@ -58,13 +65,15 @@ public final class UpdateCheckUtils {
if (compareVersion(lastRelease, version) <= 0) { if (compareVersion(lastRelease, version) <= 0) {
return; return;
} }
sender.sendMessage(String.format("§a插件 §l%s§a 发布了新版本 %s", pluginName, lastRelease)); sender.sendMessage(Component.text(String.format("§a插件 §l%s§a 发布了新版本 %s", pluginName, lastRelease)));
sender.sendMessage(String.format("§b下载链接: §n%s", downloadUrl)); sender.sendMessage(Component.text(String.format("§b下载链接: §n%s", downloadUrl)));
} }
public static int compareVersion(@NotNull String version1, String version2) { 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> collect1 = Arrays.stream(version1.split("[+-]")[0].split("\\."))
List<Integer> collect2 = Arrays.stream(version2.split("[+-]")[0].split("\\.")).map(Integer::parseInt).collect(Collectors.toList()); .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()); int max = Math.max(collect1.size(), collect2.size());
for (int i = 0; i < max; i++) { for (int i = 0; i < max; i++) {
int v1 = i < collect1.size() ? collect1.get(i) : 0; 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);
}
} }

View File

@@ -15,7 +15,6 @@ import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter; import lombok.Getter;
import net.kyori.adventure.text.Component;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.File; import java.io.File;
@@ -97,7 +96,7 @@ public class HamsterCorePlugin {
} }
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
YamlConfig config = YamlConfig.load(reader); YamlConfig config = YamlConfig.load(reader);
UpdateCheckUtils.showUpdate(pluginName, config, (s) -> proxyServer.sendMessage(Component.text(s))); UpdateCheckUtils.checkUpdate(pluginName, config);
} catch (IOException ignored) { } catch (IOException ignored) {
} }
} catch (IOException e) { } catch (IOException e) {