From fd82d645a1f9918e48e84a26449371a5453866bc Mon Sep 17 00:00:00 2001 From: dfsek Date: Thu, 8 Oct 2020 02:17:25 -0700 Subject: [PATCH] Implement custom trees --- .../com/dfsek/terra/config/base/ConfigPack.java | 8 ++++++++ .../dfsek/terra/config/genconfig/TreeConfig.java | 10 +++++++++- .../config/genconfig/biome/BiomeTreeConfig.java | 14 +++++++++++--- 3 files changed, 28 insertions(+), 4 deletions(-) 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 4fb836cc7..4cf73cca4 100644 --- a/src/main/java/com/dfsek/terra/config/base/ConfigPack.java +++ b/src/main/java/com/dfsek/terra/config/base/ConfigPack.java @@ -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 abstractBiomes; private final Map biomes; private final Map grids; + private final Map 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); + } } diff --git a/src/main/java/com/dfsek/terra/config/genconfig/TreeConfig.java b/src/main/java/com/dfsek/terra/config/genconfig/TreeConfig.java index a710fb876..5c823e899 100644 --- a/src/main/java/com/dfsek/terra/config/genconfig/TreeConfig.java +++ b/src/main/java/com/dfsek/terra/config/genconfig/TreeConfig.java @@ -26,6 +26,7 @@ import java.util.Set; public class TreeConfig extends TerraConfig implements Tree { private final Set spawnable; private final String id; + private final int yOffset; private final ProbabilityCollection 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 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; } } 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 135d2e799..ca6c0b581 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 @@ -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 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()); } } }