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+ @ Example ("pull player along vector(1,1,1) at speed 1.5" )
28+ @ Since ({"1.4.6" , "INSERT VERSION (push towards)" })
2429public class EffPush extends Effect {
2530
2631 static {
27- Skript .registerEffect (EffPush .class , "(push|thrust) %entities% [along] %direction% [(at|with) (speed|velocity|force) %-number%]" );
32+ Skript .registerEffect (EffPush .class ,
33+ "(push|thrust) %entities% [along] %direction% [(at|with) [a] (speed|velocity|force) [of] %-number%]" ,
34+ "(push|thrust|pull) %entities% (towards|away:away from) %location% [(at|with) [a] (speed|velocity|force) [of] %-number%]" );
2835 }
2936
3037 private Expression <Entity > entities ;
31- private Expression <Direction > direction ;
38+ private @ Nullable Expression <Direction > direction ;
39+ private @ Nullable Expression <Location > target ;
40+ private boolean awayFrom = false ;
3241 private @ Nullable Expression <Number > speed = null ;
3342
3443 @ SuppressWarnings ({"unchecked" , "null" })
3544 @ Override
3645 public boolean init (Expression <?>[] exprs , int matchedPattern , Kleenean isDelayed , ParseResult parseResult ) {
3746 entities = (Expression <Entity >) exprs [0 ];
38- direction = (Expression <Direction >) exprs [1 ];
47+ if (matchedPattern == 0 ) {
48+ direction = (Expression <Direction >) exprs [1 ];
49+ } else {
50+ target = (Expression <Location >) exprs [1 ];
51+ awayFrom = parseResult .hasTag ("away" );
52+ }
3953 speed = (Expression <Number >) exprs [2 ];
4054 return true ;
4155 }
4256
4357 @ Override
4458 protected void execute (Event event ) {
45- Direction direction = this .direction .getSingle (event );
46- if (direction == null )
47- return ;
4859 Number speed = this .speed != null ? this .speed .getSingle (event ) : null ;
4960 if (this .speed != null && speed == null )
5061 return ;
62+
63+ Function <Entity , Vector > getDirection ;
64+ if (this .direction != null ) {
65+ // push along
66+ Direction direction = this .direction .getSingle (event );
67+ if (direction == null )
68+ return ;
69+ getDirection = direction ::getDirection ;
70+ } else {
71+ // push towards
72+ assert this .target != null ;
73+ Location target = this .target .getSingle (event );
74+ if (target == null )
75+ return ;
76+ Vector targetVector = target .toVector ();
77+ getDirection = entity -> {
78+ Vector direction = targetVector .subtract (entity .getLocation ().toVector ());
79+ if (awayFrom )
80+ direction .multiply (-1 );
81+ return direction ;
82+ };
83+ }
84+
5185 Entity [] entities = this .entities .getArray (event );
5286 for (Entity entity : entities ) {
53- Vector pushDirection = direction . getDirection (entity );
87+ Vector pushDirection = getDirection . apply (entity );
5488 if (speed != null )
5589 pushDirection .normalize ().multiply (speed .doubleValue ());
5690 if (!(Double .isFinite (pushDirection .getX ()) && Double .isFinite (pushDirection .getY ()) && Double .isFinite (pushDirection .getZ ()))) {
@@ -60,11 +94,23 @@ protected void execute(Event event) {
6094 entity .setVelocity (entity .getVelocity ().add (pushDirection ));
6195 }
6296 }
63-
97+
6498 @ Override
6599 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 ) : "" );
100+ var ssb = new SyntaxStringBuilder (event , debug ).append ("push" , entities );
101+ if (direction != null ) {
102+ ssb .append (direction );
103+ } else {
104+ assert target != null ;
105+ if (awayFrom ) {
106+ ssb .append ("away from" , target );
107+ } else {
108+ ssb .append ("towards" , target );
109+ }
110+ }
111+ if (speed != null )
112+ ssb .append ("at a speed of" , speed );
113+ return ssb .toString ();
68114 }
69115
70116}
0 commit comments