88import ch .njol .skript .lang .Effect ;
99import ch .njol .skript .lang .Expression ;
1010import ch .njol .skript .lang .SkriptParser .ParseResult ;
11+ import ch .njol .skript .lang .SyntaxStringBuilder ;
1112import ch .njol .skript .util .Direction ;
1213import ch .njol .util .Kleenean ;
14+ import org .bukkit .Location ;
1315import org .bukkit .entity .Entity ;
1416import org .bukkit .event .Event ;
1517import org .bukkit .util .Vector ;
1618import org .jetbrains .annotations .Nullable ;
1719
20+ import java .util .function .Function ;
21+
1822@ Name ("Push" )
19- @ Description ("Push entities around ." )
23+ @ Description ("Push entities in a given direction or towards a specific location ." )
2024@ Example ("push the player upwards" )
2125@ Example ("push the victim downwards at speed 0.5" )
22- @ Example ("push player along vector from player to player's target at speed 2" )
23- @ Since ("1.4.6" )
26+ @ Example ("push player towards player's target at speed 2" )
27+ @ Since ({ "1.4.6" , "INSERT VERSION (push towards)" } )
2428public class EffPush extends Effect {
2529
2630 static {
27- Skript .registerEffect (EffPush .class , "(push|thrust) %entities% [along] %direction% [(at|with) (speed|velocity|force) %-number%]" );
31+ Skript .registerEffect (EffPush .class ,
32+ "(push|thrust) %entities% [along] %direction% [(at|with) [a] (speed|velocity|force) [of] %-number%]" ,
33+ "(push|thrust|pull) %entities% towards %location% [(at|with) [a] (speed|velocity|force) [of] %-number%]" );
2834 }
2935
3036 private Expression <Entity > entities ;
31- private Expression <Direction > direction ;
37+ private @ Nullable Expression <Direction > direction ;
38+ private @ Nullable Expression <Location > target ;
3239 private @ Nullable Expression <Number > speed = null ;
3340
3441 @ SuppressWarnings ({"unchecked" , "null" })
3542 @ Override
3643 public boolean init (Expression <?>[] exprs , int matchedPattern , Kleenean isDelayed , ParseResult parseResult ) {
3744 entities = (Expression <Entity >) exprs [0 ];
38- direction = (Expression <Direction >) exprs [1 ];
45+ if (matchedPattern == 0 ) {
46+ direction = (Expression <Direction >) exprs [1 ];
47+ } else {
48+ target = (Expression <Location >) exprs [1 ];
49+ }
3950 speed = (Expression <Number >) exprs [2 ];
4051 return true ;
4152 }
4253
4354 @ Override
4455 protected void execute (Event event ) {
45- Direction direction = this .direction .getSingle (event );
46- if (direction == null )
47- return ;
4856 Number speed = this .speed != null ? this .speed .getSingle (event ) : null ;
4957 if (this .speed != null && speed == null )
5058 return ;
59+
60+ Function <Entity , Vector > getDirection ;
61+ if (this .direction != null ) {
62+ // push along
63+ Direction direction = this .direction .getSingle (event );
64+ if (direction == null )
65+ return ;
66+ getDirection = direction ::getDirection ;
67+ } else {
68+ // push towards
69+ assert this .target != null ;
70+ Location target = this .target .getSingle (event );
71+ if (target == null )
72+ return ;
73+ Vector targetVector = target .toVector ();
74+ getDirection = entity -> targetVector .subtract (entity .getLocation ().toVector ());
75+ }
76+
5177 Entity [] entities = this .entities .getArray (event );
5278 for (Entity entity : entities ) {
53- Vector pushDirection = direction . getDirection (entity );
79+ Vector pushDirection = getDirection . apply (entity );
5480 if (speed != null )
5581 pushDirection .normalize ().multiply (speed .doubleValue ());
5682 if (!(Double .isFinite (pushDirection .getX ()) && Double .isFinite (pushDirection .getY ()) && Double .isFinite (pushDirection .getZ ()))) {
@@ -60,11 +86,19 @@ protected void execute(Event event) {
6086 entity .setVelocity (entity .getVelocity ().add (pushDirection ));
6187 }
6288 }
63-
89+
6490 @ Override
6591 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 ) : "" );
92+ var ssb = new SyntaxStringBuilder (event , debug ).append ("push" , entities );
93+ if (direction != null ) {
94+ ssb .append (direction );
95+ } else {
96+ assert target != null ;
97+ ssb .append ("towards" , target );
98+ }
99+ if (speed != null )
100+ ssb .append ("at a speed of" , speed );
101+ return ssb .toString ();
68102 }
69103
70104}
0 commit comments