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; package com.dfsek.terra.config.base;
import com.dfsek.terra.Debug;
import com.dfsek.terra.Terra; import com.dfsek.terra.Terra;
import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.carving.UserDefinedCarver; 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.biome.BiomeConfig;
import com.dfsek.terra.config.genconfig.structure.StructureConfig; import com.dfsek.terra.config.genconfig.structure.StructureConfig;
import com.dfsek.terra.config.lang.LangUtil; import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.tree.TreeRegistry;
import com.dfsek.terra.util.StructureTypeEnum; import com.dfsek.terra.util.StructureTypeEnum;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
@ -67,7 +69,7 @@ public class ConfigPack extends YamlConfiguration {
private final Map<String, AbstractBiomeConfig> abstractBiomes; private final Map<String, AbstractBiomeConfig> abstractBiomes;
private final Map<String, BiomeConfig> biomes; private final Map<String, BiomeConfig> biomes;
private final Map<String, BiomeGridConfig> grids; 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 Set<StructureConfig> allStructures = new HashSet<>();
private final File dataFolder; private final File dataFolder;
private final String id; private final String id;
@ -90,7 +92,12 @@ public class ConfigPack extends YamlConfiguration {
structures = ConfigLoader.load(new File(file, "structures").toPath(), this, StructureConfig.class); 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); 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); return flora.get(id);
} }
public TreeConfig getTree(String id) { public TreeRegistry getTreeRegistry() {
return trees.get(id); return treeRegistry;
} }
} }

View File

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

View File

@ -66,16 +66,6 @@ ores:
min-height: 0 min-height: 0
max-height: 16 max-height: 16
trees:
chance: 15
density: 1
items:
SPRUCE:
weight: 1
y:
min: 58
max: 72
flora: flora:
chance: 60 chance: 60
attempts: 2 attempts: 2

View File

@ -66,16 +66,6 @@ ores:
min-height: 0 min-height: 0
max-height: 16 max-height: 16
trees:
chance: 15
density: 1
items:
SPRUCE:
weight: 1
y:
min: 58
max: 72
flora: flora:
chance: 60 chance: 60
attempts: 2 attempts: 2

View File

@ -66,16 +66,6 @@ ores:
min-height: 0 min-height: 0
max-height: 16 max-height: 16
trees:
chance: 15
density: 1
items:
SPRUCE:
weight: 1
y:
min: 58
max: 72
flora: flora:
chance: 60 chance: 60
attempts: 2 attempts: 2

View File

@ -43,7 +43,7 @@ flora:
trees: trees:
density: 75 density: 75
items: items:
SPRUCE_CUSTOM: SPRUCE:
weight: 1 weight: 1
y: y:
min: 58 min: 58

View File

@ -36,7 +36,7 @@ flora:
trees: trees:
density: 75 density: 75
items: items:
SPRUCE_CUSTOM: SPRUCE:
weight: 1 weight: 1
y: y:
min: 58 min: 58

View File

@ -22,7 +22,7 @@ snow:
max: 255 max: 255
chance: 100 chance: 100
trees: trees:
density: 200 density: 60
items: items:
SPRUCE: SPRUCE:
weight: 1 weight: 1

View File

@ -22,7 +22,7 @@ snow:
max: 255 max: 255
chance: 100 chance: 100
trees: trees:
density: 200 density: 60
items: items:
SPRUCE: SPRUCE:
weight: 1 weight: 1

View File

@ -22,7 +22,7 @@ snow:
max: 255 max: 255
chance: 100 chance: 100
trees: trees:
density: 200 density: 60
items: items:
SPRUCE: SPRUCE:
weight: 1 weight: 1

View File

@ -6,7 +6,7 @@ files:
spruce5: 1 spruce5: 1
spruce6: 1 spruce6: 1
spruce7: 1 spruce7: 1
id: SPRUCE_CUSTOM id: SPRUCE
y-offset: 0 y-offset: 0
spawnable: spawnable:
- "minecraft:grass_block" - "minecraft:grass_block"