From 8fbe69e163de71486cbfd0b469134f3c6f007dc9 Mon Sep 17 00:00:00 2001 From: Adubbz Date: Fri, 14 Feb 2020 11:01:09 +1100 Subject: [PATCH] Simplified standard biome weights config --- .../biomesoplenty/common/biome/BiomeBOP.java | 5 ++ .../common/biome/BiomeConfigData.java | 21 ++++-- .../common/biome/BiomeRegistry.java | 66 +++++++++++++++---- .../common/biome/NetherBiomeBOP.java | 5 -- 4 files changed, 73 insertions(+), 24 deletions(-) diff --git a/src/main/java/biomesoplenty/common/biome/BiomeBOP.java b/src/main/java/biomesoplenty/common/biome/BiomeBOP.java index b5322ac67..fd60fcfb1 100644 --- a/src/main/java/biomesoplenty/common/biome/BiomeBOP.java +++ b/src/main/java/biomesoplenty/common/biome/BiomeBOP.java @@ -76,4 +76,9 @@ public class BiomeBOP extends Biome { return this.weightMap; } + + public boolean hasWeights() + { + return !this.weightMap.isEmpty() && !this.weightMap.entrySet().stream().allMatch((entry) -> entry.getValue().equals(0)); + } } diff --git a/src/main/java/biomesoplenty/common/biome/BiomeConfigData.java b/src/main/java/biomesoplenty/common/biome/BiomeConfigData.java index 404c449c6..5e75e1d05 100644 --- a/src/main/java/biomesoplenty/common/biome/BiomeConfigData.java +++ b/src/main/java/biomesoplenty/common/biome/BiomeConfigData.java @@ -8,6 +8,7 @@ package biomesoplenty.common.biome; import biomesoplenty.api.enums.BOPClimates; +import biomesoplenty.core.BiomesOPlenty; import com.google.common.collect.Maps; import com.google.gson.annotations.SerializedName; @@ -16,15 +17,25 @@ import java.util.Map; public class BiomeConfigData { @SerializedName("standard_weights") - public Map> standardBiomeWeights = Maps.newHashMap(); + public Map standardBiomeWeights = Maps.newHashMap(); @SerializedName("sub_biome_weights") public Map subBiomeEntries = Maps.newHashMap(); - @SerializedName("island_biome_weights") - public Map islandBiomeEntries = Maps.newHashMap(); + //@SerializedName("island_biome_weights") + //public Map islandBiomeEntries = Maps.newHashMap(); - static public class SubBiomeEntry + public static class StandardBiomeEntry + { + public int weight; + + public StandardBiomeEntry(int weight) + { + this.weight = weight; + } + } + + public static class SubBiomeEntry { public int weight; public float rarity; @@ -36,7 +47,7 @@ public class BiomeConfigData } } - static public class IslandBiomeEntry + public static class IslandBiomeEntry { public int weight; diff --git a/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java b/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java index fb985fa12..6a1f6e69a 100644 --- a/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java +++ b/src/main/java/biomesoplenty/common/biome/BiomeRegistry.java @@ -15,6 +15,7 @@ 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.common.collect.Sets; import com.google.gson.reflect.TypeToken; import net.minecraft.util.ResourceLocation; import net.minecraft.util.registry.Registry; @@ -22,6 +23,8 @@ import net.minecraft.world.biome.Biome; import net.minecraftforge.common.BiomeManager; import net.minecraftforge.fml.loading.FMLPaths; import net.minecraftforge.registries.ForgeRegistries; +import org.apache.commons.lang3.tuple.Pair; +import org.lwjgl.system.CallbackI; import java.io.File; @@ -30,6 +33,7 @@ import java.nio.file.Paths; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.function.Consumer; public class BiomeRegistry @@ -56,7 +60,7 @@ public class BiomeRegistry public static void configureStandardBiomes() { List standardRegistrations = deferrances.get(RegistrationType.STANDARD_BIOME); - Map> defaultStandardBiomeWeights = Maps.newHashMap(); + Map defaultEntries = Maps.newHashMap(); Map regDataMap = Maps.newHashMap(); for (DeferredRegistration registration : standardRegistrations) @@ -64,22 +68,23 @@ public class BiomeRegistry StandardBiomeRegistrationData regData = registration.regData; // Ignore biomes which don't have any weights set by default - if (!((BiomeBOP)regData.getBiome()).getWeightMap().isEmpty()) + if (((BiomeBOP)regData.getBiome()).hasWeights()) { String biomeName = new ResourceLocation(BiomesOPlenty.MOD_ID, regData.getName()).toString(); - defaultStandardBiomeWeights.put(biomeName, registration.regData.getWeights()); + Pair primaryWeight = regData.getPrimaryWeight(); + defaultEntries.put(biomeName, new BiomeConfigData.StandardBiomeEntry(primaryWeight.getValue())); regDataMap.put(biomeName, registration.regData); } } BiomeConfigData defaultConfigData = new BiomeConfigData(); - defaultConfigData.standardBiomeWeights = defaultStandardBiomeWeights; + defaultConfigData.standardBiomeWeights = defaultEntries; BiomeConfigData configData = getConfigData(defaultConfigData); - Map> revisedStandardBiomeWeights = Maps.newHashMap(defaultStandardBiomeWeights); + Map revisedStandardBiomeWeights = Maps.newHashMap(defaultEntries); // Merge the config file with the default values - for (Map.Entry> biomeEntry : configData.standardBiomeWeights.entrySet()) + for (Map.Entry biomeEntry : configData.standardBiomeWeights.entrySet()) { if (revisedStandardBiomeWeights.containsKey(biomeEntry.getKey())) { @@ -91,16 +96,16 @@ public class BiomeRegistry configData.standardBiomeWeights = revisedStandardBiomeWeights; JsonUtil.writeFile(getConfigFile(), configData); - for (Map.Entry> biomeEntry : configData.standardBiomeWeights.entrySet()) + for (Map.Entry biomeEntry : configData.standardBiomeWeights.entrySet()) { String name = biomeEntry.getKey(); - Map weightMap = biomeEntry.getValue(); + BiomeConfigData.StandardBiomeEntry weight = 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); + registrationData.setPrimaryWeight(weight.weight); } } } @@ -233,6 +238,20 @@ public class BiomeRegistry if (!deferrances.containsKey(type)) return; + if (type == RegistrationType.SUB_BIOME) + { + Set children = Sets.newHashSet(); + deferrances.get(RegistrationType.SUB_BIOME).forEach((reg) -> { + Biome biome = ((SubBiomeRegistrationData)reg.regData).getChild(); + if (children.contains(biome)) + { + throw new RuntimeException(String.format("Sub biome %s cannot be added to multiple parents", biome.delegate.name().toString())); + } + children.add(biome); + }); + + } + for (DeferredRegistration reg : deferrances.get(type)) { reg.register(); @@ -338,6 +357,7 @@ public class BiomeRegistry super(biome); this.name = name; this.weightMap = Maps.newHashMap(biome.getWeightMap()); + this.ensureSingleWeight(); } public String getName() @@ -350,11 +370,6 @@ 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); @@ -363,6 +378,29 @@ public class BiomeRegistry public void setWeight(BOPClimates climate, int weight) { this.weightMap.put(climate, weight); + this.ensureSingleWeight(); + } + + public Pair getPrimaryWeight() + { + List> pairs = Lists.newArrayList(); + this.weightMap.entrySet().forEach((entry) -> pairs.add(Pair.of(entry.getKey(), entry.getValue()))); + return pairs.get(0); + } + + public void setPrimaryWeight(int value) + { + BOPClimates climate = this.getPrimaryWeight().getKey(); + this.setWeight(climate, value); + } + + // This limitation is enforced for config file simplicity, and because we don't need it at this time + private void ensureSingleWeight() + { + if (this.weightMap.size() > 1) + { + throw new RuntimeException(String.format("%s cannot be assigned to multiple climates!\n%s", new ResourceLocation(BiomesOPlenty.MOD_ID, name).toString(), this.weightMap)); + } } } diff --git a/src/main/java/biomesoplenty/common/biome/NetherBiomeBOP.java b/src/main/java/biomesoplenty/common/biome/NetherBiomeBOP.java index ebbe3b9e2..b3258a459 100644 --- a/src/main/java/biomesoplenty/common/biome/NetherBiomeBOP.java +++ b/src/main/java/biomesoplenty/common/biome/NetherBiomeBOP.java @@ -14,11 +14,6 @@ import java.util.Map; public class NetherBiomeBOP extends BiomeBOP { - protected Map weightMap = new HashMap(); - public boolean canSpawnInBiome; - public int beachBiomeId = -1; - public int riverBiomeId = -1; - public NetherBiomeBOP(Builder builder) { super(builder);