Preliminary biome weight config file implementation

This commit is contained in:
Adubbz 2020-02-11 19:32:56 +11:00
parent 656399c37d
commit 9a8f7727b4
4 changed files with 238 additions and 3 deletions

View file

@ -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<String, Map<BOPClimates, Integer>> standardBiomeWeights = Maps.newHashMap();
@SerializedName("sub_biome_weights")
public Map<String, SubBiomeEntry> subBiomeEntries = Maps.newHashMap();
@SerializedName("island_biome_weights")
public Map<String, IslandBiomeEntry> 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;
}
}
}

View file

@ -9,16 +9,23 @@ package biomesoplenty.common.biome;
import biomesoplenty.api.biome.BOPBiomes; import biomesoplenty.api.biome.BOPBiomes;
import biomesoplenty.api.enums.BOPClimates; import biomesoplenty.api.enums.BOPClimates;
import biomesoplenty.common.util.config.JsonUtil;
import biomesoplenty.core.BiomesOPlenty; import biomesoplenty.core.BiomesOPlenty;
import biomesoplenty.init.ModBiomes; import biomesoplenty.init.ModBiomes;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.gson.reflect.TypeToken;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import net.minecraft.world.biome.Biome; import net.minecraft.world.biome.Biome;
import net.minecraftforge.common.BiomeManager; import net.minecraftforge.common.BiomeManager;
import net.minecraftforge.fml.loading.FMLPaths;
import net.minecraftforge.registries.ForgeRegistries; 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.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -26,6 +33,8 @@ import java.util.function.Consumer;
public class BiomeRegistry public class BiomeRegistry
{ {
private static final String CONFIG_FILE_NAME = "biome_weights.json";
private static Map<RegistrationType, List<DeferredRegistration>> deferrances = Maps.newHashMap(); private static Map<RegistrationType, List<DeferredRegistration>> deferrances = Maps.newHashMap();
public static void deferStandardRegistration(BiomeBOP biome, String name) public static void deferStandardRegistration(BiomeBOP biome, String name)
@ -43,6 +52,171 @@ public class BiomeRegistry
defer(RegistrationType.ISLAND_BIOME, new IslandBiomeRegistrationData(biome, climate, weight)); defer(RegistrationType.ISLAND_BIOME, new IslandBiomeRegistrationData(biome, climate, weight));
} }
public static void configureStandardBiomes()
{
List<DeferredRegistration> standardRegistrations = deferrances.get(RegistrationType.STANDARD_BIOME);
Map<String, Map<BOPClimates, Integer>> defaultStandardBiomeWeights = Maps.newHashMap();
Map<String, StandardBiomeRegistrationData> regDataMap = Maps.newHashMap();
for (DeferredRegistration<StandardBiomeRegistrationData> 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<String, Map<BOPClimates, Integer>> revisedStandardBiomeWeights = Maps.newHashMap(defaultStandardBiomeWeights);
// Merge the config file with the default values
for (Map.Entry<String, Map<BOPClimates, Integer>> 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<String, Map<BOPClimates, Integer>> biomeEntry : configData.standardBiomeWeights.entrySet())
{
String name = biomeEntry.getKey();
Map<BOPClimates, Integer> 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<DeferredRegistration> subBiomeRegistrations = deferrances.get(RegistrationType.SUB_BIOME);
Map<String, BiomeConfigData.SubBiomeEntry> defaultSubBiomeEntries = Maps.newHashMap();
Map<String, SubBiomeRegistrationData> regDataMap = Maps.newHashMap();
for (DeferredRegistration<SubBiomeRegistrationData> 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<String, BiomeConfigData.SubBiomeEntry> revisedSubBiomeEntries = Maps.newHashMap(defaultSubBiomeEntries);
// Merge the config file with the default values
for (Map.Entry<String, BiomeConfigData.SubBiomeEntry> 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<String, BiomeConfigData.SubBiomeEntry> 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<DeferredRegistration> islandBiomeReistrations = deferrances.get(RegistrationType.ISLAND_BIOME);
// Map<String, BiomeConfigData.IslandBiomeEntry> defaultIslandBiomeEntries = Maps.newHashMap();
// Map<String, IslandBiomeRegistrationData> regDataMap = Maps.newHashMap();
//
// for (DeferredRegistration<IslandBiomeRegistrationData> 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<String, BiomeConfigData.IslandBiomeEntry> revisedIslandBiomeEntries = Maps.newHashMap(defaultIslandBiomeEntries);
//
// // Merge the config file with the default values
// for (Map.Entry<String, BiomeConfigData.IslandBiomeEntry> 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<String, BiomeConfigData.IslandBiomeEntry> 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<BiomeConfigData>(){}.getType());
return configData;
}
private static <T extends RegistrationData> void defer(RegistrationType type, T data) private static <T extends RegistrationData> void defer(RegistrationType type, T data)
{ {
if (!deferrances.containsKey(type)) 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 // 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)))) 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; return;
} }
@ -90,6 +264,7 @@ public class BiomeRegistry
{ {
BOPClimates climate = entry.getKey(); BOPClimates climate = entry.getKey();
int weight = entry.getValue(); 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); climate.addBiome(weight, biome);
} }
} }
@ -111,6 +286,8 @@ public class BiomeRegistry
return; 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())); ModBiomes.subBiomes.put(Registry.BIOME.getId(data.getParent()), new ModBiomes.WeightedSubBiome(data.getChild(), data.getRarity(), data.getWeight()));
}), }),
ISLAND_BIOME((IslandBiomeRegistrationData data) -> { ISLAND_BIOME((IslandBiomeRegistrationData data) -> {
@ -120,6 +297,8 @@ public class BiomeRegistry
return; 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())); ModBiomes.islandBiomeIds.add(Registry.BIOME.getId(data.getBiome()));
data.getClimate().addIslandBiome(data.getWeight(), data.getBiome()); data.getClimate().addIslandBiome(data.getWeight(), data.getBiome());
}); });
@ -150,7 +329,7 @@ public class BiomeRegistry
private static class StandardBiomeRegistrationData extends RegistrationData private static class StandardBiomeRegistrationData extends RegistrationData
{ {
private final String name; private final String name;
private final Map<BOPClimates, Integer> weightMap; private Map<BOPClimates, Integer> weightMap;
public StandardBiomeRegistrationData(BiomeBOP biome, String name) public StandardBiomeRegistrationData(BiomeBOP biome, String name)
{ {
@ -169,6 +348,11 @@ public class BiomeRegistry
return ImmutableMap.copyOf(this.weightMap); return ImmutableMap.copyOf(this.weightMap);
} }
public void setWeights(Map<BOPClimates, Integer> weights)
{
this.weightMap = weights;
}
public int getWeight(BOPClimates climate) public int getWeight(BOPClimates climate)
{ {
return this.weightMap.get(climate); return this.weightMap.get(climate);

View file

@ -41,7 +41,7 @@ public class JsonUtil
return null; return null;
} }
protected static boolean writeFile(File outputFile, Object obj) public static boolean writeFile(File outputFile, Object obj)
{ {
try try
{ {

View file

@ -124,6 +124,7 @@ public class ModBiomes
registerBiome(new UndergardenBiome(), "undergarden"); registerBiome(new UndergardenBiome(), "undergarden");
registerBiome(new VisceralHeapBiome(), "visceral_heap"); registerBiome(new VisceralHeapBiome(), "visceral_heap");
BiomeRegistry.configureStandardBiomes();
BiomeRegistry.finalizeRegistrations(BiomeRegistry.RegistrationType.STANDARD_BIOME); BiomeRegistry.finalizeRegistrations(BiomeRegistry.RegistrationType.STANDARD_BIOME);
//Sub/Island Biomes (Note: Rarity supports two decimal places) //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(snowy_coniferous_forest, snowy_fir_clearing, 0.5F, 100);
registerSubBiome(temperate_rainforest, temperate_rainforest_hills, 0.8F, 100); registerSubBiome(temperate_rainforest, temperate_rainforest_hills, 0.8F, 100);
BiomeRegistry.configureSubBiomes();
BiomeRegistry.finalizeRegistrations(BiomeRegistry.RegistrationType.SUB_BIOME); BiomeRegistry.finalizeRegistrations(BiomeRegistry.RegistrationType.SUB_BIOME);
registerIslandBiome(origin_hills, BOPClimates.COOL_TEMPERATE, 50); registerIslandBiome(origin_hills, BOPClimates.COOL_TEMPERATE, 50);
@ -155,6 +157,7 @@ public class ModBiomes
registerIslandBiome(tropics, BOPClimates.TROPICAL, 50); registerIslandBiome(tropics, BOPClimates.TROPICAL, 50);
registerIslandBiome(tropics, BOPClimates.HOT_DESERT, 50); registerIslandBiome(tropics, BOPClimates.HOT_DESERT, 50);
//BiomeRegistry.configureIslandBiomes();
BiomeRegistry.finalizeRegistrations(BiomeRegistry.RegistrationType.ISLAND_BIOME); BiomeRegistry.finalizeRegistrations(BiomeRegistry.RegistrationType.ISLAND_BIOME);
registerBiomeDictionaryTags(); registerBiomeDictionaryTags();