Load BiomeGrids like normal configs

This commit is contained in:
dfsek 2020-09-17 16:34:17 -07:00
parent d1afe912c5
commit e2823b808d
9 changed files with 61 additions and 47 deletions

View File

@ -1,7 +1,6 @@
package com.dfsek.terra; package com.dfsek.terra;
import com.dfsek.terra.biome.TerraBiomeGrid; import com.dfsek.terra.biome.TerraBiomeGrid;
import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.config.WorldConfig; import com.dfsek.terra.config.WorldConfig;
import com.dfsek.terra.population.CavePopulator; import com.dfsek.terra.population.CavePopulator;
import com.dfsek.terra.population.FaunaPopulator; import com.dfsek.terra.population.FaunaPopulator;
@ -10,17 +9,13 @@ import com.dfsek.terra.population.TreePopulator;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.generator.BlockPopulator; import org.bukkit.generator.BlockPopulator;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.generation.GaeaChunkGenerator; import org.polydev.gaea.generation.GaeaChunkGenerator;
import org.polydev.gaea.generation.GenerationPopulator; import org.polydev.gaea.generation.GenerationPopulator;
import org.polydev.gaea.math.FastNoise; import org.polydev.gaea.math.FastNoise;
import org.polydev.gaea.math.InterpolationType; import org.polydev.gaea.math.InterpolationType;
import org.polydev.gaea.math.MathUtil;
import org.polydev.gaea.population.PopulationManager; import org.polydev.gaea.population.PopulationManager;
import org.polydev.gaea.profiler.ProfileFuture;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;

View File

