From 95bb1e90fe8a1dbd4405026ba664487122070072 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sun, 22 Jun 2025 21:30:14 +0200 Subject: [PATCH] Refactor ParticleConfig with Jackson, add ParticleData models, and update dependencies 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`. --- build.gradle.kts | 16 ++- .../java/com/alttd/config/ParticleConfig.java | 104 +++++++++--------- .../java/com/alttd/models/ParticleData.java | 77 +++++++++++++ .../java/com/alttd/models/ParticleInfo.java | 39 +++++++ src/main/java/com/alttd/util/Logger.java | 6 + 5 files changed, 191 insertions(+), 51 deletions(-) create mode 100644 src/main/java/com/alttd/models/ParticleData.java create mode 100644 src/main/java/com/alttd/models/ParticleInfo.java diff --git a/build.gradle.kts b/build.gradle.kts index c6801ba..0a60891 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -36,6 +36,18 @@ tasks { } dependencies { - compileOnly("com.alttd:Galaxy-API:1.21-R0.1-SNAPSHOT") + // Galaxy + compileOnly("com.alttd:Galaxy-API:1.21-R0.1-SNAPSHOT") { + isChanging = true + } + // Lombok + compileOnly("org.projectlombok:lombok:1.18.32") + annotationProcessor("org.projectlombok:lombok:1.18.32") + // Premium vanish compileOnly("com.github.LeonMangler:PremiumVanishAPI:2.9.0-4") -} \ No newline at end of file + // Jackson/Dynamic Beans Integration + implementation("com.fasterxml.jackson.module:jackson-module-parameter-names:2.15.2") + // Jackson for JSON Parsing + implementation("com.fasterxml.jackson.core:jackson-databind:2.15.2") + implementation("com.fasterxml.jackson.core:jackson-annotations:2.15.2") +} diff --git a/src/main/java/com/alttd/config/ParticleConfig.java b/src/main/java/com/alttd/config/ParticleConfig.java index a90ae07..298d6f7 100644 --- a/src/main/java/com/alttd/config/ParticleConfig.java +++ b/src/main/java/com/alttd/config/ParticleConfig.java @@ -1,36 +1,33 @@ package com.alttd.config; - -import com.alttd.objects.APartType; +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 org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; 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) @@ -60,34 +57,31 @@ public class ParticleConfig { return files; } - 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"); - boolean stationary = (boolean) jsonObject.get("stationary"); - JSONObject frames = (JSONObject) jsonObject.get("frames"); + /** + * 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 loadedFrames = new ArrayList<>(); - for (Object key : frames.keySet()) { - Object o = frames.get(key); - List aParticleList = new ArrayList<>(); - for (Object o1 : ((JSONArray) o)) { - JSONObject pData = (JSONObject) o1; - Particle particleType = Particle.valueOf((String) pData.get("particle_type")); + double randomOffset = particleData.getRandomOffset(); + + // Process each frame + for (Map.Entry> entry : particleData.getFrames().entrySet()) { + List 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(); - 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")); + + // 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)) { @@ -103,38 +97,50 @@ public class ParticleConfig { //TODO implement } else if (particleType.getDataType().equals(ItemStack.class)) { //TODO implement - } else { - double data = (double) pData.get("extra"); - particleBuilder.extra(data); + } else if (particleInfo.getExtra() != null) { + particleBuilder.extra(particleInfo.getExtra()); } + aParticleList.add(new AParticle(x, y, z, randomOffset, particleBuilder)); } + loadedFrames.add(new Frame(aParticleList)); } - return new ParticleSet(loadedFrames, displayName, List.of(lore.split("\n")), frameDelay, repeat, repeatDelay, stationary, aPartType, particleName, permission, packagePermission, displayItem); + + // 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()) { - JSONParser parser = new JSONParser(); try { - Object obj = parser.parse(new FileReader(file)); - ParticleSet particleSet = instance.loadParticle((JSONObject) obj); + ParticleData particleData = objectMapper.readValue(file, ParticleData.class); + + ParticleSet particleSet = instance.convertToParticleSet(particleData); + ParticleStorage.addParticleSet(particleSet.getAPartType(), particleSet); - } catch (FileNotFoundException e) { - e.printStackTrace(); } catch (IOException e) { - e.printStackTrace(); - } catch (ParseException e) { - e.printStackTrace(); + Logger.error("Error reading particle file " + file.getName(), e); } catch (Exception exception) { - exception.printStackTrace(); + Logger.error("Error processing particle file " + file.getName(), exception); } } - - //TODO implement } } - diff --git a/src/main/java/com/alttd/models/ParticleData.java b/src/main/java/com/alttd/models/ParticleData.java new file mode 100644 index 0000000..4a2c336 --- /dev/null +++ b/src/main/java/com/alttd/models/ParticleData.java @@ -0,0 +1,77 @@ +package com.alttd.models; + +import com.alttd.objects.APartType; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +import java.util.Map; +import java.util.List; + +/** + * Represents the configuration data for a particle effect, including its name, type, display properties, + * animations, and permissions. This class is primarily used for managing particle data in the context + * of custom particle effects and animations. + *

+ * Fields: + * - particleName: The unique name of the particle effect, used internally. + * - displayName: The name displayed to the user. + * - particleType: The type of the particle effect, which corresponds to an {@link APartType}. + * - lore: Additional descriptive text associated with the particle effect. + * - displayItem: An item representation for display purposes in order to visually represent the effect. + * - permission: The permission string required for accessing the particle effect. + * - packagePermission: A specific permission string linked to a grouped set of effects. + * - frameDelay: The delay between animation frames, in milliseconds. + * - repeat: The number of times the particle animation should repeat. + * - repeatDelay: The delay between repeat executions, in milliseconds. + * - randomOffset: A random position offset applied to the particle effect for variances. + * - stationary: Determines if the particle effect remains static or follows movement. + * - frames: A map defining animation frames for the particle effect. The key is an identifier, and the + * value is a list of {@link ParticleInfo} objects representing the frame's particle configuration. + *

+ * Methods: + * - getAPartType(): Converts the particleType string field into an equivalent {@link APartType} enum value. + * This allows for accessing predefined properties of the particle type. + */ +@Setter +@Getter +public class ParticleData { + @JsonProperty("particle_name") + private String particleName; + + @JsonProperty("display_name") + private String displayName; + + @JsonProperty("particle_type") + private String particleType; + + private String lore; + + @JsonProperty("display_item") + private String displayItem; + + private String permission; + + @JsonProperty("package_permission") + private String packagePermission; + + @JsonProperty("frame_delay") + private int frameDelay; + + private int repeat; + + @JsonProperty("repeat_delay") + private int repeatDelay; + + @JsonProperty("random_offset") + private double randomOffset; + + private boolean stationary; + + private Map> frames; + + public APartType getAPartType() { + return APartType.valueOf(particleType); + } + +} diff --git a/src/main/java/com/alttd/models/ParticleInfo.java b/src/main/java/com/alttd/models/ParticleInfo.java new file mode 100644 index 0000000..8e04f07 --- /dev/null +++ b/src/main/java/com/alttd/models/ParticleInfo.java @@ -0,0 +1,39 @@ +package com.alttd.models; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +/** + * Represents information about a particle, including its type, position, and additional properties. + * This class is used to describe the details of individual particles, including support for + * particle-specific attributes such as color and extra data. + *

+ * Fields: + * - particleType: The type of the particle, as defined by its name or identifier. + * - x, y, z: Coordinates representing the position of the particle in the 3D space. + * - color: A string representing the color of the particle, used primarily for "DustOptions". + * - extra: An additional property used for some specific particle types, allowing for further customization. + *

+ * The class is annotated for JSON serialization and deserialization using the Jackson library, + * ensuring smooth integration with JSON-based configurations. + *

+ * This object is used in the context of particle data configurations and animations. + */ +@Setter +@Getter +public class ParticleInfo { + @JsonProperty("particle_type") + private String particleType; + + private double x; + private double y; + private double z; + + // For DustOptions + private String color; + + // For other particle types + private Double extra; + +} diff --git a/src/main/java/com/alttd/util/Logger.java b/src/main/java/com/alttd/util/Logger.java index 4087ea0..9666470 100644 --- a/src/main/java/com/alttd/util/Logger.java +++ b/src/main/java/com/alttd/util/Logger.java @@ -2,6 +2,8 @@ package com.alttd.util; import com.alttd.AltitudeParticles; +import java.util.logging.Level; + public class Logger { static private final java.util.logging.Logger logger; @@ -33,4 +35,8 @@ public class Logger { } logger.severe(severe); } + + public static void error(String error, Throwable throwable) { + logger.log(Level.SEVERE, error, throwable); + } }