Replaced JSON-Simple with Jackson for particle configuration parsing. Introduced `ParticleData` and `ParticleInfo` models for structured data. Updated `ParticleConfig` structure for clarity and modularity. Added Jackson and Lombok dependencies in `build.gradle.kts`.
147 lines
5.7 KiB
Java
147 lines
5.7 KiB
Java
package com.alttd.config;
|
|
|
|
import com.alttd.models.ParticleData;
|
|
import com.alttd.models.ParticleInfo;
|
|
import com.alttd.objects.AParticle;
|
|
import com.alttd.objects.Frame;
|
|
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.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 java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.HexFormat;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
public class ParticleConfig {
|
|
|
|
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();
|
|
|
|
private static ParticleConfig getInstance() {
|
|
if (instance == null)
|
|
instance = new ParticleConfig();
|
|
return instance;
|
|
}
|
|
|
|
/**
|
|
* Finds all files in particles directory that are valid .json files
|
|
* @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");
|
|
return files;
|
|
}
|
|
if (!particlesDir.isDirectory()) {
|
|
Logger.warning("Particles directory doesn't exist (it's a file??)");
|
|
return files;
|
|
}
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Converts a ParticleData object to a ParticleSet
|
|
* @param particleData The ParticleData object to convert
|
|
* @return A ParticleSet created from the ParticleData
|
|
*/
|
|
public ParticleSet convertToParticleSet(ParticleData particleData) {
|
|
List<Frame> loadedFrames = new ArrayList<>();
|
|
double randomOffset = particleData.getRandomOffset();
|
|
|
|
// Process each frame
|
|
for (Map.Entry<String, List<ParticleInfo>> entry : particleData.getFrames().entrySet()) {
|
|
List<AParticle> aParticleList = new ArrayList<>();
|
|
|
|
// Process each particle in the frame
|
|
for (ParticleInfo particleInfo : entry.getValue()) {
|
|
Particle particleType = Particle.valueOf(particleInfo.getParticleType());
|
|
double x = particleInfo.getX();
|
|
double y = particleInfo.getY();
|
|
double z = particleInfo.getZ();
|
|
|
|
ParticleBuilder particleBuilder = new ParticleBuilder(particleType);
|
|
|
|
// Handle different particle data types
|
|
if (particleType.getDataType().equals(Particle.DustOptions.class) && particleInfo.getColor() != null) {
|
|
int rgb = HexFormat.fromHexDigits(particleInfo.getColor());
|
|
particleBuilder.data(new Particle.DustOptions(Color.fromRGB(rgb), 1));
|
|
}
|
|
// else if (particleType.getDataType().equals(MaterialData.class)) {
|
|
// //TODO implement
|
|
// }
|
|
else if (particleType.getDataType().equals(BlockData.class)) {
|
|
//TODO implement
|
|
} else if (particleType.getDataType().equals(Integer.class)) {
|
|
//TODO implement
|
|
} else if (particleType.getDataType().equals(Float.class)) {
|
|
//TODO implement
|
|
} else if (particleType.getDataType().equals(Particle.DustTransition.class)) {
|
|
//TODO implement
|
|
} else if (particleType.getDataType().equals(ItemStack.class)) {
|
|
//TODO implement
|
|
} else if (particleInfo.getExtra() != null) {
|
|
particleBuilder.extra(particleInfo.getExtra());
|
|
}
|
|
|
|
aParticleList.add(new AParticle(x, y, z, randomOffset, particleBuilder));
|
|
}
|
|
|
|
loadedFrames.add(new Frame(aParticleList));
|
|
}
|
|
|
|
// Create and return the ParticleSet
|
|
ItemStack displayItem = new ItemStack(Material.valueOf(particleData.getDisplayItem()));
|
|
return new ParticleSet(
|
|
loadedFrames,
|
|
particleData.getDisplayName(),
|
|
List.of(particleData.getLore().split("\n")),
|
|
particleData.getFrameDelay(),
|
|
particleData.getRepeat(),
|
|
particleData.getRepeatDelay(),
|
|
particleData.isStationary(),
|
|
particleData.getAPartType(),
|
|
particleData.getParticleName(),
|
|
particleData.getPermission(),
|
|
particleData.getPackagePermission(),
|
|
displayItem
|
|
);
|
|
}
|
|
|
|
public static void reload() {
|
|
ParticleStorage.clear();
|
|
ParticleConfig instance = getInstance();
|
|
|
|
for (File file : instance.getJsonFiles()) {
|
|
try {
|
|
ParticleData particleData = objectMapper.readValue(file, ParticleData.class);
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|