@ -30,7 +30,7 @@ public class UserDefinedCarver extends Carver {
@Override @Override
public boolean isChunkCarved(World w, int chunkX, int chunkZ, Random random) { public boolean isChunkCarved(World w, int chunkX, int chunkZ, Random random) {
UserDefinedBiome b = (UserDefinedBiome) TerraBiomeGrid.fromWorld(w).getBiome((chunkX << 4) + 8, (chunkZ << 4) + 8); UserDefinedBiome b = (UserDefinedBiome) TerraBiomeGrid.fromWorld(w).getBiome(chunkX << 4, chunkZ << 4);
return random.nextInt(100) < BiomeConfig.fromBiome(b).getCarverChance(config); return random.nextInt(100) < BiomeConfig.fromBiome(b).getCarverChance(config);
} }

View File

@ -129,6 +129,12 @@ public class BiomeConfig extends YamlConfiguration {
UserDefinedDecorator dec = new UserDefinedDecorator(fauna, trees, getInt("fauna-chance", 0), getInt("tree-chance", 0), getInt("tree-density", 0)); UserDefinedDecorator dec = new UserDefinedDecorator(fauna, trees, getInt("fauna-chance", 0), getInt("tree-chance", 0), getInt("tree-density", 0));
String eq = Objects.requireNonNull(getString("noise-equation")); String eq = Objects.requireNonNull(getString("noise-equation"));
try {
this.vanillaBiome = org.bukkit.block.Biome.valueOf(getString("vanilla"));
} catch(IllegalArgumentException e) {
throw new InvalidConfigurationException("Invalid Vanilla biome: " + getString("vanilla"));
}
try { try {
this.biome = new UserDefinedBiome(vanillaBiome, dec, new UserDefinedGenerator(eq, Collections.emptyList(), paletteMap)); this.biome = new UserDefinedBiome(vanillaBiome, dec, new UserDefinedGenerator(eq, Collections.emptyList(), paletteMap));
} catch(ParseException e) { } catch(ParseException e) {
@ -147,11 +153,7 @@ public class BiomeConfig extends YamlConfiguration {
if(!contains("vanilla")) throw new InvalidConfigurationException("Vanilla Biome unspecified!"); if(!contains("vanilla")) throw new InvalidConfigurationException("Vanilla Biome unspecified!");
if(!contains("palette")) throw new InvalidConfigurationException("Palette unspecified!"); if(!contains("palette")) throw new InvalidConfigurationException("Palette unspecified!");
try {
this.vanillaBiome = org.bukkit.block.Biome.valueOf(getString("vanilla"));
} catch(IllegalArgumentException e) {
throw new InvalidConfigurationException("Invalid Vanilla biome: " + getString("vanilla"));
}
isEnabled = true; isEnabled = true;
} }

View File

@ -2,26 +2,33 @@ package com.dfsek.terra.config;
import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.biome.UserDefinedGrid; import com.dfsek.terra.biome.UserDefinedGrid;
import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.commons.io.FilenameUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
public class BiomeGridConfig extends YamlConfiguration { public class BiomeGridConfig extends YamlConfiguration {
private UserDefinedGrid grid; private static final Map<String, BiomeGridConfig> biomeGrids = new HashMap<>();
private String gridID; private String gridID;
private String friendlyName; private String friendlyName;
private boolean isEnabled = false; private boolean isEnabled = false;
private final World world;
private final UserDefinedBiome[][] gridRaw = new UserDefinedBiome[16][16]; private final UserDefinedBiome[][] gridRaw = new UserDefinedBiome[16][16];
public BiomeGridConfig(File file, World w) throws IOException, InvalidConfigurationException { public BiomeGridConfig(File file) throws IOException, InvalidConfigurationException {
super(); super();
this.world = w;
load(file); load(file);
} }
@Override @Override
@ -46,8 +53,6 @@ public class BiomeGridConfig extends YamlConfiguration {
} catch(ClassCastException e) { } catch(ClassCastException e) {
throw new InvalidConfigurationException("Malformed grid!"); throw new InvalidConfigurationException("Malformed grid!");
} }
this.grid = new UserDefinedGrid(world, 1f/512, 1f/1024, this);// TODO: custom frequency
isEnabled = true; isEnabled = true;
} }
@ -67,7 +72,38 @@ public class BiomeGridConfig extends YamlConfiguration {
return gridID; return gridID;
} }
public UserDefinedGrid getGrid() { public UserDefinedGrid getGrid(World w) {
return grid; WorldConfig c = WorldConfig.fromWorld(w);
return new UserDefinedGrid(w, c.freq1, c.freq2, this);
}
protected static void loadBiomeGrids(JavaPlugin main) {
File biomeGridFolder = new File(main.getDataFolder() + File.separator + "grids");
biomeGridFolder.mkdirs();
try (Stream<Path> paths = Files.walk(biomeGridFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
main.getLogger().info("Loading BiomeGrid from " + path.toString());
try {
BiomeGridConfig grid = new BiomeGridConfig(path.toFile());
biomeGrids.put(grid.getGridID(), grid);
main.getLogger().info("Friendly name: " + grid.getFriendlyName());
main.getLogger().info("ID: " + grid.getGridID());
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
Bukkit.getLogger().severe("[Terra] Configuration error for BiomeGrid. ");
Bukkit.getLogger().severe("[Terra] " + e.getMessage());
Bukkit.getLogger().severe("[Terra] Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
}
public static Map<String, BiomeGridConfig> getBiomeGrids() {
return biomeGrids;
} }
} }

View File

@ -110,6 +110,7 @@ public class CarverConfig extends YamlConfiguration {
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
main.getLogger().info("Loaded " + caveConfig.size() + " carvers.");
} }
public static List<CarverConfig> getCarvers() { public static List<CarverConfig> getCarvers() {
return new ArrayList<>(caveConfig.values()); return new ArrayList<>(caveConfig.values());

View File

@ -21,6 +21,8 @@ public class ConfigUtil {
BiomeConfig.loadBiomes(main); BiomeConfig.loadBiomes(main);
BiomeGridConfig.loadBiomeGrids(main);
WorldConfig.reloadAll(); WorldConfig.reloadAll();
} }

View File

@ -122,6 +122,7 @@ public class OreConfig extends YamlConfiguration {
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
main.getLogger().info("Loaded " + ores.size() + " ores.");
} }
public static OreConfig fromID(String id) { public static OreConfig fromID(String id) {
return ores.get(id); return ores.get(id);

View File

@ -25,7 +25,7 @@ import java.util.stream.Stream;
public class WorldConfig { public class WorldConfig {
private static JavaPlugin main; private static JavaPlugin main;
private static final Map<World, WorldConfig> configs = new HashMap<>(); private static final Map<World, WorldConfig> configs = new HashMap<>();
private final Map<String, BiomeGridConfig> biomeGrids = new HashMap<>();
public float zoneFreq; public float zoneFreq;
public float freq1; public float freq1;
public float freq2; public float freq2;
@ -72,30 +72,8 @@ public class WorldConfig {
freq1 = 1f/config.getInt("frequencies.grid-1", 256); freq1 = 1f/config.getInt("frequencies.grid-1", 256);
freq2 = 1f/config.getInt("frequencies.grid-2", 512); freq2 = 1f/config.getInt("frequencies.grid-2", 512);
// Load BiomeGrids.
File biomeGridFolder = new File(main.getDataFolder() + File.separator + "grids"); configs.put(w, this); // WorldConfig must be included in map before Grids are loaded.
biomeGridFolder.mkdirs();
try (Stream<Path> paths = Files.walk(biomeGridFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
main.getLogger().info("Loading BiomeGrid from " + path.toString());
try {
BiomeGridConfig grid = new BiomeGridConfig(path.toFile(), w);
biomeGrids.put(grid.getGridID(), grid);
main.getLogger().info("Friendly name: " + grid.getFriendlyName());
main.getLogger().info("ID: " + grid.getGridID());
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
Bukkit.getLogger().severe("[Terra] Configuration error for BiomeGrid. ");
Bukkit.getLogger().severe("[Terra] " + e.getMessage());
Bukkit.getLogger().severe("[Terra] Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
for(int i = 0; i < 32; i++) { for(int i = 0; i < 32; i++) {
String partName = config.getStringList("grids").get(i); String partName = config.getStringList("grids").get(i);
@ -109,10 +87,10 @@ public class WorldConfig {
} }
definedGrids[i] = new UserDefinedGrid(w, freq1, freq2, temp); definedGrids[i] = new UserDefinedGrid(w, freq1, freq2, temp);
main.getLogger().info("Loaded single-biome grid " + partName); main.getLogger().info("Loaded single-biome grid " + partName);
} else definedGrids[i] = biomeGrids.get(partName).getGrid(); } else definedGrids[i] = BiomeGridConfig.getBiomeGrids().get(partName).getGrid(w);
} }
configs.put(w, this);
main.getLogger().info("World load complete. Time elapsed: " + ((double) (System.nanoTime() - start)) / 1000000 + "ms"); main.getLogger().info("World load complete. Time elapsed: " + ((double) (System.nanoTime() - start)) / 1000000 + "ms");
} }

View File

@ -2,7 +2,6 @@ package com.dfsek.terra.population;
import com.dfsek.terra.TerraProfiler; import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.config.CarverConfig; import com.dfsek.terra.config.CarverConfig;
import com.dfsek.terra.config.ConfigUtil;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.generator.ChunkGenerator; import org.bukkit.generator.ChunkGenerator;
import org.polydev.gaea.generation.GenerationPopulator; import org.polydev.gaea.generation.GenerationPopulator;