Implement structure random selection

This commit is contained in:
dfsek 2020-10-02 23:35:39 -07:00
parent 28ebd9c31d
commit 9d05aed065
4 changed files with 32 additions and 12 deletions

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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<GaeaStructure> 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<String, Object> 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() {

View File

@ -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);