Skip to content

Commit cf6d1e0

Browse files
ShinyDeagleTheComputerGeek2
authored andcommitted
Rotate Spell | affectPitch, Face and Mimic Options
Example Rotate Spell Config RotateSpell: spell-class: ".targeted.RotateSpell" rotation: 10 rotation-pitch: 0 affect-pitch: false mimic-direction: false face-target: false face-caster: false Notes: rotation only affects the Yaw value of an entity New Features: rotation-pitch: adds/remove pitch of an entity affect-pitch: a safe flag for if you want the pitch unaffected. This must be true for rotation-pitch to work mimic-direction: mimics the direction of the target/caster so that the target/caster faces in the same relative direction. face-caster: forces the target to face the caster face-target: forces the caster to face the target Cool Ideas: You can make a cut scene with the new rotate spell features. Simple set face-target to true and have a mob be followed as it moves across a screen. You can make a dummy npc that faces your direction by constantly spamming it with face-caster rotate spells. Sky's the limit...
1 parent 0e0d1ca commit cf6d1e0

1 file changed

Lines changed: 94 additions & 18 deletions

File tree

Lines changed: 94 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,138 @@
11
package com.nisovin.magicspells.spells.targeted;
22

3+
import java.util.Random;
4+
35
import org.bukkit.Location;
46
import org.bukkit.entity.LivingEntity;
57
import org.bukkit.entity.Player;
8+
import org.bukkit.util.Vector;
69

710
import com.nisovin.magicspells.spelleffects.EffectPosition;
11+
import com.nisovin.magicspells.spells.TargetedEntityFromLocationSpell;
812
import com.nisovin.magicspells.spells.TargetedEntitySpell;
13+
import com.nisovin.magicspells.spells.TargetedLocationSpell;
914
import com.nisovin.magicspells.spells.TargetedSpell;
1015
import com.nisovin.magicspells.util.MagicConfig;
1116
import com.nisovin.magicspells.util.TargetInfo;
1217
import com.nisovin.magicspells.util.Util;
1318

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();
1530

16-
boolean random;
17-
int rotation;
18-
1931
public RotateSpell(MagicConfig config, String spellName) {
2032
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);
2343
}
2444

2545
@Override
2646
public PostCastAction castSpell(Player player, SpellCastState state, float power, String[] args) {
2747
if (state == SpellCastState.NORMAL) {
2848
TargetInfo<LivingEntity> target = getTargetedEntity(player, power);
2949
if (target == null) return noTarget(player);
30-
spin(target.getTarget());
50+
spin(player, target.getTarget());
3151
playSpellEffects(player, target.getTarget());
3252
}
3353
return PostCastAction.HANDLE_NORMALLY;
3454
}
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) {
3961
loc.setYaw(Util.getRandomInt(360));
40-
target.teleport(loc);
62+
if (affectPitch) loc.setPitch(randomizer.nextInt(181) - 90);
4163
} 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);
4566
}
67+
//Finally teleport the target so they have the new Pitch and Yaw
68+
target.teleport(loc);
4669
}
4770

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
49116
public boolean castAtEntity(Player caster, LivingEntity target, float power) {
50-
spin(target);
117+
spin(caster, target);
51118
playSpellEffects(caster, target);
52119
return true;
53120
}
54121

55-
@Override
56122
public boolean castAtEntity(LivingEntity target, float power) {
57123
spin(target);
58124
playSpellEffects(EffectPosition.TARGET, target);
59125
return true;
60126
}
61127

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+
62138
}

0 commit comments

Comments
 (0)