Trees are now backed with a registry, custom trees can override Vanilla ones.

This commit is contained in:
dfsek
2020-11-09 01:19:24 -07:00
parent 0e3577063e
commit 57cefea2f3
12 changed files with 56 additions and 48 deletions

View File

@@ -1,5 +1,6 @@
package com.dfsek.terra.config.base;
import com.dfsek.terra.Debug;
import com.dfsek.terra.Terra;
import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.carving.UserDefinedCarver;
@@ -16,6 +17,7 @@ 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.util.StructureTypeEnum;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
@@ -67,7 +69,7 @@ public class ConfigPack extends YamlConfiguration {
private final Map<String, AbstractBiomeConfig> abstractBiomes;
private final Map<String, BiomeConfig> biomes;
private final Map<String, BiomeGridConfig> grids;
private final Map<String, TreeConfig> trees;
private final TreeRegistry treeRegistry = new TreeRegistry();
private final Set<StructureConfig> allStructures = new HashSet<>();
private final File dataFolder;
private final String id;
@@ -90,7 +92,12 @@ public class ConfigPack extends YamlConfiguration {
structures = ConfigLoader.load(new File(file, "structures").toPath(), this, StructureConfig.class);
trees = ConfigLoader.load(new File(file, "trees").toPath(), this, TreeConfig.class);
Map<String, TreeConfig> trees = ConfigLoader.load(new File(file, "trees").toPath(), this, TreeConfig.class);
for(Map.Entry<String, TreeConfig> entry : trees.entrySet()) {
if(treeRegistry.add(entry.getKey(), entry.getValue()))
Debug.info("Overriding Vanilla tree: " + entry.getKey());
}
abstractBiomes = ConfigLoader.load(new File(file, "abstract" + File.separator + "biomes").toPath(), this, AbstractBiomeConfig.class);
@@ -278,7 +285,7 @@ public class ConfigPack extends YamlConfiguration {
return flora.get(id);
}
public TreeConfig getTree(String id) {
return trees.get(id);
public TreeRegistry getTreeRegistry() {
return treeRegistry;
}
}

View File

@@ -8,7 +8,6 @@ import org.bukkit.configuration.InvalidConfigurationException;
import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.math.Range;
import org.polydev.gaea.tree.Tree;
import org.polydev.gaea.tree.TreeType;
import java.util.HashMap;
import java.util.Map;
@@ -32,13 +31,9 @@ public class BiomeTreeConfig extends TerraConfigSection {
Map<?, ?> y = ((ConfigurationSection) val.get("y")).getValues(false);
Tree tree;
try {
tree = TreeType.valueOf(e.getKey());
} catch(IllegalArgumentException ex) {
try {
tree = Objects.requireNonNull(parent.getConfig().getTree(e.getKey()));
} catch(NullPointerException ex2) {
throw new ConfigException("Invalid tree type: \"" + e.getKey() + "\"", parent.getID());
}
tree = Objects.requireNonNull(parent.getConfig().getTreeRegistry().get(e.getKey()));
} catch(NullPointerException ex2) {
throw new ConfigException("Invalid tree type: \"" + 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,36 @@
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);
}
}