Skip to content

Commit

Permalink
Added worldgen utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragonoidzero committed Apr 3, 2021
1 parent e9602ae commit 3fadaa0
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.2

# Mod Properties
mod_version = 1.2.3
mod_version = 1.3.0
maven_group = azzy.fabric
archives_base_name = incubus-core

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package azzy.fabric.incubus_core.worldgen;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.block.BlockState;
import net.minecraft.world.gen.UniformIntDistribution;
import net.minecraft.world.gen.feature.FeatureConfig;

public class BiFeatureConfig implements FeatureConfig {
@SuppressWarnings("CodeBlock2Expr")
public static final Codec<BiFeatureConfig> CODEC = RecordCodecBuilder.create(biFeatureConfigInstance -> {
return biFeatureConfigInstance.group(BlockState.CODEC.fieldOf("primState").forGetter((diskFeatureConfig) -> {
return diskFeatureConfig.primaryState;
}), BlockState.CODEC.fieldOf("secState").forGetter((diskFeatureConfig) -> {
return diskFeatureConfig.secondaryState;
}), Codec.floatRange(0, 1).fieldOf("chance").forGetter((diskFeatureConfig) -> {
return diskFeatureConfig.chance;
}), UniformIntDistribution.CODEC.fieldOf("scale").forGetter((diskFeatureConfig) -> {
return diskFeatureConfig.scale;
})).apply(biFeatureConfigInstance, BiFeatureConfig::new);
});

public final BlockState primaryState;
public final BlockState secondaryState;
public final Float chance;
public final UniformIntDistribution scale;

public BiFeatureConfig(BlockState primaryState, BlockState secondaryState, float chance, UniformIntDistribution scale) {
this.primaryState = primaryState;
this.secondaryState = secondaryState;
this.chance = chance;
this.scale = scale;
}
}
90 changes: 90 additions & 0 deletions src/main/java/azzy/fabric/incubus_core/worldgen/CrustFeature.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package azzy.fabric.incubus_core.worldgen;

import com.mojang.serialization.Codec;
import net.minecraft.block.BlockState;
import net.minecraft.tag.FluidTags;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.Feature;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

public class CrustFeature extends Feature<BiFeatureConfig> {

Queue<BlockPos> centers;

public CrustFeature(Codec<BiFeatureConfig> configCodec) {
super(configCodec);
}

@Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos center, BiFeatureConfig config) {
center = center.down();

int radius = config.scale.getValue(random);

centers = generateCircleAndCenters(world, center, radius, random, config);

radius = config.scale.getValue(random);

Queue<BlockPos> holder = new LinkedList<>();

for (BlockPos blockPos : centers) {
holder.addAll(generateCircleAndCenters(world, blockPos, radius, random, config));
}

centers.clear();
centers.addAll(holder);

radius = config.scale.getValue(random);

for (BlockPos newCenter : centers) {
generateCircleAndCenters(world, newCenter, radius, random, config);
}

return true;
}

