From 2978704edef4e2f73338edd52cb963b04b486bc3 Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Thu, 11 Dec 2025 03:57:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9B=B4=E6=96=B0=E6=A3=80?= =?UTF-8?q?=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/translations/en.yml | 2 + .../src/main/resources/translations/zh_cn.yml | 2 + .../craftengine/core/plugin/CraftEngine.java | 88 +++++++++++++++++++ gradle.properties | 4 +- 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/common-files/src/main/resources/translations/en.yml b/common-files/src/main/resources/translations/en.yml index 1a2907784..7e1cf4413 100644 --- a/common-files/src/main/resources/translations/en.yml +++ b/common-files/src/main/resources/translations/en.yml @@ -52,6 +52,8 @@ info.resource_pack.upload: "Completed uploading resource pack" info.host.self.netty_server: "Netty HTTP server started on port: " info.host.cache.load: "[] Loaded cached resource pack metadata" info.compatibility: "[Compatibility] hooked" +info.update.available: "New version is available: " +info.update.latest: "You're running the latest version" command.reload.config.success: "Configs reloaded in ms. (Async: ms | Sync: ms)" command.reload.config.failure: "Config reload failed. Check console logs." command.reload.pack.success: "Resource pack reloaded in ms." diff --git a/common-files/src/main/resources/translations/zh_cn.yml b/common-files/src/main/resources/translations/zh_cn.yml index 7ef4922c2..babcafa21 100644 --- a/common-files/src/main/resources/translations/zh_cn.yml +++ b/common-files/src/main/resources/translations/zh_cn.yml @@ -52,6 +52,8 @@ info.resource_pack.upload: "资源包上传完成" info.host.self.netty_server: "Netty HTTP 服务已在端口 开启" info.host.cache.load: "[] 已加载缓存的资源包元数据" info.compatibility: "[兼容性] 已挂钩 " +info.update.available: "新版本 已可供下载: " +info.update.latest: "插件已是最新版本" command.reload.config.success: "重新加载配置完成. 耗时 毫秒 (异步: ms | 同步: ms)" command.reload.config.failure: "重新加载配置失败, 请检查控制台日志" command.reload.pack.success: "资源包重新加载完成. 耗时 毫秒" diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/CraftEngine.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/CraftEngine.java index 68fea801d..e61d94def 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/CraftEngine.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/CraftEngine.java @@ -1,5 +1,6 @@ package net.momirealms.craftengine.core.plugin; +import com.google.gson.JsonObject; import net.momirealms.craftengine.core.advancement.AdvancementManager; import net.momirealms.craftengine.core.block.BlockManager; import net.momirealms.craftengine.core.entity.furniture.FurnitureManager; @@ -40,14 +41,23 @@ import net.momirealms.craftengine.core.plugin.network.NetworkManager; import net.momirealms.craftengine.core.plugin.scheduler.SchedulerAdapter; import net.momirealms.craftengine.core.sound.SoundManager; import net.momirealms.craftengine.core.util.CompletableFutures; +import net.momirealms.craftengine.core.util.GsonHelper; +import net.momirealms.craftengine.core.util.VersionHelper; import net.momirealms.craftengine.core.world.WorldManager; import net.momirealms.craftengine.core.world.score.TeamManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.Logger; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URL; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import java.util.function.Consumer; @@ -338,9 +348,87 @@ public abstract class CraftEngine implements Plugin { this.scheduler.executeAsync(() -> this.packManager.initCachedAssets()); // 正式完成重载 this.reloadEventDispatcher.accept(this); + // 检查更新 + if (Config.checkUpdate()) { + this.scheduler.executeAsync(this::checkUpdates); + } }); } + private void checkUpdates() { + boolean downloadFromPolymart = this.polymart.equals("1"); + boolean downloadFromBBB = this.buildByBit.equals("true"); + String link; + if (VersionHelper.PREMIUM) { + if (downloadFromPolymart) { + link = "https://polymart.org/product/7624/"; + } else if (downloadFromBBB) { + link = "https://builtbybit.com/resources/82674/"; + } else { + if (Locale.getDefault() == Locale.SIMPLIFIED_CHINESE) { + link = "QQ群[1039968907]"; + } else { + return; + } + } + } else { + link = "https://modrinth.com/plugin/craftengine/"; + } + try { + String lv = getLatestVersion(); + if (lv == null) return; + if (compareVer(lv, pluginVersion())) { + this.logger.warn(TranslationManager.instance().translateLog("info.update.available", lv, link)); + } else { + this.logger.info(TranslationManager.instance().translateLog("info.update.latest")); + } + } catch (Exception ignored) { + } + } + + private boolean compareVer(String v1, String v2) { + String[] parts1 = v1.split("\\."); + String[] parts2 = v2.split("\\."); + int maxLength = Math.max(parts1.length, parts2.length); + for (int i = 0; i < maxLength; i++) { + int num1 = i < parts1.length ? Integer.parseInt(parts1[i]) : 0; + int num2 = i < parts2.length ? Integer.parseInt(parts2[i]) : 0; + if (num1 != num2) { + return num1 > num2; + } + } + return false; + } + + @Nullable + private static String getLatestVersion() throws Exception { + String apiUrl = "https://api.spiget.org/v2/resources/128871/versions/latest"; + URL url = new URI(apiUrl).toURL(); + // 创建HTTP连接 + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setRequestProperty("User-Agent", "Mozilla/5.0"); + connection.setConnectTimeout(5000); + connection.setReadTimeout(5000); + // 获取响应代码 + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + // 读取响应内容 + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder response = new StringBuilder(); + String inputLine; + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + JsonObject jsonResponse = GsonHelper.get().fromJson(response.toString(), JsonObject.class); + if (jsonResponse.has("name")) { + return jsonResponse.get("name").getAsString(); + } + } + return null; + } + protected void onPluginDisable() { if (this.networkManager != null) this.networkManager.disable(); if (this.fontManager != null) this.fontManager.disable(); diff --git a/gradle.properties b/gradle.properties index 7cd4d5e25..88baa4d4f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,9 +1,9 @@ org.gradle.jvmargs=-Xmx4G # Project settings -project_version=0.0.66.3 +project_version=0.0.66.4 config_version=61 -lang_version=43 +lang_version=44 project_group=net.momirealms latest_supported_version=1.21.11