Refactored Biome Config handling (#63)

This commit is contained in:
Frank 2022-01-17 14:06:22 +01:00
parent 3584432a96
commit 05ed11978d
9 changed files with 389 additions and 172 deletions

View file

@ -25,7 +25,7 @@ import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class BCLBiome {
public class BCLBiome extends BCLBiomeSettings {
private final List<ConfiguredStructureFeature> structures = Lists.newArrayList();
private final WeightedList<BCLBiome> subbiomes = new WeightedList<>();
private final Map<String, Object> customData = Maps.newHashMap();
@ -35,13 +35,6 @@ public class BCLBiome {
private Consumer<Biome> surfaceInit;
private BCLBiome biomeParent;
private Biome actualBiome;
private BCLBiome edge;
private float terrainHeight = 0.1F;
private float fogDensity = 1.0F;
private float genChance = 1.0F;
private int edgeSize = 0;
private boolean vertical;
/**
* Create wrapper for existing biome using its {@link ResourceLocation} identifier.
@ -56,7 +49,7 @@ public class BCLBiome {
* @param biomeID {@link ResourceLocation} biome ID.
*/
public BCLBiome(ResourceLocation biomeID) {
this(biomeID, BuiltinRegistries.BIOME.get(biomeID));
this(biomeID, BuiltinRegistries.BIOME.get(biomeID), null);
}
/**
@ -64,13 +57,36 @@ public class BCLBiome {
* @param biome {@link Biome} to wrap.
*/
public BCLBiome(Biome biome) {
this(BuiltinRegistries.BIOME.getKey(biome), biome);
this(biome, null);
}
/**
* Create wrapper for existing biome using biome instance from {@link BuiltinRegistries}.
* @param biome {@link Biome} to wrap.
* @param settings The Settings for this Biome or {@code null} if you want to apply default settings
*/
public BCLBiome(Biome biome, VanillaBiomeSettings settings) {
this(BuiltinRegistries.BIOME.getKey(biome), biome, settings);
}
public BCLBiome(ResourceLocation biomeID, Biome biome) {
this(biomeID, biome, null);
}
/**
* Create a new Biome
* @param biomeID {@link ResourceLocation} biome ID.
* @param biome {@link Biome} to wrap.
* @param defaults The Settings for this Biome or null if you want to apply the defaults
*/
public BCLBiome(ResourceLocation biomeID, Biome biome, BCLBiomeSettings defaults) {
this.subbiomes.add(this, 1.0F);
this.biomeID = biomeID;
this.biome = biome;
if (defaults !=null){
defaults.applyWithDefaults(this);
}
}
/**
@ -87,30 +103,12 @@ public class BCLBiome {
* @param edge {@link BCLBiome} as the edge biome.
* @return same {@link BCLBiome}.
*/
public BCLBiome setEdge(BCLBiome edge) {
BCLBiome setEdge(BCLBiome edge) {
this.edge = edge;
edge.biomeParent = this;
return this;
}
/**
* Getter for biome edge size.
* @return edge size in blocks.
*/
public int getEdgeSize() {
return edgeSize;
}
/**
* Set edges size for this biome. Size is in blocks.
* @param size as a float value.
* @return same {@link BCLBiome}.
*/
public BCLBiome setEdgeSize(int size) {
edgeSize = size;
return this;
}
/**
* Adds sub-biome into this biome instance. Biome chance will be interpreted as a sub-biome generation chance.
* Biome itself has chance 1.0 compared to all its sub-biomes.
@ -167,23 +165,6 @@ public class BCLBiome {
return biomeID;
}
/**
* Getter for fog density, used in custom for renderer.
* @return fog density as a float.
*/
public float getFogDensity() {
return fogDensity;
}
/**
* Sets fog density for this biome.
* @param fogDensity
* @return same {@link BCLBiome}.
*/
public BCLBiome setFogDensity(float fogDensity) {
this.fogDensity = fogDensity;
return this;
}
/**
* Getter for biome from buil-in registry. For datapack biomes will be same as actual biome.
@ -201,23 +182,7 @@ public class BCLBiome {
return this.actualBiome;
}
/**
* Getter for biome generation chance, used in {@link ru.bclib.world.generator.BiomePicker} and in custom generators.
* @return biome generation chance as float.
*/
public float getGenChance() {
return this.genChance;
}
/**
* Set gen chance for this biome, default value is 1.0.
* @param genChance chance of this biome to be generated.
* @return same {@link BCLBiome}.
*/
public BCLBiome setGenChance(float genChance) {
this.genChance = genChance;
return this;
}
/**
* Recursively update biomes to correct world biome registry instances, for internal usage only.
@ -289,50 +254,6 @@ public class BCLBiome {
return this;
}
/**
* Setter for terrain height, can be used in custom terrain generator.
* @param terrainHeight a relative float terrain height value.
* @return same {@link BCLBiome}.
*/
public BCLBiome setTerrainHeight(float terrainHeight) {
this.terrainHeight = terrainHeight;
return this;
}
/**
* Getter for terrain height, can be used in custom terrain generator.
* @return terrain height.
*/
public float getTerrainHeight() {
return terrainHeight;
}
/**
* Set biome vertical distribution (for tall Nether only).
* @return same {@link BCLBiome}.
*/
public BCLBiome setVertical() {
return setVertical(true);
}
/**
* Set biome vertical distribution (for tall Nether only).
* @param vertical {@code boolean} value.
* @return same {@link BCLBiome}.
*/
public BCLBiome setVertical(boolean vertical) {
this.vertical = vertical;
return this;
}
/**
* Checks if biome is vertical, for tall Nether only (or for custom generators).
* @return is biome vertical or not.
*/
public boolean isVertical() {
return vertical;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
@ -406,29 +327,62 @@ public class BCLBiome {
}
private boolean didLoadConfig = false;
/**
* For internal use.
* Set Biome configuartion from Config. This method is called for all Biomes that get registered
* to a {@link ru.bclib.world.generator.BCLBiomeSource}.
*
* @return This instance
*/
public BCLBiome setupFromConfig() {
if (didLoadConfig) return this;
didLoadConfig = true;
String group = this.configGroup();
float chance = Configs.BIOMES_CONFIG.getFloat(group, "generation_chance", this.getGenChance());
float fog = Configs.BIOMES_CONFIG.getFloat(group, "fog_density", this.getFogDensity());
this.setGenChance(chance).setFogDensity(fog);
if (this.getEdge()!=null){
int edgeSize = Configs.BIOMES_CONFIG.getInt(group, "edge_size", this.getEdgeSize());
this.setEdgeSize(edgeSize);
}
Configs.BIOMES_CONFIG.saveChanges();
return this;
}
// /**
// * Set gen chance for this biome, default value is 1.0.
// * @param genChance chance of this biome to be generated.
// * @return same {@link BCLBiome}.
// */
// public BCLBiome setGenChance(float genChance) {
// super.setGenChance(genChance);
// return this;
// }
//
// /**
// * Setter for terrain height, can be used in custom terrain generator.
// * @param terrainHeight a relative float terrain height value.
// * @return same {@link BCLBiome}.
// */
// public BCLBiome setTerrainHeight(float terrainHeight) {
// super.setTerrainHeight(genChance);
// return this;
// }
//
// /**
// * Set biome vertical distribution (for tall Nether only).
// * @return same {@link BCLBiome}.
// */
// public BCLBiome setVertical() {
// return setVertical(true);
// }
//
// /**
// * Set biome vertical distribution (for tall Nether only).
// * @param vertical {@code boolean} value.
// * @return same {@link BCLBiome}.
// */
// public BCLBiome setVertical(boolean vertical) {
// super.setVertical(vertical);
// return this;
// }
//
// /**
// * Sets fog density for this biome.
// * @param fogDensity
// * @return same {@link BCLBiome}.
// */
// public BCLBiome setFogDensity(float fogDensity) {
// super.setFogDensity(fogDensity);
// return this;
// }
//
// /**
// * Set edges size for this biome. Size is in blocks.
// * @param size as a float value.
// * @return same {@link BCLBiome}.
// */
// public BCLBiome setEdgeSize(int size) {
// edgeSize = size;
// return this;
// }
}

View file

@ -0,0 +1,185 @@
package ru.bclib.world.biomes;
import net.minecraft.world.level.biome.Biome;
import ru.bclib.config.Configs;
import ru.bclib.world.biomes.VanillaBiomeSettings.Builder;
public class BCLBiomeSettings {
public static Builder createBCL(){
return new Builder();
}
public static class Builder extends CommonBuilder<BCLBiomeSettings, Builder>{
public Builder(){
super(new BCLBiomeSettings());
}
}
public static class CommonBuilder<T extends BCLBiomeSettings, R extends CommonBuilder>{
private final T storage;
CommonBuilder(T storage){
this.storage = storage;
}
public T build(){
return storage;
}
/**
* Set gen chance for this biome, default value is 1.0.
* @param genChance chance of this biome to be generated.
* @return same {@link BCLBiomeSettings}.
*/
public R setGenChance(float genChance) {
storage.genChance = genChance;
return (R)this;
}
/**
* Setter for terrain height, can be used in custom terrain generator.
* @param terrainHeight a relative float terrain height value.
* @return same {@link Builder}.
*/
public R setTerrainHeight(float terrainHeight) {
storage.terrainHeight = terrainHeight;
return (R)this;
}
/**
* Set biome vertical distribution (for tall Nether only).
* @return same {@link Builder}.
*/
public R setVertical() {
return setVertical(true);
}
/**
* Set biome vertical distribution (for tall Nether only).
* @param vertical {@code boolean} value.
* @return same {@link Builder}.
*/
public R setVertical(boolean vertical) {
storage.vertical = vertical;
return (R)this;
}
/**
* Set edges size for this biome. Size is in blocks.
* @param size as a float value.
* @return same {@link Builder}.
*/
public R setEdgeSize(int size) {
storage.edgeSize = size;
return (R)this;
}
/**
* Set edges:biome for this biome.
* @param edge The {@link Biome}.
* @return same {@link Builder}.
*/
public R setEdge(BCLBiome edge) {
storage.edge = edge;
return (R)this;
}
/**
* Sets fog density for this biome.
* @param fogDensity
* @return same {@link Builder}.
*/
public R setFogDensity(float fogDensity) {
storage.fogDensity = fogDensity;
return (R)this;
}
}
protected BCLBiomeSettings(){
this.terrainHeight = 0.1F;
this.fogDensity = 1.0F;
this.genChance = 1.0F;
this.edgeSize = 0;
this.vertical = false;
this.edge = null;
}
float terrainHeight;
float fogDensity;
float genChance;
int edgeSize;
boolean vertical;
BCLBiome edge;
/**
* Getter for biome generation chance, used in {@link ru.bclib.world.generator.BiomePicker} and in custom generators.
* @return biome generation chance as float.
*/
public float getGenChance() {
return this.genChance;
}
/**
* Checks if biome is vertical, for tall Nether only (or for custom generators).
* @return is biome vertical or not.
*/
public boolean isVertical() {
return vertical;
}
/**
* Getter for terrain height, can be used in custom terrain generator.
* @return terrain height.
*/
public float getTerrainHeight() {
return terrainHeight;
}
/**
* Getter for fog density, used in custom for renderer.
* @return fog density as a float.
*/
public float getFogDensity() {
return fogDensity;
}
/**
* Getter for biome edge size.
* @return edge size in blocks.
*/
public int getEdgeSize() {
return edgeSize;
}
/**
* Getter for edge-biome.
* @return The assigned edge biome.
*/
public BCLBiome getEdge() {
return edge;
}
/**
* Load values from Config and apply to the passed Biome. The Default values for the loaded settings
* are derifed from the values store in this object
* @param biome {@link BCLBiome} to assign values to
*/
public void applyWithDefaults(BCLBiome biome){
final String group = biome.configGroup();
biome.genChance = Configs.BIOMES_CONFIG.getFloat(group, "generation_chance", this.genChance);
if (edge!=null){
biome.edgeSize = Configs.BIOMES_CONFIG.getInt(group, "edge_size", this.edgeSize);
if (edgeSize>0) {
biome.setEdge(edge);
}
}
if (!(this instanceof VanillaBiomeSettings)){
biome.fogDensity = Configs.BIOMES_CONFIG.getFloat(group, "fog_density", this.fogDensity);
biome.vertical = Configs.BIOMES_CONFIG.getBoolean(group, "vertical", this.vertical);
biome.terrainHeight = Configs.BIOMES_CONFIG.getFloat(group, "terrain_height", this.terrainHeight);
}
Configs.BIOMES_CONFIG.saveChanges();
}
}

View file

@ -0,0 +1,14 @@
package ru.bclib.world.biomes;
public class VanillaBiomeSettings extends BCLBiomeSettings{
public static class Builder extends BCLBiomeSettings.CommonBuilder<VanillaBiomeSettings, VanillaBiomeSettings.Builder>{
public Builder(){
super(new VanillaBiomeSettings());
}
}
public static Builder createVanilla(){
return new Builder();
}
}

View file

@ -5,6 +5,7 @@ import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.BiomeSource;
import ru.bclib.api.biomes.BiomeAPI;
import ru.bclib.world.biomes.BCLBiome;
import ru.bclib.world.biomes.BCLBiomeSettings;
import java.util.List;
@ -25,14 +26,4 @@ public abstract class BCLBiomeSource extends BiomeSource {
BiomeAPI.initRegistry(biomeRegistry);
}
/**
* Set Biome configuartion from Config
* @param bclBiome The biome you want to configure
* @return The input biome
*/
public static BCLBiome setupFromConfig(BCLBiome bclBiome) {
bclBiome.setupFromConfig();
return bclBiome;
}
}

View file

@ -58,7 +58,7 @@ public class BCLibEndBiomeSource extends BCLBiomeSource {
String group = key.getNamespace() + "." + key.getPath();
if (!BiomeAPI.hasBiome(key)) {
BCLBiome bclBiome = setupFromConfig(new BCLBiome(key, biome));
BCLBiome bclBiome = new BCLBiome(key, biome);
if (includeVoid.contains(key.toString())) {
BiomeAPI.END_VOID_BIOME_PICKER.addBiomeMutable(bclBiome);
@ -70,8 +70,6 @@ public class BCLibEndBiomeSource extends BCLBiomeSource {
else {
BCLBiome bclBiome = BiomeAPI.getBiome(key);
if (bclBiome != BiomeAPI.EMPTY_BIOME) {
setupFromConfig(bclBiome);
if (bclBiome.getParentBiome() == null) {
if (!BiomeAPI.END_LAND_BIOME_PICKER.containsImmutable(key) && !BiomeAPI.END_VOID_BIOME_PICKER.containsImmutable(key)) {
if (includeVoid.contains(key.toString())) {

View file

@ -64,14 +64,12 @@ public class BCLibNetherBiomeSource extends BCLBiomeSource {
ResourceLocation key = biomeRegistry.getKey(biome);
if (!BiomeAPI.hasBiome(key)) {
BCLBiome bclBiome = setupFromConfig(new BCLBiome(key, biome));
BCLBiome bclBiome = new BCLBiome(key, biome);
BiomeAPI.NETHER_BIOME_PICKER.addBiomeMutable(bclBiome);
}
else {
BCLBiome bclBiome = BiomeAPI.getBiome(key);
if (bclBiome != BiomeAPI.EMPTY_BIOME) {
setupFromConfig(bclBiome);
if (bclBiome.getParentBiome() == null) {
if (!BiomeAPI.NETHER_BIOME_PICKER.containsImmutable(key)) {
BiomeAPI.NETHER_BIOME_PICKER.addBiomeMutable(bclBiome);