2022-06-03 22:45:40 +00:00
|
|
|
package com.alttd.config;
|
|
|
|
|
|
2022-08-30 03:19:05 +00:00
|
|
|
|
2022-06-03 22:45:40 +00:00
|
|
|
import com.alttd.objects.APartType;
|
|
|
|
|
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;
|
2022-08-30 03:19:05 +00:00
|
|
|
import org.bukkit.Color;
|
2022-06-03 22:45:40 +00:00
|
|
|
import org.bukkit.Material;
|
|
|
|
|
import org.bukkit.Particle;
|
2022-08-30 03:19:05 +00:00
|
|
|
import org.bukkit.block.data.BlockData;
|
2022-06-03 22:45:40 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2022-08-30 03:19:05 +00:00
|
|
|
import org.bukkit.material.MaterialData;
|
|
|
|
|
import org.json.simple.JSONArray;
|
|
|
|
|
import org.json.simple.JSONObject;
|
|
|
|
|
import org.json.simple.parser.JSONParser;
|
|
|
|
|
import org.json.simple.parser.ParseException;
|
2022-06-03 22:45:40 +00:00
|
|
|
|
|
|
|
|
import java.io.File;
|
2022-08-30 03:19:05 +00:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileReader;
|
|
|
|
|
import java.io.IOException;
|
2022-06-03 22:45:40 +00:00
|
|
|
import java.util.ArrayList;
|
2022-08-30 03:19:05 +00:00
|
|
|
import java.util.HexFormat;
|
2022-06-03 22:45:40 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
2022-08-30 03:19:05 +00:00
|
|
|
public class ParticleConfig {
|
2022-06-03 22:45:40 +00:00
|
|
|
|
2022-08-30 03:19:05 +00:00
|
|
|
private static final File particlesDir = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs"
|
|
|
|
|
+ File.separator + "AltitudeParticles" + File.separator + "particles");
|
|
|
|
|
private static ParticleConfig instance = null;
|
2022-06-03 22:45:40 +00:00
|
|
|
|
2022-08-30 03:19:05 +00:00
|
|
|
private static ParticleConfig getInstance() {
|
|
|
|
|
if (instance == null)
|
|
|
|
|
instance = new ParticleConfig();
|
|
|
|
|
return instance;
|
2022-06-03 22:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-30 03:19:05 +00:00
|
|
|
/**
|
|
|
|
|
* 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;
|
2022-06-03 22:45:40 +00:00
|
|
|
}
|
2022-08-30 03:19:05 +00:00
|
|
|
if (!particlesDir.isDirectory()) {
|
|
|
|
|
Logger.warning("Particles directory doesn't exist (it's a file??)");
|
|
|
|
|
return files;
|
2022-06-03 22:45:40 +00:00
|
|
|
}
|
2022-08-30 03:19:05 +00:00
|
|
|
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;
|
2022-06-03 22:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-30 03:19:05 +00:00
|
|
|
public ParticleSet loadParticle(JSONObject jsonObject) throws Exception {
|
|
|
|
|
String particleName = (String) jsonObject.get("particle_name");
|
|
|
|
|
String displayName = (String) jsonObject.get("display_name");
|
|
|
|
|
APartType aPartType = APartType.valueOf((String) jsonObject.get("particle_type"));
|
|
|
|
|
String lore = (String) jsonObject.get("lore");
|
|
|
|
|
ItemStack displayItem = new ItemStack(Material.valueOf((String) jsonObject.get("display_item")));
|
|
|
|
|
String permission = (String) jsonObject.get("permission");
|
|
|
|
|
String packagePermission = (String) jsonObject.get("package_permission");
|
|
|
|
|
int frameDelay = (int) (long) jsonObject.get("frame_delay");
|
|
|
|
|
int repeat = (int) (long) jsonObject.get("repeat");
|
|
|
|
|
int repeatDelay = (int) (long) jsonObject.get("repeat_delay");
|
|
|
|
|
double randomOffset = (double) jsonObject.get("random_offset");
|
2022-09-07 20:39:48 +00:00
|
|
|
boolean stationary = (boolean) jsonObject.get("stationary");
|
2022-08-30 03:19:05 +00:00
|
|
|
JSONObject frames = (JSONObject) jsonObject.get("frames");
|
|
|
|
|
List<Frame> loadedFrames = new ArrayList<>();
|
|
|
|
|
for (Object key : frames.keySet()) {
|
|
|
|
|
Object o = frames.get(key);
|
2022-06-03 22:45:40 +00:00
|
|
|
List<AParticle> aParticleList = new ArrayList<>();
|
2022-08-30 03:19:05 +00:00
|
|
|
for (Object o1 : ((JSONArray) o)) {
|
|
|
|
|
JSONObject pData = (JSONObject) o1;
|
|
|
|
|
Particle particleType = Particle.valueOf((String) pData.get("particle_type"));
|
|
|
|
|
|
|
|
|
|
double x = (double) pData.get("x");
|
|
|
|
|
double y = (double) pData.get("y");
|
|
|
|
|
double z = (double) pData.get("z");
|
|
|
|
|
ParticleBuilder particleBuilder = new ParticleBuilder(particleType);
|
|
|
|
|
if (particleType.getDataType().equals(Particle.DustOptions.class)) {
|
|
|
|
|
int rgb = HexFormat.fromHexDigits((String) pData.get("color"));
|
2022-08-31 03:12:55 +00:00
|
|
|
particleBuilder.data(new Particle.DustOptions(Color.fromRGB(rgb), 1));
|
2022-08-30 03:19:05 +00:00
|
|
|
} 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
|
2022-09-07 20:39:48 +00:00
|
|
|
} else {
|
|
|
|
|
double data = (double) pData.get("extra");
|
|
|
|
|
particleBuilder.extra(data);
|
2022-08-30 03:19:05 +00:00
|
|
|
}
|
|
|
|
|
aParticleList.add(new AParticle(x, y, z, randomOffset, particleBuilder));
|
2022-06-03 22:45:40 +00:00
|
|
|
}
|
2022-08-30 03:19:05 +00:00
|
|
|
loadedFrames.add(new Frame(aParticleList));
|
2022-06-03 22:45:40 +00:00
|
|
|
}
|
2022-09-07 20:39:48 +00:00
|
|
|
return new ParticleSet(loadedFrames, displayName, List.of(lore.split("\n")), frameDelay, repeat, repeatDelay, stationary, aPartType, particleName, permission, packagePermission, displayItem);
|
2022-06-03 22:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-30 03:19:05 +00:00
|
|
|
public static void reload() {
|
|
|
|
|
ParticleStorage.clear();
|
|
|
|
|
ParticleConfig instance = getInstance();
|
|
|
|
|
for (File file : instance.getJsonFiles()) {
|
|
|
|
|
JSONParser parser = new JSONParser();
|
|
|
|
|
try {
|
|
|
|
|
Object obj = parser.parse(new FileReader(file));
|
|
|
|
|
ParticleSet particleSet = instance.loadParticle((JSONObject) obj);
|
|
|
|
|
ParticleStorage.addParticleSet(particleSet.getAPartType(), particleSet);
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (Exception exception) {
|
|
|
|
|
exception.printStackTrace();
|
|
|
|
|
}
|
2022-06-03 22:45:40 +00:00
|
|
|
}
|
2022-08-30 03:19:05 +00:00
|
|
|
|
|
|
|
|
//TODO implement
|
2022-06-03 22:45:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-08-30 03:19:05 +00:00
|
|
|
|