Fix more biome abstraction issues, add more informative crash messages.

This commit is contained in:
dfsek 2020-09-23 14:26:01 -07:00
parent b5efa7cd79
commit 8045c77558
7 changed files with 64 additions and 38 deletions

View File

@ -2,6 +2,7 @@ package com.dfsek.terra.biome;
import com.dfsek.terra.config.ConfigUtil;
import com.dfsek.terra.config.WorldConfig;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.polydev.gaea.biome.Biome;
@ -11,25 +12,42 @@ import java.util.HashMap;
import java.util.Map;
public class TerraBiomeGrid extends BiomeGrid {
private static int failNum = 0;
private static final Map<World, TerraBiomeGrid> grids = new HashMap<>();
private final World w;
public TerraBiomeGrid(World w) {
super(w, WorldConfig.fromWorld(w).freq1, WorldConfig.fromWorld(w).freq2);
private TerraBiomeGrid(World w, float freq1, float freq2, boolean blank) {
super(w, freq1, freq2);
this.w = w;
grids.put(w, this);
if(!blank) grids.put(w, this);
}
public static TerraBiomeGrid fromWorld(World w) {
if(grids.containsKey(w)) return grids.get(w);
else return new TerraBiomeGrid(w);
try {
if(grids.containsKey(w)) return grids.get(w);
else return new TerraBiomeGrid(w, WorldConfig.fromWorld(w).freq1, WorldConfig.fromWorld(w).freq2, false);
} catch(NullPointerException e) {
if(ConfigUtil.debug) e.printStackTrace();
if(failNum % 256 == 0) Bukkit.getLogger().severe("[Terra] A severe configuration error has prevented Terra from properly generating terrain. Please check your configuration for errors. Any config errors will have been reported above.");
failNum++;
return new TerraBiomeGrid(w, 0.001f, 0.002f, true);
}
}
@Override
public Biome getBiome(int x, int z) {
return BiomeZone.fromWorld(w).getGrid(x, z).getBiome(x, z);
try {
return BiomeZone.fromWorld(w).getGrid(x, z).getBiome(x, z);
} catch(NullPointerException e) {
if(ConfigUtil.debug) e.printStackTrace();
if(failNum % 256 == 0) Bukkit.getLogger().severe("[Terra] A severe configuration error has prevented Terra from properly generating terrain at coordinates: " + x + ", " + z + ". Please check your configuration for errors. Any config errors will have been reported above.");
failNum++;
return null;
}
}
@Override

View File

@ -39,7 +39,7 @@ public class ConfigLoader {
} catch(IllegalAccessException | InstantiationException | NoSuchMethodException e) {
e.printStackTrace();
} catch(IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
if(ConfigUtil.debug) e.printStackTrace();
main.getLogger().severe("Configuration error for Terra object. File: " + path.toString());
main.getLogger().severe(((e instanceof InvocationTargetException) ? "INVOCATION: " + e.getCause().getMessage() : e.getMessage()));
main.getLogger().severe("Correct this before proceeding!");

View File

@ -9,6 +9,7 @@ import com.dfsek.terra.config.genconfig.CarverConfig;
import com.dfsek.terra.config.genconfig.FaunaConfig;
import com.dfsek.terra.config.genconfig.OreConfig;
import com.dfsek.terra.config.genconfig.PaletteConfig;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
@ -17,7 +18,13 @@ import java.util.logging.Logger;
import java.util.stream.Collectors;
public class ConfigUtil {
public static boolean debug;
public static void loadConfig(JavaPlugin main) {
main.saveDefaultConfig();
FileConfiguration config = main.getConfig();
debug = config.getBoolean("debug", false);
Logger logger = main.getLogger();
logger.info("Loading config values");

View File

@ -105,13 +105,17 @@ public class WorldConfig {
definedGrids = new UserDefinedGrid[biomeList.size()];
for(int i = 0; i < biomeList.size(); i++) {
String partName = biomeList.get(i);
if(partName.startsWith("BIOME:")) {
UserDefinedBiome[][] temp = new UserDefinedBiome[1][1];
UserDefinedBiome b = BiomeConfig.fromID(partName.substring(6)).getBiome();
temp[0][0] = b;
definedGrids[i] = new UserDefinedGrid(w, freq1, freq2, temp);
main.getLogger().info("Loaded single-biome grid " + partName);
} else definedGrids[i] = BiomeGridConfig.getBiomeGrids().get(partName).getGrid(w);
try {
if(partName.startsWith("BIOME:")) {
UserDefinedBiome[][] temp = new UserDefinedBiome[1][1];
UserDefinedBiome b = BiomeConfig.fromID(partName.substring(6)).getBiome();
temp[0][0] = b;
definedGrids[i] = new UserDefinedGrid(w, freq1, freq2, temp);
main.getLogger().info("Loaded single-biome grid " + partName);
} else definedGrids[i] = BiomeGridConfig.getBiomeGrids().get(partName).getGrid(w);
} catch(NullPointerException e) {
Bukkit.getLogger().severe("No such BiomeGrid " + partName);
}
}
Bukkit.getLogger().info("Loaded " + biomeList.size() + " BiomeGrids from list.");
@ -121,10 +125,6 @@ public class WorldConfig {
main.getLogger().severe("Unable to load configuration for world " + w + ".");
}
main.getLogger().info("World load complete. Time elapsed: " + ((double) (System.nanoTime() - start)) / 1000000 + "ms");
}
}

View File

@ -31,9 +31,9 @@ public class AbstractBiomeConfig extends TerraConfigObject {
private Map<CarverConfig, Integer> carvers;
private ProbabilityCollection<Fauna> fauna;
private ProbabilityCollection<Tree> trees;
private int faunaChance = 0;
private int treeChance = 0;
private int treeDensity = 0;
private int faunaChance;
private int treeChance;
private int treeDensity;
private String equation;
private TreeMap<Integer, BlockPalette> paletteMap;
@ -93,7 +93,7 @@ public class AbstractBiomeConfig extends TerraConfigObject {
fauna = new ProbabilityCollection<>();
for(Map.Entry<String, Object> e : Objects.requireNonNull(getConfigurationSection("fauna")).getValues(false).entrySet()) {
try {
Bukkit.getLogger().info("[Terra] Adding " + e.getKey() + " to astract biome's fauna list with weight " + e.getValue());
Bukkit.getLogger().info("[Terra] Adding " + e.getKey() + " to abstract biome's fauna list with weight " + e.getValue());
fauna.add(FaunaType.valueOf(e.getKey()), (Integer) e.getValue());
} catch(IllegalArgumentException ex) {
try {

View File

@ -125,10 +125,25 @@ public class BiomeConfig extends TerraConfigObject {
}
} else carvers = new HashMap<>();
int faunaChance, treeChance, treeDensity;
// Get various simple values using getOrDefault config methods.
try {
faunaChance = getInt("fauna-chance", Objects.requireNonNull(abstractBiome).getFaunaChance());
treeChance = getInt("tree-chance", Objects.requireNonNull(abstractBiome).getTreeChance());
treeDensity = getInt("tree-density", Objects.requireNonNull(abstractBiome).getTreeDensity());
eq = getString("noise-equation", Objects.requireNonNull(abstractBiome).getEquation());
} catch(NullPointerException e) {
faunaChance = getInt("fauna-chance", 0);
treeChance = getInt("tree-chance", 0);
treeDensity = getInt("tree-density", 0);
eq = getString("noise-equation", null);
}
// Check if fauna should be handled by super biome.
if(extending && abstractBiome.getFauna() != null && !contains("fauna")) {
fauna = abstractBiome.getFauna();
Bukkit.getLogger().info("Using super fauna");
Bukkit.getLogger().info("Using super fauna (" + fauna.size() + " entries, " + faunaChance + " % chance)");
} else if(contains("fauna")) {
for(Map.Entry<String, Object> e : Objects.requireNonNull(getConfigurationSection("fauna")).getValues(false).entrySet()) {
try {
@ -159,21 +174,6 @@ public class BiomeConfig extends TerraConfigObject {
}
} else trees = new ProbabilityCollection<>();
int faunaChance, treeChance, treeDensity;
// Get various simple values using getOrDefault config methods.
try {
faunaChance = getInt("fauna-chance", Objects.requireNonNull(abstractBiome).getFaunaChance());
treeChance = getInt("tree-chance", Objects.requireNonNull(abstractBiome).getTreeChance());
treeDensity = getInt("tree-density", Objects.requireNonNull(abstractBiome).getTreeDensity());
eq = getString("noise-equation", Objects.requireNonNull(abstractBiome).getEquation());
} catch(NullPointerException e) {
faunaChance = getInt("fauna-chance", 0);
treeChance = getInt("tree-chance", 0);
treeDensity = getInt("tree-density", 0);
eq = getString("noise-equation", null);
}
//Make sure equation is non-null
if(eq == null) throw new InvalidConfigurationException("Noise equation must be specified in biome or super biome.");

View File

@ -0,0 +1 @@
debug: false