Skip to content

Commit 002b556

Browse files
Added instant.VariableCastSpell
This spell is designed to cast a spell based upon an internal spell name contained by a string variable. There is a required option on the spell called variable-name which accepts a string matching the name of the variable to read from for determining what spell to cast. There is another option which may be specified on the VariableCastSpell called str-doesnt-contain-spell. This option specifies the message to display to the caster if their variable does not contain the internal spell name of a valid spell. Create data structure, VariableDouble. This will aid in making spell fields more flexible and dynamic.
1 parent f23ee40 commit 002b556

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/com/nisovin/magicspells/spells/command/TomeSpell.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.nisovin.magicspells.util.MagicConfig;
2424
import com.nisovin.magicspells.util.Util;
2525

26+
// TODO this should not be hardcoded to use a book
2627
/**
2728
* Configuration fields:
2829
* <ul>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.nisovin.magicspells.spells.instant;
2+
3+
import com.nisovin.magicspells.MagicSpells;
4+
import com.nisovin.magicspells.Spell;
5+
import com.nisovin.magicspells.spells.InstantSpell;
6+
import com.nisovin.magicspells.util.MagicConfig;
7+
import org.bukkit.entity.Player;
8+
9+
public class VariableCastSpell extends InstantSpell {
10+
11+
private String variableName;
12+
private String strDoesntContainSpell;
13+
14+
public VariableCastSpell(MagicConfig config, String spellName) {
15+
super(config, spellName);
16+
17+
this.variableName = getConfigString("variable-name", null);
18+
this.strDoesntContainSpell = getConfigString("str-doesnt-contain-spell", "You do not have a valid spell in memory");
19+
}
20+
21+
@Override
22+
public void initialize() {
23+
// Super
24+
super.initialize();
25+
26+
if (MagicSpells.getVariableManager().getVariable(this.variableName) == null) {
27+
MagicSpells.error("variable-name references an invalid variable for ");
28+
}
29+
}
30+
31+
@Override
32+
public PostCastAction castSpell(Player player, SpellCastState state, float power, String[] args) {
33+
if (state == SpellCastState.NORMAL) {
34+
// Just keep it clean for the players
35+
if (this.variableName == null) return PostCastAction.HANDLE_NORMALLY;
36+
String strValue = MagicSpells.getVariableManager().getVariable(this.variableName).getStringValue(player);
37+
Spell toCast = MagicSpells.getSpellByInternalName(strValue);
38+
if (toCast == null) {
39+
player.sendMessage(this.strDoesntContainSpell);
40+
return PostCastAction.NO_MESSAGES;
41+
}
42+
toCast.cast(player, power, args);
43+
}
44+
return PostCastAction.HANDLE_NORMALLY;
45+
}
46+
47+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.nisovin.magicspells.util.dynamicvalues;
2+
3+
import com.nisovin.magicspells.util.VariableMod;
4+
import org.bukkit.entity.Player;
5+
6+
public class VariableDouble {
7+
8+
private VariableMod primaryValue = null;
9+
private VariableMod secondaryValue = null;
10+
11+
public VariableDouble(VariableMod primaryValue, VariableMod secondaryValue) {
12+
if (primaryValue == null) throw new IllegalArgumentException("Primary value cannot be null");
13+
this.primaryValue = primaryValue;
14+
this.secondaryValue = secondaryValue;
15+
}
16+
17+
18+
public VariableDouble(String raw) {
19+
if (raw == null) throw new IllegalArgumentException("VariableDouble cannot be created from null");
20+
if (raw.isEmpty()) throw new IllegalArgumentException("VariableDouble cannot");
21+
if (raw.contains(" ")) {
22+
String[] splits = raw.split(" ");
23+
secondaryValue = new VariableMod(splits[1]);
24+
raw = splits[0];
25+
}
26+
primaryValue = new VariableMod(raw);
27+
}
28+
29+
30+
public boolean hasSecondaryValue() {
31+
return this.secondaryValue != null;
32+
}
33+
34+
public double getPrimaryValue(Player caster, Player target) {
35+
return this.primaryValue.getValue(caster, target);
36+
}
37+
38+
public double getSecondaryValue(Player caster, Player target) {
39+
return this.secondaryValue.getValue(caster, target);
40+
}
41+
42+
public double calculateValue(Player caster, Player target) {
43+
double ret = getPrimaryValue(caster, target);
44+
if (this.hasSecondaryValue()) {
45+
ret = this.secondaryValue.getValue(caster, target, ret);
46+
}
47+
return ret;
48+
}
49+
50+
}

0 commit comments

Comments
 (0)