Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions common-legacy-api/src/main/java/taboolib/common5/FileWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <K, V> ConfigurationSection.getMap(path: String): Map<K, V> {
inline fun <reified K, V> ConfigurationSection.getMap(path: String): Map<K, V> {
val map = HashMap<K, V>()
getConfigurationSection(path)?.let { section ->
section.getKeys(false).forEach { key ->
try {
map[key as K] = section[key] as V
val convertedKey = convertKey<K>(key)
map[convertedKey] = section[key] as V
} catch (ex: Throwable) {
ex.printStackTrace()
}
}
}
return map
}

@Suppress("UNCHECKED_CAST")
inline fun <reified K> 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
}
}
Loading