Automatically load particles that are added to or updated in the particles folder
This commit is contained in:
@@ -98,4 +98,9 @@ public final class Config extends AbstractConfig {
|
||||
CLICK_BLOCK_COOL_DOWN = config.getInt("cool_down.click-block", CLICK_BLOCK_COOL_DOWN);
|
||||
TELEPORT_ARRIVE_COOL_DOWN = config.getInt("cool_down.teleport-arrive", TELEPORT_ARRIVE_COOL_DOWN);
|
||||
}
|
||||
|
||||
public static String AUTO_RELOAD_PATH = "/mnt/configs/AltitudeParticles/particles";
|
||||
private static void loadAutoReload() {
|
||||
AUTO_RELOAD_PATH = config.getString("auto-reload.path", AUTO_RELOAD_PATH);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,26 +8,32 @@ import com.alttd.objects.ParticleSet;
|
||||
import com.alttd.storage.ParticleStorage;
|
||||
import com.alttd.util.Logger;
|
||||
import com.destroystokyo.paper.ParticleBuilder;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Particle;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.*;
|
||||
|
||||
public class ParticleConfig {
|
||||
|
||||
private static final int MAX_DEPTH = 1;
|
||||
private static final File particlesDir = new File(File.separator + "mnt" + File.separator + "configs"
|
||||
+ File.separator + "AltitudeParticles" + File.separator + "particles");
|
||||
private static ParticleConfig instance = null;
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
static {
|
||||
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
objectMapper.disable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
|
||||
}
|
||||
|
||||
private static ParticleConfig getInstance() {
|
||||
if (instance == null)
|
||||
@@ -37,27 +43,89 @@ public class ParticleConfig {
|
||||
|
||||
/**
|
||||
* Finds all files in particles directory that are valid .json files
|
||||
* Only searches one level deep into subdirectories
|
||||
*
|
||||
* @return all files found
|
||||
*/
|
||||
private List<File> getJsonFiles() {
|
||||
List<File> files = new ArrayList<>();
|
||||
if (!particlesDir.exists()) {
|
||||
if (!particlesDir.mkdir())
|
||||
Logger.warning("Unable to create particles directory");
|
||||
|
||||
// Ensure particles directory exists
|
||||
if (!ensureParticlesDirectoryExists()) {
|
||||
return files;
|
||||
}
|
||||
if (!particlesDir.isDirectory()) {
|
||||
Logger.warning("Particles directory doesn't exist (it's a file??)");
|
||||
return files;
|
||||
|
||||
try {
|
||||
Files.walkFileTree(particlesDir.toPath(), getJsonFileVistor(files));
|
||||
} catch (IOException e) {
|
||||
Logger.warning("Error while traversing directory: " + e.getMessage());
|
||||
}
|
||||
File[] validFiles = particlesDir.listFiles(file -> file.isFile() && file.canRead() && file.getName().endsWith(".json"));
|
||||
if (validFiles == null)
|
||||
return files;
|
||||
files.addAll(List.of(validFiles));
|
||||
|
||||
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++;
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull FileVisitResult visitFile(@NotNull Path file, @NotNull BasicFileAttributes attrs) {
|
||||
File physicalFile = file.toFile();
|
||||
if (isValidJsonFile(physicalFile)) {
|
||||
files.add(physicalFile);
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull FileVisitResult postVisitDirectory(@NotNull Path dir, IOException exc) {
|
||||
depth--;
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the particles directory exists and is a directory
|
||||
*
|
||||
* @return true if directory exists or was created successfully, false otherwise
|
||||
*/
|
||||
private boolean ensureParticlesDirectoryExists() {
|
||||
if (!particlesDir.exists()) {
|
||||
if (!particlesDir.mkdirs()) {
|
||||
Logger.warning("Unable to create particles directory");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!particlesDir.isDirectory()) {
|
||||
Logger.warning("Particles path exists but is not a directory: " + particlesDir.getAbsolutePath());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a file is a valid JSON file
|
||||
*
|
||||
* @param file the file to check
|
||||
* @return true if the file is a valid JSON file
|
||||
*/
|
||||
private boolean isValidJsonFile(File file) {
|
||||
return file.isFile() && file.canRead() && file.getName().endsWith(".json");
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a ParticleData object to a ParticleSet
|
||||
*
|
||||
@@ -149,20 +217,25 @@ public class ParticleConfig {
|
||||
|
||||
public static void reload() {
|
||||
ParticleStorage.clear();
|
||||
ParticleConfig instance = getInstance();
|
||||
instance = getInstance();
|
||||
|
||||
for (File file : instance.getJsonFiles()) {
|
||||
try {
|
||||
ParticleData particleData = objectMapper.readValue(file, ParticleData.class);
|
||||
loadParticleFromFile(file);
|
||||
}
|
||||
}
|
||||
|
||||
ParticleSet particleSet = instance.convertToParticleSet(particleData);
|
||||
public static void loadParticleFromFile(File file) {
|
||||
instance = getInstance();
|
||||
try {
|
||||
ParticleData particleData = objectMapper.readValue(file, ParticleData.class);
|
||||
|
||||
ParticleStorage.addParticleSet(particleSet.getAPartType(), particleSet);
|
||||
} catch (IOException e) {
|
||||
Logger.error("Error reading particle file " + file.getName(), e);
|
||||
} catch (Exception exception) {
|
||||
Logger.error("Error processing particle file " + file.getName(), exception);
|
||||
}
|
||||
ParticleSet particleSet = instance.convertToParticleSet(particleData);
|
||||
|
||||
ParticleStorage.addParticleSet(particleSet.getAPartType(), particleSet);
|
||||
} catch (IOException e) {
|
||||
Logger.error("Error reading particle file " + file.getName(), e);
|
||||
} catch (Exception exception) {
|
||||
Logger.error("Error processing particle file " + file.getName(), exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user