diff --git a/src/main/java/biomesoplenty/common/biome/BiomeConfigData.java b/src/main/java/biomesoplenty/common/biome/BiomeConfigData.java new file mode 100644 index 000000000..404c449c6 --- /dev/null +++ b/src/main/java/biomesoplenty/common/biome/BiomeConfigData.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright 2014-2019, the Biomes O' Plenty Team + * + * This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License. + * + * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/. + ******************************************************************************/ +package biomesoplenty.common.biome; + +import biomesoplenty.api.enums.BOPClimates; +import com.google.common.collect.Maps; +import com.google.gson.annotations.SerializedName; + +import java.util.Map; + +public class BiomeConfigData +{ + @SerializedName("standard_weights") + public Map> standardBiomeWeights = Maps.newHashMap(); + + @SerializedName("sub_biome_weights") + public Map subBiomeEntries = Maps.newHashMap(); + + @SerializedName("island_biome_weights") + public Map islandBiomeEntries = Maps.newHashMap(); + + static public class SubBiomeEntry + { + public int weight; + public float rarity; + + public SubBiomeEntry(int weight, float rarity) + { + this.weight = weight; + this.rarity = rarity; + } + } + + static public class IslandBiomeEntry + { + public int weight; + + public IslandBiomeEntry(int weight) + { + this.weight = weight; + } + } +} diff --git a/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java b/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java index 3e1ca1814..906ec9025 100644 --- a/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java +++ b/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java @@ -9,16 +9,23 @@ package biomesoplenty.common.biome; import biomesoplenty.api.biome.BOPBiomes; import biomesoplenty.api.enums.BOPClimates; +import biomesoplenty.common.util.config.JsonUtil; import biomesoplenty.core.BiomesOPlenty; import biomesoplenty.init.ModBiomes; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.google.gson.reflect.TypeToken; import net.minecraft.util.registry.Registry; import net.minecraft.world.biome.Biome; import net.minecraftforge.common.BiomeManager; +import net.minecraftforge.fml.loading.FMLPaths; import net.minecraftforge.registries.ForgeRegistries; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; import java.util.Map; import java.util.Optional; @@ -26,6 +33,8 @@ import java.util.function.Consumer; public class BiomeRegistry { + private static final String CONFIG_FILE_NAME = "biome_weights.json"; + private static Map> deferrances = Maps.newHashMap(); public static void deferStandardRegistration(BiomeBOP biome, String name) @@ -43,6 +52,171 @@ public class BiomeRegistry defer(RegistrationType.ISLAND_BIOME, new IslandBiomeRegistrationData(biome, climate, weight)); } + public static void configureStandardBiomes() + { + List standardRegistrations = deferrances.get(RegistrationType.STANDARD_BIOME); + Map> defaultStandardBiomeWeights = Maps.newHashMap(); + Map regDataMap = Maps.newHashMap(); + + for (DeferredRegistration registration : standardRegistrations) + { + StandardBiomeRegistrationData regData = registration.regData; + + // Ignore biomes which don't have any weights set by default + if (!((BiomeBOP)regData.getBiome()).getWeightMap().isEmpty()) + { + defaultStandardBiomeWeights.put(registration.regData.getName(), registration.regData.getWeights()); + regDataMap.put(registration.regData.getName(), registration.regData); + } + } + + BiomeConfigData defaultConfigData = new BiomeConfigData(); + defaultConfigData.standardBiomeWeights = defaultStandardBiomeWeights; + BiomeConfigData configData = getConfigData(defaultConfigData); + + Map> revisedStandardBiomeWeights = Maps.newHashMap(defaultStandardBiomeWeights); + + // Merge the config file with the default values + for (Map.Entry> biomeEntry : configData.standardBiomeWeights.entrySet()) + { + if (revisedStandardBiomeWeights.containsKey(biomeEntry.getKey())) + { + revisedStandardBiomeWeights.put(biomeEntry.getKey(), biomeEntry.getValue()); + } + } + + // Write back to the config file + configData.standardBiomeWeights = revisedStandardBiomeWeights; + JsonUtil.writeFile(getConfigFile(), configData); + + for (Map.Entry> biomeEntry : configData.standardBiomeWeights.entrySet()) + { + String name = biomeEntry.getKey(); + Map weightMap = biomeEntry.getValue(); + + // Replace the default weight map for this biome with those from the config file + if (regDataMap.containsKey(name)) + { + StandardBiomeRegistrationData registrationData = regDataMap.get(name); + registrationData.setWeights(weightMap); + } + } + } + + public static void configureSubBiomes() + { + List subBiomeRegistrations = deferrances.get(RegistrationType.SUB_BIOME); + Map defaultSubBiomeEntries = Maps.newHashMap(); + Map regDataMap = Maps.newHashMap(); + + for (DeferredRegistration registration : subBiomeRegistrations) + { + SubBiomeRegistrationData regData = registration.regData; + String biomeName = registration.regData.getChild().delegate.name().toString(); + defaultSubBiomeEntries.put(biomeName, new BiomeConfigData.SubBiomeEntry(regData.getWeight(), regData.getRarity())); + regDataMap.put(biomeName, registration.regData); + } + + BiomeConfigData defaultConfigData = new BiomeConfigData(); + defaultConfigData.subBiomeEntries = defaultSubBiomeEntries; + BiomeConfigData configData = getConfigData(defaultConfigData); + + Map revisedSubBiomeEntries = Maps.newHashMap(defaultSubBiomeEntries); + + // Merge the config file with the default values + for (Map.Entry biomeEntry : configData.subBiomeEntries.entrySet()) + { + if (revisedSubBiomeEntries.containsKey(biomeEntry.getKey())) + { + revisedSubBiomeEntries.put(biomeEntry.getKey(), biomeEntry.getValue()); + } + } + + // Write back to the config file + configData.subBiomeEntries = revisedSubBiomeEntries; + JsonUtil.writeFile(getConfigFile(), configData); + + for (Map.Entry biomeEntry : configData.subBiomeEntries.entrySet()) + { + String name = biomeEntry.getKey(); + BiomeConfigData.SubBiomeEntry subBiomeEntry = biomeEntry.getValue(); + + // Replace the default values for this biome with those from the config file + if (regDataMap.containsKey(name)) + { + SubBiomeRegistrationData registrationData = regDataMap.get(name); + registrationData.setWeight(subBiomeEntry.weight); + registrationData.setRarity(subBiomeEntry.rarity); + } + } + } + + 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 defaultIslandBiomeEntries = Maps.newHashMap(); +// Map regDataMap = Maps.newHashMap(); +// +// for (DeferredRegistration registration : islandBiomeReistrations) +// { +// IslandBiomeRegistrationData regData = registration.regData; +// String biomeName = registration.regData.getBiome().delegate.name().toString(); +// defaultIslandBiomeEntries.put(biomeName, new BiomeConfigData.IslandBiomeEntry(regData.getWeight())); +// regDataMap.put(biomeName, registration.regData); +// } +// +// BiomeConfigData defaultConfigData = new BiomeConfigData(); +// defaultConfigData.islandBiomeEntries = defaultIslandBiomeEntries; +// BiomeConfigData configData = getConfigData(defaultConfigData); +// +// Map revisedIslandBiomeEntries = Maps.newHashMap(defaultIslandBiomeEntries); +// +// // Merge the config file with the default values +// for (Map.Entry biomeEntry : configData.islandBiomeEntries.entrySet()) +// { +// if (revisedIslandBiomeEntries.containsKey(biomeEntry.getKey())) +// { +// revisedIslandBiomeEntries.put(biomeEntry.getKey(), biomeEntry.getValue()); +// } +// } +// +// // Write back to the config file +// configData.islandBiomeEntries = revisedIslandBiomeEntries; +// JsonUtil.writeFile(getConfigFile(), configData); +// +// for (Map.Entry biomeEntry : configData.islandBiomeEntries.entrySet()) +// { +// String name = biomeEntry.getKey(); +// BiomeConfigData.IslandBiomeEntry islandBiomeEntry = biomeEntry.getValue(); +// +// // Replace the default values for this biome with those from the config file +// if (regDataMap.containsKey(name)) +// { +// IslandBiomeRegistrationData registrationData = regDataMap.get(name); +// registrationData.setWeight(islandBiomeEntry.weight); +// } +// } + } + + private static File getConfigDirFile() + { + Path configPath = FMLPaths.CONFIGDIR.get(); + Path bopConfigPath = Paths.get(configPath.toAbsolutePath().toString(), "biomesoplenty"); + return bopConfigPath.toFile(); + } + + private static File getConfigFile() + { + return new File(getConfigDirFile(), CONFIG_FILE_NAME); + } + + private static BiomeConfigData getConfigData(BiomeConfigData defaultConfigData) + { + BiomeConfigData configData = JsonUtil.getOrCreateConfigFile(getConfigDirFile(), CONFIG_FILE_NAME, defaultConfigData, new TypeToken(){}.getType()); + return configData; + } + private static void defer(RegistrationType type, T data) { if (!deferrances.containsKey(type)) @@ -72,7 +246,7 @@ public class BiomeRegistry // Don't register biomes with their weight set to 0, that normally have weights that are non-zero if (!biome.getWeightMap().isEmpty() && (data.weightMap.isEmpty() || data.weightMap.entrySet().stream().allMatch((entry) -> entry.getValue().equals(0)))) { - BiomesOPlenty.logger.warn("Weights absent for " + data.getName() + ", disabling..."); + BiomesOPlenty.logger.info("Weights absent for " + data.getName() + ", disabling..."); return; } @@ -90,6 +264,7 @@ public class BiomeRegistry { BOPClimates climate = entry.getKey(); int weight = entry.getValue(); + BiomesOPlenty.logger.info(String.format("%s weight set to %d for climate %s", name, weight, climate.name())); climate.addBiome(weight, biome); } } @@ -111,6 +286,8 @@ public class BiomeRegistry return; } + String childName = data.getChild().delegate.name().toString(); + BiomesOPlenty.logger.info(String.format("Sub biome %s weight set to %d", childName, data.getWeight())); ModBiomes.subBiomes.put(Registry.BIOME.getId(data.getParent()), new ModBiomes.WeightedSubBiome(data.getChild(), data.getRarity(), data.getWeight())); }), ISLAND_BIOME((IslandBiomeRegistrationData data) -> { @@ -120,6 +297,8 @@ public class BiomeRegistry return; } + String biomeName = data.getBiome().delegate.name().toString(); + BiomesOPlenty.logger.info(String.format("Island biome %s weight set to %d for climate %s", biomeName, data.getWeight(), data.getClimate().name())); ModBiomes.islandBiomeIds.add(Registry.BIOME.getId(data.getBiome())); data.getClimate().addIslandBiome(data.getWeight(), data.getBiome()); }); @@ -150,7 +329,7 @@ public class BiomeRegistry private static class StandardBiomeRegistrationData extends RegistrationData { private final String name; - private final Map weightMap; + private Map weightMap; public StandardBiomeRegistrationData(BiomeBOP biome, String name) { @@ -169,6 +348,11 @@ public class BiomeRegistry return ImmutableMap.copyOf(this.weightMap); } + public void setWeights(Map weights) + { + this.weightMap = weights; + } + public int getWeight(BOPClimates climate) { return this.weightMap.get(climate); diff --git a/src/main/java/biomesoplenty/common/util/config/JsonUtil.java b/src/main/java/biomesoplenty/common/util/config/JsonUtil.java index a9f3c6281..112e7bbec 100644 --- a/src/main/java/biomesoplenty/common/util/config/JsonUtil.java +++ b/src/main/java/biomesoplenty/common/util/config/JsonUtil.java @@ -41,7 +41,7 @@ public class JsonUtil return null; } - protected static boolean writeFile(File outputFile, Object obj) + public static boolean writeFile(File outputFile, Object obj) { try { diff --git a/src/main/java/biomesoplenty/init/ModBiomes.java b/src/main/java/biomesoplenty/init/ModBiomes.java index 79e0a92aa..4981ea136 100644 --- a/src/main/java/biomesoplenty/init/ModBiomes.java +++ b/src/main/java/biomesoplenty/init/ModBiomes.java @@ -124,6 +124,7 @@ public class ModBiomes registerBiome(new UndergardenBiome(), "undergarden"); registerBiome(new VisceralHeapBiome(), "visceral_heap"); + BiomeRegistry.configureStandardBiomes(); BiomeRegistry.finalizeRegistrations(BiomeRegistry.RegistrationType.STANDARD_BIOME); //Sub/Island Biomes (Note: Rarity supports two decimal places) @@ -137,6 +138,7 @@ public class ModBiomes registerSubBiome(snowy_coniferous_forest, snowy_fir_clearing, 0.5F, 100); registerSubBiome(temperate_rainforest, temperate_rainforest_hills, 0.8F, 100); + BiomeRegistry.configureSubBiomes(); BiomeRegistry.finalizeRegistrations(BiomeRegistry.RegistrationType.SUB_BIOME); registerIslandBiome(origin_hills, BOPClimates.COOL_TEMPERATE, 50); @@ -155,6 +157,7 @@ public class ModBiomes registerIslandBiome(tropics, BOPClimates.TROPICAL, 50); registerIslandBiome(tropics, BOPClimates.HOT_DESERT, 50); + //BiomeRegistry.configureIslandBiomes(); BiomeRegistry.finalizeRegistrations(BiomeRegistry.RegistrationType.ISLAND_BIOME); registerBiomeDictionaryTags();