From 9d05aed06566d06baff46567dd541e1eb2ae0300 Mon Sep 17 00:00:00 2001 From: dfsek Date: Fri, 2 Oct 2020 23:35:39 -0700 Subject: [PATCH] Implement structure random selection --- src/main/java/com/dfsek/terra/Debug.java | 4 +++ src/main/java/com/dfsek/terra/TerraWorld.java | 3 ++ .../config/genconfig/StructureConfig.java | 35 +++++++++++++------ .../terra/population/StructurePopulator.java | 2 +- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/dfsek/terra/Debug.java b/src/main/java/com/dfsek/terra/Debug.java index be0c97d49..3047b504b 100644 --- a/src/main/java/com/dfsek/terra/Debug.java +++ b/src/main/java/com/dfsek/terra/Debug.java @@ -20,4 +20,8 @@ public class Debug { public static void error(String message) { if(ConfigUtil.debug) main.getLogger().severe(message); } + + public static void stack(Exception e) { + if(ConfigUtil.debug) e.printStackTrace(); + } } diff --git a/src/main/java/com/dfsek/terra/TerraWorld.java b/src/main/java/com/dfsek/terra/TerraWorld.java index 8e708eb1b..7b863f35f 100644 --- a/src/main/java/com/dfsek/terra/TerraWorld.java +++ b/src/main/java/com/dfsek/terra/TerraWorld.java @@ -41,6 +41,9 @@ public class TerraWorld { } catch(NullPointerException e) { if(ConfigUtil.debug) e.printStackTrace(); Bukkit.getLogger().severe("No such BiomeGrid " + partName); + Bukkit.getLogger().severe("Please check configuration files for errors. Configuration errors will have been reported during initialization."); + Bukkit.getLogger().severe("ONLY report this to Terra if you are SURE your config is error-free."); + Bukkit.getLogger().severe("Terrain will NOT generate properly at this point. Correct your config before using your server!"); } } zone = new BiomeZone(w, worldConfig, definedGrids); diff --git a/src/main/java/com/dfsek/terra/config/genconfig/StructureConfig.java b/src/main/java/com/dfsek/terra/config/genconfig/StructureConfig.java index 8d0af15e8..c83676617 100644 --- a/src/main/java/com/dfsek/terra/config/genconfig/StructureConfig.java +++ b/src/main/java/com/dfsek/terra/config/genconfig/StructureConfig.java @@ -1,25 +1,27 @@ package com.dfsek.terra.config.genconfig; +import com.dfsek.terra.Debug; import com.dfsek.terra.config.TerraConfig; +import com.dfsek.terra.config.TerraConfigObject; +import com.dfsek.terra.config.base.ConfigUtil; import com.dfsek.terra.config.exception.ConfigException; import com.dfsek.terra.config.exception.NotFoundException; -import org.polydev.gaea.math.Range; -import com.dfsek.terra.Terra; -import com.dfsek.terra.config.base.ConfigUtil; -import com.dfsek.terra.config.TerraConfigObject; import com.dfsek.terra.population.StructurePopulator; -import com.dfsek.terra.structure.GaeaStructure; import com.dfsek.terra.procgen.GridSpawn; +import com.dfsek.terra.structure.GaeaStructure; import org.bukkit.configuration.InvalidConfigurationException; +import org.polydev.gaea.math.ProbabilityCollection; +import org.polydev.gaea.math.Range; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; -import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.Random; public class StructureConfig extends TerraConfigObject { - private final GaeaStructure structure; + private final ProbabilityCollection structure = new ProbabilityCollection<>(); private final GridSpawn spawn; private final String id; private final Range searchStart; @@ -29,9 +31,20 @@ public class StructureConfig extends TerraConfigObject { super(file, config); if(!contains("id")) throw new ConfigException("No ID specified!", "null"); id = getString("id"); + if(!contains("files")) throw new ConfigException("No files specified!", getID()); try { - File structureFile = new File(config.getDataFolder() + File.separator + "structures" + File.separator + "data", Objects.requireNonNull(getString("file"))); - structure = GaeaStructure.load(structureFile); + for(Map.Entry e : Objects.requireNonNull(getConfigurationSection("files")).getValues(false).entrySet()) { + try { + File structureFile = new File(config.getDataFolder() + File.separator + "structures" + File.separator + "data", e.getKey() + ".tstructure"); + structure.add(GaeaStructure.load(structureFile), (Integer) e.getValue()); + } catch(FileNotFoundException ex) { + Debug.stack(ex); + throw new NotFoundException("Structure File", e.getKey(), getID()); + } catch(ClassCastException ex) { + Debug.stack(ex); + throw new ConfigException("Unable to parse Structure configuration! Check YAML syntax.", getID()); + } + } } catch(IOException | NullPointerException e) { if(ConfigUtil.debug) { e.printStackTrace(); @@ -54,8 +67,8 @@ public class StructureConfig extends TerraConfigObject { return id; } - public GaeaStructure getStructure() { - return structure; + public GaeaStructure getStructure(Random r) { + return structure.get(r); } public GridSpawn getSpawn() { diff --git a/src/main/java/com/dfsek/terra/population/StructurePopulator.java b/src/main/java/com/dfsek/terra/population/StructurePopulator.java index 5f7f7420f..02631e04c 100644 --- a/src/main/java/com/dfsek/terra/population/StructurePopulator.java +++ b/src/main/java/com/dfsek/terra/population/StructurePopulator.java @@ -34,9 +34,9 @@ public class StructurePopulator extends BlockPopulator { TerraConfig config = tw.getConfig(); UserDefinedBiome b = (UserDefinedBiome) grid.getBiome(cx+ 8, cz + 8, GenerationPhase.POPULATE); structure: for(StructureConfig conf : config.getBiome(b).getStructures()) { - GaeaStructure struc = conf.getStructure(); Location spawn = conf.getSpawn().getNearestSpawn(cx + 8, cz + 8, world.getSeed()).toLocation(world); Random r2 = new Random(spawn.hashCode()); + GaeaStructure struc = conf.getStructure(r2); main: for(int y = conf.getSearchStart().get(r2); y > 0; y--) { if(y > conf.getBound().getMax() || y < conf.getBound().getMin()) continue structure; spawn.setY(y);