From 08de2cf9e0fedf3fef33173317ea121abb26f321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=98=BF=E9=B9=B0?= Date: Mon, 17 Nov 2025 10:25:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(configuration):=20=E4=BF=AE=E5=A4=8D=20?= =?UTF-8?q?getMap=20=E6=96=B9=E6=B3=95=E6=B3=9B=E5=9E=8B=20K=20=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E6=AD=A3=E7=A1=AE=E8=BD=AC=E6=8D=A2=E9=9D=9E=20String?= =?UTF-8?q?=20=E7=B1=BB=E5=9E=8B=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 ConfigurationSection.getMap 扩展函数,使其支持将配置键从 String 类型转换为其他基本类型。 主要改动: - 将泛型参数 K 添加 reified 修饰符,使其在运行时保留类型信息 - 新增 convertKey 函数,支持将 String 键转换为目标类型 - 使用 taboolib.common5 的类型转换扩展函数(cint、clong 等) 修复前:section.getKeys(false) 返回的 String 类型键直接强转为 K,无法正确转换为 Int 等数值类型 修复后:根据泛型 K 的实际类型,自动将 String 键转换为对应类型 --- .../module/configuration/util/SectionToMap.kt | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/module/basic/basic-configuration/src/main/kotlin/taboolib/module/configuration/util/SectionToMap.kt b/module/basic/basic-configuration/src/main/kotlin/taboolib/module/configuration/util/SectionToMap.kt index 786edc918..0e15daeb9 100644 --- a/module/basic/basic-configuration/src/main/kotlin/taboolib/module/configuration/util/SectionToMap.kt +++ b/module/basic/basic-configuration/src/main/kotlin/taboolib/module/configuration/util/SectionToMap.kt @@ -1,18 +1,42 @@ package taboolib.module.configuration.util +import taboolib.common5.cbool +import taboolib.common5.cbyte +import taboolib.common5.cchar +import taboolib.common5.cdouble +import taboolib.common5.cfloat +import taboolib.common5.cint +import taboolib.common5.clong +import taboolib.common5.cshort import taboolib.library.configuration.ConfigurationSection @Suppress("UNCHECKED_CAST") -fun ConfigurationSection.getMap(path: String): Map { +inline fun ConfigurationSection.getMap(path: String): Map { val map = HashMap() getConfigurationSection(path)?.let { section -> section.getKeys(false).forEach { key -> try { - map[key as K] = section[key] as V + val convertedKey = convertKey(key) + map[convertedKey] = section[key] as V } catch (ex: Throwable) { ex.printStackTrace() } } } return map +} + +@Suppress("UNCHECKED_CAST") +inline fun convertKey(key: String): K { + return when (K::class) { + Byte::class -> key.cbyte as K + Short::class -> key.cshort as K + Int::class -> key.cint as K + Long::class -> key.clong as K + Double::class -> key.cdouble as K + Float::class -> key.cfloat as K + Boolean::class -> key.cbool as K + Char::class -> key.cchar as K + else -> key as K + } } \ No newline at end of file From 46432b3d92b4d14d5dffec47fc74cc2b67e6b647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=98=BF=E9=B9=B0?= Date: Thu, 20 Nov 2025 09:29:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(file-watcher):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9B=91=E5=90=AC=E8=B7=AF=E5=8A=B2=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 通过 WatchKey 获取监听目录,构建完整变更路径 - 更新 handleEvent 方法参数为完整路径 - 优化目录和文件路径匹配逻辑 - 确保回调函数接收正确的文件路径 - 保持原有 relativize 路径关系检查机制 --- .../main/java/taboolib/common5/FileWatcher.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/common-legacy-api/src/main/java/taboolib/common5/FileWatcher.java b/common-legacy-api/src/main/java/taboolib/common5/FileWatcher.java index 063194842..9c8db39ee 100755 --- a/common-legacy-api/src/main/java/taboolib/common5/FileWatcher.java +++ b/common-legacy-api/src/main/java/taboolib/common5/FileWatcher.java @@ -55,12 +55,16 @@ public FileWatcher(int interval) { this.executorService.scheduleAtFixedRate(() -> { WatchKey key; while ((key = watchService.poll()) != null) { + WatchKey finalKey = key; key.pollEvents().forEach(event -> { if (event.context() instanceof Path) { Path changedPath = (Path) event.context(); + // 通过 WatchKey 获取监听的目录,构建完整路径 + Path watchedPath = (Path) finalKey.watchable(); + Path fullChangedPath = watchedPath.resolve(changedPath); fileListenerMap.forEach((file, listener) -> { try { - listener.handleEvent(changedPath); + listener.handleEvent(fullChangedPath); } catch (Throwable ex) { ex.printStackTrace(); } @@ -153,21 +157,20 @@ static class FileListener { ); } - public void handleEvent(Path changedPath) { - Path fullPath = file.getParentFile().toPath().resolve(changedPath); + public void handleEvent(Path fullChangedPath) { // 监听目录 if (file.isDirectory()) { try { // 使用 relativize 检查路径关系,更加准确 - file.toPath().relativize(fullPath); - callback.accept(fullPath.toFile()); + file.toPath().relativize(fullChangedPath); + callback.accept(fullChangedPath.toFile()); } catch (IllegalArgumentException ignored) { // 如果不是子路径,会抛出异常,直接忽略 } } // 监听文件 - else if (isSameFile(fullPath, file.toPath())) { - callback.accept(fullPath.toFile()); + else if (isSameFile(fullChangedPath, file.toPath())) { + callback.accept(fullChangedPath.toFile()); } }