|
1 | 1 | package com.nisovin.magicspells.spells.targeted; |
2 | 2 |
|
| 3 | +import java.util.Random; |
| 4 | + |
3 | 5 | import org.bukkit.Location; |
4 | 6 | import org.bukkit.entity.LivingEntity; |
5 | 7 | import org.bukkit.entity.Player; |
| 8 | +import org.bukkit.util.Vector; |
6 | 9 |
|
7 | 10 | import com.nisovin.magicspells.spelleffects.EffectPosition; |
| 11 | +import com.nisovin.magicspells.spells.TargetedEntityFromLocationSpell; |
8 | 12 | import com.nisovin.magicspells.spells.TargetedEntitySpell; |
| 13 | +import com.nisovin.magicspells.spells.TargetedLocationSpell; |
9 | 14 | import com.nisovin.magicspells.spells.TargetedSpell; |
10 | 15 | import com.nisovin.magicspells.util.MagicConfig; |
11 | 16 | import com.nisovin.magicspells.util.TargetInfo; |
12 | 17 | import com.nisovin.magicspells.util.Util; |
13 | 18 |
|
14 | | -public class RotateSpell extends TargetedSpell implements TargetedEntitySpell { |
| 19 | +public class RotateSpell extends TargetedSpell implements TargetedEntitySpell, TargetedLocationSpell { |
| 20 | + |
| 21 | + boolean randomAngle; |
| 22 | + boolean faceTarget; |
| 23 | + boolean faceCaster; |
| 24 | + boolean affectPitch; |
| 25 | + boolean mimicDirection; |
| 26 | + int rotationYaw; |
| 27 | + int rotationPitch; |
| 28 | + |
| 29 | + Random randomizer = new Random(); |
15 | 30 |
|
16 | | - boolean random; |
17 | | - int rotation; |
18 | | - |
19 | 31 | public RotateSpell(MagicConfig config, String spellName) { |
20 | 32 | super(config, spellName); |
21 | | - random = getConfigBoolean("random", false); |
22 | | - rotation = getConfigInt("rotation", 10); |
| 33 | + |
| 34 | + randomAngle = getConfigBoolean("random", false); |
| 35 | + faceTarget = getConfigBoolean("face-target", false); |
| 36 | + faceCaster = getConfigBoolean("face-caster", false); |
| 37 | + mimicDirection = getConfigBoolean("mimic-direction", false); |
| 38 | + //If this is true, this also applies for random rotations |
| 39 | + affectPitch = getConfigBoolean("affect-pitch", false); |
| 40 | + rotationYaw = getConfigInt("rotation", 10); |
| 41 | + //affect pitch must be true for this to have any use |
| 42 | + rotationPitch = getConfigInt("rotation-pitch", 0); |
23 | 43 | } |
24 | 44 |
|
25 | 45 | @Override |
26 | 46 | public PostCastAction castSpell(Player player, SpellCastState state, float power, String[] args) { |
27 | 47 | if (state == SpellCastState.NORMAL) { |
28 | 48 | TargetInfo<LivingEntity> target = getTargetedEntity(player, power); |
29 | 49 | if (target == null) return noTarget(player); |
30 | | - spin(target.getTarget()); |
| 50 | + spin(player, target.getTarget()); |
31 | 51 | playSpellEffects(player, target.getTarget()); |
32 | 52 | } |
33 | 53 | return PostCastAction.HANDLE_NORMALLY; |
34 | 54 | } |
35 | | - |
36 | | - void spin(LivingEntity target) { |
37 | | - if (random) { |
38 | | - Location loc = target.getLocation(); |
| 55 | + |
| 56 | + //Basic spin for a single target. This was there to begin with. |
| 57 | + private void spin(LivingEntity target) { |
| 58 | + Location loc = target.getLocation(); |
| 59 | + //Affect Pitch must be added to affect the pitch of a target. |
| 60 | + if (randomAngle) { |
39 | 61 | loc.setYaw(Util.getRandomInt(360)); |
40 | | - target.teleport(loc); |
| 62 | + if (affectPitch) loc.setPitch(randomizer.nextInt(181) - 90); |
41 | 63 | } else { |
42 | | - Location loc = target.getLocation(); |
43 | | - loc.setYaw(loc.getYaw() + rotation); |
44 | | - target.teleport(loc); |
| 64 | + loc.setYaw(loc.getYaw() + rotationYaw); |
| 65 | + if (affectPitch) loc.setPitch(loc.getPitch() + rotationPitch); |
45 | 66 | } |
| 67 | + //Finally teleport the target so they have the new Pitch and Yaw |
| 68 | + target.teleport(loc); |
46 | 69 | } |
47 | 70 |
|
48 | | - @Override |
| 71 | + //If I'm spinning an entity relative to another entity. |
| 72 | + private void spin(LivingEntity caster, LivingEntity target) { |
| 73 | + //Get the directions of the caster and target. |
| 74 | + Location targetLoc = target.getLocation(); |
| 75 | + Location casterLoc = caster.getLocation(); |
| 76 | + |
| 77 | + //If they want to make the caster face the target they targeted. |
| 78 | + if (faceTarget) caster.teleport(changeDirection(casterLoc, targetLoc)); |
| 79 | + //If they want to make the target face the caster, lets teleport the target. |
| 80 | + else if (faceCaster) target.teleport(changeDirection(targetLoc, casterLoc)); |
| 81 | + //If there are no face options, we'll just spin the target normally. |
| 82 | + else spin(target); |
| 83 | + } |
| 84 | + |
| 85 | + //If I'm spinning an entity relative to a location and nothing else. |
| 86 | + private void spin(LivingEntity entity, Location target) { |
| 87 | + entity.teleport(changeDirection(entity.getLocation(), target)); |
| 88 | + } |
| 89 | + |
| 90 | + /*The main function that tries to process the directions sent through. |
| 91 | + It will always process the caster location relative to the target*/ |
| 92 | + private Location changeDirection(Location caster, Location target) { |
| 93 | + //A cloned location so that I don't tamper with the caster's. |
| 94 | + Location loc = caster.clone(); |
| 95 | + //Mimicing the direction of the target. |
| 96 | + if (mimicDirection) { |
| 97 | + //Set the Yaw and Pitch of the target to be the same as the caster's |
| 98 | + if (affectPitch) loc.setPitch(target.getPitch()); |
| 99 | + loc.setYaw(target.getYaw()); |
| 100 | + |
| 101 | + } else { |
| 102 | + //If I'm not mimicing anything, I'm going to face the target location |
| 103 | + loc.setDirection(getVectorDir(caster, target)); |
| 104 | + //I'll face in the direction of the target but still maintain the pitch if it should be unaffected |
| 105 | + if (!affectPitch) loc.setPitch(caster.getPitch()); |
| 106 | + } |
| 107 | + return loc; |
| 108 | + } |
| 109 | + |
| 110 | + //A function that allows me to properly get the direction of a target relative to a caster. |
| 111 | + private Vector getVectorDir(Location caster, Location target) { |
| 112 | + return target.clone().subtract(caster.toVector()).toVector(); |
| 113 | + } |
| 114 | + |
| 115 | + //Cast Methods |
49 | 116 | public boolean castAtEntity(Player caster, LivingEntity target, float power) { |
50 | | - spin(target); |
| 117 | + spin(caster, target); |
51 | 118 | playSpellEffects(caster, target); |
52 | 119 | return true; |
53 | 120 | } |
54 | 121 |
|
55 | | - @Override |
56 | 122 | public boolean castAtEntity(LivingEntity target, float power) { |
57 | 123 | spin(target); |
58 | 124 | playSpellEffects(EffectPosition.TARGET, target); |
59 | 125 | return true; |
60 | 126 | } |
61 | 127 |
|
| 128 | + public boolean castAtLocation(Player caster, Location target, float power) { |
| 129 | + spin(caster, target); |
| 130 | + playSpellEffects(EffectPosition.TARGET, target); |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + public boolean castAtLocation(Location target, float power) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
62 | 138 | } |
0 commit comments