From d15feb2e074075c24850c890d222bdb34bfd42cd Mon Sep 17 00:00:00 2001 From: dfsek Date: Sat, 3 Oct 2020 02:40:13 -0700 Subject: [PATCH] Split biome config into many classes --- .../genconfig/biome/AbstractBiomeConfig.java | 25 ++-- .../genconfig/biome/BiomeCarverConfig.java | 48 ++++++++ .../config/genconfig/biome/BiomeConfig.java | 112 ++++++------------ .../genconfig/biome/BiomeOreConfig.java | 44 +++++++ .../genconfig/biome/BiomePaletteConfig.java | 5 +- .../genconfig/biome/BiomeTreeConfig.java | 36 ++++++ 6 files changed, 176 insertions(+), 94 deletions(-) create mode 100644 src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeCarverConfig.java create mode 100644 src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeOreConfig.java create mode 100644 src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeTreeConfig.java diff --git a/src/main/java/com/dfsek/terra/config/genconfig/biome/AbstractBiomeConfig.java b/src/main/java/com/dfsek/terra/config/genconfig/biome/AbstractBiomeConfig.java index 759f7f2a9..c8d17f9e6 100644 --- a/src/main/java/com/dfsek/terra/config/genconfig/biome/AbstractBiomeConfig.java +++ b/src/main/java/com/dfsek/terra/config/genconfig/biome/AbstractBiomeConfig.java @@ -38,6 +38,9 @@ public class AbstractBiomeConfig extends TerraConfig { private List structureConfigs; private BiomePaletteConfig palette; private BiomeFloraConfig flora; + private BiomeCarverConfig carving; + private BiomeTreeConfig trees; + private BiomeOreConfig ores; public AbstractBiomeConfig(File file, ConfigPack config) throws IOException, InvalidConfigurationException { super(file, config); @@ -45,15 +48,15 @@ public class AbstractBiomeConfig extends TerraConfig { if(!contains("id")) throw new ConfigException("Abstract Biome ID unspecified!", "null"); this.biomeID = getString("id"); - if(contains("carving")) carvingData = getMapList("carving"); + if(contains("carving")) carving = new BiomeCarverConfig(this); if(contains("palette")) palette = new BiomePaletteConfig(this); if(contains("flora")) flora = new BiomeFloraConfig(this); - if(contains("trees")) treeData = Objects.requireNonNull(getConfigurationSection("trees")).getValues(false); + if(contains("trees")) trees = new BiomeTreeConfig(this); - if(contains("ores")) oreData = Objects.requireNonNull(getConfigurationSection("ores")).getValues(false); + if(contains("ores")) ores = new BiomeOreConfig(this); floraChance = getInt("flora-chance", 0); floraAttempts = getInt("flora-attempts", 1); @@ -141,20 +144,16 @@ public class AbstractBiomeConfig extends TerraConfig { return flora; } - public Map getFloraData() { - return floraData; + public BiomeCarverConfig getCarving() { + return carving; } - public Map getOreData() { - return oreData; + public BiomeTreeConfig getTrees() { + return trees; } - public Map getTreeData() { - return treeData; - } - - public List> getCarvingData() { - return carvingData; + public BiomeOreConfig getOres() { + return ores; } public int getSeaLevel() { diff --git a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeCarverConfig.java b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeCarverConfig.java new file mode 100644 index 000000000..e60925b1d --- /dev/null +++ b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeCarverConfig.java @@ -0,0 +1,48 @@ +package com.dfsek.terra.config.genconfig.biome; + +import com.dfsek.terra.Debug; +import com.dfsek.terra.config.TerraConfig; +import com.dfsek.terra.config.TerraConfigSection; +import com.dfsek.terra.config.exception.ConfigException; +import com.dfsek.terra.config.exception.NotFoundException; +import com.dfsek.terra.config.genconfig.CarverConfig; +import org.bukkit.Bukkit; +import org.bukkit.block.data.BlockData; +import org.bukkit.configuration.InvalidConfigurationException; +import org.polydev.gaea.math.ProbabilityCollection; +import org.polydev.gaea.world.palette.Palette; +import org.polydev.gaea.world.palette.RandomPalette; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.TreeMap; + +public class BiomeCarverConfig extends TerraConfigSection { + private final Map carvers = new HashMap<>(); + @SuppressWarnings("unchecked") + public BiomeCarverConfig(TerraConfig parent) throws InvalidConfigurationException { + super(parent); + List> cfg = parent.getMapList("carving"); + if(cfg.size() == 0) return; + for(Map e : cfg) { + for(Map.Entry entry : e.entrySet()) { + try { + CarverConfig c = parent.getConfig().getCarver((String) entry.getKey()); + if(c == null) throw new NotFoundException("Carver", (String) entry.getKey(), parent.getID()); + Debug.info("Got carver " + c + ". Adding with weight " + entry.getValue()); + carvers.put(c, (Integer) entry.getValue()); + } catch(ClassCastException ex) { + throw new ConfigException("Unable to parse Carver configuration! Check YAML syntax.", parent.getID()); + } catch(NullPointerException ex) { + throw new NotFoundException("carver", (String) entry.getKey(), parent.getID()); + } + } + } + } + + public Map getCarvers() { + return carvers; + } +} diff --git a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeConfig.java b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeConfig.java index dec5181e9..5d89785ae 100644 --- a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeConfig.java +++ b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeConfig.java @@ -85,48 +85,9 @@ public class BiomeConfig extends TerraConfig { } } - TreeMap> paletteMap; - // Check if biome is extending abstract biome, only use abstract biome's palette if palette is NOT defined for current biome. - if(extending && abstractBiome.getPaletteData() != null && ! contains("palette")) { - paletteMap = abstractBiome.getPaletteData().getPaletteMap(); - Debug.info("Using super palette"); - } else paletteMap = new BiomePaletteConfig(this).getPaletteMap(); - if(paletteMap == null) throw new ConfigException("No Palette specified in biome or super biome.", getID()); - - // Check if carving should be handled by super biome. - List> carvingData; - try { - if(extending && abstractBiome.getCarvingData() != null && ! contains("carving")) { - carvingData = abstractBiome.getCarvingData(); - Debug.info("Using super carvers"); - } else carvingData = getMapList("carving"); - } catch(NullPointerException e) { - carvingData = null; - } - - carvers = new HashMap<>(); - if(carvingData != null) { - for(Map e : carvingData) { - for(Map.Entry entry : e.entrySet()) { - try { - CarverConfig c = getConfig().getCarver((String) entry.getKey()); - if(c == null) throw new NotFoundException("Carver", (String) entry.getKey(), getID()); - Debug.info("Got carver " + c + ". Adding with weight " + entry.getValue()); - carvers.put(c, (Integer) entry.getValue()); - } catch(ClassCastException ex) { - throw new ConfigException("Unable to parse Carver configuration! Check YAML syntax.", getID()); - } catch(NullPointerException ex) { - throw new NotFoundException("carver", (String) entry.getKey(), getID()); - } - } - } - } - - int floraChance, treeChance, treeDensity; - // Get various simple values using getOrDefault config methods. float floraFreq; - int floraSeed; + int floraSeed, floraChance, treeChance, treeDensity; try { slabThreshold = getDouble("slabs.threshold", Objects.requireNonNull(abstractBiome).getSlabThreshold()); floraChance = getInt("flora-chance", Objects.requireNonNull(abstractBiome).getFloraChance()); @@ -157,28 +118,45 @@ public class BiomeConfig extends TerraConfig { floraNoise.setFrequency(floraFreq); } + TreeMap> paletteMap; + // Check if biome is extending abstract biome, only use abstract biome's palette if palette is NOT defined for current biome. + if(extending && abstractBiome.getPaletteData() != null && ! contains("palette")) { + paletteMap = abstractBiome.getPaletteData().getPaletteMap(); + Debug.info("Using super palette"); + } else paletteMap = new BiomePaletteConfig(this).getPaletteMap(); + + // Palette must not be null + if(paletteMap == null) throw new ConfigException("No Palette specified in biome or super biome.", getID()); + + // Check if carving should be handled by super biome. + if(extending && abstractBiome.getCarving() != null && ! contains("carving")) { + carvers = abstractBiome.getCarving().getCarvers(); + Debug.info("Using super carvers"); + } else carvers = new BiomeCarverConfig(this).getCarvers(); + // Check if flora should be handled by super biome. - if(extending && abstractBiome.getFloraData() != null && ! contains("flora")) { + if(extending && abstractBiome.getFlora() != null && ! contains("flora")) { flora = abstractBiome.getFlora(); Debug.info("Using super flora (" + flora.getFlora().size() + " entries, " + floraChance + " % chance)"); } else flora = new BiomeFloraConfig(this); // Check if trees should be handled by super biome. - Map treeData; - ProbabilityCollection trees = new ProbabilityCollection<>(); - try { - if(extending && abstractBiome.getTreeData() != null && ! contains("trees")) { - treeData = abstractBiome.getTreeData(); - Debug.info("Using super trees"); - } else treeData = Objects.requireNonNull(getConfigurationSection("trees")).getValues(false); - } catch(NullPointerException e) { - treeData = null; + ProbabilityCollection trees; + if(extending && abstractBiome.getTrees() != null && ! contains("trees")) { + trees = abstractBiome.getTrees().getTrees(); + Debug.info("Using super trees"); + } else trees = new BiomeTreeConfig(this).getTrees(); + + // Check if ores should be handled by super biome. + if(extending && abstractBiome.getOres() != null && ! contains("ores")) { + oreHeights = abstractBiome.getOres().getOreHeights(); + ores = abstractBiome.getOres().getOres(); + Debug.info("Using super ores"); + } else { + BiomeOreConfig oreConfig = new BiomeOreConfig(this); + oreHeights = oreConfig.getOreHeights(); + ores = oreConfig.getOres(); } - if(treeData != null) { - for(Map.Entry e : treeData.entrySet()) { - trees.add(TreeType.valueOf(e.getKey()), (Integer) e.getValue()); - } - } else trees = new ProbabilityCollection<>(); //Make sure equation is non-null if(eq == null || eq.equals("")) throw new ConfigException("Could not find noise equation! Biomes must include a noise equation, or extend an abstract biome with one.", getID()); @@ -195,30 +173,6 @@ public class BiomeConfig extends TerraConfig { throw new ConfigException("Invalid Vanilla biome: \"" + getString("vanilla") + "\"", getID()); } - // Check if ores should be handled by super biome. - oreHeights = new HashMap<>(); - ores = new HashMap<>(); - Map oreData; - try { - if(extending && abstractBiome.getOreData() != null && ! contains("ores")) { - oreData = abstractBiome.getOreData(); - Debug.info("Using super ores"); - } else oreData = Objects.requireNonNull(getConfigurationSection("ores")).getValues(false); - } catch(NullPointerException e) { - oreData = null; - } - if(oreData != null) { - for(Map.Entry m : oreData.entrySet()) { - OreConfig ore = config.getOre(m.getKey()); - if(ore == null) throw new NotFoundException("Ore", m.getKey(), getID()); - ores.put(ore, new Range(((ConfigurationSection) m.getValue()).getInt("min"), ((ConfigurationSection) m.getValue()).getInt("max"))); - oreHeights.put(ore, new Range(((ConfigurationSection) m.getValue()).getInt("min-height"), ((ConfigurationSection) m.getValue()).getInt("max-height"))); - } - } else { - ores = new HashMap<>(); - oreHeights = new HashMap<>(); - } - // Ocean stuff String oceanPalette; try { diff --git a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeOreConfig.java b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeOreConfig.java new file mode 100644 index 000000000..20a86216c --- /dev/null +++ b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeOreConfig.java @@ -0,0 +1,44 @@ +package com.dfsek.terra.config.genconfig.biome; + +import com.dfsek.terra.config.TerraConfig; +import com.dfsek.terra.config.TerraConfigSection; +import com.dfsek.terra.config.base.ConfigUtil; +import com.dfsek.terra.config.exception.ConfigException; +import com.dfsek.terra.config.exception.NotFoundException; +import com.dfsek.terra.config.genconfig.OreConfig; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.InvalidConfigurationException; +import org.polydev.gaea.math.Range; + +import java.util.HashMap; +import java.util.Map; + +public class BiomeOreConfig extends TerraConfigSection { + private final Map ores = new HashMap<>(); + private final Map oreHeights = new HashMap<>(); + public BiomeOreConfig(TerraConfig parent) throws InvalidConfigurationException { + super(parent); + ConfigurationSection c = parent.getConfigurationSection("ores"); + if(c == null) return; + Map cfg = c.getValues(false); + try { + for(Map.Entry m : cfg.entrySet()) { + OreConfig ore = parent.getConfig().getOre(m.getKey()); + if(ore == null) throw new NotFoundException("Ore", m.getKey(), parent.getID()); + ores.put(ore, new Range(((ConfigurationSection) m.getValue()).getInt("min"), ((ConfigurationSection) m.getValue()).getInt("max"))); + oreHeights.put(ore, new Range(((ConfigurationSection) m.getValue()).getInt("min-height"), ((ConfigurationSection) m.getValue()).getInt("max-height"))); + } + } catch(ClassCastException e) { + if(ConfigUtil.debug) e.printStackTrace(); + throw new ConfigException("Unable to parse Flora configuration! Check YAML syntax.", parent.getID()); + } + } + + public Map getOres() { + return ores; + } + + public Map getOreHeights() { + return oreHeights; + } +} diff --git a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomePaletteConfig.java b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomePaletteConfig.java index 35f67e0bb..0d3645e10 100644 --- a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomePaletteConfig.java +++ b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomePaletteConfig.java @@ -18,12 +18,13 @@ import java.util.Random; import java.util.TreeMap; public class BiomePaletteConfig extends TerraConfigSection { - private final TreeMap> paletteMap = new TreeMap<>(); + private TreeMap> paletteMap; @SuppressWarnings("unchecked") public BiomePaletteConfig(TerraConfig parent) throws InvalidConfigurationException { super(parent); List> cfg = parent.getMapList("palette"); - if(cfg.size() == 0) throw new ConfigException("Palette unspecified.", parent.getID()); + if(cfg.size() == 0) return; + paletteMap = new TreeMap<>(); for(Map e : cfg) { for(Map.Entry entry : e.entrySet()) { try { diff --git a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeTreeConfig.java b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeTreeConfig.java new file mode 100644 index 000000000..dc012d74c --- /dev/null +++ b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeTreeConfig.java @@ -0,0 +1,36 @@ +package com.dfsek.terra.config.genconfig.biome; + +import com.dfsek.terra.config.TerraConfig; +import com.dfsek.terra.config.TerraConfigSection; +import com.dfsek.terra.config.exception.ConfigException; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.InvalidConfigurationException; +import org.polydev.gaea.math.ProbabilityCollection; +import org.polydev.gaea.tree.Tree; +import org.polydev.gaea.tree.TreeType; + +import java.util.Map; + +public class BiomeTreeConfig extends TerraConfigSection { + private final ProbabilityCollection trees = new ProbabilityCollection<>(); + public BiomeTreeConfig(TerraConfig parent) throws InvalidConfigurationException { + super(parent); + ConfigurationSection c = parent.getConfigurationSection("trees"); + if(c == null) return; + Map cfg = c.getValues(false); + if(cfg.size() == 0) return; + for(Map.Entry e : cfg.entrySet()) { + try { + trees.add(TreeType.valueOf(e.getKey()), (Integer) e.getValue()); + } catch(ClassCastException ex) { + throw new ConfigException("Unable to parse Tree configuration! Check YAML syntax.", parent.getID()); + } catch(IllegalArgumentException ex) { + throw new ConfigException("Invalid tree type: \"" + e.getKey() + "\"", parent.getID()); + } + } + } + + public ProbabilityCollection getTrees() { + return trees; + } +}