Skip to content

Commit feb4182

Browse files
committed
Ordering
1 parent ec0ca80 commit feb4182

20 files changed

+245
-134
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.sovdee.skriptparticles.elements.effects;
2+
3+
import ch.njol.skript.Skript;
4+
import ch.njol.skript.doc.Description;
5+
import ch.njol.skript.doc.Examples;
6+
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.Since;
8+
import ch.njol.skript.lang.Effect;
9+
import ch.njol.skript.lang.Expression;
10+
import ch.njol.skript.lang.SkriptParser.ParseResult;
11+
import ch.njol.util.Kleenean;
12+
import com.sovdee.skriptparticles.shapes.Shape;
13+
import org.bukkit.event.Event;
14+
import org.bukkit.util.Vector;
15+
import org.checkerframework.checker.nullness.qual.Nullable;
16+
17+
import java.util.Comparator;
18+
19+
@Name("Shape Animation Ordering")
20+
@Description({
21+
"Controls the order in which the draw animation effect will draw points. Currently WIP, only supports 2 special orderings.",
22+
"lowest-to-highest, which draws from -x -y -z to x y z, and the reverse."
23+
})
24+
@Examples(
25+
"set the animation order of {_circle} to lowest-to-highest"
26+
)
27+
@Since("1.3.0")
28+
public class EffSetOrdering extends Effect {
29+
30+
static {
31+
Skript.registerEffect(EffSetOrdering.class,
32+
"set the animation order of %shapes% to (default|1:lowest-to-highest|2:highest-to-lowest)");
33+
}
34+
35+
private Expression<Shape> shapes;
36+
private int order;
37+
38+
@Override
39+
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
40+
shapes = (Expression<Shape>) expressions[0];
41+
order = parseResult.mark;
42+
return true;
43+
}
44+
45+
@Override
46+
protected void execute(Event event) {
47+
@Nullable Comparator<Vector> order = switch (this.order) {
48+
case 1 -> (o1, o2) -> {
49+
double value1 = o1.getX() + o1.getY() + o1.getZ();
50+
double value2 = o2.getX() + o2.getY() + o2.getZ();
51+
return Double.compare(value1, value2);
52+
};
53+
case 2 -> (o1, o2) -> {
54+
double value1 = o1.getX() + o1.getY() + o1.getZ();
55+
double value2 = o2.getX() + o2.getY() + o2.getZ();
56+
return -1 * Double.compare(value1, value2);
57+
};
58+
default -> null;
59+
};
60+
for (Shape shape : shapes.getArray(event)) {
61+
shape.setOrdering(order);
62+
}
63+
}
64+
65+
@Override
66+
public String toString(@Nullable Event event, boolean debug) {
67+
return "set the animation order of " + shapes.toString(event, debug) + " to " + switch (order) {
68+
case 0 -> "default";
69+
case 1 -> "lowest-to-highest";
70+
case 2 -> "highest-to-lowest";
71+
default -> "error";
72+
};
73+
}
74+
}

src/main/java/com/sovdee/skriptparticles/elements/types/ParticleTypes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public Vibration[] executeSimple(Object[][] params) {
169169
return new Vibration[0];
170170
}
171171
Location destination = (Location) params[0][0];
172-
int arrivalTime = (int) ((Timespan) params[1][0]).getTicks_i();
172+
int arrivalTime = (int) ((Timespan) params[1][0]).getTicks();
173173
Vibration vibration = new Vibration(new Vibration.Destination.BlockDestination(destination), arrivalTime);
174174
return new Vibration[]{vibration};
175175
}

src/main/java/com/sovdee/skriptparticles/shapes/AbstractShape.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
import org.joml.Quaternionf;
1616

1717
import java.util.Collection;
18+
import java.util.Comparator;
1819
import java.util.Iterator;
1920
import java.util.LinkedHashSet;
2021
import java.util.List;
2122
import java.util.Set;
23+
import java.util.TreeSet;
2224
import java.util.UUID;
2325
import java.util.function.Consumer;
2426

