Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
- Fixed potential Null pointer crash
- Ore Overwrite warning only appears when actually overwriting values
- Fixed world features not generating per chance
- Deprecated genTree func is now using standard world feature + density code
  • Loading branch information
UselessBullets committed Nov 13, 2023
1 parent 5e42b55 commit 5702766
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 58 deletions.
9 changes: 5 additions & 4 deletions src/main/java/useless/terrainapi/config/OreConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public class OreConfig extends APIConfig {
* @param range Value from [0, 1], it's the fraction from the bottom of the world to the surface that the ore can generate
*/
public void setOreValues(String modID, Block block, int clusterSize, int chances, float range){
if (this.clusterSize.get(block.getKey()) != null){
String key = block.getKey();
if (this.clusterSize.containsKey(key) && this.getConfigOverride()){
return;
}
if (this.clusterSize.getOrDefault(key, clusterSize) != clusterSize || this.chancesPerChunk.getOrDefault(key, chances) != chances || this.verticalRange.getOrDefault(key, range) != range){
TerrainMain.LOGGER.warn(modID + String.format(" has changed block %s to generate %d blocks with %d chances and a range of %f", block.getKey(), clusterSize, chances, range));
}
setOreValues(block, clusterSize, chances, range);
Expand All @@ -46,9 +50,6 @@ public void setOreValues(String modID, Block block, int clusterSize, int chances
*/
@ApiStatus.Internal
protected void setOreValues(Block block, int clusterSize, int chances, float range){
if (this.clusterSize.containsKey(block.getKey()) && this.getConfigOverride()){
return;
}
this.clusterSize.put(block.getKey(), clusterSize);
this.chancesPerChunk.put(block.getKey(), chances);
this.verticalRange.put(block.getKey(), range);
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/useless/terrainapi/generation/ChunkDecoratorAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jetbrains.annotations.ApiStatus;

import java.util.Random;
import java.util.function.Function;

public abstract class ChunkDecoratorAPI implements ChunkDecorator {
public final World world;
Expand Down Expand Up @@ -62,11 +63,24 @@ public void decorate(Chunk chunk){
public abstract void generateRandomFeatures(Biome biome, int x, int z, Random random, Chunk chunk);
@ApiStatus.Internal
public abstract void generateBiomeFeature(Biome biome, int x, int z, Random random, Chunk chunk);
/**
* @Deprecated Implementation causes world features to be decided before generating multiple instead of each attempt creating patches of "clone" world features
*/
@ApiStatus.Internal
@Deprecated
public void generateWithChancesUnderground(WorldFeature worldFeature, float chances, int rangeY, int x, int z, Random random){
generateWithChancesUnderground(worldFeature, chances, rangeY, x, z, 0, 0, random);
}
@ApiStatus.Internal
public void generateWithChancesUnderground(Function<Parameters, WorldFeature> featureFunction, Parameters parameters, float chances, int rangeY, int x, int z, Random random){
generateWithChancesUnderground(featureFunction, parameters, chances, rangeY, x, z, 0, 0, random);
}

/**
* @Deprecated Implementation causes world features to be decided before generating multiple instead of each attempt creating patches of "clone" world features
*/
@ApiStatus.Internal
@Deprecated
public void generateWithChancesUnderground(WorldFeature worldFeature, float chances, int rangeY, int x, int z, int xOff, int zOff, Random random){
for (int i = 0; i < chances; i++) {
int posX = x + random.nextInt(16) + xOff;
Expand All @@ -76,10 +90,31 @@ public void generateWithChancesUnderground(WorldFeature worldFeature, float chan
}
}
@ApiStatus.Internal
public void generateWithChancesUnderground(Function<Parameters, WorldFeature> featureFunction, Parameters parameters, float chances, int rangeY, int x, int z, int xOff, int zOff, Random random){
for (int i = 0; i < chances; i++) {
int posX = x + random.nextInt(16) + xOff;
int posY = minY + random.nextInt(rangeY);
int posZ = z + random.nextInt(16) + zOff;
featureFunction.apply(parameters).generate(world, random, posX, posY, posZ);
}
}
/**
* @Deprecated Implementation causes world features to be decided before generating multiple instead of each attempt creating patches of "clone" world features
*/
@ApiStatus.Internal
@Deprecated
public void generateWithChancesSurface(WorldFeature worldFeature, float chances, int x, int z, Random random){
generateWithChancesSurface(worldFeature, chances, x, z, 0, 0, random);
}
@ApiStatus.Internal
public void generateWithChancesSurface(Function<Parameters, WorldFeature> featureFunction, Parameters parameters, float chances, int x, int z, Random random){
generateWithChancesSurface(featureFunction, parameters, chances, x, z, 0, 0, random);
}
/**
* @Deprecated Implementation causes world features to be decided before generating multiple instead of each attempt creating patches of "clone" world features
*/
@ApiStatus.Internal
@Deprecated
public void generateWithChancesSurface(WorldFeature worldFeature, float chances, int x, int z, int xOff, int zOff, Random random){
for (int i = 0; i < chances; i++) {
int posX = x + random.nextInt(16) + xOff;
Expand All @@ -89,6 +124,15 @@ public void generateWithChancesSurface(WorldFeature worldFeature, float chances,
}
}
@ApiStatus.Internal
public void generateWithChancesSurface(Function<Parameters, WorldFeature> featureFunction, Parameters parameters, float chances, int x, int z, int xOff, int zOff, Random random){
for (int i = 0; i < chances; i++) {
int posX = x + random.nextInt(16) + xOff;
int posZ = z + random.nextInt(16) + zOff;
int posY = this.world.getHeightValue(posX, posZ);
featureFunction.apply(parameters).generate(world, random, posX, posY, posZ);
}
}
@ApiStatus.Internal
public void freezeSurface(int x, int z){
int oceanY = this.world.getWorldType().getOceanY();
for (int dx = x + 8; dx < x + 8 + 16; ++dx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ public static Integer getTreeDensity(Parameters parameters){
}

/**Vanilla hell tree generator
* @param parameters Parameters Container, takes two custom parameters getTreeFeature function and getTreeDensity function
* @param parameters Parameters Container
* @return null
*/
@Deprecated
public static Void generateTrees(Parameters parameters){
int x = parameters.chunk.xPosition * 16;
int z = parameters.chunk.zPosition * 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import useless.terrainapi.generation.overworld.OverworldRandomFeatures;

import java.util.Random;
import java.util.function.Function;

public class ChunkDecoratorOverworldHellAPI extends ChunkDecoratorAPI {
public final PerlinNoise treeDensityNoise;
Expand Down Expand Up @@ -56,50 +57,55 @@ public void generateStructures(Biome biome, Chunk chunk, Random random){
public void generateOreFeatures(Biome biome, int x, int z, Random random, Chunk chunk){
int featureSize = oreFeatures.featureFunctionsList.size();
for (int i = 0; i < featureSize; i++) {
WorldFeature feature = oreFeatures.featureFunctionsList.get(i)
.apply(new Parameters(parameterBase, oreFeatures.featureParametersList.get(i)));

int density = oreFeatures.densityFunctionsList.get(i)
.apply(new Parameters(parameterBase, oreFeatures.densityParametersList.get(i)));

float rangeModifier = oreFeatures.rangeModifierList.get(i);
generateWithChancesUnderground(feature, density, (int) (rangeModifier * rangeY), x, z, random);
generateWithChancesUnderground(oreFeatures.featureFunctionsList.get(i),
new Parameters(parameterBase, oreFeatures.featureParametersList.get(i)),
density, (int) (rangeModifier * rangeY), x, z, random);
}
}
@ApiStatus.Internal
public void generateRandomFeatures(Biome biome, int x, int z, Random random, Chunk chunk){
int featureSize = randomFeatures.featureFunctionsList.size();
for (int i = 0; i < featureSize; i++) {
if (random.nextInt(randomFeatures.inverseProbabilityList.get(i)) != 0) {continue;}
WorldFeature feature = randomFeatures.featureFunctionsList.get(i)
.apply(new Parameters(parameterBase, randomFeatures.featureParametersList.get(i)));
Function<Parameters, WorldFeature> featureFunction = randomFeatures.featureFunctionsList.get(i);

int density = randomFeatures.densityFunctionsList.get(i)
.apply(new Parameters(parameterBase, randomFeatures.densityParametersList.get(i)));

float rangeModifier = randomFeatures.rangeModifierList.get(i);
if (-1.01 <= rangeModifier && rangeModifier <= -0.99){
generateWithChancesSurface(feature, density, x, z, 8, 8, random);
generateWithChancesSurface(featureFunction,
new Parameters(parameterBase, randomFeatures.featureParametersList.get(i)),
density, x, z, 8, 8, random);
} else {
generateWithChancesUnderground(feature, density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
generateWithChancesUnderground(featureFunction,
new Parameters(parameterBase, randomFeatures.featureParametersList.get(i)),
density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
}
}
}
@ApiStatus.Internal
public void generateBiomeFeature(Biome biome, int x, int z, Random random, Chunk chunk){
int featureSize = biomeFeatures.featureFunctionsList.size();
for (int i = 0; i < featureSize; i++) {
WorldFeature feature = biomeFeatures.featureFunctionsList.get(i)
.apply(new Parameters(parameterBase, biomeFeatures.featureParametersList.get(i)));
Function<Parameters, WorldFeature> featureFunction = biomeFeatures.featureFunctionsList.get(i);

int density = biomeFeatures.densityFunctionsList.get(i)
.apply(new Parameters(parameterBase, biomeFeatures.densityParametersList.get(i)));

float rangeModifier = biomeFeatures.rangeModifierList.get(i);
if (-1.01 <= rangeModifier && rangeModifier <= -0.99){
generateWithChancesSurface(feature, density, x, z, 8, 8, random);
generateWithChancesSurface(featureFunction,
new Parameters(parameterBase, biomeFeatures.featureParametersList.get(i)),
density, x, z, 8, 8, random);
} else {
generateWithChancesUnderground(feature, density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
generateWithChancesUnderground(featureFunction,
new Parameters(parameterBase, biomeFeatures.featureParametersList.get(i)),
density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import useless.terrainapi.generation.nether.NetherRandomFeatures;

import java.util.Random;
import java.util.function.Function;

public class ChunkDecoratorNetherAPI extends ChunkDecoratorAPI {
public static NetherConfig netherConfig = ConfigManager.getConfig("nether", NetherConfig.class);
Expand Down Expand Up @@ -71,50 +72,55 @@ public void generateStructures(Biome biome, Chunk chunk, Random random){
public void generateOreFeatures(Biome biome, int x, int z, Random random, Chunk chunk){
int featureSize = oreFeatures.featureFunctionsList.size();
for (int i = 0; i < featureSize; i++) {
WorldFeature feature = oreFeatures.featureFunctionsList.get(i)
.apply(new Parameters(parameterBase, oreFeatures.featureParametersList.get(i)));

int density = oreFeatures.densityFunctionsList.get(i)
.apply(new Parameters(parameterBase, oreFeatures.densityParametersList.get(i)));

float rangeModifier = oreFeatures.rangeModifierList.get(i);
generateWithChancesUnderground(feature, density, (int) (rangeModifier * rangeY), x, z, random);
generateWithChancesUnderground(oreFeatures.featureFunctionsList.get(i),
new Parameters(parameterBase, oreFeatures.featureParametersList.get(i)),
density, (int) (rangeModifier * rangeY), x, z, random);
}
}
@ApiStatus.Internal
public void generateRandomFeatures(Biome biome, int x, int z, Random random, Chunk chunk){
int featureSize = randomFeatures.featureFunctionsList.size();
for (int i = 0; i < featureSize; i++) {
if (random.nextInt(randomFeatures.inverseProbabilityList.get(i)) != 0) {continue;}
WorldFeature feature = randomFeatures.featureFunctionsList.get(i)
.apply(new Parameters(parameterBase, randomFeatures.featureParametersList.get(i)));
Function<Parameters, WorldFeature> featureFunction = randomFeatures.featureFunctionsList.get(i);

int density = randomFeatures.densityFunctionsList.get(i)
.apply(new Parameters(parameterBase, randomFeatures.densityParametersList.get(i)));

float rangeModifier = randomFeatures.rangeModifierList.get(i);
if (-1.01 <= rangeModifier && rangeModifier <= -0.99){
generateWithChancesSurface(feature, density, x, z, 8, 8, random);
generateWithChancesSurface(featureFunction,
new Parameters(parameterBase, randomFeatures.featureParametersList.get(i)),
density, x, z, 8, 8, random);
} else {
generateWithChancesUnderground(feature, density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
generateWithChancesUnderground(featureFunction,
new Parameters(parameterBase, randomFeatures.featureParametersList.get(i)),
density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
}
}
}
@ApiStatus.Internal
public void generateBiomeFeature(Biome biome, int x, int z, Random random, Chunk chunk){
int featureSize = biomeFeatures.featureFunctionsList.size();
for (int i = 0; i < featureSize; i++) {
WorldFeature feature = biomeFeatures.featureFunctionsList.get(i)
.apply(new Parameters(parameterBase, biomeFeatures.featureParametersList.get(i)));
Function<Parameters, WorldFeature> featureFunction = biomeFeatures.featureFunctionsList.get(i);

int density = biomeFeatures.densityFunctionsList.get(i)
.apply(new Parameters(parameterBase, biomeFeatures.densityParametersList.get(i)));

float rangeModifier = biomeFeatures.rangeModifierList.get(i);
if (-1.01 <= rangeModifier && rangeModifier <= -0.99){
generateWithChancesSurface(feature, density, x, z, 8, 8, random);
generateWithChancesSurface(featureFunction,
new Parameters(parameterBase, biomeFeatures.featureParametersList.get(i)),
density, x, z, 8, 8, random);
} else {
generateWithChancesUnderground(feature, density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
generateWithChancesUnderground(featureFunction,
new Parameters(parameterBase, biomeFeatures.featureParametersList.get(i)),
density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.core.world.biome.Biome;
import org.jetbrains.annotations.NotNull;
import useless.terrainapi.config.OreConfig;
import useless.terrainapi.util.Utilities;

import javax.annotation.Nullable;
import java.util.HashMap;
Expand Down Expand Up @@ -40,15 +41,15 @@ public void addRandomGrassBlock(Biome biome, Block block) {
*/
@Nullable
public Block getRandomGrassBlock(Biome biome){
return Block.getBlockByName(biomeRandomGrassBlock.get(Registries.BIOMES.getKey(biome)));
return Utilities.getBlock(biomeRandomGrassBlock.get(Registries.BIOMES.getKey(biome)));
}

/**
* @return Biome's random grass block, returns defaultValue if there is no entry for the biome
*/
@NotNull
public Block getRandomGrassBlock(Biome biome, Block defaultValue){
Block returnBlock = Block.getBlockByName(biomeRandomGrassBlock.get(Registries.BIOMES.getKey(biome)));
Block returnBlock = Utilities.getBlock(biomeRandomGrassBlock.get(Registries.BIOMES.getKey(biome)));
if (returnBlock == null){
returnBlock = defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ public static Void generateRandomFluid(Parameters parameters){
* @param parameters Parameters Container, takes two custom parameters getTreeFeature function and getTreeDensity function
* @return null
*/
@Deprecated
public static Void generateTrees(Parameters parameters){
int x = parameters.chunk.xPosition * 16;
int z = parameters.chunk.zPosition * 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import useless.terrainapi.generation.overworld.OverworldRandomFeatures;

import java.util.Random;
import java.util.function.Function;

public class ChunkDecoratorOverworldAPI extends ChunkDecoratorAPI {
public static OverworldConfig overworldConfig = ConfigManager.getConfig("overworld", OverworldConfig.class);
Expand Down Expand Up @@ -57,50 +58,55 @@ public void generateStructures(Biome biome, Chunk chunk, Random random){
public void generateOreFeatures(Biome biome, int x, int z, Random random, Chunk chunk){
int featureSize = oreFeatures.featureFunctionsList.size();
for (int i = 0; i < featureSize; i++) {
WorldFeature feature = oreFeatures.featureFunctionsList.get(i)
.apply(new Parameters(parameterBase, oreFeatures.featureParametersList.get(i)));

int density = oreFeatures.densityFunctionsList.get(i)
.apply(new Parameters(parameterBase, oreFeatures.densityParametersList.get(i)));

float rangeModifier = oreFeatures.rangeModifierList.get(i);
generateWithChancesUnderground(feature, density, (int) (rangeModifier * rangeY), x, z, random);
generateWithChancesUnderground(oreFeatures.featureFunctionsList.get(i),
new Parameters(parameterBase, oreFeatures.featureParametersList.get(i)),
density, (int) (rangeModifier * rangeY), x, z, random);
}
}
@ApiStatus.Internal
public void generateRandomFeatures(Biome biome, int x, int z, Random random, Chunk chunk){
int featureSize = randomFeatures.featureFunctionsList.size();
for (int i = 0; i < featureSize; i++) {
if (random.nextInt(randomFeatures.inverseProbabilityList.get(i)) != 0) {continue;}
WorldFeature feature = randomFeatures.featureFunctionsList.get(i)
.apply(new Parameters(parameterBase, randomFeatures.featureParametersList.get(i)));
Function<Parameters, WorldFeature> featureFunction = randomFeatures.featureFunctionsList.get(i);

int density = randomFeatures.densityFunctionsList.get(i)
.apply(new Parameters(parameterBase, randomFeatures.densityParametersList.get(i)));

float rangeModifier = randomFeatures.rangeModifierList.get(i);
if (-1.01 <= rangeModifier && rangeModifier <= -0.99){
generateWithChancesSurface(feature, density, x, z, 8, 8, random);
generateWithChancesSurface(featureFunction,
new Parameters(parameterBase, randomFeatures.featureParametersList.get(i)),
density, x, z, 8, 8, random);
} else {
generateWithChancesUnderground(feature, density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
generateWithChancesUnderground(featureFunction,
new Parameters(parameterBase, randomFeatures.featureParametersList.get(i)),
density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
}
}
}
@ApiStatus.Internal
public void generateBiomeFeature(Biome biome, int x, int z, Random random, Chunk chunk){
int featureSize = biomeFeatures.featureFunctionsList.size();
for (int i = 0; i < featureSize; i++) {
WorldFeature feature = biomeFeatures.featureFunctionsList.get(i)
.apply(new Parameters(parameterBase, biomeFeatures.featureParametersList.get(i)));
Function<Parameters, WorldFeature> featureFunction = biomeFeatures.featureFunctionsList.get(i);

int density = biomeFeatures.densityFunctionsList.get(i)
.apply(new Parameters(parameterBase, biomeFeatures.densityParametersList.get(i)));

float rangeModifier = biomeFeatures.rangeModifierList.get(i);
if (-1.01 <= rangeModifier && rangeModifier <= -0.99){
generateWithChancesSurface(feature, density, x, z, 8, 8, random);
generateWithChancesSurface(featureFunction,
new Parameters(parameterBase, biomeFeatures.featureParametersList.get(i)),
density, x, z, 8, 8, random);
} else {
generateWithChancesUnderground(feature, density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
generateWithChancesUnderground(featureFunction,
new Parameters(parameterBase, biomeFeatures.featureParametersList.get(i)),
density, (int) (rangeModifier * rangeY), x, z, 8, 8, random);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static Integer getTreeDensity(Parameters parameters){
* @param parameters Parameters Container, takes two custom parameters getTreeFeature function and getTreeDensity function
* @return null
*/
@Deprecated
public static Void generateTrees(Parameters parameters){
int x = parameters.chunk.xPosition * 16;
int z = parameters.chunk.zPosition * 16;
Expand Down
Loading

0 comments on commit 5702766

Please sign in to comment.