22
33import org .bukkit .Location ;
44import org .bukkit .block .Block ;
5+ import org .bukkit .block .BlockFace ;
56import org .bukkit .util .RayTraceResult ;
67
78import com .nisovin .magicspells .util .*;
1213
1314public class BlinkSpell extends TargetedSpell implements TargetedLocationSpell {
1415
15- private final ConfigData <Boolean > passThroughCeiling ;
16-
1716 private final String strCantBlink ;
1817
18+ private final ConfigData <Integer > requireGroundDistance ;
19+
20+ private final ConfigData <Boolean > snapToGround ;
21+ private final ConfigData <Boolean > passThroughCeiling ;
22+
1923 public BlinkSpell (MagicConfig config , String spellName ) {
2024 super (config , spellName );
2125
2226 strCantBlink = getConfigString ("str-cant-blink" , "You can't blink there." );
2327
28+ requireGroundDistance = getConfigDataInt ("require-ground-distance" , 0 );
29+
30+ snapToGround = getConfigDataBoolean ("snap-to-ground" , false );
2431 passThroughCeiling = getConfigDataBoolean ("pass-through-ceiling" , false );
2532 }
2633
@@ -30,21 +37,25 @@ public CastResult cast(SpellData data) {
3037 if (result == null ) return noTarget (strCantBlink , data );
3138
3239 Block found = result .getHitBlock ();
33- Block prev = found .getRelative (result .getHitBlockFace ());
40+ BlockFace face = result .getHitBlockFace ();
41+ Block prev = found .getRelative (face );
3442
3543 Location loc = null ;
36- if (!passThroughCeiling .get (data ) && found .getRelative (0 , -1 , 0 ).equals (prev ) && prev .isPassable ()) {
37- Block under = prev .getRelative (0 , -1 , 0 );
38- if (under .isPassable ()) loc = under .getLocation ().add (0.5 , 0 , 0.5 );
39- } else if (found .getRelative (0 , 1 , 0 ).isPassable () && found .getRelative (0 , 2 , 0 ).isPassable ()) {
40- loc = found .getLocation ().add (0.5 , 1 , 0.5 );
41- } else if (prev .isPassable () && prev .getRelative (0 , 1 , 0 ).isPassable ()) {
42- loc = prev .getLocation ().add (0.5 , 0 , 0.5 );
44+
45+ boolean snapToGround = this .snapToGround .get (data );
46+ int requireGroundDistance = this .requireGroundDistance .get (data );
47+
48+ // Under
49+ if (face == BlockFace .DOWN && !passThroughCeiling .get (data )) {
50+ Location target = prev .getLocation ().add (0.5 , -1 , 0.5 );
51+ loc = BlockUtils .adjustToSafeLocation (data .caster (), target , requireGroundDistance , snapToGround );
4352 }
44- if (loc == null ) return noTarget (strCantBlink , data );
53+ // Above
54+ if (loc == null ) loc = BlockUtils .adjustToSafeLocation (data .caster (), found .getLocation ().add (0.5 , 0 , 0.5 ));
55+ // Side
56+ if (loc == null ) loc = BlockUtils .adjustToSafeLocation (data .caster (), prev .getLocation ().add (0.5 , 0 , 0.5 ), requireGroundDistance , snapToGround );
4557
46- loc .setPitch (data .caster ().getPitch ());
47- loc .setYaw (data .caster ().getYaw ());
58+ if (loc == null ) return noTarget (strCantBlink , data );
4859
4960 SpellTargetLocationEvent targetEvent = new SpellTargetLocationEvent (this , data , loc );
5061 if (!targetEvent .callEvent ()) return noTarget (strCantBlink , targetEvent );
0 commit comments