Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ public void variableModsCasted(SpellCastedEvent event) {
public void variableModsTarget(SpellTargetEvent event) {
Multimap<String, VariableMod> varMods = event.getSpell().getVariableModsTarget();
if (varMods == null || varMods.isEmpty()) return;
if (!(event.getCaster() instanceof Player caster)) return;
Player target = event.getTarget() instanceof Player ? (Player) event.getTarget() : null;

Player caster = event.getCaster() instanceof Player player ? player : null;
Player target = event.getTarget() instanceof Player player ? player : null;

for (Map.Entry<String, VariableMod> entry : varMods.entries()) {
VariableMod mod = entry.getValue();
if (mod == null) continue;
Expand All @@ -90,7 +92,10 @@ public void variableModsTarget(SpellTargetEvent event) {
String variableName = entry.getKey();
String[] splits = variableName.split(":");
if (splits.length > 1) {
if (splits[0].equalsIgnoreCase("caster")) playerToMod = caster;
if (splits[0].equalsIgnoreCase("caster")) {
if (caster == null) continue;
playerToMod = caster;
}
variableName = splits[1];
}
// Target was expected, but they were not a player.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class VelocitySpell extends InstantSpell implements TargetedEntitySpell,

private final ConfigData<Double> speed;

private final ConfigData<Vector> velocity;

private final ConfigData<Boolean> cancelDamage;
private final ConfigData<Boolean> powerAffectsSpeed;
private final ConfigData<Boolean> addVelocityInstead;
Expand All @@ -41,6 +43,8 @@ public VelocitySpell(MagicConfig config, String spellName) {

speed = getConfigDataDouble("speed", 40);

velocity = getConfigDataVector("velocity", null);

cancelDamage = getConfigDataBoolean("cancel-damage", true);
powerAffectsSpeed = getConfigDataBoolean("power-affects-speed", true);
addVelocityInstead = getConfigDataBoolean("add-velocity-instead", false);
Expand All @@ -60,33 +64,31 @@ public CastResult cast(SpellData data) {

@Override
public CastResult castAtEntity(SpellData data) {
double speed = this.speed.get(data) / 10;
if (powerAffectsSpeed.get(data)) speed *= data.power();

Vector velocity = data.target().getLocation().getDirection().normalize().multiply(speed);

if (addVelocityInstead.get(data)) data.target().setVelocity(data.target().getVelocity().add(velocity));
else data.target().setVelocity(velocity);

velocityMonitor.add(new VelocityData(this, data, cancelDamage.get(data)));
playSpellEffects(data);

return new CastResult(PostCastAction.HANDLE_NORMALLY, data);
return applyVelocity(data, data.target().getLocation().getDirection().normalize());
}

@Override
public CastResult castAtEntityFromLocation(SpellData data) {
double speed = this.speed.get(data) / 10;
if (powerAffectsSpeed.get(data)) speed *= data.power();
return applyVelocity(data, data.location().getDirection().normalize());
}

private CastResult applyVelocity(SpellData data, Vector direction) {
Vector velocity = this.velocity.get(data);

if (velocity == null) {
double speed = this.speed.get(data) / 10;
if (powerAffectsSpeed.get(data)) speed *= data.power();

Vector velocity = data.location().getDirection().normalize().multiply(speed);
velocity = direction.multiply(speed);
}

if (addVelocityInstead.get(data)) velocity = data.target().getVelocity().add(velocity);

if (addVelocityInstead.get(data)) data.target().setVelocity(data.target().getVelocity().add(velocity));
else data.target().setVelocity(velocity);
data.target().setVelocity(velocity);

velocityMonitor.add(new VelocityData(this, data, cancelDamage.get(data)));
playSpellEffects(data);

playSpellEffects(data);
return new CastResult(PostCastAction.HANDLE_NORMALLY, data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ private void initialize() {
addMetaVariableType("max_health", new MaxHealthVariable());
addMetaVariableType("current_health", new CurrentHealthVariable());
addMetaVariableType("health_scale", new HealthScaleVariable());
addMetaVariableType("held_item_slot", new HeldItemSlotVariable());
addMetaVariableType("compass_target_x", new CompassTargetXVariable());
addMetaVariableType("compass_target_y", new CompassTargetYVariable());
addMetaVariableType("compass_target_z", new CompassTargetZVariable());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.nisovin.magicspells.variables.meta;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import com.nisovin.magicspells.variables.variabletypes.MetaVariable;

public class HeldItemSlotVariable extends MetaVariable {

@Override
public double getValue(String player) {
Player p = Bukkit.getPlayerExact(player);
return p != null ? p.getInventory().getHeldItemSlot() : 0;
}

@Override
public void set(String player, double amount) {
Player p = Bukkit.getPlayerExact(player);
if (p == null) return;

try {
p.getInventory().setHeldItemSlot((int) amount);
} catch (IllegalArgumentException _) {}
}

}
Loading