Skip to content

Commit ccb754e

Browse files
Add better protection to modifier variable setting
1 parent abce5b5 commit ccb754e

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

src/com/nisovin/magicspells/castmodifiers/conditions/MultiCondition.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,14 @@ public boolean setVar(String var) {
6666
if (modifierStrings == null) return false;
6767

6868
String passConditionString = config.getString(configPrefix + ".pass-condition", "ALL").toUpperCase();
69-
PassCondition configPassCondition = PassCondition.valueOf(passConditionString);
70-
if (configPassCondition != null) passCondition = configPassCondition;
69+
try {
70+
passCondition = PassCondition.valueOf(passConditionString);
71+
} catch (IllegalArgumentException badPassCondition) {
72+
MagicSpells.error("Invalid value for \"pass-condition\" of \"" + passConditionString + "\".");
73+
// To preserve old behavior, just default it to "ALL"
74+
MagicSpells.error("Defaulting pass-condition to \"ALL\"");
75+
passCondition = PassCondition.ALL;
76+
}
7177

7278
modifiers = new ArrayList<>();
7379
for (String modString: modifierStrings) {

src/com/nisovin/magicspells/castmodifiers/conditions/SpellCastStateCondition.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.nisovin.magicspells.castmodifiers.conditions;
22

3+
import com.nisovin.magicspells.MagicSpells;
34
import org.bukkit.Location;
45
import org.bukkit.entity.LivingEntity;
56
import org.bukkit.entity.Player;
@@ -59,8 +60,13 @@ public boolean check(Player player) {
5960
@Override
6061
public boolean setVar(String var) {
6162
if (var == null) return false;
62-
this.state = SpellCastState.valueOf(var.trim().toUpperCase());
63-
return this.state != null;
63+
try {
64+
this.state = SpellCastState.valueOf(var.trim().toUpperCase());
65+
return true;
66+
} catch (IllegalArgumentException badValueString) {
67+
MagicSpells.error("Invalid SpellCastState of \"" + var.trim() + "\" on this modifier var");
68+
return false;
69+
}
6470
}
6571

6672
@Override

src/com/nisovin/magicspells/castmodifiers/conditions/VariableEqualsCondition.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public boolean setVar(String var) {
2323
} catch (NumberFormatException e) {
2424
DebugHandler.debugNumberFormat(e);
2525
return false;
26+
} catch (ArrayIndexOutOfBoundsException missingColon) {
27+
MagicSpells.error("You likely forgot to add a colon in this modifier var.");
28+
return false;
2629
}
2730
}
2831

src/com/nisovin/magicspells/castmodifiers/conditions/VariableLessThanCondition.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public boolean setVar(String var) {
2323
} catch (NumberFormatException e) {
2424
DebugHandler.debugNumberFormat(e);
2525
return false;
26+
} catch (ArrayIndexOutOfBoundsException missingColon) {
27+
MagicSpells.error("You likely forgot to add a colon in this modifier var.");
28+
return false;
2629
}
2730
}
2831

src/com/nisovin/magicspells/castmodifiers/conditions/VariableMoreThanCondition.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public boolean setVar(String var) {
2323
} catch (NumberFormatException e) {
2424
DebugHandler.debugNumberFormat(e);
2525
return false;
26+
} catch (ArrayIndexOutOfBoundsException missingColon) {
27+
MagicSpells.error("You likely forgot to add a colon in this modifier var.");
28+
return false;
2629
}
2730
}
2831

0 commit comments

Comments
 (0)