Skip to content

Commit 5efc56b

Browse files
authored
Merge pull request #210 from HSGamer/particle-serialize
Serialization for ParticleDisplay
2 parents 3ffb294 + 0890e7a commit 5efc56b

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

src/main/java/com/cryptomorin/xseries/particles/ParticleDisplay.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,76 @@ public static ParticleDisplay edit(@Nonnull ParticleDisplay display, @Nonnull Co
381381
return display;
382382
}
383383

384+
/**
385+
* Serialize a ParticleDisplay into a ConfigurationSection
386+
*
387+
* @param display The ParticleDisplay to serialize
388+
* @param section The ConfigurationSection to serialize into
389+
*/
390+
@SuppressWarnings("deprecation")
391+
public static void serialize(ParticleDisplay display, ConfigurationSection section) {
392+
section.set("particle", display.particle.name());
393+
394+
if (display.count != 1) {
395+
section.set("count", display.count);
396+
}
397+
398+
if (display.extra != 0) {
399+
section.set("extra", display.extra);
400+
}
401+
402+
if (display.force) {
403+
section.set("force", true);
404+
}
405+
406+
if (display.offset != null) {
407+
section.set("offset", display.offset.getX() + ", " + display.offset.getY() + ", " + display.offset.getZ());
408+
}
409+
410+
if (display.rotation != null) {
411+
section.set("rotation", Math.toDegrees(display.rotation.getX()) + ", " + Math.toDegrees(display.rotation.getY()) + ", " + Math.toDegrees(display.rotation.getZ()));
412+
}
413+
414+
if (display.rotationOrder != DEFAULT_ROTATION_ORDER) {
415+
Axis[] order = display.rotationOrder;
416+
section.set("rotation-order", order[0].name() + order[1].name() + order[2].name());
417+
}
418+
419+
if (display.data instanceof float[]) {
420+
float size = 1f;
421+
float[] datas = (float[]) display.data;
422+
StringJoiner colorJoiner = new StringJoiner(", ");
423+
if (datas.length >= 3) {
424+
if (datas.length > 3) {
425+
size = datas[3];
426+
}
427+
Color color1 = new Color(datas[0], datas[1], datas[2]);
428+
colorJoiner.add(Integer.toString(color1.getRed()));
429+
colorJoiner.add(Integer.toString(color1.getGreen()));
430+
colorJoiner.add(Integer.toString(color1.getBlue()));
431+
}
432+
if (datas.length >= 7) {
433+
Color color2 = new Color(datas[4], datas[5], datas[6]);
434+
colorJoiner.add(Integer.toString(color2.getRed()));
435+
colorJoiner.add(Integer.toString(color2.getGreen()));
436+
colorJoiner.add(Integer.toString(color2.getBlue()));
437+
}
438+
section.set("color", colorJoiner.toString());
439+
section.set("size", size);
440+
}
441+
442+
if (ISFLAT) {
443+
if (display.data instanceof BlockData) {
444+
section.set("blockdata", ((BlockData) display.data).getMaterial().name());
445+
}
446+
}
447+
if (display.data instanceof ItemStack) {
448+
section.set("itemstack", ((ItemStack) display.data).getType().name());
449+
} else if (display.data instanceof MaterialData) {
450+
section.set("materialdata", ((MaterialData) display.data).getItemType().name());
451+
}
452+
}
453+
384454
/**
385455
* Rotates the given location vector around a certain axis.
386456
*
@@ -474,6 +544,34 @@ public void withParticle(@Nonnull Particle particle) {
474544
this.particle = Objects.requireNonNull(particle, "Particle cannot be null");
475545
}
476546

547+
/**
548+
* Get the particle.
549+
*
550+
* @return the particle.
551+
*/
552+
@Nonnull
553+
public Particle getParticle() {
554+
return particle;
555+
}
556+
557+
/**
558+
* Get the count of the particle.
559+
*
560+
* @return the count of the particle.
561+
*/
562+
public int getCount() {
563+
return count;
564+
}
565+
566+
/**
567+
* Get the extra data of the particle.
568+
*
569+
* @return the extra data of the particle.
570+
*/
571+
public double getExtra() {
572+
return extra;
573+
}
574+
477575
/**
478576
* Get the data object. Currently, it can be instance of float[] with [R, G, B, size],
479577
* or instance of {@link BlockData}, {@link MaterialData} for legacy usage or {@link ItemStack}

0 commit comments

Comments
 (0)