@@ -34,6 +36,7 @@ public abstract class AbstractShape implements Shape {
3436
private Vector offset;
3537
private @Nullable DynamicLocation location;
3638
private Particle particle;
39+
private @Nullable Comparator<Vector> ordering;
3740
private double particleDensity = 0.25; // todo: make this configurable
3841
private long animationDuration = 0;
3942

@@ -69,7 +72,11 @@ public Set<Vector> getPoints() {
6972
public Set<Vector> getPoints(Quaternion orientation) {
7073
State state = getState(orientation);
7174
if (needsUpdate || !lastState.equals(state) || points.isEmpty()) {
72-
points = generatePoints();
75+
if (ordering != null)
76+
points = new TreeSet<>(ordering);
77+
else
78+
points = new LinkedHashSet<>();
79+
generatePoints(points);
7380
for (Vector point : points) {
7481
orientation.transform(point);
7582
point.multiply(scale);
@@ -88,14 +95,14 @@ public void setPoints(Set<Vector> points) {
8895

8996
@Override
9097
@Contract(pure = true)
91-
public Set<Vector> generateSurface() {
92-
return generateOutline();
98+
public void generateSurface(Set<Vector> points) {
99+
generateOutline(points);
93100
}
94101

95102
@Override
96103
@Contract(pure = true)
97-
public Set<Vector> generateFilled() {
98-
return generateSurface();
104+
public void generateFilled(Set<Vector> points) {
105+
generateSurface(points);
99106
}
100107

101108
@Override
@@ -295,6 +302,17 @@ public void setParticle(Particle particle) {
295302
this.particle = particle;
296303
}
297304

305+
@Override
306+
public @Nullable Comparator<Vector> getOrdering() {
307+
return ordering;
308+
}
309+
310+
@Override
311+
public void setOrdering(Comparator<Vector> comparator) {
312+
ordering = comparator;
313+
this.setNeedsUpdate(true);
314+
}
315+
298316
@Override
299317
public double getParticleDensity() {
300318
return particleDensity;

src/main/java/com/sovdee/skriptparticles/shapes/Arc.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public Arc(double radius, double height, double cutoffAngle) {
3434

3535
@Override
3636
@Contract(pure = true)
37-
public Set<Vector> generateSurface() {
38-
return generateFilled();
37+
public void generateSurface(Set<Vector> points) {
38+
generateFilled(points);
3939
}
4040

4141
@Override

src/main/java/com/sovdee/skriptparticles/shapes/BezierCurve.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ private List<Vector> evaluateControlPoints() {
6464
return controlPoints;
6565
}
6666

67+
@SuppressWarnings("ConstantConditions")
6768
@Override
68-
public Set<Vector> generateOutline() {
69-
Set<Vector> points = new LinkedHashSet<>();
69+
public void generateOutline(Set<Vector> points) {
7070
List<Vector> controlPoints = evaluateControlPoints();
7171

7272
int steps = (int) (estimateLength(controlPoints) / getParticleDensity());
@@ -82,7 +82,6 @@ public Set<Vector> generateOutline() {
8282
}
8383
points.add(tempCP.get(0));
8484
}
85-
return points;
8685
}
8786

8887
private double estimateLength() {

src/main/java/com/sovdee/skriptparticles/shapes/Circle.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,35 @@ public Circle(double radius, double height) {
4141

4242
@Override
4343
@Contract(pure = true)
44-
public Set<Vector> generateOutline() {
44+
@SuppressWarnings("ConstantConditions")
45+
public void generateOutline(Set<Vector> points) {
4546
Set<Vector> circle = MathUtil.calculateCircle(radius, this.getParticleDensity(), cutoffAngle);
4647
if (height != 0)
47-
return MathUtil.fillVertically(circle, height, this.getParticleDensity());
48-
return circle;
48+
points.addAll(MathUtil.fillVertically(circle, height, this.getParticleDensity()));
49+
else
50+
points.addAll(circle);
4951
}
5052

5153

5254
@Override
5355
@Contract(pure = true)
54-
public Set<Vector> generateSurface() {
56+
@SuppressWarnings("ConstantConditions")
57+
public void generateSurface(Set<Vector> points) {
5558
if (height != 0)
56-
return MathUtil.calculateCylinder(radius, height, this.getParticleDensity(), cutoffAngle);
57-
return MathUtil.calculateDisc(radius, this.getParticleDensity(), cutoffAngle);
59+
points.addAll(MathUtil.calculateCylinder(radius, height, this.getParticleDensity(), cutoffAngle));
60+
else
61+
points.addAll(MathUtil.calculateDisc(radius, this.getParticleDensity(), cutoffAngle));
5862
}
5963

6064
@Override
6165
@Contract(pure = true)
62-
public Set<Vector> generateFilled() {
66+
@SuppressWarnings("ConstantConditions")
67+
public void generateFilled(Set<Vector> points) {
6368
Set<Vector> disc = MathUtil.calculateDisc(radius, this.getParticleDensity(), cutoffAngle);
6469
if (height != 0)
65-
return MathUtil.fillVertically(disc, height, this.getParticleDensity());
66-
return disc;
70+
points.addAll(MathUtil.fillVertically(disc, height, this.getParticleDensity()));
71+
else
72+
points.addAll(disc);
6773
}
6874

6975
@Override

src/main/java/com/sovdee/skriptparticles/shapes/Cuboid.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import org.jetbrains.annotations.Contract;
1111
import org.jetbrains.annotations.NotNull;
1212

13-
import java.util.HashSet;
14-
import java.util.LinkedHashSet;
1513
import java.util.Set;
1614

1715
/*
@@ -104,10 +102,10 @@ private void calculateSteps() {
104102
heightStep = 2 * halfHeight / Math.round(2 * halfHeight / this.getParticleDensity());
105103
}
106104

105+
@SuppressWarnings("ConstantConditions")
107106
@Override
108107
@Contract(pure = true)
109-
public Set<Vector> generateOutline() {
110-
HashSet<Vector> points = new LinkedHashSet<>();
108+
public void generateOutline(Set<Vector> points) {
111109
for (double x = -halfLength; x <= halfLength; x += lengthStep) {
112110
points.add(new Vector(x, -halfHeight, -halfWidth));
113111
points.add(new Vector(x, -halfHeight, halfWidth));
@@ -126,13 +124,12 @@ public Set<Vector> generateOutline() {
126124
points.add(new Vector(halfLength, -halfHeight, z));
127125
points.add(new Vector(halfLength, halfHeight, z));
128126
}
129-
return points;
130127
}
131128

129+
@SuppressWarnings("ConstantConditions")
132130
@Override
133131
@Contract(pure = true)
134-
public Set<Vector> generateSurface() {
135-
HashSet<Vector> points = new LinkedHashSet<>();
132+
public void generateSurface(Set<Vector> points) {
136133
for (double x = -halfLength; x <= halfLength; x += lengthStep) {
137134
for (double z = -halfWidth; z <= halfWidth; z += widthStep) {
138135
points.add(new Vector(x, -halfHeight, z));
@@ -151,26 +148,24 @@ public Set<Vector> generateSurface() {
151148
points.add(new Vector(x, y, halfWidth));
152149
}
153150
}
154-
return points;
155151
}
156152

153+
@SuppressWarnings("ConstantConditions")
157154
@Override
158155
@Contract(pure = true)
159-
public Set<Vector> generateFilled() {
160-
HashSet<Vector> points = new LinkedHashSet<>();
156+
public void generateFilled(Set<Vector> points) {
161157
for (double x = -halfLength; x <= halfLength; x += lengthStep) {
162158
for (double y = -halfHeight; y <= halfHeight; y += heightStep) {
163159
for (double z = -halfWidth; z <= halfWidth; z += widthStep) {
164160
points.add(new Vector(x, y, z));
165161
}
166162
}
167163
}
168-
return points;
169164
}
170165

171166
@Override
172167
@Contract(pure = true)
173-
public Set<Vector> generatePoints() {
168+
public void generatePoints(Set<Vector> points) {
174169
if (isDynamic) {
175170
assert negativeCorner != null;
176171
assert positiveCorner != null;
@@ -182,9 +177,8 @@ public Set<Vector> generatePoints() {
182177
this.setLocation(new DynamicLocation(negative.clone().add(positive.subtract(negative).toVector().multiply(0.5))));
183178
}
184179
calculateSteps();
185-
Set<Vector> points = super.generatePoints();
180+
super.generatePoints(points);
186181
points.forEach(vector -> vector.add(centerOffset));
187-
return points;
188182
}
189183

190184
// Ensure that the points are always needing to be updated if the start or end location is dynamic

src/main/java/com/sovdee/skriptparticles/shapes/Ellipse.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,37 @@ public Ellipse(double xRadius, double zRadius, double height) {
4343
this.cutoffAngle = 2 * Math.PI;
4444
}
4545

46+
@SuppressWarnings("ConstantConditions")
4647
@Override
4748
@Contract(pure = true)
48-
public Set<Vector> generateOutline() {
49+
public void generateOutline(Set<Vector> points) {
4950
Set<Vector> ellipse = new LinkedHashSet<>(MathUtil.calculateEllipse(xRadius, zRadius, this.getParticleDensity(), cutoffAngle));
5051
if (height != 0)
51-
return MathUtil.fillVertically(ellipse, height, this.getParticleDensity());
52-
return ellipse;
52+
points.addAll(MathUtil.fillVertically(ellipse, height, this.getParticleDensity()));
53+
else
54+
points.addAll(ellipse);
5355
}
5456

57+
@SuppressWarnings("ConstantConditions")
5558
@Override
5659
@Contract(pure = true)
57-
public Set<Vector> generateSurface() {
60+
public void generateSurface(Set<Vector> points) {
5861
// if height is not 0, make it a cylinder
59-
if (height != 0) {
60-
return MathUtil.calculateCylinder(xRadius, zRadius, height, this.getParticleDensity(), cutoffAngle);
61-
}
62-
return MathUtil.calculateEllipticalDisc(xRadius, zRadius, this.getParticleDensity(), cutoffAngle);
62+
if (height != 0)
63+
points.addAll(MathUtil.calculateCylinder(xRadius, zRadius, height, this.getParticleDensity(), cutoffAngle));
64+
else
65+
points.addAll(MathUtil.calculateEllipticalDisc(xRadius, zRadius, this.getParticleDensity(), cutoffAngle));
6366
}
6467

68+
@SuppressWarnings("ConstantConditions")
6569
@Override
6670
@Contract(pure = true)
67-
public Set<Vector> generateFilled() {
71+
public void generateFilled(Set<Vector> points) {
6872
Set<Vector> disc = MathUtil.calculateEllipticalDisc(xRadius, zRadius, this.getParticleDensity(), cutoffAngle);
6973
if (height != 0)
70-
return MathUtil.fillVertically(disc, height, this.getParticleDensity());
71-
return disc;
74+
points.addAll(MathUtil.fillVertically(disc, height, this.getParticleDensity()));
75+
else
76+
points.addAll(disc);
7277
}
7378

7479
@Override

0 commit comments

Comments
 (0)