Refactor particle directory handling and enhance logging
This commit is contained in:
parent
26b1e6f46c
commit
d7c744c20b
|
|
@ -10,6 +10,7 @@ import com.alttd.util.Logger;
|
|||
import com.destroystokyo.paper.ParticleBuilder;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Particle;
|
||||
|
|
@ -23,9 +24,10 @@ import java.nio.file.*;
|
|||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
public class ParticleConfig {
|
||||
|
||||
private static final int MAX_DEPTH = 1;
|
||||
private static final int MAX_DEPTH = 2;
|
||||
private static final File particlesDir = new File(File.separator + "mnt" + File.separator + "configs"
|
||||
+ File.separator + "AltitudeParticles" + File.separator + "particles");
|
||||
private static ParticleConfig instance = null;
|
||||
|
|
@ -50,45 +52,58 @@ public class ParticleConfig {
|
|||
private List<File> getJsonFiles() {
|
||||
List<File> files = new ArrayList<>();
|
||||
|
||||
// Ensure particles directory exists
|
||||
if (!ensureParticlesDirectoryExists()) {
|
||||
log.debug("Particles directory missing or not creatable: {}", particlesDir.getAbsolutePath());
|
||||
return files;
|
||||
}
|
||||
|
||||
log.debug("Traversing particles directory: {} (exists={}, isDirectory={})", particlesDir.getAbsolutePath(),
|
||||
particlesDir.exists(), particlesDir.isDirectory());
|
||||
|
||||
try {
|
||||
Files.walkFileTree(particlesDir.toPath(), getJsonFileVistor(files));
|
||||
Files.walkFileTree(particlesDir.toPath(),
|
||||
EnumSet.of(FileVisitOption.FOLLOW_LINKS),
|
||||
ParticleConfig.MAX_DEPTH,
|
||||
getJsonFileVistor(files));
|
||||
|
||||
} catch (IOException e) {
|
||||
Logger.warning("Error while traversing directory: " + e.getMessage());
|
||||
log.error("Error while traversing directory: {}", e.getMessage(), e);
|
||||
}
|
||||
|
||||
log.info("Found {} json files in particles directory", files.size());
|
||||
return files;
|
||||
}
|
||||
|
||||
private FileVisitor<? super @NotNull Path> getJsonFileVistor(List<File> files) {
|
||||
return new SimpleFileVisitor<>() {
|
||||
private int depth = 0;
|
||||
|
||||
@Override
|
||||
public @NotNull FileVisitResult preVisitDirectory(@NotNull Path dir, @NotNull BasicFileAttributes attrs) {
|
||||
if (depth > ParticleConfig.MAX_DEPTH) {
|
||||
return FileVisitResult.SKIP_SUBTREE;
|
||||
}
|
||||
depth++;
|
||||
log.debug("preVisitDirectory: {}", dir.toAbsolutePath());
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull FileVisitResult visitFile(@NotNull Path file, @NotNull BasicFileAttributes attrs) {
|
||||
if (!attrs.isRegularFile()) {
|
||||
log.debug("Skipping non-regular file path: {} (isDirectory={}, isOther={})", file.toAbsolutePath(), attrs.isDirectory(), attrs.isOther());
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
File physicalFile = file.toFile();
|
||||
log.debug("visitFile: {} (isFile={}, canRead={}, name={})", physicalFile.getAbsolutePath(), physicalFile.isFile(), physicalFile.canRead(), physicalFile.getName());
|
||||
if (isValidJsonFile(physicalFile)) {
|
||||
log.debug("Found JSON file: {}", physicalFile.getAbsolutePath());
|
||||
files.add(physicalFile);
|
||||
} else {
|
||||
log.debug("Ignoring non-json or unreadable file: {}", physicalFile.getAbsolutePath());
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull FileVisitResult postVisitDirectory(@NotNull Path dir, IOException exc) {
|
||||
depth--;
|
||||
log.debug("postVisitDirectory: {}", dir.toAbsolutePath());
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
};
|
||||
|
|
@ -101,15 +116,17 @@ public class ParticleConfig {
|
|||
*/
|
||||
private boolean ensureParticlesDirectoryExists() {
|
||||
if (!particlesDir.exists()) {
|
||||
log.info("Particles directory does not exist, attempting to create: {}", particlesDir.getAbsolutePath());
|
||||
if (!particlesDir.mkdirs()) {
|
||||
Logger.warning("Unable to create particles directory");
|
||||
Logger.warning("Unable to create particles directory at {}", particlesDir.getAbsolutePath());
|
||||
return false;
|
||||
}
|
||||
log.info("Created particles directory at {}", particlesDir.getAbsolutePath());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!particlesDir.isDirectory()) {
|
||||
Logger.warning("Particles path exists but is not a directory: " + particlesDir.getAbsolutePath());
|
||||
Logger.warning("Particles path exists but is not a directory: {}", particlesDir.getAbsolutePath());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -218,6 +235,7 @@ public class ParticleConfig {
|
|||
public static void reload() {
|
||||
ParticleStorage.clear();
|
||||
instance = getInstance();
|
||||
log.info("Reloading particles...");
|
||||
|
||||
for (File file : instance.getJsonFiles()) {
|
||||
loadParticleFromFile(file);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user