feat: 完成 gitea 更新检测功能

This commit is contained in:
2024-03-18 15:03:35 +08:00
parent 57f06c3ce2
commit bcfbaab30c
19 changed files with 203 additions and 218 deletions

View File

@@ -45,7 +45,7 @@ dependencies {
tasks {
processResources {
filesMatching("plugin.yml") {
expand(project.properties)
expand(rootProject.properties)
}
}
withType<Jar> {

View File

@@ -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");
}

View File

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

View File

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

View File

@@ -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);
}
Files.copy(stream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new IllegalArgumentException("在插件 " + plugin.getName() + " 内部读取文件 " + filename + " 时发生错误");
}

View File

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

View File

@@ -14,9 +14,6 @@ softdepend:
- PlayerPoints
- PlaceholderAPI
loadbefore:
- HamsterAPI
commands:
hamster-core:
aliases: [ hcore ]

View 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/