Skip to content

Commit fda4fad

Browse files
authored
Visual effects rework (#3939)
1 parent 640fc7b commit fda4fad

File tree

11 files changed

+1644
-1491
lines changed

11 files changed

+1644
-1491
lines changed

src/main/java/ch/njol/skript/classes/data/SkriptClasses.java

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818
*/
1919
package ch.njol.skript.classes.data;
2020

21-
import java.io.StreamCorruptedException;
22-
import java.util.Locale;
23-
import java.util.regex.Pattern;
24-
25-
import org.bukkit.Material;
26-
import org.bukkit.enchantments.Enchantment;
27-
import org.bukkit.inventory.ItemStack;
28-
import org.eclipse.jdt.annotation.Nullable;
29-
3021
import ch.njol.skript.Skript;
3122
import ch.njol.skript.aliases.Aliases;
3223
import ch.njol.skript.aliases.ItemData;
@@ -59,11 +50,19 @@
5950
import ch.njol.skript.util.Timeperiod;
6051
import ch.njol.skript.util.Timespan;
6152
import ch.njol.skript.util.Utils;
62-
import ch.njol.skript.util.VisualEffect;
63-
import ch.njol.skript.util.VisualEffectDummy;
6453
import ch.njol.skript.util.WeatherType;
6554
import ch.njol.skript.util.slot.Slot;
55+
import ch.njol.skript.util.visual.VisualEffect;
56+
import ch.njol.skript.util.visual.VisualEffects;
6657
import ch.njol.yggdrasil.Fields;
58+
import org.bukkit.Material;
59+
import org.bukkit.enchantments.Enchantment;
60+
import org.bukkit.inventory.ItemStack;
61+
import org.eclipse.jdt.annotation.Nullable;
62+
63+
import java.io.StreamCorruptedException;
64+
import java.util.Locale;
65+
import java.util.regex.Pattern;
6766

6867
/**
6968
* @author Peter Güttinger
@@ -697,9 +696,9 @@ public String getVariableNamePattern() {
697696
@Override
698697
@Nullable
699698
public Color parse(String input, ParseContext context) {
700-
if (ColorRGB.isRGBColor(input)) {
701-
return ColorRGB.fromString(input);
702-
}
699+
Color rgbColor = ColorRGB.fromString(input);
700+
if (rgbColor != null)
701+
return rgbColor;
703702
return SkriptColor.fromName(input);
704703
}
705704

@@ -851,41 +850,38 @@ public Experience deserialize(final String s) {
851850
}
852851
}
853852
}));
854-
if (Skript.classExists("org.bukkit.Particle")) {
855-
Classes.registerClass(new ClassInfo<>(VisualEffect.class, "visualeffect")
856-
.name("Visual Effect")
857-
.description("A visible effect, e.g. particles.")
858-
.examples("show wolf hearts on the clicked wolf",
859-
"play mob spawner flames at the targeted block to the player")
860-
.usage(VisualEffect.getAllNames())
861-
.since("2.1")
862-
.user("(visual|particle) effects?")
863-
.parser(new Parser<VisualEffect>() {
864-
@Override
865-
@Nullable
866-
public VisualEffect parse(final String s, final ParseContext context) {
867-
return VisualEffect.parse(s);
868-
}
869853

870-
@Override
871-
public String toString(final VisualEffect e, final int flags) {
872-
return e.toString(flags);
873-
}
854+
Classes.registerClass(new ClassInfo<>(VisualEffect.class, "visualeffect")
855+
.name("Visual Effect")
856+
.description("A visible effect, e.g. particles.")
857+
.examples("show wolf hearts on the clicked wolf",
858+
"play mob spawner flames at the targeted block to the player")
859+
.usage(VisualEffects.getAllNames())
860+
.since("2.1")
861+
.user("(visual|particle) effects?")
862+
.parser(new Parser<VisualEffect>() {
863+
@Override
864+
@Nullable
865+
public VisualEffect parse(String s, ParseContext context) {
866+
return VisualEffects.parse(s);
867+
}
874868

875-
@Override
876-
public String toVariableNameString(final VisualEffect e) {
877-
return e.toString();
878-
}
869+
@Override
870+
public String toString(VisualEffect e, int flags) {
871+
return e.toString(flags);
872+
}
879873

880-
@Override
881-
public String getVariableNamePattern() {
882-
return ".*";
883-
}
884-
})
885-
.serializer(new YggdrasilSerializer<VisualEffect>()));
886-
} else {
887-
Classes.registerClass(new ClassInfo<>(VisualEffectDummy.class, "visualeffect"));
888-
}
874+
@Override
875+
public String toVariableNameString(VisualEffect e) {
876+
return e.toString();
877+
}
878+
879+
@Override
880+
public String getVariableNamePattern() {
881+
return ".*";
882+
}
883+
})
884+
.serializer(new YggdrasilSerializer<>()));
889885

890886
Classes.registerClass(new ClassInfo<>(GameruleValue.class, "gamerulevalue")
891887
.user("gamerule values?")

src/main/java/ch/njol/skript/effects/EffVisualEffect.java

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,30 @@
3434
import ch.njol.skript.lang.Literal;
3535
import ch.njol.skript.lang.SkriptParser.ParseResult;
3636
import ch.njol.skript.util.Direction;
37-
import ch.njol.skript.util.VisualEffect;
37+
import ch.njol.skript.util.visual.VisualEffect;
3838
import ch.njol.util.Kleenean;
3939

40-
/**
41-
* @author Peter Güttinger
42-
*/
4340
@Name("Play Effect")
4441
@Description({"Plays a <a href='classes.html#visualeffect'>visual effect</a> at a given location or on a given entity.",
4542
"Please note that some effects can only be played on entities, e.g. wolf hearts or the hurt effect, and that these are always visible to all players."})
4643
@Examples({"play wolf hearts on the clicked wolf",
4744
"show mob spawner flames at the targeted block to the player"})
4845
@Since("2.1")
4946
public class EffVisualEffect extends Effect {
47+
5048
static {
51-
Skript.registerEffect(EffVisualEffect.class, "(play|show) %visualeffects% (on|%directions%) %entities/locations% [(to %-players%|in (radius|range) of %number%)]",
52-
"(play|show) %number% %visualeffects% (on|%directions%) %locations% [(to %-players%|in (radius|range) of %number%)]");
49+
Skript.registerEffect(EffVisualEffect.class,
50+
"(play|show) %visualeffects% (on|%directions%) %entities/locations% [(to %-players%|in (radius|range) of %number%)]",
51+
"(play|show) %number% %visualeffects% (on|%directions%) %locations% [(to %-players%|in (radius|range) of %number%)]");
5352
}
54-
55-
@SuppressWarnings("null")
53+
54+
@SuppressWarnings("NotNullFieldNotInitialized")
5655
private Expression<VisualEffect> effects;
57-
@SuppressWarnings("null")
56+
@SuppressWarnings("NotNullFieldNotInitialized")
5857
private Expression<Direction> direction;
59-
@SuppressWarnings("null")
58+
@SuppressWarnings("NotNullFieldNotInitialized")
6059
private Expression<?> where;
60+
6161
@Nullable
6262
private Expression<Player> players;
6363
@Nullable
@@ -67,7 +67,7 @@ public class EffVisualEffect extends Effect {
6767

6868
@SuppressWarnings({"unchecked", "null"})
6969
@Override
70-
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
70+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
7171
int base = 0;
7272
if (matchedPattern == 1) {
7373
count = (Expression<Number>) exprs[0];
@@ -77,50 +77,57 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
7777
effects = (Expression<VisualEffect>) exprs[base];
7878
direction = (Expression<Direction>) exprs[base + 1];
7979
where = exprs[base + 2];
80-
if (exprs[base + 3] != null) {
81-
if (exprs[3].getReturnType() == Player.class)
82-
players = (Expression<Player>) exprs[base + 3];
83-
else
84-
radius = (Expression<Number>) exprs[base + 3];
85-
}
80+
players = (Expression<Player>) exprs[base + 3];
81+
radius = (Expression<Number>) exprs[base + 4];
82+
8683
if (effects instanceof Literal) {
87-
final VisualEffect[] effs = effects.getAll(null);
88-
boolean hasLocationEffect = false, hasEntityEffect = false;
89-
for (final VisualEffect e : effs) {
90-
if (e.isEntityEffect())
84+
//noinspection ConstantConditions
85+
VisualEffect[] effs = effects.getArray(null);
86+
87+
boolean hasLocationEffect = false;
88+
boolean hasEntityEffect = false;
89+
for (VisualEffect e : effs) {
90+
if (e.getType().isEntityEffect())
9191
hasEntityEffect = true;
9292
else
9393
hasLocationEffect = true;
9494
}
95+
9596
if (!hasLocationEffect && players != null)
9697
Skript.warning("Entity effects are visible to all players");
9798
if (!hasLocationEffect && !direction.isDefault())
9899
Skript.warning("Entity effects are always played on an entity");
99-
if (hasEntityEffect && !Entity.class.isAssignableFrom(where.getReturnType()))
100-
Skript.warning("Entity effects can only be played on entities");
100+
if (hasEntityEffect && !Entity.class.isAssignableFrom(where.getReturnType())) {
101+
Skript.error("Entity effects can only be played on entities");
102+
return false;
103+
}
101104
}
105+
102106
return true;
103107
}
104108

105109
@Override
106-
protected void execute(final Event e) {
107-
final VisualEffect[] effs = effects.getArray(e);
108-
final Direction[] dirs = direction.getArray(e);
109-
final Object[] os = where.getArray(e);
110-
final Player[] ps = players != null ? players.getArray(e) : null;
111-
final Number rad = radius != null ? radius.getSingle(e) : 32; // 32=default particle radius
112-
final Number cnt = count != null ? count.getSingle(e) : 0;
113-
assert rad != null;
114-
assert cnt != null;
115-
for (final Direction d : dirs) {
116-
for (final Object o : os) {
110+
protected void execute(Event e) {
111+
VisualEffect[] effects = this.effects.getArray(e);
112+
Direction[] directions = direction.getArray(e);
113+
Object[] os = where.getArray(e);
114+
Player[] ps = players != null ? players.getArray(e) : null;
115+
Number rad = radius != null ? radius.getSingle(e) : 32; // 32=default particle radius
116+
Number cnt = count != null ? count.getSingle(e) : 0;
117+
118+
// noinspection ConstantConditions
119+
if (effects == null || directions == null || os == null || rad == null || cnt == null)
120+
return;
121+
122+
for (Direction d : directions) {
123+
for (Object o : os) {
117124
if (o instanceof Entity) {
118-
for (final VisualEffect eff : effs) {
125+
for (VisualEffect eff : effects) {
119126
eff.play(ps, d.getRelative((Entity) o), (Entity) o, cnt.intValue(), rad.intValue());
120127
}
121128
} else if (o instanceof Location) {
122-
for (final VisualEffect eff : effs) {
123-
if (eff.isEntityEffect())
129+
for (VisualEffect eff : effects) {
130+
if (eff.getType().isEntityEffect())
124131
continue;
125132
eff.play(ps, d.getRelative((Location) o), null, cnt.intValue(), rad.intValue());
126133
}
@@ -132,8 +139,9 @@ protected void execute(final Event e) {
132139
}
133140

134141
@Override
135-
public String toString(final @Nullable Event e, final boolean debug) {
136-
return "play " + effects.toString(e, debug) + " " + direction.toString(e, debug) + " " + where.toString(e, debug) + (players != null ? " to " + players.toString(e, debug) : "");
142+
public String toString(@Nullable Event e, boolean debug) {
143+
return "play " + effects.toString(e, debug) + " " + direction.toString(e, debug) + " "
144+
+ where.toString(e, debug) + (players != null ? " to " + players.toString(e, debug) : "");
137145
}
138146

139147
}

src/main/java/ch/njol/skript/util/ColorRGB.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@
1818
*/
1919
package ch.njol.skript.util;
2020

21-
import java.io.NotSerializableException;
22-
import java.io.StreamCorruptedException;
23-
21+
import ch.njol.skript.variables.Variables;
22+
import ch.njol.util.Math2;
23+
import ch.njol.yggdrasil.Fields;
2424
import org.apache.commons.lang.math.NumberUtils;
2525
import org.bukkit.DyeColor;
2626
import org.eclipse.jdt.annotation.Nullable;
2727

28-
import ch.njol.skript.variables.Variables;
29-
import ch.njol.util.Math2;
30-
import ch.njol.yggdrasil.Fields;
28+
import java.io.NotSerializableException;
29+
import java.io.StreamCorruptedException;
30+
import java.util.regex.Matcher;
31+
import java.util.regex.Pattern;
3132

3233
public class ColorRGB implements Color {
33-
34+
35+
private static final Pattern RGB_PATTERN = Pattern.compile("(?>rgb|RGB) (\\d+), (\\d+), (\\d+)");
36+
3437
private org.bukkit.Color bukkit;
3538
@Nullable
3639
private DyeColor dye;
@@ -56,21 +59,21 @@ public DyeColor asDyeColor() {
5659

5760
@Override
5861
public String getName() {
59-
return "RED:" + bukkit.getRed() + ", GREEN:" + bukkit.getGreen() + ", BLUE:" + bukkit.getBlue();
62+
return "rgb " + bukkit.getRed() + ", " + bukkit.getGreen() + ", " + bukkit.getBlue();
6063
}
61-
64+
65+
@Nullable
6266
public static ColorRGB fromString(String string) {
63-
String[] split = string.split(", ");
64-
int red = NumberUtils.toInt(split[0].replace("RED:", ""));
65-
int green = NumberUtils.toInt(split[1].replace("GREEN:", ""));
66-
int blue = NumberUtils.toInt(split[2].replace("BLUE:", ""));
67-
return new ColorRGB(red, green, blue);
68-
}
69-
70-
public static boolean isRGBColor(String string) {
71-
return string.startsWith("RED:");
67+
Matcher matcher = RGB_PATTERN.matcher(string);
68+
if (!matcher.matches())
69+
return null;
70+
return new ColorRGB(
71+
NumberUtils.toInt(matcher.group(1)),
72+
NumberUtils.toInt(matcher.group(2)),
73+
NumberUtils.toInt(matcher.group(3))
74+
);
7275
}
73-
76+
7477
@Override
7578
public Fields serialize() throws NotSerializableException {
7679
return new Fields(this, Variables.yggdrasil);

0 commit comments

Comments
 (0)