Skip to content

Commit ee76f99

Browse files
ChronokenTheComputerGeek2
authored andcommitted
Improved ForcetossSpell.
Added - rotation option (float) rotates the toss location (allows you to toss your target to the left or right) Improved: - horizontal-force and vertical-force now accept float values Fixed: - a server crash when the forcetoss spell was targeting the caster while he was sneaking - horizontal-force didn't work if the caster was targeting himself - damage option didnt work if the forcetoss spell was casted by targeted multi or aoe spells
1 parent 5453e75 commit ee76f99

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

src/com/nisovin/magicspells/spells/targeted/ForcetossSpell.java

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
package com.nisovin.magicspells.spells.targeted;
22

3-
import org.bukkit.entity.LivingEntity;
3+
import org.bukkit.util.Vector;
44
import org.bukkit.entity.Player;
5+
import org.bukkit.entity.LivingEntity;
6+
import com.nisovin.magicspells.util.Util;
57
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
6-
import org.bukkit.util.Vector;
78

8-
import com.nisovin.magicspells.events.MagicSpellsEntityDamageByEntityEvent;
9-
import com.nisovin.magicspells.spells.TargetedEntitySpell;
9+
import com.nisovin.magicspells.util.TargetInfo;
10+
import com.nisovin.magicspells.util.MagicConfig;
1011
import com.nisovin.magicspells.spells.TargetedSpell;
1112
import com.nisovin.magicspells.util.compat.EventUtil;
12-
import com.nisovin.magicspells.util.MagicConfig;
13-
import com.nisovin.magicspells.util.TargetInfo;
13+
import com.nisovin.magicspells.spells.TargetedEntitySpell;
14+
import com.nisovin.magicspells.events.MagicSpellsEntityDamageByEntityEvent;
1415

1516
public class ForcetossSpell extends TargetedSpell implements TargetedEntitySpell {
1617

1718
private int damage;
1819
private float hForce;
1920
private float vForce;
21+
private float rotation;
2022
private boolean checkPlugins;
2123
private boolean powerAffectsForce;
2224
private boolean avoidDamageModification;
23-
25+
2426
public ForcetossSpell(MagicConfig config, String spellName) {
2527
super(config, spellName);
26-
28+
2729
damage = getConfigInt("damage", 0);
28-
hForce = getConfigInt("horizontal-force", 20) / 10.0F;
29-
vForce = getConfigInt("vertical-force", 10) / 10.0F;
30+
hForce = getConfigFloat("horizontal-force", 20) / 10.0F;
31+
vForce = getConfigFloat("vertical-force", 10) / 10.0F;
32+
rotation = getConfigFloat("rotation", 0);
3033
checkPlugins = getConfigBoolean("check-plugins", true);
3134
powerAffectsForce = getConfigBoolean("power-affects-force", true);
3235
avoidDamageModification = getConfigBoolean("avoid-damage-modification", false);
@@ -38,37 +41,39 @@ public PostCastAction castSpell(Player player, SpellCastState state, float power
3841
// Get target
3942
TargetInfo<LivingEntity> targetInfo = getTargetedEntity(player, power);
4043
if (targetInfo == null) return noTarget(player);
41-
LivingEntity target = targetInfo.getTarget();
42-
power = targetInfo.getPower();
43-
44-
// Do damage
45-
if (damage > 0) {
46-
double damage = this.damage * power;
47-
if (target instanceof Player && checkPlugins) {
48-
MagicSpellsEntityDamageByEntityEvent event = new MagicSpellsEntityDamageByEntityEvent(player, target, DamageCause.ENTITY_ATTACK, damage);
49-
EventUtil.call(event);
50-
if (event.isCancelled()) return noTarget(player);
51-
if (!avoidDamageModification) damage = event.getDamage();
52-
}
53-
target.damage(damage);
54-
}
55-
44+
5645
// Throw target
57-
toss(player, target, power);
58-
59-
sendMessages(player, target);
46+
toss(player, targetInfo.getTarget(), targetInfo.getPower());
47+
48+
sendMessages(player, targetInfo.getTarget());
6049
return PostCastAction.NO_MESSAGES;
6150
}
6251
return PostCastAction.HANDLE_NORMALLY;
6352
}
64-
53+
6554
private void toss(Player player, LivingEntity target, float power) {
6655
if (!powerAffectsForce) power = 1f;
67-
Vector v = target.getLocation().toVector().subtract(player.getLocation().toVector())
68-
.setY(0)
69-
.normalize()
70-
.multiply(hForce * power)
71-
.setY(vForce * power);
56+
57+
// Deal damage
58+
if (damage > 0) {
59+
double damage = this.damage * power;
60+
if (target instanceof Player && checkPlugins) {
61+
MagicSpellsEntityDamageByEntityEvent event = new MagicSpellsEntityDamageByEntityEvent(player, target, DamageCause.ENTITY_ATTACK, damage);
62+
EventUtil.call(event);
63+
if (!avoidDamageModification) damage = event.getDamage();
64+
}
65+
target.damage(damage);
66+
}
67+
68+
Vector v;
69+
if (player.equals(target)) {
70+
v = player.getLocation().getDirection();
71+
} else {
72+
v = target.getLocation().toVector().subtract(player.getLocation().toVector());
73+
}
74+
if (v == null) throw new NullPointerException("v");
75+
v.setY(0).normalize().multiply(hForce * power).setY(vForce * power);
76+
if (rotation != 0) Util.rotateVector(v, rotation);
7277
target.setVelocity(v);
7378
playSpellEffects(player, target);
7479
}

0 commit comments

Comments
 (0)