Implement custom trees

This commit is contained in:
dfsek 2020-10-08 02:17:25 -07:00
parent e6f6a63194
commit fd82d645a1
3 changed files with 28 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.carving.UserDefinedCarver;
import com.dfsek.terra.config.ConfigLoader;
import com.dfsek.terra.config.exception.ConfigException;
import com.dfsek.terra.config.genconfig.TreeConfig;
import com.dfsek.terra.config.genconfig.biome.AbstractBiomeConfig;
import com.dfsek.terra.config.genconfig.biome.BiomeConfig;
import com.dfsek.terra.config.genconfig.BiomeGridConfig;
@ -41,6 +42,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 File dataFolder;
private final String id;
@ -78,6 +80,8 @@ 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);
abstractBiomes = ConfigLoader.load(new File(file, "abstract" + File.separator + "biomes").toPath(), this, AbstractBiomeConfig.class);
biomes = ConfigLoader.load(new File(file, "biomes").toPath(), this, BiomeConfig.class);
@ -209,4 +213,8 @@ public class ConfigPack extends YamlConfiguration {
public BiomeGridConfig getBiomeGrid(String id) {
return grids.get(id);
}
public TreeConfig getTree(String id) {
return trees.get(id);
}
}

View File

@ -26,6 +26,7 @@ import java.util.Set;
public class TreeConfig extends TerraConfig implements Tree {
private final Set<Material> spawnable;
private final String id;
private final int yOffset;
private final ProbabilityCollection<GaeaStructure> structure = new ProbabilityCollection<>();
public TreeConfig(File file, ConfigPack config) throws IOException, InvalidConfigurationException {
super(file, config);
@ -33,6 +34,7 @@ public class TreeConfig extends TerraConfig implements Tree {
if(!contains("id")) throw new ConfigException("No ID specified!", "null");
id = getString("id");
if(!contains("files")) throw new ConfigException("No files specified!", getID());
yOffset = getInt("y-offset", 0);
try {
for(Map.Entry<String, Object> e : Objects.requireNonNull(getConfigurationSection("files")).getValues(false).entrySet()) {
try {
@ -61,6 +63,12 @@ public class TreeConfig extends TerraConfig implements Tree {
@Override
public boolean plant(Location location, Random random, boolean b, JavaPlugin javaPlugin) {
return false;
Location mut = location.clone().subtract(0, yOffset, 0);
if(!spawnable.contains(location.getBlock().getType())) return false;
GaeaStructure struc = structure.get(random);
GaeaStructure.Rotation rotation = GaeaStructure.Rotation.fromDegrees(random.nextInt(4) * 90);
if(!struc.checkSpawns(mut, rotation)) return false;
struc.paste(mut, rotation);
return true;
}
}

View File

@ -13,6 +13,7 @@ import org.polydev.gaea.world.Flora;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class BiomeTreeConfig extends TerraConfigSection {
private final ProbabilityCollection<Tree> trees = new ProbabilityCollection<>();
@ -31,13 +32,20 @@ public class BiomeTreeConfig extends TerraConfigSection {
try {
Map<?, ?> val = ((ConfigurationSection) e.getValue()).getValues(false);
Map<?, ?> y = ((ConfigurationSection) val.get("y")).getValues(false);
Tree tree = TreeType.valueOf(e.getKey());
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());
}
}
trees.add(tree, (Integer) val.get("weight"));
treeHeights.put(tree, new Range((Integer) y.get("min"), (Integer) y.get("max")));
} 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());
}
}
}