9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-19 15:09:15 +00:00

添加更新检查

This commit is contained in:
XiaoMoMi
2025-12-11 03:57:43 +08:00
parent 14f9af489e
commit 2978704ede
4 changed files with 94 additions and 2 deletions

View File

@@ -52,6 +52,8 @@ info.resource_pack.upload: "Completed uploading resource pack"
info.host.self.netty_server: "Netty HTTP server started on port: <arg:0>"
info.host.cache.load: "[<arg:0>] Loaded cached resource pack metadata"
info.compatibility: "[Compatibility] <arg:0> hooked"
info.update.available: "New version <arg:0> is available: <arg:1>"
info.update.latest: "You're running the latest version"
command.reload.config.success: "<white>Configs reloaded in <green><arg:0></green> ms.</white> <gray>(Async: <arg:1>ms | Sync: <arg:2>ms)</gray>"
command.reload.config.failure: "<red>Config reload failed. Check console logs.</red>"
command.reload.pack.success: "<white>Resource pack reloaded in <green><arg:0></green> ms.</white>"

View File

@@ -52,6 +52,8 @@ info.resource_pack.upload: "资源包上传完成"
info.host.self.netty_server: "Netty HTTP 服务已在端口 <arg:0> 开启"
info.host.cache.load: "[<arg:0>] 已加载缓存的资源包元数据"
info.compatibility: "[兼容性] 已挂钩 <arg:0>"
info.update.available: "新版本 <arg:0> 已可供下载: <arg:1>"
info.update.latest: "插件已是最新版本"
command.reload.config.success: "<white>重新加载配置完成. 耗时 <green><arg:0></green> 毫秒</white> <gray>(异步: <arg:1>ms | 同步: <arg:2>ms)</gray>"
command.reload.config.failure: "<red>重新加载配置失败, 请检查控制台日志</red>"
command.reload.pack.success: "<white>资源包重新加载完成. 耗时 <green><arg:0></green> 毫秒</white>"

View File

@@ -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();

View File

@@ -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