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