feat: 完成 gitea 更新检测功能
This commit is contained in:
@@ -44,15 +44,8 @@ subprojects {
|
||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||
}
|
||||
processResources {
|
||||
val map = mutableMapOf<String, String>()
|
||||
map["BUILD_ID"] = System.getenv().getOrDefault("BUILD_ID", "DEV")
|
||||
map["BUILD_NUMBER"] = System.getenv().getOrDefault("BUILD_NUMBER", "DEV")
|
||||
map["BUILD_DISPLAY_NAME"] = System.getenv().getOrDefault("BUILD_DISPLAY_NAME", "DEV")
|
||||
map["JOB_URL"] = System.getenv().getOrDefault("JOB_URL", "DEV")
|
||||
map["BUILD_URL"] = System.getenv().getOrDefault("BUILD_URL", "DEV")
|
||||
map["GIT_COMMIT"] = System.getenv().getOrDefault("GIT_COMMIT", "DEV")
|
||||
filesMatching("jenkins.yml") {
|
||||
expand(map)
|
||||
filesMatching("update.yml") {
|
||||
expand(rootProject.properties)
|
||||
}
|
||||
}
|
||||
build {
|
||||
|
@@ -45,7 +45,7 @@ dependencies {
|
||||
tasks {
|
||||
processResources {
|
||||
filesMatching("plugin.yml") {
|
||||
expand(project.properties)
|
||||
expand(rootProject.properties)
|
||||
}
|
||||
}
|
||||
withType<Jar> {
|
||||
|
@@ -9,25 +9,30 @@ import cn.hamster3.mc.plugin.core.bukkit.hook.PointAPI;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.hook.VaultAPI;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.CallbackListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.DebugListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.JenkinsUpdateListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.page.handler.PageHandler;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.page.listener.PageListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.MinecraftVersion;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
|
||||
import cn.hamster3.mc.plugin.core.common.util.UpdateCheckUtils;
|
||||
import lombok.Getter;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryView;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
@@ -74,11 +79,11 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
}
|
||||
File configFile = new File(dataFolder, "config.yml");
|
||||
if (!configFile.exists()) {
|
||||
Files.copy(
|
||||
Objects.requireNonNull(getResource("config.yml")),
|
||||
configFile.toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING
|
||||
);
|
||||
try (InputStream stream = getResource("config.yml")) {
|
||||
if (stream != null) {
|
||||
Files.copy(stream, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
}
|
||||
CoreBukkitAPI.init(configFile);
|
||||
logger.info("已初始化 CoreAPI");
|
||||
@@ -108,13 +113,25 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
logger.info("已注册 CallbackListener");
|
||||
Bukkit.getPluginManager().registerEvents(DebugListener.INSTANCE, this);
|
||||
logger.info("已注册 DebugListener");
|
||||
Bukkit.getPluginManager().registerEvents(JenkinsUpdateListener.INSTANCE, this);
|
||||
logger.info("已注册 JenkinsUpdateListener");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
sync(() -> {
|
||||
PointAPI.reloadPlayerPointAPIHook();
|
||||
VaultAPI.reloadVaultHook();
|
||||
JenkinsUpdateListener.showUpdate(Bukkit.getConsoleSender());
|
||||
async(() -> {
|
||||
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
try (InputStream stream = plugin.getResource("update.yml")) {
|
||||
if (stream == null) {
|
||||
continue;
|
||||
}
|
||||
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
|
||||
YamlConfig config = YamlConfig.load(reader);
|
||||
UpdateCheckUtils.showUpdate(plugin.getName(), config, Bukkit.getConsoleSender()::sendMessage);
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
logger.info("仓鼠核心启动完成,总计耗时 " + time + " ms");
|
||||
}
|
||||
|
@@ -1,73 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.listener;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
|
||||
import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
|
||||
import cn.hamster3.mc.plugin.core.common.util.JenkinsUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashSet;
|
||||
import java.util.UUID;
|
||||
|
||||
public class JenkinsUpdateListener implements Listener {
|
||||
public static final JenkinsUpdateListener INSTANCE = new JenkinsUpdateListener();
|
||||
public static HashSet<UUID> SHOWED = new HashSet<>();
|
||||
|
||||
private JenkinsUpdateListener() {
|
||||
}
|
||||
|
||||
public static void showUpdate(@NotNull CommandSender sender) {
|
||||
HamsterCorePlugin.async(() -> {
|
||||
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
InputStream resource = plugin.getResource("jenkins.yml");
|
||||
if (resource == null) {
|
||||
continue;
|
||||
}
|
||||
try (InputStreamReader reader = new InputStreamReader(resource, StandardCharsets.UTF_8)) {
|
||||
YamlConfig jenkinsConfig = YamlConfig.load(reader);
|
||||
String jobUrl = jenkinsConfig.getString("JOB_URL");
|
||||
if (jobUrl == null || jobUrl.equalsIgnoreCase("DEV")) {
|
||||
continue;
|
||||
}
|
||||
String buildNumberString = jenkinsConfig.getString("BUILD_NUMBER");
|
||||
if (buildNumberString == null || buildNumberString.equalsIgnoreCase("DEV")) {
|
||||
continue;
|
||||
}
|
||||
int lastStableBuild = JenkinsUtils.getLastStableBuild(jobUrl, null, null);
|
||||
int buildNumber = Integer.parseInt(buildNumberString);
|
||||
int version = lastStableBuild - buildNumber;
|
||||
if (version <= 0) {
|
||||
continue;
|
||||
}
|
||||
String pluginName = plugin.getName();
|
||||
sender.sendMessage(String.format(
|
||||
"§a检测到插件 %s 有 %d 个版本更新, 下载链接: §n§l%s", pluginName, version, jobUrl
|
||||
));
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (!player.hasPermission("hamster.core.admin")) {
|
||||
return;
|
||||
}
|
||||
if (!SHOWED.add(player.getUniqueId())) {
|
||||
return;
|
||||
}
|
||||
showUpdate(player);
|
||||
}
|
||||
}
|
@@ -48,15 +48,14 @@ public class PageManager {
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(pageConfigFile);
|
||||
return new PageConfig(plugin, config);
|
||||
}
|
||||
try (InputStream resource = plugin.getResource("pages/" + filename)) {
|
||||
if (resource == null) {
|
||||
throw new IllegalArgumentException("在插件 " + plugin.getName() + " 的 Jar 文件内部未找到 /pages/" + filename + " ");
|
||||
try (InputStream stream = plugin.getResource("pages/" + filename)) {
|
||||
if (stream != null) {
|
||||
Files.copy(stream, pageConfigFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
Files.copy(resource, pageConfigFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(pageConfigFile);
|
||||
return new PageConfig(plugin, config);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("为插件 " + pluginName + " 加载页面配置文件 " + filename + " 时出错", e);
|
||||
}
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(pageConfigFile);
|
||||
return new PageConfig(plugin, config);
|
||||
}
|
||||
}
|
||||
|
@@ -312,10 +312,9 @@ public final class CoreBukkitUtils {
|
||||
if (!file.exists()) {
|
||||
plugin.getLogger().info("生成配置文件: " + filename);
|
||||
try (InputStream stream = plugin.getResource(filename)) {
|
||||
if (stream == null) {
|
||||
throw new NullPointerException("在插件 " + plugin.getName() + " 的文件内部未找到 " + filename + " ");
|
||||
}
|
||||
if (stream != null) {
|
||||
Files.copy(stream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("在插件 " + plugin.getName() + " 内部读取文件 " + filename + " 时发生错误");
|
||||
}
|
||||
|
@@ -1,6 +0,0 @@
|
||||
BUILD_ID: ${BUILD_ID}
|
||||
BUILD_NUMBER: ${BUILD_NUMBER}
|
||||
BUILD_DISPLAY_NAME: ${BUILD_DISPLAY_NAME}
|
||||
JOB_URL: ${JOB_URL}
|
||||
BUILD_URL: ${BUILD_URL}
|
||||
GIT_COMMIT: ${GIT_COMMIT}
|
@@ -14,9 +14,6 @@ softdepend:
|
||||
- PlayerPoints
|
||||
- PlaceholderAPI
|
||||
|
||||
loadbefore:
|
||||
- HamsterAPI
|
||||
|
||||
commands:
|
||||
hamster-core:
|
||||
aliases: [ hcore ]
|
||||
|
6
core-bukkit/src/main/resources/update.yml
Normal file
6
core-bukkit/src/main/resources/update.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
version: ${version}
|
||||
CHECK_TYPE: GITEA_RELEASES
|
||||
GIT_BASE_URL: https://git.airgame.net
|
||||
GIT_REPO: MiniDay/hamster-core
|
||||
GIT_TOKEN: a44a69a4d1b8601bf6091403247759cd28764d5e
|
||||
DOWNLOAD_URL: https://jenkins.airgame.net/job/opensource/job/hamster-core/
|
@@ -3,14 +3,12 @@ package cn.hamster3.mc.plugin.core.bungee;
|
||||
import cn.hamster3.mc.plugin.core.bungee.api.CoreBungeeAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
|
||||
import cn.hamster3.mc.plugin.core.common.util.JenkinsUtils;
|
||||
import cn.hamster3.mc.plugin.core.common.util.UpdateCheckUtils;
|
||||
import lombok.Getter;
|
||||
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -41,11 +39,11 @@ public class HamsterCorePlugin extends Plugin {
|
||||
}
|
||||
File configFile = new File(dataFolder, "config.yml");
|
||||
if (!configFile.exists()) {
|
||||
Files.copy(
|
||||
getResourceAsStream("config.yml"),
|
||||
configFile.toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING
|
||||
);
|
||||
try (InputStream stream = getResourceAsStream("config.yml")) {
|
||||
if (stream != null) {
|
||||
Files.copy(stream, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
}
|
||||
CoreBungeeAPI.init(configFile);
|
||||
logger.info("已初始化 CoreAPI");
|
||||
@@ -66,7 +64,24 @@ public class HamsterCorePlugin extends Plugin {
|
||||
logger.info("已创建 AudienceProvider");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
logger.info("仓鼠核心启动完成,总计耗时 " + time + " ms");
|
||||
showUpdate(ProxyServer.getInstance().getConsole());
|
||||
|
||||
CoreAPI.getInstance().getExecutorService().submit(() -> {
|
||||
for (Plugin plugin : ProxyServer.getInstance().getPluginManager().getPlugins()) {
|
||||
try (InputStream stream = plugin.getResourceAsStream("update.yml")) {
|
||||
if (stream == null) {
|
||||
continue;
|
||||
}
|
||||
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
|
||||
YamlConfig config = YamlConfig.load(reader);
|
||||
UpdateCheckUtils.showUpdate(
|
||||
plugin.getDescription().getName(), config,
|
||||
(s) -> ProxyServer.getInstance().getConsole().sendMessage(TextComponent.fromLegacyText(s))
|
||||
);
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,37 +99,4 @@ public class HamsterCorePlugin extends Plugin {
|
||||
long time = System.currentTimeMillis() - start;
|
||||
logger.info("仓鼠核心已关闭,总计耗时 " + time + " ms");
|
||||
}
|
||||
|
||||
private void showUpdate(@NotNull CommandSender sender) {
|
||||
ProxyServer.getInstance().getScheduler().runAsync(HamsterCorePlugin.getInstance(), () -> {
|
||||
for (Plugin plugin : ProxyServer.getInstance().getPluginManager().getPlugins()) {
|
||||
InputStream resource = plugin.getResourceAsStream("jenkins.yml");
|
||||
if (resource == null) {
|
||||
continue;
|
||||
}
|
||||
try (InputStreamReader reader = new InputStreamReader(resource, StandardCharsets.UTF_8)) {
|
||||
YamlConfig jenkinsConfig = YamlConfig.load(reader);
|
||||
String jobUrl = jenkinsConfig.getString("JOB_URL");
|
||||
if (jobUrl == null || jobUrl.equalsIgnoreCase("DEV")) {
|
||||
continue;
|
||||
}
|
||||
String buildNumberString = jenkinsConfig.getString("BUILD_NUMBER");
|
||||
if (buildNumberString == null || buildNumberString.equalsIgnoreCase("DEV")) {
|
||||
continue;
|
||||
}
|
||||
int lastStableBuild = JenkinsUtils.getLastStableBuild(jobUrl, null, null);
|
||||
int buildNumber = Integer.parseInt(buildNumberString);
|
||||
int version = lastStableBuild - buildNumber;
|
||||
if (version <= 0) {
|
||||
continue;
|
||||
}
|
||||
String pluginName = plugin.getDescription().getName();
|
||||
sender.sendMessage(TextComponent.fromLegacyText(String.format(
|
||||
"§a检测到插件 %s 落后 %d 个版本更新, 下载链接: §n§l%s", pluginName, version, jobUrl
|
||||
)));
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
@@ -66,11 +67,11 @@ public final class CoreBungeeCordUtils {
|
||||
}
|
||||
File configFile = new File(plugin.getDataFolder(), filename);
|
||||
try {
|
||||
Files.copy(
|
||||
plugin.getResourceAsStream(filename),
|
||||
configFile.toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING
|
||||
);
|
||||
try (InputStream stream = plugin.getResourceAsStream(filename)) {
|
||||
if (stream != null) {
|
||||
Files.copy(stream, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
|
@@ -1,6 +0,0 @@
|
||||
BUILD_ID: ${BUILD_ID}
|
||||
BUILD_NUMBER: ${BUILD_NUMBER}
|
||||
BUILD_DISPLAY_NAME: ${BUILD_DISPLAY_NAME}
|
||||
JOB_URL: ${JOB_URL}
|
||||
BUILD_URL: ${BUILD_URL}
|
||||
GIT_COMMIT: ${GIT_COMMIT}
|
6
core-bungee/src/main/resources/update.yml
Normal file
6
core-bungee/src/main/resources/update.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
version: ${version}
|
||||
CHECK_TYPE: GITEA_RELEASES
|
||||
GIT_BASE_URL: https://git.airgame.net
|
||||
GIT_REPO: MiniDay/hamster-core
|
||||
GIT_TOKEN: a44a69a4d1b8601bf6091403247759cd28764d5e
|
||||
DOWNLOAD_URL: https://jenkins.airgame.net/job/opensource/job/hamster-core/
|
@@ -1,41 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.core.common.util;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
public final class JenkinsUtils {
|
||||
private static final JsonParser JSON_PARSER = new JsonParser();
|
||||
|
||||
private JenkinsUtils() {
|
||||
}
|
||||
|
||||
public static int getLastStableBuild(@NotNull String jobUrl, @Nullable String username, @Nullable String apiToken) throws IOException {
|
||||
URL url = new URL(jobUrl + "api/json");
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setDoInput(true);
|
||||
connection.setRequestMethod("GET");
|
||||
if (username != null && apiToken != null) {
|
||||
String token = username + ":" + apiToken;
|
||||
String base64 = Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8));
|
||||
connection.setRequestProperty("Authorization", "Basic " + base64);
|
||||
}
|
||||
connection.connect();
|
||||
try (InputStream stream = connection.getInputStream()) {
|
||||
try (InputStreamReader reader = new InputStreamReader(stream)) {
|
||||
JsonObject object = JSON_PARSER.parse(reader).getAsJsonObject();
|
||||
JsonObject lastStableBuild = object.getAsJsonObject("lastStableBuild");
|
||||
return lastStableBuild.get("number").getAsInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
package cn.hamster3.mc.plugin.core.common.util;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public final class UpdateCheckUtils {
|
||||
private static final JsonParser JSON_PARSER = new JsonParser();
|
||||
|
||||
private UpdateCheckUtils() {
|
||||
}
|
||||
|
||||
public static void showUpdate(@NotNull String pluginName, @NotNull ConfigSection config, @NotNull UpdateReceiver sender) {
|
||||
String version = config.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");
|
||||
if (baseUrl == null || gitRepo == null || downloadUrl == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
switch (checkType) {
|
||||
case "GITEA_RELEASES": {
|
||||
String gitToken = config.getString("GIT_TOKEN");
|
||||
String lastRelease = getGiteaLastRelease(baseUrl, gitRepo, gitToken);
|
||||
if (lastRelease == null) {
|
||||
break;
|
||||
}
|
||||
sender.sendMessage(String.format("§a插件 §l%s§a 发布了新版本 %s", pluginName, lastRelease));
|
||||
sender.sendMessage(String.format("§b下载链接: §n%s", downloadUrl));
|
||||
break;
|
||||
}
|
||||
case "": {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getGiteaLastRelease(@NotNull String baseUrl, @NotNull String repo, @Nullable String token) throws IOException {
|
||||
URL url = new URL(baseUrl + "/api/v1/repos/" + repo + "/releases?limit=10");
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setDoInput(true);
|
||||
connection.setRequestMethod("GET");
|
||||
if (token != null) {
|
||||
connection.setRequestProperty("Authorization", "token " + token);
|
||||
}
|
||||
connection.connect();
|
||||
try (InputStream stream = connection.getInputStream()) {
|
||||
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
|
||||
JsonArray array = JSON_PARSER.parse(reader).getAsJsonArray();
|
||||
if (array.size() >= 1) {
|
||||
JsonObject object = array.get(0).getAsJsonObject();
|
||||
return object.get("name").getAsString();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public interface UpdateReceiver {
|
||||
void sendMessage(@NotNull String message);
|
||||
}
|
||||
}
|
@@ -36,7 +36,7 @@ val templateDest = layout.buildDirectory.dir("generated/sources/templates")
|
||||
val generateTemplates = tasks.register<Copy>("generateTemplates") {
|
||||
from(templateSource)
|
||||
into(templateDest)
|
||||
expand(project.properties)
|
||||
expand(rootProject.properties)
|
||||
}
|
||||
|
||||
sourceSets.main.get().java.srcDir(generateTemplates.map { it.outputs })
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package cn.hamster3.mc.plugin.core.velocity;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
|
||||
import cn.hamster3.mc.plugin.core.common.util.UpdateCheckUtils;
|
||||
import cn.hamster3.mc.plugin.core.velocity.api.CoreVelocityAPI;
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.event.PostOrder;
|
||||
@@ -8,17 +10,22 @@ import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
||||
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
|
||||
import com.velocitypowered.api.plugin.Plugin;
|
||||
import com.velocitypowered.api.plugin.PluginContainer;
|
||||
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import lombok.Getter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Objects;
|
||||
|
||||
@Plugin(
|
||||
id = "hamster-core",
|
||||
@@ -53,11 +60,11 @@ public class HamsterCorePlugin {
|
||||
}
|
||||
File configFile = new File(dataFolder, "config.yml");
|
||||
if (!configFile.exists()) {
|
||||
Files.copy(
|
||||
Objects.requireNonNull(HamsterCorePlugin.class.getResourceAsStream("/config.yml")),
|
||||
configFile.toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING
|
||||
);
|
||||
try (InputStream stream = HamsterCorePlugin.class.getResourceAsStream("/config.yml")) {
|
||||
if (stream != null) {
|
||||
Files.copy(stream, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
}
|
||||
CoreVelocityAPI.init(configFile);
|
||||
slf4jLogger.info("已初始化 CoreAPI");
|
||||
@@ -74,6 +81,30 @@ public class HamsterCorePlugin {
|
||||
slf4jLogger.info("仓鼠核心正在启动");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
slf4jLogger.info("仓鼠核心启动完成,总计耗时 " + time + " ms");
|
||||
CoreAPI.getInstance().getExecutorService().submit(() -> {
|
||||
for (PluginContainer plugin : proxyServer.getPluginManager().getPlugins()) {
|
||||
String pluginName = plugin.getDescription().getName().orElse(null);
|
||||
if (pluginName == null) {
|
||||
continue;
|
||||
}
|
||||
Object pluginObject = plugin.getInstance().orElse(null);
|
||||
if (pluginObject == null) {
|
||||
continue;
|
||||
}
|
||||
try (InputStream stream = pluginObject.getClass().getResourceAsStream("/update.yml")) {
|
||||
if (stream == null) {
|
||||
continue;
|
||||
}
|
||||
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
|
||||
YamlConfig config = YamlConfig.load(reader);
|
||||
UpdateCheckUtils.showUpdate(pluginName, config, (s) -> proxyServer.sendMessage(Component.text(s)));
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Subscribe(order = PostOrder.LAST)
|
||||
|
@@ -1,6 +0,0 @@
|
||||
BUILD_ID: ${BUILD_ID}
|
||||
BUILD_NUMBER: ${BUILD_NUMBER}
|
||||
BUILD_DISPLAY_NAME: ${BUILD_DISPLAY_NAME}
|
||||
JOB_URL: ${JOB_URL}
|
||||
BUILD_URL: ${BUILD_URL}
|
||||
GIT_COMMIT: ${GIT_COMMIT}
|
6
core-velocity/src/main/resources/update.yml
Normal file
6
core-velocity/src/main/resources/update.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
version: ${version}
|
||||
CHECK_TYPE: GITEA_RELEASES
|
||||
GIT_BASE_URL: https://git.airgame.net
|
||||
GIT_REPO: MiniDay/hamster-core
|
||||
GIT_TOKEN: a44a69a4d1b8601bf6091403247759cd28764d5e
|
||||
DOWNLOAD_URL: https://jenkins.airgame.net/job/opensource/job/hamster-core/
|
Reference in New Issue
Block a user