Refactor configs with per-class static getters

This commit is contained in:
dfsek 2020-09-17 15:27:02 -07:00
parent ad83a3054b
commit d1afe912c5
15 changed files with 345 additions and 332 deletions

View File

@ -2,6 +2,7 @@ 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.biome.UserDefinedBiome;
import com.dfsek.terra.config.BiomeConfig;
import com.dfsek.terra.config.ConfigUtil; import com.dfsek.terra.config.ConfigUtil;
import com.dfsek.terra.config.OreConfig; import com.dfsek.terra.config.OreConfig;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -25,7 +26,7 @@ public class TerraCommand implements CommandExecutor {
break; break;
case "biome": case "biome":
if(!(sender instanceof Player)) return false; if(!(sender instanceof Player)) return false;
sender.sendMessage("You are in " + ((UserDefinedBiome) TerraBiomeGrid.fromWorld(((Player) sender).getWorld()).getBiome(((Player) sender).getLocation())).getConfig().getFriendlyName()); sender.sendMessage("You are in " + BiomeConfig.fromBiome((UserDefinedBiome) TerraBiomeGrid.fromWorld(((Player) sender).getWorld()).getBiome(((Player) sender).getLocation())).getFriendlyName());
break; break;
case "profile": case "profile":
if(! (sender instanceof Player)) { if(! (sender instanceof Player)) {
@ -69,7 +70,7 @@ public class TerraCommand implements CommandExecutor {
return true; return true;
} }
Block bl = ((Player) sender).getTargetBlockExact(25); Block bl = ((Player) sender).getTargetBlockExact(25);
OreConfig ore = ConfigUtil.getOre(args[1]); OreConfig ore = OreConfig.fromID(args[1]);
if(ore == null) { if(ore == null) {
sender.sendMessage("Unable to find Ore"); sender.sendMessage("Unable to find Ore");
return true; return true;

View File

@ -2,9 +2,9 @@ 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.biome.UserDefinedBiome;
import com.dfsek.terra.config.BiomeConfig;
import com.dfsek.terra.config.CarverConfig; import com.dfsek.terra.config.CarverConfig;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.polydev.gaea.world.carving.Carver; import org.polydev.gaea.world.carving.Carver;
import org.polydev.gaea.world.carving.Worm; import org.polydev.gaea.world.carving.Worm;
@ -31,7 +31,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) + 8, (chunkZ << 4) + 8);
return random.nextInt(100) < b.getCarverChance(config); return random.nextInt(100) < BiomeConfig.fromBiome(b).getCarverChance(config);
} }
private static class UserDefinedWorm extends Worm { private static class UserDefinedWorm extends Worm {

View File

@ -29,60 +29,15 @@ import java.util.TreeMap;
public class UserDefinedBiome implements Biome { public class UserDefinedBiome implements Biome {
private final UserDefinedGenerator gen; private final UserDefinedGenerator gen;
private final BiomeConfig config;
private final UserDefinedDecorator decorator; private final UserDefinedDecorator decorator;
private final List<CarverConfig> carvers = new ArrayList<>(); private final org.bukkit.block.Biome vanilla;
private final Map<CarverConfig, Integer> carverChance = new HashMap<>();
public UserDefinedBiome(BiomeConfig config) throws ParseException, InvalidConfigurationException {
this.config = config;
TreeMap<Integer, BlockPalette> paletteMap = new TreeMap<>();
for(Map<?, ?> e : config.getMapList("palette")) {
for(Map.Entry<?, ?> entry : e.entrySet()) {
try {
if(((String) entry.getKey()).startsWith("BLOCK:")) {
try {
paletteMap.put((Integer) entry.getValue(), new BlockPalette().addBlockData(new ProbabilityCollection<BlockData>().add(Bukkit.createBlockData(((String) entry.getKey()).substring(6)), 1), 1));
} catch(IllegalArgumentException ex) {
throw new InvalidConfigurationException("SEVERE configuration error for BlockPalettes in biome " + config.getFriendlyName() + ", ID: " + config.getBiomeID() + ". BlockData " + entry.getKey() + " is invalid!");
}
}
else {
try {
paletteMap.put((Integer) entry.getValue(), ConfigUtil.getPalette((String) entry.getKey()).getPalette());
} catch(NullPointerException ex) {
throw new InvalidConfigurationException("SEVERE configuration error for BlockPalettes in biome " + config.getFriendlyName() + ", ID: " + config.getBiomeID() + "\n\nPalette " + entry.getKey() + " cannot be found!");
}
}
} catch(ClassCastException ex) {
throw new InvalidConfigurationException("SEVERE configuration error for BlockPalettes in biome" + config.getFriendlyName() + ", ID: " + config.getBiomeID());
}
}
}
if(config.contains("carving")) {
for(Map<?, ?> e : config.getMapList("carving")) {
for(Map.Entry<?, ?> entry : e.entrySet()) {
try {
//carvers.add(new UserDefinedCarver((Integer) entry.getValue(), ConfigUtil.getCarver((String) entry.getKey())));
carverChance.put(ConfigUtil.getCarver((String) entry.getKey()), (Integer) entry.getValue());
} catch(ClassCastException ex) {
throw new InvalidConfigurationException("SEVERE configuration error for Carvers in biome" + config.getFriendlyName() + ", ID: " + config.getBiomeID());
}
}
}
}
this.decorator = new UserDefinedDecorator(config); public UserDefinedBiome(org.bukkit.block.Biome vanilla, UserDefinedDecorator dec, UserDefinedGenerator gen) {
gen = new UserDefinedGenerator(Objects.requireNonNull(config.getString("noise-equation")), Collections.emptyList(), paletteMap); this.vanilla = vanilla;
} this.decorator = dec;
this.gen = gen;
public List<CarverConfig> getCarvers() {
return carvers;
}
public BiomeConfig getConfig() {
return config;
} }
/** /**
@ -92,7 +47,7 @@ public class UserDefinedBiome implements Biome {
*/ */
@Override @Override
public org.bukkit.block.Biome getVanillaBiome() { public org.bukkit.block.Biome getVanillaBiome() {
return config.getVanillaBiome(); return vanilla;
} }
/** /**
@ -124,8 +79,4 @@ public class UserDefinedBiome implements Biome {
public Decorator getDecorator() { public Decorator getDecorator() {
return decorator; return decorator;
} }
public int getCarverChance(CarverConfig c) {
return carverChance.getOrDefault(c, 0);
}
} }

View File

@ -17,41 +17,19 @@ import java.util.Map;
public class UserDefinedDecorator extends Decorator { public class UserDefinedDecorator extends Decorator {
private final ProbabilityCollection<Fauna> fauna = new ProbabilityCollection<>(); private final ProbabilityCollection<Fauna> fauna;
private final ProbabilityCollection<Tree> trees = new ProbabilityCollection<>(); private final ProbabilityCollection<Tree> trees;
private final int faunaChance; private final int faunaChance;
private final int treeChance; private final int treeChance;
private final int treeDensity; private final int treeDensity;
public UserDefinedDecorator(BiomeConfig config) { public UserDefinedDecorator(ProbabilityCollection<Fauna> fauna, ProbabilityCollection<Tree> trees, int faunaChance, int treeChance, int treeDensity) {
if(config.contains("fauna")) { this.fauna = fauna;
for(Map.Entry<String, Object> e : config.getConfigurationSection("fauna").getValues(false).entrySet()) { this.trees = trees;
try {
Bukkit.getLogger().info("[Terra] Adding " + e.getKey() + " to biome's fauna list with weight " + e.getValue()); this.faunaChance = faunaChance;
fauna.add(FaunaType.valueOf(e.getKey()), (Integer) e.getValue()); this.treeChance = treeChance;
} catch(IllegalArgumentException ex) { this.treeDensity = treeDensity;
try {
Bukkit.getLogger().info("[Terra] Is custom fauna: true");
Fauna faunaCustom = ConfigUtil.getFauna(e.getKey());
fauna.add(faunaCustom, (Integer) e.getValue());
} catch(NullPointerException ex2) {
throw new IllegalArgumentException("SEVERE configuration error for fauna in biome " + config.getFriendlyName() + ", ID " + config.getBiomeID() + "\n\nFauna with ID " + e.getKey() + " cannot be found!");
}
}
}
}
if(config.contains("trees")) {
for(Map.Entry<String, Object> e : config.getConfigurationSection("trees").getValues(false).entrySet()) {
if(e.getKey().startsWith("TERRA:")) {
trees.add(TerraTree.valueOf(e.getKey().substring(6)), (Integer) e.getValue());
} else {
trees.add(TreeType.valueOf(e.getKey()), (Integer) e.getValue());
}
}
}
faunaChance = config.getInt("fauna-chance", 0);
treeChance = config.getInt("tree-chance", 0);
treeDensity = config.getInt("tree-density", 0);
} }
@Override @Override

View File

@ -18,7 +18,6 @@ import java.util.TreeMap;
public class UserDefinedGenerator extends BiomeTerrain { public class UserDefinedGenerator extends BiomeTerrain {
private final Expression noiseExp; private final Expression noiseExp;
private final List<Variable> vars;
private final Scope s = new Scope(); private final Scope s = new Scope();
private final Variable xVar = s.getVariable("x");; private final Variable xVar = s.getVariable("x");;
private final Variable yVar = s.getVariable("y"); private final Variable yVar = s.getVariable("y");
@ -26,17 +25,14 @@ public class UserDefinedGenerator extends BiomeTerrain {
private final TreeMap<Integer, BlockPalette> paletteMap; private final TreeMap<Integer, BlockPalette> paletteMap;
private final NoiseFunction2 n2 = new NoiseFunction2(); private final NoiseFunction2 n2 = new NoiseFunction2();
private final NoiseFunction3 n3 = new NoiseFunction3(); private final NoiseFunction3 n3 = new NoiseFunction3();
private final Parser p = new Parser();
{
p.registerFunction("noise2", n2);
p.registerFunction("noise3", n3);
}
private static final Object noiseLock = new Object(); private static final Object noiseLock = new Object();
public UserDefinedGenerator(String e, List<Variable> v, TreeMap<Integer, BlockPalette> pa) throws ParseException { public UserDefinedGenerator(String e, List<Variable> v, TreeMap<Integer, BlockPalette> pa) throws ParseException {
this.vars = v; Parser p = new Parser();
p.registerFunction("noise2", n2);
p.registerFunction("noise3", n3);
this.paletteMap = pa; this.paletteMap = pa;
this.noiseExp = p.parse(e, s); this.noiseExp = p.parse(e, s);
} }
@ -94,27 +90,4 @@ public class UserDefinedGenerator extends BiomeTerrain {
return null; return null;
} }
private static class Range {
private final int min;
private final int max;
/**
* Instantiates a Range object with a minimum value (inclusive) and a maximum value (exclusive).
* @param min The minimum value (inclusive).
* @param max The maximum value (exclusive).
*/
public Range(int min, int max) {
this.min = min;
this.max = max;
}
/**
* Tests if a value is within range.
* @param val The value to test.
* @return boolean - Whether the value is within range.
*/
public boolean isInRange(int val) {
return val >= min && val < max;
}
}
} }

View File

@ -1,19 +1,42 @@
package com.dfsek.terra.config; package com.dfsek.terra.config;
import com.dfsek.terra.MaxMin; import com.dfsek.terra.MaxMin;
import com.dfsek.terra.TerraTree;
import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.biome.UserDefinedDecorator;
import com.dfsek.terra.biome.UserDefinedGenerator;
import org.bukkit.Bukkit;
import org.bukkit.block.data.BlockData;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
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 org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.math.parsii.tokenizer.ParseException; import org.polydev.gaea.math.parsii.tokenizer.ParseException;
import org.polydev.gaea.tree.Tree;
import org.polydev.gaea.tree.TreeType;
import org.polydev.gaea.world.BlockPalette;
import org.polydev.gaea.world.Fauna;
import org.polydev.gaea.world.FaunaType;
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.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.logging.Logger;
import java.util.stream.Stream;
public class BiomeConfig extends YamlConfiguration { public class BiomeConfig extends YamlConfiguration {
private static final Map<String, BiomeConfig> biomes = new HashMap<>();
private UserDefinedBiome biome; private UserDefinedBiome biome;
private String biomeID; private String biomeID;
private String friendlyName; private String friendlyName;
@ -21,15 +44,15 @@ public class BiomeConfig extends YamlConfiguration {
private boolean isEnabled = false; private boolean isEnabled = false;
private final Map<OreConfig, MaxMin> ores = new HashMap<>(); private final Map<OreConfig, MaxMin> ores = new HashMap<>();
private final Map<OreConfig, MaxMin> oreHeights = new HashMap<>(); private final Map<OreConfig, MaxMin> oreHeights = new HashMap<>();
private final Map<CarverConfig, Integer> carvers = new HashMap<>();
private final ProbabilityCollection<Fauna> fauna = new ProbabilityCollection<>();
private final ProbabilityCollection<Tree> trees = new ProbabilityCollection<>();
public BiomeConfig(File file) throws InvalidConfigurationException, IOException { public BiomeConfig(File file) throws InvalidConfigurationException, IOException {
super(); super();
load(file); load(file);
} }
@Override @Override
public void load(@NotNull File file) throws InvalidConfigurationException, IOException { public void load(@NotNull File file) throws InvalidConfigurationException, IOException {
isEnabled = false; isEnabled = false;
@ -39,8 +62,75 @@ public class BiomeConfig extends YamlConfiguration {
if(!contains("name")) throw new InvalidConfigurationException("Biome Name unspecified!"); if(!contains("name")) throw new InvalidConfigurationException("Biome Name unspecified!");
this.friendlyName = getString("name"); this.friendlyName = getString("name");
if(!contains("noise-equation")) throw new InvalidConfigurationException("No noise equation included in biome!"); if(!contains("noise-equation")) throw new InvalidConfigurationException("No noise equation included in biome!");
TreeMap<Integer, BlockPalette> paletteMap = new TreeMap<>();
for(Map<?, ?> e : getMapList("palette")) {
for(Map.Entry<?, ?> entry : e.entrySet()) {
try { try {
this.biome = new UserDefinedBiome(this); if(((String) entry.getKey()).startsWith("BLOCK:")) {
try {
paletteMap.put((Integer) entry.getValue(), new BlockPalette().addBlockData(new ProbabilityCollection<BlockData>().add(Bukkit.createBlockData(((String) entry.getKey()).substring(6)), 1), 1));
} catch(IllegalArgumentException ex) {
throw new InvalidConfigurationException("SEVERE configuration error for BlockPalettes in biome " + getFriendlyName() + ", ID: " + biomeID + ". BlockData " + entry.getKey() + " is invalid!");
}
}
else {
try {
paletteMap.put((Integer) entry.getValue(), PaletteConfig.fromID((String) entry.getKey()).getPalette());
} catch(NullPointerException ex) {
throw new InvalidConfigurationException("SEVERE configuration error for BlockPalettes in biome " + getFriendlyName() + ", ID: " + biomeID + "\n\nPalette " + entry.getKey() + " cannot be found!");
}
}
} catch(ClassCastException ex) {
throw new InvalidConfigurationException("SEVERE configuration error for BlockPalettes in biome" + getFriendlyName() + ", ID: " + biomeID);
}
}
}
if(contains("carving")) {
for(Map<?, ?> e : getMapList("carving")) {
for(Map.Entry<?, ?> entry : e.entrySet()) {
try {
//carvers.add(new UserDefinedCarver((Integer) entry.getValue(), ConfigUtil.getCarver((String) entry.getKey())));
carvers.put(CarverConfig.fromID((String) entry.getKey()), (Integer) entry.getValue());
} catch(ClassCastException ex) {
throw new InvalidConfigurationException("SEVERE configuration error for Carvers in biome" + getFriendlyName() + ", ID: " + biomeID);
}
}
}
}
if(contains("fauna")) {
for(Map.Entry<String, Object> e : getConfigurationSection("fauna").getValues(false).entrySet()) {
try {
Bukkit.getLogger().info("[Terra] Adding " + e.getKey() + " to biome's fauna list with weight " + e.getValue());
fauna.add(FaunaType.valueOf(e.getKey()), (Integer) e.getValue());
} catch(IllegalArgumentException ex) {
try {
Bukkit.getLogger().info("[Terra] Is custom fauna: true");
Fauna faunaCustom = FaunaConfig.fromID(e.getKey());
fauna.add(faunaCustom, (Integer) e.getValue());
} catch(NullPointerException ex2) {
throw new IllegalArgumentException("SEVERE configuration error for fauna in biome " + getFriendlyName() + ", ID " + getBiomeID() + "\n\nFauna with ID " + e.getKey() + " cannot be found!");
}
}
}
}
if(contains("trees")) {
for(Map.Entry<String, Object> e : getConfigurationSection("trees").getValues(false).entrySet()) {
if(e.getKey().startsWith("TERRA:")) {
trees.add(TerraTree.valueOf(e.getKey().substring(6)), (Integer) e.getValue());
} else {
trees.add(TreeType.valueOf(e.getKey()), (Integer) e.getValue());
}
}
}
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"));
try {
this.biome = new UserDefinedBiome(vanillaBiome, dec, new UserDefinedGenerator(eq, Collections.emptyList(), paletteMap));
} catch(ParseException e) { } catch(ParseException e) {
e.printStackTrace(); e.printStackTrace();
throw new IllegalArgumentException("Unable to parse noise equation!"); throw new IllegalArgumentException("Unable to parse noise equation!");
@ -48,8 +138,8 @@ public class BiomeConfig extends YamlConfiguration {
if(contains("ores")) { if(contains("ores")) {
ores.clear(); ores.clear();
for(Map.Entry<String, Object> m : getConfigurationSection("ores").getValues(false).entrySet()) { for(Map.Entry<String, Object> m : getConfigurationSection("ores").getValues(false).entrySet()) {
ores.put(ConfigUtil.getOre(m.getKey()), new MaxMin(((ConfigurationSection) m.getValue()).getInt("min"), ((ConfigurationSection) m.getValue()).getInt("max"))); ores.put(OreConfig.fromID(m.getKey()), new MaxMin(((ConfigurationSection) m.getValue()).getInt("min"), ((ConfigurationSection) m.getValue()).getInt("max")));
oreHeights.put(ConfigUtil.getOre(m.getKey()), new MaxMin(((ConfigurationSection) m.getValue()).getInt("min-height"), ((ConfigurationSection) m.getValue()).getInt("max-height"))); oreHeights.put(OreConfig.fromID(m.getKey()), new MaxMin(((ConfigurationSection) m.getValue()).getInt("min-height"), ((ConfigurationSection) m.getValue()).getInt("max-height")));
} }
} }
@ -92,4 +182,50 @@ public class BiomeConfig extends YamlConfiguration {
public Map<OreConfig, MaxMin> getOres() { public Map<OreConfig, MaxMin> getOres() {
return ores; return ores;
} }
public static BiomeConfig fromBiome(UserDefinedBiome b) {
for(BiomeConfig biome : biomes.values()) {
if(biome.getBiome().equals(b)) return biome;
}
throw new IllegalArgumentException("No BiomeConfig for provided biome.");
}
public static BiomeConfig fromID(String id) {
return biomes.get(id);
}
protected static void loadBiomes(JavaPlugin main) {
// TODO: Merge all load methods
Logger logger = main.getLogger();
biomes.clear();
File biomeFolder = new File(main.getDataFolder() + File.separator + "biomes");
biomeFolder.mkdirs();
try (Stream<Path> paths = Files.walk(biomeFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
logger.info("Loading biome from " + path.toString());
try {
BiomeConfig biome = new BiomeConfig(path.toFile());
biomes.put(biome.getBiomeID(), biome);
logger.info("Friendly name: " + biome.getFriendlyName());
logger.info("ID: " + biome.getBiomeID());
logger.info("Noise equation: " + biome.get("noise-equation"));
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
logger.severe("Configuration error for Biome. ");
logger.severe(e.getMessage());
logger.severe("Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
main.getLogger().info("Loaded " + biomes.size() + " biomes.");
}
public int getCarverChance(CarverConfig c) {
return carvers.getOrDefault(c, 0);
}
} }

View File

@ -37,7 +37,7 @@ public class BiomeGridConfig extends YamlConfiguration {
for(int x = 0; x < 16; x++) { for(int x = 0; x < 16; x++) {
for(int z = 0; z < 16; z++) { for(int z = 0; z < 16; z++) {
try { try {
gridRaw[x][z] = ConfigUtil.getBiome(((List<List<String>>) getList("grid")).get(x).get(z)).getBiome(); gridRaw[x][z] = BiomeConfig.fromID(((List<List<String>>) getList("grid")).get(x).get(z)).getBiome();
} catch(NullPointerException e) { } catch(NullPointerException e) {
throw new InvalidConfigurationException("SEVERE configuration error for BiomeGrid " + getFriendlyName() + ", ID: " + getGridID() + "\n\nNo such biome " + ((List<List<String>>) getList("grid")).get(x).get(z)); throw new InvalidConfigurationException("SEVERE configuration error for BiomeGrid " + getFriendlyName() + ", ID: " + getGridID() + "\n\nNo such biome " + ((List<List<String>>) getList("grid")).get(x).get(z));
} }

View File

@ -6,15 +6,26 @@ import org.bukkit.Bukkit;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
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 org.polydev.gaea.math.ProbabilityCollection; import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.world.BlockPalette; import org.polydev.gaea.world.BlockPalette;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Logger;
import java.util.stream.Stream;
public class CarverConfig extends YamlConfiguration { public class CarverConfig extends YamlConfiguration {
private static final Map<String, CarverConfig> caveConfig = new HashMap<>();
private UserDefinedCarver carver; private UserDefinedCarver carver;
private BlockPalette inner; private BlockPalette inner;
private BlockPalette walls; private BlockPalette walls;
@ -53,13 +64,13 @@ public class CarverConfig extends YamlConfiguration {
if(Objects.requireNonNull(getString("palette.interior")).startsWith("BLOCK:")) { if(Objects.requireNonNull(getString("palette.interior")).startsWith("BLOCK:")) {
inner = new BlockPalette().addBlockData(new ProbabilityCollection<BlockData>().add(Bukkit.createBlockData(getString("palette.interior").substring(6)), 1), 1); inner = new BlockPalette().addBlockData(new ProbabilityCollection<BlockData>().add(Bukkit.createBlockData(getString("palette.interior").substring(6)), 1), 1);
} else { } else {
inner = ConfigUtil.getPalette(getString("palette.interior")).getPalette(); inner = PaletteConfig.fromID(getString("palette.interior")).getPalette();
} }
if(Objects.requireNonNull(getString("palette.walls")).startsWith("BLOCK:")) { if(Objects.requireNonNull(getString("palette.walls")).startsWith("BLOCK:")) {
walls = new BlockPalette().addBlockData(new ProbabilityCollection<BlockData>().add(Bukkit.createBlockData(getString("palette.walls").substring(6)), 1), 1); walls = new BlockPalette().addBlockData(new ProbabilityCollection<BlockData>().add(Bukkit.createBlockData(getString("palette.walls").substring(6)), 1), 1);
} else { } else {
walls = ConfigUtil.getPalette(getString("palette.walls")).getPalette(); walls = PaletteConfig.fromID(getString("palette.walls")).getPalette();
} }
@ -72,4 +83,38 @@ public class CarverConfig extends YamlConfiguration {
id = getString("id"); id = getString("id");
carver = new UserDefinedCarver(this); carver = new UserDefinedCarver(this);
} }
protected static void loadCaves(JavaPlugin main) {
// TODO: Merge all load methods
Logger logger = main.getLogger();
caveConfig.clear();
File oreFolder = new File(main.getDataFolder() + File.separator + "carving");
oreFolder.mkdirs();
try (Stream<Path> paths = Files.walk(oreFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
logger.info("Loading cave from " + path.toString());
try {
CarverConfig cave = new CarverConfig(path.toFile());
caveConfig.put(cave.getID(), cave);
logger.info("ID: " + cave.getID());
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
logger.severe("Configuration error for Carver. ");
logger.severe(e.getMessage());
logger.severe("Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
}
public static List<CarverConfig> getCarvers() {
return new ArrayList<>(caveConfig.values());
}
public static CarverConfig fromID(String id) {
return caveConfig.get(id);
}
} }

View File

@ -1,223 +1,30 @@
package com.dfsek.terra.config; package com.dfsek.terra.config;
import com.dfsek.terra.Terra;
import com.dfsek.terra.biome.TerraBiomeGrid;
import com.dfsek.terra.biome.UserDefinedBiome;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.polydev.gaea.commons.io.FilenameUtils;
import org.polydev.gaea.world.BlockPalette;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ConfigUtil { public class ConfigUtil {
private static final Map<String, BiomeConfig> biomes = new HashMap<>();
private static final Map<String, OreConfig> ores = new HashMap<>();
private static final Map<String, PaletteConfig> palettes = new HashMap<>();
private static final Map<String, FaunaConfig> faunaConfig = new HashMap<>();
private static final Map<String, CarverConfig> caveConfig = new HashMap<>();
public static void loadConfig(JavaPlugin main) { public static void loadConfig(JavaPlugin main) {
Logger logger = main.getLogger(); Logger logger = main.getLogger();
logger.info("Loading config values"); logger.info("Loading config values");
loadOres(main); OreConfig.loadOres(main);
loadPalettes(main); PaletteConfig.loadPalettes(main);
loadCaves(main); CarverConfig.loadCaves(main);
loadFauna(main); FaunaConfig.loadFauna(main);
loadBiomes(main); BiomeConfig.loadBiomes(main);
main.getLogger().info("|--------------------------------------------------|");
main.getLogger().info("Loaded " + biomes.size() + " biomes.");
main.getLogger().info("Loaded " + palettes.size() + " palettes.");
main.getLogger().info("Loaded " + faunaConfig.size() + " fauna objects.");
main.getLogger().info("|--------------------------------------------------|");
WorldConfig.reloadAll(); WorldConfig.reloadAll();
} }
private static void loadPalettes(JavaPlugin main) {
Logger logger = main.getLogger();
palettes.clear();
File paletteFolder = new File(main.getDataFolder() + File.separator + "palettes");
paletteFolder.mkdirs();
try (Stream<Path> paths = Files.walk(paletteFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
logger.info("Loading BlockPalette from " + path.toString());
try {
PaletteConfig grid = new PaletteConfig(path.toFile());
palettes.put(grid.getPaletteID(), grid);
logger.info("Friendly name: " + grid.getFriendlyName());
logger.info("ID: " + grid.getPaletteID());
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
logger.severe("Configuration error for BlockPalette. ");
logger.severe(e.getMessage());
logger.severe("Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
}
private static void loadFauna(JavaPlugin main) {
Logger logger = main.getLogger();
faunaConfig.clear();
File faunaFolder = new File(main.getDataFolder() + File.separator + "fauna");
faunaFolder.mkdirs();
try (Stream<Path> paths = Files.walk(faunaFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
try {
logger.info("Loading fauna from " + path.toString());
FaunaConfig fauna = new FaunaConfig(path.toFile());
faunaConfig.put(fauna.getID(), fauna);
logger.info("Friendly name: " + fauna.getFriendlyName());
logger.info("ID: " + fauna.getID());
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
logger.severe("Configuration error for Fauna. ");
logger.severe(e.getMessage());
logger.severe("Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
}
private static void loadBiomes(JavaPlugin main) {
Logger logger = main.getLogger();
biomes.clear();
File biomeFolder = new File(main.getDataFolder() + File.separator + "biomes");
biomeFolder.mkdirs();
try (Stream<Path> paths = Files.walk(biomeFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
logger.info("Loading biome from " + path.toString());
try {
BiomeConfig biome = new BiomeConfig(path.toFile());
biomes.put(biome.getBiomeID(), biome);
logger.info("Friendly name: " + biome.getFriendlyName());
logger.info("ID: " + biome.getBiomeID());
logger.info("Noise equation: " + biome.get("noise-equation"));
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
logger.severe("Configuration error for Biome. ");
logger.severe(e.getMessage());
logger.severe("Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
}
private static void loadOres(JavaPlugin main) {
Logger logger = main.getLogger();
ores.clear();
File oreFolder = new File(main.getDataFolder() + File.separator + "ores");
oreFolder.mkdirs();
try (Stream<Path> paths = Files.walk(oreFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
logger.info("Loading ore from " + path.toString());
try {
OreConfig ore = new OreConfig(path.toFile());
ores.put(ore.getID(), ore);
logger.info("ID: " + ore.getID());
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
logger.severe("Configuration error for Ore. ");
logger.severe(e.getMessage());
logger.severe("Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
}
private static void loadCaves(JavaPlugin main) {
Logger logger = main.getLogger();
caveConfig.clear();
File oreFolder = new File(main.getDataFolder() + File.separator + "carving");
oreFolder.mkdirs();
try (Stream<Path> paths = Files.walk(oreFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
logger.info("Loading cave from " + path.toString());
try {
CarverConfig cave = new CarverConfig(path.toFile());
caveConfig.put(cave.getID(), cave);
logger.info("ID: " + cave.getID());
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
logger.severe("Configuration error for Carver. ");
logger.severe(e.getMessage());
logger.severe("Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
}
public static BiomeConfig getBiome(String id) {
return biomes.get(id);
}
public static PaletteConfig getPalette(String id) {
return palettes.get(id);
}
public static FaunaConfig getFauna(String id) {
return faunaConfig.get(id);
}
public static CarverConfig getCarver(String id) {
return caveConfig.get(id);
}
public static OreConfig getOre(String id) {
return ores.get(id);
}
public static <E extends Enum<E>> List<E> getElements(List<String> st, Class<E> clazz) { public static <E extends Enum<E>> List<E> getElements(List<String> st, Class<E> clazz) {
return st.stream().map((s) -> E.valueOf(clazz, s)).collect(Collectors.toList()); return st.stream().map((s) -> E.valueOf(clazz, s)).collect(Collectors.toList());
} }
public static List<CarverConfig> getCarvers() {
return new ArrayList<>(caveConfig.values());
}
} }

View File

@ -8,18 +8,26 @@ import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
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 org.polydev.gaea.math.ProbabilityCollection; import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.world.BlockPalette; import org.polydev.gaea.world.BlockPalette;
import org.polydev.gaea.world.Fauna; import org.polydev.gaea.world.Fauna;
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.Map;
import java.util.Random; import java.util.Random;
import java.util.logging.Logger;
import java.util.stream.Stream;
public class FaunaConfig extends YamlConfiguration implements Fauna { public class FaunaConfig extends YamlConfiguration implements Fauna {
private static final Map<String, FaunaConfig> faunaConfig = new HashMap<>();
private BlockPalette faunaPalette = new BlockPalette(); private BlockPalette faunaPalette = new BlockPalette();
private String id; private String id;
private String friendlyName; private String friendlyName;
@ -80,4 +88,36 @@ public class FaunaConfig extends YamlConfiguration implements Fauna {
return true; return true;
} }
protected static void loadFauna(JavaPlugin main) {
// TODO: Merge all load methods
Logger logger = main.getLogger();
faunaConfig.clear();
File faunaFolder = new File(main.getDataFolder() + File.separator + "fauna");
faunaFolder.mkdirs();
try (Stream<Path> paths = Files.walk(faunaFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
try {
logger.info("Loading fauna from " + path.toString());
FaunaConfig fauna = new FaunaConfig(path.toFile());
faunaConfig.put(fauna.getID(), fauna);
logger.info("Friendly name: " + fauna.getFriendlyName());
logger.info("ID: " + fauna.getID());
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
logger.severe("Configuration error for Fauna. ");
logger.severe(e.getMessage());
logger.severe("Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
main.getLogger().info("Loaded " + faunaConfig.size() + " fauna objects.");
}
public static FaunaConfig fromID(String id) {
return faunaConfig.get(id);
}
} }

View File

@ -7,18 +7,27 @@ import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
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.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.commons.io.FilenameUtils;
import org.polydev.gaea.math.FastNoise; import org.polydev.gaea.math.FastNoise;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
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.Objects; import java.util.Objects;
import java.util.Random; import java.util.Random;
import java.util.logging.Logger;
import java.util.stream.Stream;
public class OreConfig extends YamlConfiguration { public class OreConfig extends YamlConfiguration {
private static final Map<String, OreConfig> ores = new HashMap<>();
private BlockData oreData; private BlockData oreData;
private int min; private int min;
private int max; private int max;
@ -86,4 +95,35 @@ public class OreConfig extends YamlConfiguration {
public String getFriendlyName() { public String getFriendlyName() {
return friendlyName; return friendlyName;
} }
protected static void loadOres(JavaPlugin main) {
// TODO: Merge all load methods
Logger logger = main.getLogger();
ores.clear();
File oreFolder = new File(main.getDataFolder() + File.separator + "ores");
oreFolder.mkdirs();
try (Stream<Path> paths = Files.walk(oreFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
logger.info("Loading ore from " + path.toString());
try {
OreConfig ore = new OreConfig(path.toFile());
ores.put(ore.getID(), ore);
logger.info("ID: " + ore.getID());
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
logger.severe("Configuration error for Ore. ");
logger.severe(e.getMessage());
logger.severe("Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
}
public static OreConfig fromID(String id) {
return ores.get(id);
}
} }

View File

@ -5,16 +5,24 @@ import org.bukkit.block.data.BlockData;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
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 org.polydev.gaea.math.ProbabilityCollection; import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.world.BlockPalette; import org.polydev.gaea.world.BlockPalette;
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.Map;
import java.util.logging.Logger;
import java.util.stream.Stream;
public class PaletteConfig extends YamlConfiguration { public class PaletteConfig extends YamlConfiguration {
private static final Map<String, PaletteConfig> palettes = new HashMap<>();
private BlockPalette palette; private BlockPalette palette;
private String paletteID; private String paletteID;
private boolean isEnabled = false; private boolean isEnabled = false;
@ -50,7 +58,7 @@ public class PaletteConfig extends YamlConfiguration {
return paletteID; return paletteID;
} }
public static BlockPalette getPalette(List<Map<?, ?>> maps) throws InvalidConfigurationException { protected static BlockPalette getPalette(List<Map<?, ?>> maps) throws InvalidConfigurationException {
BlockPalette p = new BlockPalette(); BlockPalette p = new BlockPalette();
for(Map<?, ?> m : maps) { for(Map<?, ?> m : maps) {
try { try {
@ -67,4 +75,38 @@ public class PaletteConfig extends YamlConfiguration {
} }
return p; return p;
} }
protected static void loadPalettes(JavaPlugin main) {
// TODO: Merge all load methods
Logger logger = main.getLogger();
palettes.clear();
File paletteFolder = new File(main.getDataFolder() + File.separator + "palettes");
paletteFolder.mkdirs();
try (Stream<Path> paths = Files.walk(paletteFolder.toPath())) {
paths
.filter(path -> FilenameUtils.wildcardMatch(path.toFile().getName(), "*.yml"))
.forEach(path -> {
logger.info("Loading BlockPalette from " + path.toString());
try {
PaletteConfig grid = new PaletteConfig(path.toFile());
palettes.put(grid.getPaletteID(), grid);
logger.info("Friendly name: " + grid.getFriendlyName());
logger.info("ID: " + grid.getPaletteID());
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {
logger.severe("Configuration error for BlockPalette. ");
logger.severe(e.getMessage());
logger.severe("Correct this before proceeding!");
}
});
} catch(IOException e) {
e.printStackTrace();
}
main.getLogger().info("Loaded " + palettes.size() + " palettes.");
}
public static PaletteConfig fromID(String id) {
return palettes.get(id);
}
} }

View File

@ -101,7 +101,7 @@ public class WorldConfig {
String partName = config.getStringList("grids").get(i); String partName = config.getStringList("grids").get(i);
if(partName.startsWith("BIOME:")) { if(partName.startsWith("BIOME:")) {
UserDefinedBiome[][] temp = new UserDefinedBiome[16][16]; UserDefinedBiome[][] temp = new UserDefinedBiome[16][16];
UserDefinedBiome b = ConfigUtil.getBiome(partName.substring(6)).getBiome(); UserDefinedBiome b = BiomeConfig.fromID(partName.substring(6)).getBiome();
for(int x = 0; x < 16; x++) { for(int x = 0; x < 16; x++) {
for(int z = 0; z < 16; z++) { for(int z = 0; z < 16; z++) {
temp[x][z] = b; temp[x][z] = b;

View File

@ -15,7 +15,7 @@ public class CavePopulator extends GenerationPopulator {
@Override @Override
public ChunkGenerator.ChunkData populate(World world, ChunkGenerator.ChunkData chunk, Random random, int chunkX, int chunkZ) { public ChunkGenerator.ChunkData populate(World world, ChunkGenerator.ChunkData chunk, Random random, int chunkX, int chunkZ) {
ProfileFuture cave = TerraProfiler.fromWorld(world).measure("CaveTime"); ProfileFuture cave = TerraProfiler.fromWorld(world).measure("CaveTime");
for(CarverConfig c : ConfigUtil.getCarvers()) { for(CarverConfig c : CarverConfig.getCarvers()) {
chunk = c.getCarver().carve(chunkX, chunkZ, world).merge(chunk, true); chunk = c.getCarver().carve(chunkX, chunkZ, world).merge(chunk, true);
} }
if(cave != null) cave.complete(); if(cave != null) cave.complete();

View File

@ -22,12 +22,12 @@ public class OrePopulator extends GaeaBlockPopulator {
public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk chunk) { public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk chunk) {
Location l = chunk.getBlock(8, 0, 0).getLocation(); Location l = chunk.getBlock(8, 0, 0).getLocation();
Biome b = TerraBiomeGrid.fromWorld(world).getBiome(l.getBlockX(), l.getBlockZ()); Biome b = TerraBiomeGrid.fromWorld(world).getBiome(l.getBlockX(), l.getBlockZ());
for(Map.Entry<OreConfig, MaxMin> e : ((UserDefinedBiome) b).getConfig().getOres().entrySet()) { for(Map.Entry<OreConfig, MaxMin> e : BiomeConfig.fromBiome((UserDefinedBiome) b).getOres().entrySet()) {
int num = e.getValue().get(random); int num = e.getValue().get(random);
for(int i = 0; i < num; i++) { for(int i = 0; i < num; i++) {
int x = random.nextInt(16); int x = random.nextInt(16);
int z = random.nextInt(16); int z = random.nextInt(16);
int y = ((UserDefinedBiome) b).getConfig().getOreHeight(e.getKey()).get(random); int y = BiomeConfig.fromBiome((UserDefinedBiome) b).getOreHeight(e.getKey()).get(random);
e.getKey().doVein(chunk.getBlock(x, y, z).getLocation(), random); e.getKey().doVein(chunk.getBlock(x, y, z).getLocation(), random);
} }
} }