Back Flora configs with registry.

This commit is contained in:
dfsek 2020-11-12 16:47:59 -07:00
parent 53f1b0abc0
commit bd0dbb49de
8 changed files with 90 additions and 66 deletions

View File

@ -3,7 +3,7 @@ package com.dfsek.terra;
import com.dfsek.terra.async.AsyncStructureFinder;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.config.genconfig.structure.StructureConfig;
import com.dfsek.terra.tree.TreeRegistry;
import com.dfsek.terra.registry.TreeRegistry;
import com.dfsek.terra.util.StructureTypeEnum;
import org.bukkit.Material;
import org.bukkit.block.Block;

View File

@ -17,7 +17,8 @@ import com.dfsek.terra.config.genconfig.biome.AbstractBiomeConfig;
import com.dfsek.terra.config.genconfig.biome.BiomeConfig;
import com.dfsek.terra.config.genconfig.structure.StructureConfig;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.tree.TreeRegistry;
import com.dfsek.terra.registry.FloraRegistry;
import com.dfsek.terra.registry.TreeRegistry;
import com.dfsek.terra.util.StructureTypeEnum;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
@ -66,12 +67,12 @@ public class ConfigPack extends YamlConfiguration {
private final Map<String, OreConfig> ores;
private final Map<String, PaletteConfig> palettes;
private final Map<String, CarverConfig> carvers;
private final Map<String, FloraConfig> flora;
private final Map<String, StructureConfig> structures;
private final Map<String, AbstractBiomeConfig> abstractBiomes;
private final Map<String, BiomeConfig> biomes;
private final Map<String, BiomeGridConfig> grids;
private final TreeRegistry treeRegistry = new TreeRegistry();
private final FloraRegistry floraRegistry = new FloraRegistry();
private final Set<StructureConfig> allStructures = new HashSet<>();
private final File dataFolder;
private final String id;
@ -90,7 +91,11 @@ public class ConfigPack extends YamlConfiguration {
carvers = ConfigLoader.load(new File(file, "carving").toPath(), this, CarverConfig.class);
flora = ConfigLoader.load(new File(file, "flora").toPath(), this, FloraConfig.class);
Map<String, FloraConfig> flora = ConfigLoader.load(new File(file, "flora").toPath(), this, FloraConfig.class);
for(Map.Entry<String, FloraConfig> entry : flora.entrySet()) {
if(floraRegistry.add(entry.getKey(), entry.getValue()))
Debug.info("Overriding Gaea flora: " + entry.getKey());
}
structures = ConfigLoader.load(new File(file, "structures").toPath(), this, StructureConfig.class);
@ -285,8 +290,8 @@ public class ConfigPack extends YamlConfiguration {
return fill;
}
public FloraConfig getFlora(String id) {
return flora.get(id);
public FloraRegistry getFloraRegistry() {
return floraRegistry;
}
public TreeRegistry getTreeRegistry() {

View File

@ -3,7 +3,6 @@ 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.base.ConfigUtil;
import com.dfsek.terra.config.exception.ConfigException;
import com.dfsek.terra.config.exception.NotFoundException;
import org.bukkit.configuration.ConfigurationSection;
@ -12,13 +11,13 @@ import org.polydev.gaea.math.FastNoiseLite;
import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.math.Range;
import org.polydev.gaea.world.Flora;
import org.polydev.gaea.world.FloraType;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class BiomeFloraConfig extends TerraConfigSection {
private final ProbabilityCollection<Flora> flora = new ProbabilityCollection<>();
private final ProbabilityCollection<Flora> floras = new ProbabilityCollection<>();
private final Map<Flora, Range> floraHeights = new HashMap<>();
private int floraAttempts;
private int floraChance;
@ -40,35 +39,28 @@ public class BiomeFloraConfig extends TerraConfigSection {
floraNoise.setFrequency(floraFreq);
}
try {
for(Map.Entry<String, Object> e : cfg.getValues(false).entrySet()) {
for(Map.Entry<String, Object> e : cfg.getValues(false).entrySet()) {
try {
Map<?, ?> val = ((ConfigurationSection) e.getValue()).getValues(false);
Map<?, ?> y = ((ConfigurationSection) val.get("y")).getValues(false);
Flora flora;
try {
Debug.info("Adding " + e.getKey() + " to biome's flora list with weight " + e.getValue());
Flora floraObj = FloraType.valueOf(e.getKey());
flora.add(floraObj, (Integer) val.get("weight"));
floraHeights.put(floraObj, new Range((Integer) y.get("min"), (Integer) y.get("max")));
} catch(IllegalArgumentException ex) {
try {
Debug.info("[Terra] Is custom flora: true");
Flora floraCustom = parent.getConfig().getFlora(e.getKey());
if(floraCustom == null) throw new NotFoundException("Flora", e.getKey(), parent.getID());
flora.add(floraCustom, (Integer) val.get("weight"));
floraHeights.put(floraCustom, new Range((Integer) y.get("min"), (Integer) y.get("max")));
} catch(NullPointerException ex2) {
throw new NotFoundException("Flora", e.getKey(), parent.getID());
}
flora = Objects.requireNonNull(parent.getConfig().getFloraRegistry().get(e.getKey()));
} catch(NullPointerException ex) {
throw new NotFoundException("Flora", e.getKey(), parent.getID());
}
floras.add(flora, (Integer) val.get("weight"));
floraHeights.put(flora, new Range((Integer) y.get("min"), (Integer) y.get("max")));
} catch(ClassCastException ex) {
Debug.stack(ex);
throw new ConfigException("Unable to parse Flora configuration! Check YAML syntax.", parent.getID());
}
} catch(ClassCastException e) {
if(ConfigUtil.debug) e.printStackTrace();
throw new ConfigException("Unable to parse Flora configuration! Check YAML syntax.", parent.getID());
}
}
public ProbabilityCollection<Flora> getFlora() {
return flora;
return floras;
}
public Map<Flora, Range> getFloraHeights() {

View File

@ -3,6 +3,7 @@ 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 com.dfsek.terra.config.exception.NotFoundException;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.polydev.gaea.math.ProbabilityCollection;
@ -25,6 +26,7 @@ public class BiomeTreeConfig extends TerraConfigSection {
Map<String, Object> cfg = c.getValues(false);
if(cfg.size() == 0) return;
treeDensity = parent.getInt("trees.density", 0);
for(Map.Entry<String, Object> e : cfg.entrySet()) {
try {
Map<?, ?> val = ((ConfigurationSection) e.getValue()).getValues(false);
@ -33,7 +35,7 @@ public class BiomeTreeConfig extends TerraConfigSection {
try {
tree = Objects.requireNonNull(parent.getConfig().getTreeRegistry().get(e.getKey()));
} catch(NullPointerException ex2) {
throw new ConfigException("Invalid tree type: \"" + e.getKey() + "\"", parent.getID());
throw new NotFoundException("Tree", e.getKey(), parent.getID());
}
trees.add(tree, (Integer) val.get("weight"));
treeHeights.put(tree, new Range((Integer) y.get("min"), (Integer) y.get("max")));

View File

@ -0,0 +1,10 @@
package com.dfsek.terra.registry;
import org.polydev.gaea.world.Flora;
import org.polydev.gaea.world.FloraType;
public class FloraRegistry extends TerraRegistry<Flora> {
public FloraRegistry() {
for(FloraType f : FloraType.values()) add(f.toString(), f);
}
}

View File

@ -0,0 +1,41 @@
package com.dfsek.terra.registry;
import java.util.HashMap;
import java.util.Map;
public abstract class TerraRegistry<T> {
private final Map<String, T> objects = new HashMap<>();
/**
* Add an object to the registry with a name.
*
* @param name Name of the tree.
* @param value Object to add
* @return True if tree was overwritten.
*/
public boolean add(String name, T value) {
boolean exists = objects.containsKey(name);
objects.put(name, value);
return exists;
}
/**
* Check if the registry contains an object.
*
* @param name Name of the object.
* @return Whether the registry contains the object.
*/
public boolean contains(String name) {
return objects.containsKey(name);
}
/**
* Get an object from the registry,
*
* @param id ID of object to get
* @return Object
*/
public T get(String id) {
return objects.get(id);
}
}

View File

@ -0,0 +1,10 @@
package com.dfsek.terra.registry;
import org.polydev.gaea.tree.Tree;
import org.polydev.gaea.tree.TreeType;
public class TreeRegistry extends TerraRegistry<Tree> {
public TreeRegistry() {
for(TreeType t : TreeType.values()) add(t.toString(), t); // Populate registry with default trees.
}
}

View File

@ -1,36 +0,0 @@
package com.dfsek.terra.tree;
import org.polydev.gaea.tree.Tree;
import org.polydev.gaea.tree.TreeType;
import java.util.HashMap;
import java.util.Map;
public class TreeRegistry {
private final Map<String, Tree> trees = new HashMap<>();
public TreeRegistry() {
for(TreeType t : TreeType.values()) trees.put(t.toString(), t); // Populate registry with default trees.
}
/**
* Add a tree to the registry with a name.
*
* @param name Name of the tree.
* @param value Tree to add
* @return True if tree was overwritten.
*/
public boolean add(String name, Tree value) {
boolean exists = trees.containsKey(name);
trees.put(name, value);
return exists;
}
public boolean contains(String name) {
return trees.containsKey(name);
}
public Tree get(String id) {
return trees.get(id);
}
}