Skip to content

Commit 34b09da

Browse files
Use safer checks for biome parsing
1 parent 3a5d179 commit 34b09da

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed
Lines changed: 19 additions & 11 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.util.Util;
34
import org.bukkit.Location;
45
import org.bukkit.block.Biome;
56
import org.bukkit.entity.LivingEntity;
@@ -8,20 +9,31 @@
89
import com.nisovin.magicspells.DebugHandler;
910
import com.nisovin.magicspells.castmodifiers.Condition;
1011

11-
public class BiomeCondition extends Condition {
12+
import java.util.EnumSet;
1213

13-
Biome[] biomes;
14+
public class BiomeCondition extends Condition {
15+
16+
private EnumSet<Biome> biomes = EnumSet.noneOf(Biome.class);
1417

1518
@Override
1619
public boolean setVar(String var) {
1720
String[] s = var.split(",");
18-
biomes = new Biome[s.length];
1921
for (int i = 0; i < s.length; i++) {
20-
biomes[i] = Biome.valueOf(s[i].toUpperCase());
21-
if (biomes[i] == null) {
22+
23+
// Get the biome
24+
Biome biome = Util.enumValueSafe(Biome.class, s[i].toUpperCase());
25+
26+
// Is it null?
27+
if (biome == null) {
28+
// Rip...
2229
DebugHandler.debugBadEnumValue(Biome.class, s[i].toUpperCase());
23-
return false;
30+
31+
// NEXT!
32+
continue;
2433
}
34+
35+
// Add to the collection
36+
biomes.add(biome);
2537
}
2638
return true;
2739
}
@@ -38,11 +50,7 @@ public boolean check(Player player, LivingEntity target) {
3850

3951
@Override
4052
public boolean check(Player player, Location location) {
41-
Biome biome = location.getBlock().getBiome();
42-
for (Biome b : biomes) {
43-
if (b == biome) return true;
44-
}
45-
return false;
53+
return biomes.contains(location.getBlock().getBiome());
4654
}
4755

4856
}

src/com/nisovin/magicspells/util/Util.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,4 +790,12 @@ public static <C extends Collection<Material>> C getMaterialList(List<String> st
790790
return ret;
791791
}
792792

793+
public static <E extends Enum<E>> E enumValueSafe(Class<E> clazz, String name) {
794+
try {
795+
return Enum.valueOf(clazz, name);
796+
} catch (Exception e) {
797+
return null;
798+
}
799+
}
800+
793801
}

0 commit comments

Comments
 (0)