Skip to content

Commit 8e7f52a

Browse files
committed
Stars, Hearts, and Bug Fixes
1 parent b744426 commit 8e7f52a

File tree

9 files changed

+424
-35
lines changed

9 files changed

+424
-35
lines changed

src/main/java/com/sovdee/skriptparticles/elements/expressions/constructors/ExprHeart.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,39 @@
1212
import ch.njol.skript.lang.util.SimpleExpression;
1313
import ch.njol.util.Kleenean;
1414
import com.sovdee.skriptparticles.shapes.Heart;
15+
import com.sovdee.skriptparticles.shapes.Shape;
1516
import com.sovdee.skriptparticles.util.MathUtil;
1617
import org.bukkit.event.Event;
1718
import org.jetbrains.annotations.Nullable;
1819

1920
@Name("Particle Heart")
2021
@Description({
21-
"Creates a heart shape with the given width and height, and optionally eccentricity. The width and height must be greater than 0.",
22+
"Creates a heart shape with the given width and height, and optionally eccentricity. The width (x) and length (z) must be greater than 0.",
2223
"The eccentricity defaults to 3, but must be at least 1. This determines how round/pointy the heart is. Values between 1 and 5 are recommended.",
23-
"Note that the width and heights are not exact, but they're roughly the width and height of the heart.",
24+
"Note that the width and length are not exact, but they're roughly the width and length of the heart.",
2425
"Finally, this shape does not support the particle count expression and its particle density is not uniform. If anyone knows a good way to compute the complete elliptic integral of the second kind, please let me know."
2526
})
2627
@Examples({
27-
"set {_heart} to heart with width 5 and height 4",
28-
"set {_heart} to heart shape with width 5, height 7, and eccentricity 2",
29-
"draw a heart of width 2 and height 2 at player"
28+
"set {_heart} to heart with width 5 and length 4",
29+
"set {_heart} to heart shape with width 5, length 7, and eccentricity 2",
30+
"draw a heart of width 2 and length 2 at player"
3031
})
3132
@Since("1.0.1")
3233
public class ExprHeart extends SimpleExpression<Heart> {
3334

3435
static {
35-
Skript.registerExpression(ExprHeart.class, Heart.class, ExpressionType.COMBINED, "[a] [:solid] heart [shape] (with|of) width %number%[,] [and] height %number%[[,] [and] eccentricity %-number%]");
36+
Skript.registerExpression(ExprHeart.class, Heart.class, ExpressionType.COMBINED, "[a] [:solid] heart [shape] (with|of) width %number%[,] [and] length %number%[[,] [and] eccentricity %-number%]");
3637
}
3738

3839
private Expression<Number> width;
39-
private Expression<Number> height;
40+
private Expression<Number> length;
4041
private Expression<Number> eccentricity;
42+
private boolean isSolid;
4143

4244
@Override
4345
public boolean init(Expression<?>[] expressions, int i, Kleenean kleenean, ParseResult parseResult) {
4446
width = (Expression<Number>) expressions[0];
45-
height = (Expression<Number>) expressions[1];
47+
length = (Expression<Number>) expressions[1];
4648
if (expressions.length > 2) {
4749
eccentricity = (Expression<Number>) expressions[2];
4850
}
@@ -52,8 +54,8 @@ public boolean init(Expression<?>[] expressions, int i, Kleenean kleenean, Parse
5254
return false;
5355
}
5456

55-
if (height instanceof Literal<Number> literal && literal.getSingle().doubleValue() <= 0) {
56-
Skript.error("The height of a heart must be greater than 0.");
57+
if (length instanceof Literal<Number> literal && literal.getSingle().doubleValue() <= 0) {
58+
Skript.error("The length of a heart must be greater than 0.");
5759
return false;
5860
}
5961

@@ -62,23 +64,29 @@ public boolean init(Expression<?>[] expressions, int i, Kleenean kleenean, Parse
6264
return false;
6365
}
6466

67+
isSolid = parseResult.hasTag("solid");
68+
6569
return true;
6670
}
6771

6872
@Override
6973
@Nullable
7074
protected Heart[] get(Event event) {
7175
Number width = this.width.getSingle(event);
72-
Number height = this.height.getSingle(event);
76+
Number length = this.length.getSingle(event);
7377
Number eccentricity = this.eccentricity == null ? 3 : this.eccentricity.getSingle(event);
74-
if (width == null || height == null || eccentricity == null) {
78+
if (width == null || length == null || eccentricity == null) {
7579
return null;
7680
}
7781
width = Math.max(width.doubleValue(), MathUtil.EPSILON);
78-
height = Math.max(height.doubleValue(), MathUtil.EPSILON);
82+
length = Math.max(length.doubleValue(), MathUtil.EPSILON);
7983
eccentricity = Math.max(eccentricity.doubleValue(), 1);
8084

81-
return new Heart[] {new Heart(width.doubleValue(), height.doubleValue(), eccentricity.doubleValue())};
85+
Heart heart = new Heart(width.doubleValue(), length.doubleValue(), eccentricity.doubleValue());
86+
if (isSolid) {
87+
heart.setStyle(Shape.Style.SURFACE);
88+
}
89+
return new Heart[]{heart};
8290
}
8391

8492
@Override
@@ -93,6 +101,6 @@ public Class<? extends Heart> getReturnType() {
93101

94102
@Override
95103
public String toString(@Nullable Event event, boolean b) {
96-
return "a heart shape with width " + width.toString(event, b) + ", height " + height.toString(event, b) + ", and eccentricity " + eccentricity.toString(event, b) + ".";
104+
return "a heart shape with width " + width.toString(event, b) + ", height " + length.toString(event, b) + ", and eccentricity " + eccentricity.toString(event, b) + ".";
97105
}
98106
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.sovdee.skriptparticles.elements.expressions.constructors;
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.lang.Expression;
8+
import ch.njol.skript.lang.ExpressionType;
9+
import ch.njol.skript.lang.Literal;
10+
import ch.njol.skript.lang.SkriptParser.ParseResult;
11+
import ch.njol.skript.lang.util.SimpleExpression;
12+
import ch.njol.util.Kleenean;
13+
import com.sovdee.skriptparticles.shapes.Shape;
14+
import com.sovdee.skriptparticles.shapes.Star;
15+
import com.sovdee.skriptparticles.util.MathUtil;
16+
import org.bukkit.event.Event;
17+
import org.jetbrains.annotations.Nullable;
18+
19+
@Name("Particle Star")
20+
@Description({
21+
"Creates a star shape with the given number of points, inner radius, and outer radius. The number of points must be at least 2, and the inner and outer radii must be greater than 0.",
22+
"Note that \"points\" in this context is referring to the tips of the star, not the number of particles."
23+
})
24+
@Examples({
25+
"set {_shape} to star with 5 points, inner radius 1, and outer radius 2",
26+
"draw a star with 4 points, inner radius 2, and outer radius 4 at player"
27+
})
28+
public class ExprStar extends SimpleExpression<Star> {
29+
30+
static {
31+
Skript.registerExpression(ExprStar.class, Star.class, ExpressionType.COMBINED, "[a] [:solid] star with %number% points(,| and) inner radius %number%[,] and outer radius %number%");
32+
}
33+
34+
private Expression<Number> points;
35+
private Expression<Number> innerRadius;
36+
private Expression<Number> outerRadius;
37+
private boolean isSolid;
38+
39+
@Override
40+
public boolean init(Expression<?>[] expressions, int i, Kleenean kleenean, ParseResult parseResult) {
41+
points = (Expression<Number>) expressions[0];
42+
innerRadius = (Expression<Number>) expressions[1];
43+
outerRadius = (Expression<Number>) expressions[2];
44+
isSolid = parseResult.hasTag("solid");
45+
46+
if (points instanceof Literal<Number> literal && literal.getSingle().doubleValue() < 2) {
47+
Skript.error("A star must have at least 2 points. (points: " +
48+
literal.getSingle().doubleValue() + ")");
49+
return false;
50+
}
51+
52+
if (innerRadius instanceof Literal<Number> literal && literal.getSingle().doubleValue() <= 0) {
53+
Skript.error("The inner radius of a star must be greater than 0.");
54+
return false;
55+
}
56+
57+
if (outerRadius instanceof Literal<Number> literal && literal.getSingle().doubleValue() <= 0) {
58+
Skript.error("The outer radius of a star must be greater than 0.");
59+
return false;
60+
}
61+
62+
return true;
63+
}
64+
65+
@Override
66+
@Nullable
67+
protected Star[] get(Event event) {
68+
Number points = this.points.getSingle(event);
69+
Number innerRadius = this.innerRadius.getSingle(event);
70+
Number outerRadius = this.outerRadius.getSingle(event);
71+
if (points == null || innerRadius == null || outerRadius == null)
72+
return null;
73+
74+
double angle = Math.PI * 2 / Math.max(points.intValue(), 2);
75+
innerRadius = Math.max(innerRadius.doubleValue(), MathUtil.EPSILON);
76+
outerRadius = Math.max(outerRadius.doubleValue(), MathUtil.EPSILON);
77+
78+
Star star = new Star(innerRadius.doubleValue(), outerRadius.doubleValue(), angle);
79+
if (isSolid)
80+
star.setStyle(Shape.Style.SURFACE);
81+
82+
return new Star[]{star};
83+
}
84+
85+
@Override
86+
public boolean isSingle() {
87+
return true;
88+
}
89+
90+
@Override
91+
public Class<? extends Star> getReturnType() {
92+
return Star.class;
93+
}
94+
95+
@Override
96+
public String toString(@Nullable Event event, boolean b) {
97+
return "a " + (isSolid ? "solid " : "") + "star shape with " + points.toString(event, b) + " points, inner radius " + innerRadius.toString(event, b) + ", and outer radius " + outerRadius.toString(event, b);
98+
}
99+
}

src/main/java/com/sovdee/skriptparticles/elements/expressions/properties/ExprHelixWindingRate.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ch.njol.skript.doc.Description;
55
import ch.njol.skript.doc.Examples;
66
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.Since;
78
import ch.njol.skript.expressions.base.SimplePropertyExpression;
89
import com.sovdee.skriptparticles.shapes.Helix;
910
import com.sovdee.skriptparticles.shapes.Shape;
@@ -21,16 +22,19 @@
2122
"set winding rate of {_helix} to 1/10",
2223
"set winding rate of {_helix} to 10.4"
2324
})
24-
public class ExprHelixWindingRate extends SimplePropertyExpression<Helix, Number> {
25+
@Since("1.0.0")
26+
public class ExprHelixWindingRate extends SimplePropertyExpression<Shape, Number> {
2527

2628
static {
2729
register(ExprHelixWindingRate.class, Number.class, "winding rate", "shapes");
2830
}
2931

3032
@Override
3133
@Nullable
32-
public Number convert(Helix helix) {
33-
return 1 / (2 * Math.PI * helix.getSlope());
34+
public Number convert(Shape shape) {
35+
if (shape instanceof Helix helix)
36+
return 1 / (2 * Math.PI * helix.getSlope());
37+
return null;
3438
}
3539

3640
@Override
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.sovdee.skriptparticles.elements.expressions.properties;
2+
3+
import ch.njol.skript.classes.Changer;
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.expressions.base.SimplePropertyExpression;
9+
import com.sovdee.skriptparticles.shapes.Shape;
10+
import com.sovdee.skriptparticles.shapes.Star;
11+
import org.bukkit.event.Event;
12+
import org.jetbrains.annotations.Nullable;
13+
14+
@Name("Star Points")
15+
@Description({
16+
"Returns the number of points on a star. This is the number of points on the star, not the number of particles drawn.",
17+
"Changing this will change the number of points on the star. The number of points must be at least 2."
18+
})
19+
@Examples({
20+
"set {_shape}'s star points to 5",
21+
"set star points of {_shape} to 10",
22+
"set star points of {_shape} to 2 * (star points of {_shape})"
23+
})
24+
@Since("1.0.1")
25+
public class ExprStarPoints extends SimplePropertyExpression<Shape, Number> {
26+
27+
static {
28+
register(ExprStarPoints.class, Number.class, "star points", "shapes");
29+
}
30+
31+
@Override
32+
public @Nullable Number convert(Shape shape) {
33+
if (shape instanceof Star)
34+
return ((Star) shape).getStarPoints();
35+
return null;
36+
}
37+
38+
@Override
39+
public @Nullable Class<?>[] acceptChange(Changer.ChangeMode mode) {
40+
return switch (mode) {
41+
case SET, ADD, REMOVE -> new Class[]{Number.class};
42+
default -> null;
43+
};
44+
}
45+
46+
@Override
47+
public void change(Event event, @Nullable Object[] delta, Changer.ChangeMode mode) {
48+
if (delta == null || delta.length == 0)
49+
return;
50+
Shape[] shapes = getExpr().getArray(event);
51+
52+
int deltaValue = ((Number) delta[0]).intValue();
53+
switch (mode) {
54+
case REMOVE:
55+
deltaValue = -deltaValue;
56+
case ADD:
57+
for (Shape shape : shapes) {
58+
if (shape instanceof Star star) {
59+
star.setStarPoints(Math.max(star.getStarPoints() + deltaValue, 2));
60+
}
61+
}
62+
break;
63+
case SET:
64+
deltaValue = Math.max(deltaValue, 2);
65+
for (Shape shape : shapes) {
66+
if (shape instanceof Star star) {
67+
star.setStarPoints(deltaValue);
68+
}
69+
}
70+
break;
71+
}
72+
}
73+
74+
@Override
75+
public Class<? extends Number> getReturnType() {
76+
return Number.class;
77+
}
78+
79+
@Override
80+
protected String getPropertyName() {
81+
return "star points";
82+
}
83+
84+
}

0 commit comments

Comments
 (0)