From 38522884f20413f8954cdf351a58c79294ca5f15 Mon Sep 17 00:00:00 2001 From: Adubbz Date: Sat, 22 Feb 2020 20:02:17 +1100 Subject: [PATCH] Technical biomes can now be disabled. Fixed potential crashes when disabling certain biomes --- .../common/biome/BiomeConfigData.java | 5 +- .../common/biome/BiomeRegistry.java | 124 ++++++++++++++++-- .../common/world/layer/BOPMixOceansLayer.java | 9 +- .../common/world/layer/BOPShoreLayer.java | 13 +- .../common/world/layer/SubBiomeLayer.java | 2 +- .../java/biomesoplenty/init/ModBiomes.java | 22 +++- 6 files changed, 152 insertions(+), 23 deletions(-) diff --git a/src/main/java/biomesoplenty/common/biome/BiomeConfigData.java b/src/main/java/biomesoplenty/common/biome/BiomeConfigData.java index 04fd7c486..9c31b62b2 100644 --- a/src/main/java/biomesoplenty/common/biome/BiomeConfigData.java +++ b/src/main/java/biomesoplenty/common/biome/BiomeConfigData.java @@ -18,11 +18,14 @@ public class BiomeConfigData @SerializedName("standard_weights") public TreeMap standardBiomeWeights = Maps.newTreeMap(); + @SerializedName("technical_biome_toggles") + public TreeMap technicalBiomeEntries = Maps.newTreeMap(); + @SerializedName("sub_biome_weights") public TreeMap subBiomeEntries = Maps.newTreeMap(); @SerializedName("island_biome_toggles") - public Map islandBiomeEntries = Maps.newHashMap(); + public TreeMap islandBiomeEntries = Maps.newTreeMap(); @SerializedName("vanilla_biome_weights") public TreeMap vanillaBiomeEntries = Maps.newTreeMap(); diff --git a/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java b/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java index d74c3922e..05b9405a9 100644 --- a/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java +++ b/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java @@ -42,6 +42,11 @@ public class BiomeRegistry defer(RegistrationType.STANDARD_BIOME, new StandardBiomeRegistrationData(biome, name)); } + public static void deferTechnicalBiomeRegistration(BiomeBOP biome, String name) + { + defer(RegistrationType.TECHNICAL_BIOME, new ToggleableStandardBiomeRegistrationData(biome, name, true)); + } + public static void deferSubBiomeRegistration(Biome parent, Biome child, int weight, float rarity) { defer(RegistrationType.SUB_BIOME, new SubBiomeRegistrationData(parent, child, weight, rarity)); @@ -110,6 +115,54 @@ public class BiomeRegistry } } + public static void configureTechnicalBiomes() + { + List biomeRegistrations = deferrances.get(RegistrationType.TECHNICAL_BIOME); + TreeMap defaultBiomeEntries = Maps.newTreeMap(); + + for (DeferredRegistration registration : biomeRegistrations) + { + ToggleableStandardBiomeRegistrationData regData = registration.regData; + String biomeName = new ResourceLocation(BiomesOPlenty.MOD_ID, regData.getName()).toString(); + defaultBiomeEntries.put(biomeName, new BiomeConfigData.ToggleableBiomeEntry(true)); + } + + BiomeConfigData defaultConfigData = new BiomeConfigData(); + defaultConfigData.technicalBiomeEntries = defaultBiomeEntries; + BiomeConfigData configData = getConfigData(defaultConfigData); + + TreeMap revisedBiomeEntries = Maps.newTreeMap(defaultBiomeEntries); + + // Merge the config file with the default values + for (Map.Entry biomeEntry : configData.technicalBiomeEntries.entrySet()) + { + if (revisedBiomeEntries.containsKey(biomeEntry.getKey())) + { + revisedBiomeEntries.put(biomeEntry.getKey(), biomeEntry.getValue()); + } + } + + // Write back to the config file + configData.technicalBiomeEntries = revisedBiomeEntries; + JsonUtil.writeFile(getConfigFile(), configData); + + for (DeferredRegistration registration : biomeRegistrations) + { + ToggleableStandardBiomeRegistrationData regData = registration.regData; + String biomeName = new ResourceLocation(BiomesOPlenty.MOD_ID, regData.getName()).toString(); + + if (revisedBiomeEntries.containsKey(biomeName)) + { + BiomeConfigData.ToggleableBiomeEntry entry = revisedBiomeEntries.get(biomeName); + + if (!entry.enabled) + { + registration.regData.setEnabled(false); + } + } + } + } + public static void configureSubBiomes() { List subBiomeRegistrations = deferrances.get(RegistrationType.SUB_BIOME); @@ -160,11 +213,10 @@ public class BiomeRegistry public static void configureIslandBiomes() { - // Island biomes are currently not configurable due to them being registered multiple times for different climates - List islandBiomeReistrations = deferrances.get(RegistrationType.ISLAND_BIOME); - Map defaultBiomeEntries = Maps.newTreeMap(); + List biomeRegistrations = deferrances.get(RegistrationType.ISLAND_BIOME); + TreeMap defaultBiomeEntries = Maps.newTreeMap(); - for (DeferredRegistration registration : islandBiomeReistrations) + for (DeferredRegistration registration : biomeRegistrations) { SingleClimateRegistrationData regData = registration.regData; String biomeName = regData.getBiome().delegate.name().toString(); @@ -175,7 +227,7 @@ public class BiomeRegistry defaultConfigData.islandBiomeEntries = defaultBiomeEntries; BiomeConfigData configData = getConfigData(defaultConfigData); - Map revisedBiomeEntries = Maps.newHashMap(defaultBiomeEntries); + TreeMap revisedBiomeEntries = Maps.newTreeMap(defaultBiomeEntries); // Merge the config file with the default values for (Map.Entry biomeEntry : configData.islandBiomeEntries.entrySet()) @@ -190,7 +242,7 @@ public class BiomeRegistry configData.islandBiomeEntries = revisedBiomeEntries; JsonUtil.writeFile(getConfigFile(), configData); - for (DeferredRegistration registration : islandBiomeReistrations) + for (DeferredRegistration registration : biomeRegistrations) { SingleClimateRegistrationData regData = registration.regData; String biomeName = regData.getBiome().delegate.name().toString(); @@ -209,11 +261,11 @@ public class BiomeRegistry public static void configureVanillaBiomes() { - List islandBiomeReistrations = deferrances.get(RegistrationType.VANILLA_BIOME); + List biomeRegistrations = deferrances.get(RegistrationType.VANILLA_BIOME); TreeMap defaultBiomeEntries = Maps.newTreeMap(); Map regDataMap = Maps.newHashMap(); - for (DeferredRegistration registration : islandBiomeReistrations) + for (DeferredRegistration registration : biomeRegistrations) { SingleClimateRegistrationData regData = registration.regData; String biomeName = registration.regData.getBiome().delegate.name().toString(); @@ -348,6 +400,34 @@ public class BiomeRegistry throw new RuntimeException("Failed to set biome field " + name, e); } }), + TECHNICAL_BIOME((ToggleableStandardBiomeRegistrationData data) -> { + BiomeBOP biome = (BiomeBOP)data.getBiome(); + String name = data.getName(); + + if (!data.getEnabled()) + { + BiomesOPlenty.logger.debug("Technical biome " + data.getName() + " is disabled."); + return; + } + + biome.setRegistryName(name); + ForgeRegistries.BIOMES.register(biome); + + if (biome.canSpawnInBiome) + { + BiomeManager.addSpawnBiome(biome); + } + + // Set field in BOPBiomes + try + { + BOPBiomes.class.getField(name).set(null, Optional.of(biome)); + } + catch (Exception e) + { + throw new RuntimeException("Failed to set biome field " + name, e); + } + }), SUB_BIOME((SubBiomeRegistrationData data) -> { if (data.getWeight() == 0) { @@ -534,6 +614,34 @@ public class BiomeRegistry } } + private static class ToggleableStandardBiomeRegistrationData extends RegistrationData + { + private final String name; + private boolean enabled; + + public ToggleableStandardBiomeRegistrationData(Biome biome, String name, boolean enabled) + { + super(biome); + this.name = name; + this.enabled = enabled; + } + + public String getName() + { + return this.name; + } + + public boolean getEnabled() + { + return this.enabled; + } + + public void setEnabled(boolean enabled) + { + this.enabled = enabled; + } + } + private static class DeferredRegistration { private final Consumer regFunc; diff --git a/src/main/java/biomesoplenty/common/world/layer/BOPMixOceansLayer.java b/src/main/java/biomesoplenty/common/world/layer/BOPMixOceansLayer.java index b9fe33cbc..eaf03ffc8 100644 --- a/src/main/java/biomesoplenty/common/world/layer/BOPMixOceansLayer.java +++ b/src/main/java/biomesoplenty/common/world/layer/BOPMixOceansLayer.java @@ -60,8 +60,11 @@ public enum BOPMixOceansLayer implements IAreaTransformer3, IDimOffset0Transform break; case WASTELAND: - oceanId = Registry.BIOME.getId(BOPBiomes.wasteland.get()); - break; + if (BOPBiomes.wasteland.isPresent()) + { + oceanId = Registry.BIOME.getId(BOPBiomes.wasteland.get()); + } + // Fallthrough default: oceanId = BOPLayerUtil.OCEAN; @@ -117,7 +120,7 @@ public enum BOPMixOceansLayer implements IAreaTransformer3, IDimOffset0Transform return BOPLayerUtil.DEEP_FROZEN_OCEAN; } - if (oceanId == Registry.BIOME.getId(BOPBiomes.wasteland.get())) + if (BOPBiomes.wasteland.isPresent() && oceanId == Registry.BIOME.getId(BOPBiomes.wasteland.get())) { return Registry.BIOME.getId(BOPBiomes.wasteland.get()); } diff --git a/src/main/java/biomesoplenty/common/world/layer/BOPShoreLayer.java b/src/main/java/biomesoplenty/common/world/layer/BOPShoreLayer.java index ad932e3fd..085eecbc5 100644 --- a/src/main/java/biomesoplenty/common/world/layer/BOPShoreLayer.java +++ b/src/main/java/biomesoplenty/common/world/layer/BOPShoreLayer.java @@ -16,6 +16,8 @@ import net.minecraft.world.biome.Biomes; import net.minecraft.world.gen.INoiseRandom; import net.minecraft.world.gen.layer.traits.ICastleTransformer; +import java.util.Optional; + public enum BOPShoreLayer implements ICastleTransformer { INSTANCE; @@ -64,7 +66,7 @@ public enum BOPShoreLayer implements ICastleTransformer if (BOPLayerUtil.isOcean(northBiomeId) || BOPLayerUtil.isOcean(eastBiomeId) || BOPLayerUtil.isOcean(southBiomeId) || BOPLayerUtil.isOcean(westBiomeId)) { - return Registry.BIOME.getId(BOPBiomes.mangrove.get()); + return getBiomeIdIfPresent(BOPBiomes.mangrove, biomeId); } } else if (biomeId != MOUNTAINS && biomeId != WOODED_MOUNTAINS && biomeId != MOUNTAIN_EDGE) @@ -105,11 +107,11 @@ public enum BOPShoreLayer implements ICastleTransformer { if (biome == Biomes.JUNGLE || biome == Biomes.JUNGLE_HILLS || biome == Biomes.JUNGLE_EDGE || biome == Biomes.MODIFIED_JUNGLE || biome == Biomes.BAMBOO_JUNGLE || biome == Biomes.BAMBOO_JUNGLE_HILLS || biome == Biomes.MODIFIED_JUNGLE_EDGE) { - return Registry.BIOME.getId(BOPBiomes.mangrove.get()); + return getBiomeIdIfPresent(BOPBiomes.mangrove, biomeId); } if (biome == Biomes.TAIGA || biome == Biomes.TAIGA_MOUNTAINS || biome == Biomes.TAIGA_HILLS || biome == Biomes.GIANT_TREE_TAIGA || biome == Biomes.GIANT_SPRUCE_TAIGA || biome == Biomes.GIANT_TREE_TAIGA_HILLS || biome == Biomes.GIANT_SPRUCE_TAIGA_HILLS || biome == Biomes.BIRCH_FOREST_HILLS || biome == Biomes.BIRCH_FOREST || biome == Biomes.TALL_BIRCH_HILLS || biome == Biomes.TALL_BIRCH_FOREST || biome == Biomes.DARK_FOREST_HILLS || biome == Biomes.DARK_FOREST) { - return Registry.BIOME.getId(BOPBiomes.gravel_beach.get()); + return getBiomeIdIfPresent(BOPBiomes.gravel_beach, biomeId); } } @@ -129,6 +131,11 @@ public enum BOPShoreLayer implements ICastleTransformer return biomeId; } + private static int getBiomeIdIfPresent(Optional biome, int fallbackId) + { + return biome.isPresent() ? Registry.BIOME.getId(biome.get()) : fallbackId; + } + private static boolean isJungleCompatible(int biomeId) { if (Registry.BIOME.byId(biomeId) != null && (Registry.BIOME.byId(biomeId)).getBiomeCategory() == Biome.Category.JUNGLE) diff --git a/src/main/java/biomesoplenty/common/world/layer/SubBiomeLayer.java b/src/main/java/biomesoplenty/common/world/layer/SubBiomeLayer.java index 1bd09d8c5..a6ecece11 100644 --- a/src/main/java/biomesoplenty/common/world/layer/SubBiomeLayer.java +++ b/src/main/java/biomesoplenty/common/world/layer/SubBiomeLayer.java @@ -157,7 +157,7 @@ public enum SubBiomeLayer implements IAreaTransformer2, IDimOffset1Transformer else if (originalBiomeId == SNOWY_TAIGA) mutatedBiomeId = SNOWY_TAIGA_HILLS; //Use BOP orchard instead of vanilla forest //else if (originalBiomeId == PLAINS) mutatedBiomeId = context.random(3) == 0 ? WOODED_HILLS : FOREST; - else if (originalBiomeId == PLAINS) mutatedBiomeId = Registry.BIOME.getId(BOPBiomes.orchard.get()); + else if (originalBiomeId == PLAINS && BOPBiomes.orchard.isPresent()) mutatedBiomeId = Registry.BIOME.getId(BOPBiomes.orchard.get()); ////////// else if (originalBiomeId == SNOWY_TUNDRA) mutatedBiomeId = SNOWY_MOUNTAINS; else if (originalBiomeId == JUNGLE) mutatedBiomeId = JUNGLE_HILLS; diff --git a/src/main/java/biomesoplenty/init/ModBiomes.java b/src/main/java/biomesoplenty/init/ModBiomes.java index cd22db160..b73e963e5 100644 --- a/src/main/java/biomesoplenty/init/ModBiomes.java +++ b/src/main/java/biomesoplenty/init/ModBiomes.java @@ -47,13 +47,16 @@ public class ModBiomes public static void registerBiomes(RegistryEvent.Register event) { //Technical Biomes (Need to be registered before main biomes that use them) - registerBiome(new MangroveBiome(), "mangrove"); - registerBiome(new GravelBeachBiome(), "gravel_beach"); - registerBiome(new OriginBeachBiome(), "origin_beach"); - registerBiome(new WhiteBeachBiome(), "white_beach"); - registerBiome(new AlpsFoothillsBiome(), "alps_foothills"); - registerBiome(new RedwoodForestEdgeBiome(), "redwood_forest_edge"); - registerBiome(new VolcanoEdgeBiome(), "volcano_edge"); + registerTechnicalBiome(new MangroveBiome(), "mangrove"); + registerTechnicalBiome(new GravelBeachBiome(), "gravel_beach"); + registerTechnicalBiome(new OriginBeachBiome(), "origin_beach"); + registerTechnicalBiome(new WhiteBeachBiome(), "white_beach"); + registerTechnicalBiome(new AlpsFoothillsBiome(), "alps_foothills"); + registerTechnicalBiome(new RedwoodForestEdgeBiome(), "redwood_forest_edge"); + registerTechnicalBiome(new VolcanoEdgeBiome(), "volcano_edge"); + + BiomeRegistry.configureTechnicalBiomes(); + BiomeRegistry.finalizeRegistrations(BiomeRegistry.RegistrationType.TECHNICAL_BIOME); //Overworld Biomes registerBiome(new AlpsBiome(), "alps"); @@ -353,6 +356,11 @@ public class ModBiomes BiomeRegistry.deferStandardRegistration(biome, name); } + public static void registerTechnicalBiome(BiomeBOP biome, String name) + { + BiomeRegistry.deferTechnicalBiomeRegistration(biome, name); + } + public static void registerSubBiome(Biome parent, Optional child, float rarity, int weight) { registerSubBiome(Optional.of(parent), child, rarity, weight);