Technical biomes can now be disabled. Fixed potential crashes when disabling certain biomes

This commit is contained in:
Adubbz 2020-02-22 20:02:17 +11:00
parent b591814b1f
commit 38522884f2
6 changed files with 152 additions and 23 deletions

View File

@ -18,11 +18,14 @@ public class BiomeConfigData
@SerializedName("standard_weights") @SerializedName("standard_weights")
public TreeMap<String, WeightedBiomeEntry> standardBiomeWeights = Maps.newTreeMap(); public TreeMap<String, WeightedBiomeEntry> standardBiomeWeights = Maps.newTreeMap();
@SerializedName("technical_biome_toggles")
public TreeMap<String, ToggleableBiomeEntry> technicalBiomeEntries = Maps.newTreeMap();
@SerializedName("sub_biome_weights") @SerializedName("sub_biome_weights")
public TreeMap<String, SubBiomeEntry> subBiomeEntries = Maps.newTreeMap(); public TreeMap<String, SubBiomeEntry> subBiomeEntries = Maps.newTreeMap();
@SerializedName("island_biome_toggles") @SerializedName("island_biome_toggles")
public Map<String, ToggleableBiomeEntry> islandBiomeEntries = Maps.newHashMap(); public TreeMap<String, ToggleableBiomeEntry> islandBiomeEntries = Maps.newTreeMap();
@SerializedName("vanilla_biome_weights") @SerializedName("vanilla_biome_weights")
public TreeMap<String, WeightedBiomeEntry> vanillaBiomeEntries = Maps.newTreeMap(); public TreeMap<String, WeightedBiomeEntry> vanillaBiomeEntries = Maps.newTreeMap();

View File

