|
1 | 1 | package ch.njol.skript.effects; |
2 | 2 |
|
3 | | -import org.bukkit.entity.Entity; |
4 | | -import org.bukkit.event.Event; |
5 | | -import org.bukkit.util.Vector; |
6 | | -import org.jetbrains.annotations.Nullable; |
7 | | - |
8 | 3 | import ch.njol.skript.Skript; |
9 | 4 | import ch.njol.skript.doc.Description; |
10 | | -import ch.njol.skript.doc.Examples; |
| 5 | +import ch.njol.skript.doc.Example; |
11 | 6 | import ch.njol.skript.doc.Name; |
12 | 7 | import ch.njol.skript.doc.Since; |
13 | 8 | import ch.njol.skript.lang.Effect; |
14 | 9 | import ch.njol.skript.lang.Expression; |
15 | 10 | import ch.njol.skript.lang.SkriptParser.ParseResult; |
16 | 11 | import ch.njol.skript.util.Direction; |
17 | 12 | import ch.njol.util.Kleenean; |
| 13 | +import org.bukkit.entity.Entity; |
| 14 | +import org.bukkit.event.Event; |
| 15 | +import org.bukkit.util.Vector; |
| 16 | +import org.jetbrains.annotations.Nullable; |
18 | 17 |
|
19 | | -/** |
20 | | - * @author Peter Güttinger |
21 | | - */ |
22 | 18 | @Name("Push") |
23 | 19 | @Description("Push entities around.") |
24 | | -@Examples({"push the player upwards", |
25 | | - "push the victim downwards at speed 0.5"}) |
| 20 | +@Example("push the player upwards") |
| 21 | +@Example("push the victim downwards at speed 0.5") |
| 22 | +@Example("push player along vector from player to player's target at speed 2") |
26 | 23 | @Since("1.4.6") |
27 | 24 | public class EffPush extends Effect { |
| 25 | + |
28 | 26 | static { |
29 | | - Skript.registerEffect(EffPush.class, "(push|thrust) %entities% %direction% [(at|with) (speed|velocity|force) %-number%]"); |
| 27 | + Skript.registerEffect(EffPush.class, "(push|thrust) %entities% [along] %direction% [(at|with) (speed|velocity|force) %-number%]"); |
30 | 28 | } |
31 | | - |
32 | | - @SuppressWarnings("null") |
| 29 | + |
33 | 30 | private Expression<Entity> entities; |
34 | | - @SuppressWarnings("null") |
35 | 31 | private Expression<Direction> direction; |
36 | | - @Nullable |
37 | | - private Expression<Number> speed = null; |
| 32 | + private @Nullable Expression<Number> speed = null; |
38 | 33 |
|
39 | 34 | @SuppressWarnings({"unchecked", "null"}) |
40 | 35 | @Override |
41 | | - public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { |
| 36 | + public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { |
42 | 37 | entities = (Expression<Entity>) exprs[0]; |
43 | 38 | direction = (Expression<Direction>) exprs[1]; |
44 | 39 | speed = (Expression<Number>) exprs[2]; |
45 | 40 | return true; |
46 | 41 | } |
47 | 42 |
|
48 | 43 | @Override |
49 | | - protected void execute(final Event e) { |
50 | | - final Direction d = direction.getSingle(e); |
51 | | - if (d == null) |
| 44 | + protected void execute(Event event) { |
| 45 | + Direction direction = this.direction.getSingle(event); |
| 46 | + if (direction == null) |
52 | 47 | return; |
53 | | - final Number v = speed != null ? speed.getSingle(e) : null; |
54 | | - if (speed != null && v == null) |
| 48 | + Number speed = this.speed != null ? this.speed.getSingle(event) : null; |
| 49 | + if (this.speed != null && speed == null) |
55 | 50 | return; |
56 | | - final Entity[] ents = entities.getArray(e); |
57 | | - for (final Entity en : ents) { |
58 | | - assert en != null; |
59 | | - final Vector mod = d.getDirection(en); |
60 | | - if (v != null) |
61 | | - mod.normalize().multiply(v.doubleValue()); |
62 | | - if (!(Double.isFinite(mod.getX()) && Double.isFinite(mod.getY()) && Double.isFinite(mod.getZ()))) { |
| 51 | + Entity[] entities = this.entities.getArray(event); |
| 52 | + for (Entity entity : entities) { |
| 53 | + Vector pushDirection = direction.getDirection(entity); |
| 54 | + if (speed != null) |
| 55 | + pushDirection.normalize().multiply(speed.doubleValue()); |
| 56 | + if (!(Double.isFinite(pushDirection.getX()) && Double.isFinite(pushDirection.getY()) && Double.isFinite(pushDirection.getZ()))) { |
63 | 57 | // Some component of the mod vector is not finite, so just stop |
64 | 58 | return; |
65 | 59 | } |
66 | | - en.setVelocity(en.getVelocity().add(mod)); // REMIND add NoCheatPlus exception to players |
| 60 | + entity.setVelocity(entity.getVelocity().add(pushDirection)); |
67 | 61 | } |
68 | 62 | } |
69 | 63 |
|
70 | 64 | @Override |
71 | | - public String toString(final @Nullable Event e, final boolean debug) { |
72 | | - return "push " + entities.toString(e, debug) + " " + direction.toString(e, debug) + (speed != null ? " at speed " + speed.toString(e, debug) : ""); |
| 65 | + public String toString(@Nullable Event event, boolean debug) { |
| 66 | + return "push " + entities.toString(event, debug) + " " + direction.toString(event, debug) + |
| 67 | + (speed != null ? " at speed " + speed.toString(event, debug) : ""); |
73 | 68 | } |
74 | 69 |
|
75 | 70 | } |
0 commit comments