forked from mcMMO-Dev/mcMMO
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'mcMMO-Dev:master' into update
- Loading branch information
Showing
6 changed files
with
41 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 28 additions & 29 deletions
57
src/main/java/com/gmail/nossr50/util/adapter/BiomeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; | ||
} | ||
} |