diff --git a/src/main/java/com/dfsek/terra/EventListener.java b/src/main/java/com/dfsek/terra/EventListener.java index 1a4606946..1d9bf4f59 100644 --- a/src/main/java/com/dfsek/terra/EventListener.java +++ b/src/main/java/com/dfsek/terra/EventListener.java @@ -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; diff --git a/src/main/java/com/dfsek/terra/config/base/ConfigPack.java b/src/main/java/com/dfsek/terra/config/base/ConfigPack.java index e76565dcb..93a4830b3 100644 --- a/src/main/java/com/dfsek/terra/config/base/ConfigPack.java +++ b/src/main/java/com/dfsek/terra/config/base/ConfigPack.java @@ -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 ores; private final Map palettes; private final Map carvers; - private final Map flora; private final Map structures; private final Map abstractBiomes; private final Map biomes; private final Map grids; private final TreeRegistry treeRegistry = new TreeRegistry(); + private final FloraRegistry floraRegistry = new FloraRegistry(); private final Set 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 flora = ConfigLoader.load(new File(file, "flora").toPath(), this, FloraConfig.class); + for(Map.Entry 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() { diff --git a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeFloraConfig.java b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeFloraConfig.java index 975641091..e9e267348 100644 --- a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeFloraConfig.java +++ b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeFloraConfig.java @@ -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 = new ProbabilityCollection<>(); + private final ProbabilityCollection floras = new ProbabilityCollection<>(); private final Map 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 e : cfg.getValues(false).entrySet()) { + for(Map.Entry 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 getFlora() { - return flora; + return floras; } public Map getFloraHeights() { 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 index a74a40693..ea6797580 100644 --- a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeTreeConfig.java +++ b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeTreeConfig.java @@ -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 cfg = c.getValues(false); if(cfg.size() == 0) return; treeDensity = parent.getInt("trees.density", 0); + for(Map.Entry 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"))); diff --git a/src/main/java/com/dfsek/terra/registry/FloraRegistry.java b/src/main/java/com/dfsek/terra/registry/FloraRegistry.java new file mode 100644 index 000000000..0e47fd0c1 --- /dev/null +++ b/src/main/java/com/dfsek/terra/registry/FloraRegistry.java @@ -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 { + public FloraRegistry() { + for(FloraType f : FloraType.values()) add(f.toString(), f); + } +} diff --git a/src/main/java/com/dfsek/terra/registry/TerraRegistry.java b/src/main/java/com/dfsek/terra/registry/TerraRegistry.java new file mode 100644 index 000000000..d52a71b10 --- /dev/null +++ b/src/main/java/com/dfsek/terra/registry/TerraRegistry.java @@ -0,0 +1,41 @@ +package com.dfsek.terra.registry; + +import java.util.HashMap; +import java.util.Map; + +public abstract class TerraRegistry { + private final Map 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); + } +} diff --git a/src/main/java/com/dfsek/terra/registry/TreeRegistry.java b/src/main/java/com/dfsek/terra/registry/TreeRegistry.java new file mode 100644 index 000000000..56ecf7ab5 --- /dev/null +++ b/src/main/java/com/dfsek/terra/registry/TreeRegistry.java @@ -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 { + public TreeRegistry() { + for(TreeType t : TreeType.values()) add(t.toString(), t); // Populate registry with default trees. + } +} diff --git a/src/main/java/com/dfsek/terra/tree/TreeRegistry.java b/src/main/java/com/dfsek/terra/tree/TreeRegistry.java deleted file mode 100644 index 9fe3d71dd..000000000 --- a/src/main/java/com/dfsek/terra/tree/TreeRegistry.java +++ /dev/null @@ -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 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); - } -}