|
@@ -1,45 +1,44 @@
|
|
|
package com.gmail.nossr50.util.adapter;
|
|
|
|
|
|
import org.bukkit.block.Biome;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.util.*;
|
|
|
+import java.util.function.Function;
|
|
|
|
|
|
public class BiomeAdapter {
|
|
|
- public static final Set<Biome> WATER_BIOMES;
|
|
|
public static final Set<Biome> ICE_BIOMES;
|
|
|
|
|
|
+ static final List<String> knownColdBiomes = Arrays.asList("COLD_OCEAN", "DEEP_COLD_OCEAN", "ICE_SPIKES",
|
|
|
+ "FROZEN_PEAKS", "FROZEN_OCEAN", "FROZEN_RIVER", "DEEP_FROZEN_OCEAN", "SNOWY_TAIGA",
|
|
|
+ "OLD_GROWTH_PINE_TAIGA", "OLD_GROWTH_SPRUCE_TAIGA", "TAIGA", "SNOWY_SLOPES", "SNOWY_BEACH");
|
|
|
+
|
|
|
static {
|
|
|
- final List<Biome> allBiomes = getAllBiomes();
|
|
|
- final Set<Biome> waterBiomes = new HashSet<>();
|
|
|
final Set<Biome> iceBiomes = new HashSet<>();
|
|
|
- for (Biome biome : allBiomes) {
|
|
|
- String biomeName = getBiomeName(biome);
|
|
|
- if (isWater(biomeName) && !isCold(biomeName)) {
|
|
|
- waterBiomes.add(biome);
|
|
|
- } else if (isCold(biomeName)) {
|
|
|
- iceBiomes.add(biome);
|
|
|
- }
|
|
|
- }
|
|
|
- WATER_BIOMES = Collections.unmodifiableSet(waterBiomes);
|
|
|
+ knownColdBiomes.stream()
|
|
|
+ .map(biomeFromString())
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .forEach(iceBiomes::add);
|
|
|
ICE_BIOMES = Collections.unmodifiableSet(iceBiomes);
|
|
|
}
|
|
|
|
|
|
- @SuppressWarnings("deprecation")
|
|
|
- private static List<Biome> getAllBiomes() {
|
|
|
- return Arrays.asList(Biome.values());
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("deprecation")
|
|
|
- private static String getBiomeName(Biome biome) {
|
|
|
- return biome.name();
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean isWater(String name) {
|
|
|
- return name.contains("RIVER") || name.contains("OCEAN");
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean isCold(String name) {
|
|
|
- return (name.contains("COLD") || name.contains("ICE")
|
|
|
- || name.contains("FROZEN") || name.contains("TAIGA")) && !name.contains("WARM");
|
|
|
+ private static @NotNull Function<String, Biome> biomeFromString() {
|
|
|
+ return potentialBiome -> {
|
|
|
+ try {
|
|
|
+ Class<?> biomeClass = Class.forName("org.bukkit.block.Biome");
|
|
|
+ Method methodValueOf = biomeClass.getMethod("valueOf", String.class);
|
|
|
+ return methodValueOf.invoke(null, potentialBiome) == null
|
|
|
+ ? null
|
|
|
+ : (Biome) methodValueOf.invoke(null, potentialBiome);
|
|
|
+ } catch (IllegalArgumentException | NullPointerException e) {
|
|
|
+ return null;
|
|
|
+ } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException
|
|
|
+ | IllegalAccessException e) {
|
|
|
+ // Thrown when the method is not found or the class is not found
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ };
|
|
|
}
|
|
|
}
|