private Queue<BlockPos> generateCircleAndCenters(StructureWorldAccess world, BlockPos center, int radius, Random random, BiFeatureConfig config) {
Queue<BlockPos> centers = new LinkedList<>();
BlockPos.iterateOutwards(center, radius, 0, radius).forEach(pos -> {
if(pos.getSquaredDistance(center) > Math.pow(radius, 2))
return;

boolean add = false;

if(pos.getManhattanDistance(center) == radius && random.nextFloat() < 0.1F)
add = true;

if(world.isAir(pos) || world.isWater(pos) || !world.isWater(pos.up()) || !world.isAir(pos.up())) {
boolean fail = true;
int tries = 5;

while(tries >= -4) {
tries--;
pos = pos.offset(Direction.Axis.Y, tries);
if(!world.isAir(pos) && !world.isWater(pos) && (world.isWater(pos.up()) || world.isAir(pos.up()))) {
fail = false;
break;
}
}

if(fail)
return;
}

if(add)
centers.add(pos);

BlockState state = random.nextFloat() <= config.chance ? config.secondaryState : config.primaryState;

world.setBlockState(pos, state, 2);
world.getBlockTickScheduler().schedule(pos, state.getBlock(), 0);
});
return centers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package azzy.fabric.incubus_core.worldgen;

import com.mojang.serialization.Codec;
import net.minecraft.block.BlockState;
import net.minecraft.tag.FluidTags;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.Feature;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

public class UnderwaterCrustFeature extends Feature<BiFeatureConfig> {

Queue<BlockPos> centers;

public UnderwaterCrustFeature(Codec<BiFeatureConfig> configCodec) {
super(configCodec);
}

@Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos center, BiFeatureConfig config) {
if(!world.getFluidState(center).isIn(FluidTags.WATER))
return false;

center = center.down();

int radius = config.scale.getValue(random);

centers = generateCircleAndCenters(world, center, radius, random, config);

radius = config.scale.getValue(random);

Queue<BlockPos> holder = new LinkedList<>();

for (BlockPos blockPos : centers) {
holder.addAll(generateCircleAndCenters(world, blockPos, radius, random, config));
}

centers.clear();
centers.addAll(holder);

radius = config.scale.getValue(random);

for (BlockPos newCenter : centers) {
generateCircleAndCenters(world, newCenter, radius, random, config);
}

return true;
}

private Queue<BlockPos> generateCircleAndCenters(StructureWorldAccess world, BlockPos center, int radius, Random random, BiFeatureConfig config) {
Queue<BlockPos> centers = new LinkedList<>();
BlockPos.iterateOutwards(center, radius, 0, radius).forEach(pos -> {
if(pos.getSquaredDistance(center) > Math.pow(radius, 2))
return;

boolean add = false;

if(pos.getManhattanDistance(center) == radius && random.nextFloat() < 0.1F)
add = true;

if(world.isWater(pos) || !world.isWater(pos.up())) {
boolean fail = true;
int tries = 5;

while(tries >= -4) {
tries--;
pos = pos.offset(Direction.Axis.Y, tries);
if(!world.isAir(pos) && !world.isWater(pos) && world.isWater(pos.up())) {
fail = false;
break;
}
}

if(fail)
return;
}

if(add)
centers.add(pos);

BlockState state = random.nextFloat() <= config.chance ? config.secondaryState : config.primaryState;

world.setBlockState(pos, state, 2);
world.getBlockTickScheduler().schedule(pos, state.getBlock(), 0);
});
return centers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package azzy.fabric.incubus_core.worldgen;

import com.mojang.serialization.Codec;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.TallSeagrassBlock;
import net.minecraft.block.enums.DoubleBlockHalf;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.Heightmap;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.Feature;

import java.util.Random;

public class UnderwaterPlantFeature extends Feature<BiFeatureConfig> {

public UnderwaterPlantFeature(Codec<BiFeatureConfig> codec) {
super(codec);
}

public boolean generate(StructureWorldAccess structureWorldAccess, ChunkGenerator chunkGenerator, Random random, BlockPos pos, BiFeatureConfig config) {
boolean success = false;
int i = random.nextInt(8) - random.nextInt(8);
int j = random.nextInt(8) - random.nextInt(8);
int k = structureWorldAccess.getTopY(Heightmap.Type.OCEAN_FLOOR, pos.getX() + i, pos.getZ() + j);
BlockPos blockPos2 = new BlockPos(pos.getX() + i, k, pos.getZ() + j);
if (structureWorldAccess.getBlockState(blockPos2).isOf(Blocks.WATER)) {
boolean tall = random.nextDouble() < config.chance;
BlockState blockState = tall ? config.secondaryState : config.primaryState;
if (blockState.canPlaceAt(structureWorldAccess, blockPos2)) {
if (tall && blockState.getBlock() instanceof TallSeagrassBlock) {
BlockState blockState2 = blockState.with(TallSeagrassBlock.HALF, DoubleBlockHalf.UPPER);
BlockPos blockPos3 = blockPos2.up();
if (structureWorldAccess.getBlockState(blockPos3).isOf(Blocks.WATER)) {
structureWorldAccess.setBlockState(blockPos2, blockState, 2);
structureWorldAccess.setBlockState(blockPos3, blockState2, 2);
}
} else {
structureWorldAccess.setBlockState(blockPos2, blockState, 2);
}

success = true;
}
}

return success;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package azzy.fabric.incubus_core.worldgen;

import com.mojang.serialization.Codec;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.TickPriority;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.SingleStateFeatureConfig;

import java.util.Random;

public class UnderwaterStateFeature extends Feature<SingleStateFeatureConfig> {

public UnderwaterStateFeature(Codec<SingleStateFeatureConfig> configCodec) {
super(configCodec);
}

@Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos pos, SingleStateFeatureConfig config) {
if(!world.isWater(pos)) {
return false;
}

if(!world.isWater(pos.down())) {
world.setBlockState(pos.down(), config.state, 2);
world.getBlockTickScheduler().schedule(pos.down(), config.state.getBlock(), 0, TickPriority.EXTREMELY_HIGH);
return true;
}

return false;
}
}
6 changes: 6 additions & 0 deletions src/main/resources/data/c/tags/items/ancient_debris.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:ancient_debris"
]
}
6 changes: 6 additions & 0 deletions src/main/resources/data/c/tags/items/netherite_ingots.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:netherite_ingot"
]
}

0 comments on commit 3fadaa0

Please sign in to comment.