@ -42,6 +42,11 @@ public class BiomeRegistry
defer(RegistrationType.STANDARD_BIOME, new StandardBiomeRegistrationData(biome, name)); 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) public static void deferSubBiomeRegistration(Biome parent, Biome child, int weight, float rarity)
{ {
defer(RegistrationType.SUB_BIOME, new SubBiomeRegistrationData(parent, child, weight, rarity)); defer(RegistrationType.SUB_BIOME, new SubBiomeRegistrationData(parent, child, weight, rarity));
@ -110,6 +115,54 @@ public class BiomeRegistry
} }
} }
public static void configureTechnicalBiomes()
{
List<DeferredRegistration> biomeRegistrations = deferrances.get(RegistrationType.TECHNICAL_BIOME);
TreeMap<String, BiomeConfigData.ToggleableBiomeEntry> defaultBiomeEntries = Maps.newTreeMap();
for (DeferredRegistration<ToggleableStandardBiomeRegistrationData> 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<String, BiomeConfigData.ToggleableBiomeEntry> revisedBiomeEntries = Maps.newTreeMap(defaultBiomeEntries);
// Merge the config file with the default values
for (Map.Entry<String, BiomeConfigData.ToggleableBiomeEntry> 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<ToggleableStandardBiomeRegistrationData> 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() public static void configureSubBiomes()
{ {
List<DeferredRegistration> subBiomeRegistrations = deferrances.get(RegistrationType.SUB_BIOME); List<DeferredRegistration> subBiomeRegistrations = deferrances.get(RegistrationType.SUB_BIOME);
@ -160,11 +213,10 @@ public class BiomeRegistry
public static void configureIslandBiomes() public static void configureIslandBiomes()
{ {
// Island biomes are currently not configurable due to them being registered multiple times for different climates List<DeferredRegistration> biomeRegistrations = deferrances.get(RegistrationType.ISLAND_BIOME);
List<DeferredRegistration> islandBiomeReistrations = deferrances.get(RegistrationType.ISLAND_BIOME); TreeMap<String, BiomeConfigData.ToggleableBiomeEntry> defaultBiomeEntries = Maps.newTreeMap();
Map<String, BiomeConfigData.ToggleableBiomeEntry> defaultBiomeEntries = Maps.newTreeMap();
for (DeferredRegistration<SingleClimateRegistrationData> registration : islandBiomeReistrations) for (DeferredRegistration<SingleClimateRegistrationData> registration : biomeRegistrations)
{ {
SingleClimateRegistrationData regData = registration.regData; SingleClimateRegistrationData regData = registration.regData;
String biomeName = regData.getBiome().delegate.name().toString(); String biomeName = regData.getBiome().delegate.name().toString();
@ -175,7 +227,7 @@ public class BiomeRegistry
defaultConfigData.islandBiomeEntries = defaultBiomeEntries; defaultConfigData.islandBiomeEntries = defaultBiomeEntries;
BiomeConfigData configData = getConfigData(defaultConfigData); BiomeConfigData configData = getConfigData(defaultConfigData);
Map<String, BiomeConfigData.ToggleableBiomeEntry> revisedBiomeEntries = Maps.newHashMap(defaultBiomeEntries); TreeMap<String, BiomeConfigData.ToggleableBiomeEntry> revisedBiomeEntries = Maps.newTreeMap(defaultBiomeEntries);
// Merge the config file with the default values // Merge the config file with the default values
for (Map.Entry<String, BiomeConfigData.ToggleableBiomeEntry> biomeEntry : configData.islandBiomeEntries.entrySet()) for (Map.Entry<String, BiomeConfigData.ToggleableBiomeEntry> biomeEntry : configData.islandBiomeEntries.entrySet())
@ -190,7 +242,7 @@ public class BiomeRegistry
configData.islandBiomeEntries = revisedBiomeEntries; configData.islandBiomeEntries = revisedBiomeEntries;
JsonUtil.writeFile(getConfigFile(), configData); JsonUtil.writeFile(getConfigFile(), configData);
for (DeferredRegistration<SingleClimateRegistrationData> registration : islandBiomeReistrations) for (DeferredRegistration<SingleClimateRegistrationData> registration : biomeRegistrations)
{ {
SingleClimateRegistrationData regData = registration.regData; SingleClimateRegistrationData regData = registration.regData;
String biomeName = regData.getBiome().delegate.name().toString(); String biomeName = regData.getBiome().delegate.name().toString();
@ -209,11 +261,11 @@ public class BiomeRegistry
public static void configureVanillaBiomes() public static void configureVanillaBiomes()
{ {
List<DeferredRegistration> islandBiomeReistrations = deferrances.get(RegistrationType.VANILLA_BIOME); List<DeferredRegistration> biomeRegistrations = deferrances.get(RegistrationType.VANILLA_BIOME);
TreeMap<String, BiomeConfigData.WeightedBiomeEntry> defaultBiomeEntries = Maps.newTreeMap(); TreeMap<String, BiomeConfigData.WeightedBiomeEntry> defaultBiomeEntries = Maps.newTreeMap();
Map<String, SingleClimateRegistrationData> regDataMap = Maps.newHashMap(); Map<String, SingleClimateRegistrationData> regDataMap = Maps.newHashMap();
for (DeferredRegistration<SingleClimateRegistrationData> registration : islandBiomeReistrations) for (DeferredRegistration<SingleClimateRegistrationData> registration : biomeRegistrations)
{ {
SingleClimateRegistrationData regData = registration.regData; SingleClimateRegistrationData regData = registration.regData;
String biomeName = registration.regData.getBiome().delegate.name().toString(); 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); 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) -> { SUB_BIOME((SubBiomeRegistrationData data) -> {
if (data.getWeight() == 0) 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<T extends RegistrationData> private static class DeferredRegistration<T extends RegistrationData>
{ {
private final Consumer<T> regFunc; private final Consumer<T> regFunc;

View File

@ -60,8 +60,11 @@ public enum BOPMixOceansLayer implements IAreaTransformer3, IDimOffset0Transform
break; break;
case WASTELAND: case WASTELAND:
oceanId = Registry.BIOME.getId(BOPBiomes.wasteland.get()); if (BOPBiomes.wasteland.isPresent())
break; {
oceanId = Registry.BIOME.getId(BOPBiomes.wasteland.get());
}
// Fallthrough
default: default:
oceanId = BOPLayerUtil.OCEAN; oceanId = BOPLayerUtil.OCEAN;
@ -117,7 +120,7 @@ public enum BOPMixOceansLayer implements IAreaTransformer3, IDimOffset0Transform
return BOPLayerUtil.DEEP_FROZEN_OCEAN; 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()); return Registry.BIOME.getId(BOPBiomes.wasteland.get());
} }

View File

@ -16,6 +16,8 @@ import net.minecraft.world.biome.Biomes;
import net.minecraft.world.gen.INoiseRandom; import net.minecraft.world.gen.INoiseRandom;
import net.minecraft.world.gen.layer.traits.ICastleTransformer; import net.minecraft.world.gen.layer.traits.ICastleTransformer;
import java.util.Optional;
public enum BOPShoreLayer implements ICastleTransformer public enum BOPShoreLayer implements ICastleTransformer
{ {
INSTANCE; INSTANCE;
@ -64,7 +66,7 @@ public enum BOPShoreLayer implements ICastleTransformer
if (BOPLayerUtil.isOcean(northBiomeId) || BOPLayerUtil.isOcean(eastBiomeId) || BOPLayerUtil.isOcean(southBiomeId) || BOPLayerUtil.isOcean(westBiomeId)) 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) 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) 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) 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; return biomeId;
} }
private static int getBiomeIdIfPresent(Optional<Biome> biome, int fallbackId)
{
return biome.isPresent() ? Registry.BIOME.getId(biome.get()) : fallbackId;
}
private static boolean isJungleCompatible(int biomeId) private static boolean isJungleCompatible(int biomeId)
{ {
if (Registry.BIOME.byId(biomeId) != null && (Registry.BIOME.byId(biomeId)).getBiomeCategory() == Biome.Category.JUNGLE) if (Registry.BIOME.byId(biomeId) != null && (Registry.BIOME.byId(biomeId)).getBiomeCategory() == Biome.Category.JUNGLE)

View File

@ -157,7 +157,7 @@ public enum SubBiomeLayer implements IAreaTransformer2, IDimOffset1Transformer
else if (originalBiomeId == SNOWY_TAIGA) mutatedBiomeId = SNOWY_TAIGA_HILLS; else if (originalBiomeId == SNOWY_TAIGA) mutatedBiomeId = SNOWY_TAIGA_HILLS;
//Use BOP orchard instead of vanilla forest //Use BOP orchard instead of vanilla forest
//else if (originalBiomeId == PLAINS) mutatedBiomeId = context.random(3) == 0 ? WOODED_HILLS : 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 == SNOWY_TUNDRA) mutatedBiomeId = SNOWY_MOUNTAINS;
else if (originalBiomeId == JUNGLE) mutatedBiomeId = JUNGLE_HILLS; else if (originalBiomeId == JUNGLE) mutatedBiomeId = JUNGLE_HILLS;

View File

@ -47,13 +47,16 @@ public class ModBiomes
public static void registerBiomes(RegistryEvent.Register<Biome> event) public static void registerBiomes(RegistryEvent.Register<Biome> event)
{ {
//Technical Biomes (Need to be registered before main biomes that use them) //Technical Biomes (Need to be registered before main biomes that use them)
registerBiome(new MangroveBiome(), "mangrove"); registerTechnicalBiome(new MangroveBiome(), "mangrove");
registerBiome(new GravelBeachBiome(), "gravel_beach"); registerTechnicalBiome(new GravelBeachBiome(), "gravel_beach");
registerBiome(new OriginBeachBiome(), "origin_beach"); registerTechnicalBiome(new OriginBeachBiome(), "origin_beach");
registerBiome(new WhiteBeachBiome(), "white_beach"); registerTechnicalBiome(new WhiteBeachBiome(), "white_beach");
registerBiome(new AlpsFoothillsBiome(), "alps_foothills"); registerTechnicalBiome(new AlpsFoothillsBiome(), "alps_foothills");
registerBiome(new RedwoodForestEdgeBiome(), "redwood_forest_edge"); registerTechnicalBiome(new RedwoodForestEdgeBiome(), "redwood_forest_edge");
registerBiome(new VolcanoEdgeBiome(), "volcano_edge"); registerTechnicalBiome(new VolcanoEdgeBiome(), "volcano_edge");
BiomeRegistry.configureTechnicalBiomes();
BiomeRegistry.finalizeRegistrations(BiomeRegistry.RegistrationType.TECHNICAL_BIOME);
//Overworld Biomes //Overworld Biomes
registerBiome(new AlpsBiome(), "alps"); registerBiome(new AlpsBiome(), "alps");
@ -353,6 +356,11 @@ public class ModBiomes
BiomeRegistry.deferStandardRegistration(biome, name); BiomeRegistry.deferStandardRegistration(biome, name);
} }
public static void registerTechnicalBiome(BiomeBOP biome, String name)
{
BiomeRegistry.deferTechnicalBiomeRegistration(biome, name);
}
public static void registerSubBiome(Biome parent, Optional<Biome> child, float rarity, int weight) public static void registerSubBiome(Biome parent, Optional<Biome> child, float rarity, int weight)
{ {
registerSubBiome(Optional.of(parent), child, rarity, weight); registerSubBiome(Optional.of(parent), child, rarity, weight);