Worldgen, Compass (temp)

This commit is contained in:
Sebastian Hartte
2020-07-07 01:55:15 +02:00
parent 9220ca0048
commit 5eece068ba
42 changed files with 359 additions and 1665 deletions
@@ -0,0 +1,30 @@
package appeng.worldgen;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.block.BlockState;
import net.minecraft.world.gen.feature.FeatureConfig;
import net.minecraft.world.gen.feature.OreFeatureConfig;
/**
* Extends a {@link OreFeatureConfig} with a chance.
*/
public class ChargedQuartzOreConfig implements FeatureConfig {
public static final Codec<ChargedQuartzOreConfig> CODEC = RecordCodecBuilder.create((instance) -> instance.group(
BlockState.CODEC.fieldOf("target").forGetter((config) -> config.target),
BlockState.CODEC.fieldOf("state").forGetter((config) -> config.state),
Codec.FLOAT.fieldOf("chance").withDefault(0f).forGetter((config) -> config.chance))
.apply(instance, ChargedQuartzOreConfig::new));
public final BlockState target;
public final BlockState state;
public final float chance;
public ChargedQuartzOreConfig(BlockState target, BlockState state, float chance) {
this.target = target;
this.state = state;
this.chance = chance;
}
}
@@ -0,0 +1,56 @@
package appeng.worldgen;
import com.mojang.serialization.Codec;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.Heightmap;
import net.minecraft.world.ServerWorldAccess;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.Feature;
import java.util.Random;
import java.util.function.Predicate;
/**
* Extends {@link net.minecraft.world.gen.feature.OreFeature} by also allowing for a replacement
* chance. In addition, the feature will check every block in the chunk.
*/
public class ChargedQuartzOreFeature extends Feature<ChargedQuartzOreConfig> {
public static final ChargedQuartzOreFeature INSTANCE = new ChargedQuartzOreFeature(ChargedQuartzOreConfig.CODEC);
public ChargedQuartzOreFeature(Codec<ChargedQuartzOreConfig> codec) {
super(codec);
}
@Override
public boolean generate(ServerWorldAccess worldIn,
net.minecraft.world.gen.StructureAccessor structureAccessor,
ChunkGenerator generator,
Random rand,
BlockPos pos,
ChargedQuartzOreConfig config) {
ChunkPos chunkPos = new ChunkPos(pos);
BlockPos.Mutable bpos = new BlockPos.Mutable();
int height = worldIn.getTopY(Heightmap.Type.WORLD_SURFACE_WG, pos.getX(), pos.getZ());
Chunk chunk = worldIn.getChunk(pos);
for (int y = 0; y < height; y++) {
bpos.setY(y);
for (int x = chunkPos.getStartX(); x <= chunkPos.getEndX(); x++) {
bpos.setX(x);
for (int z = chunkPos.getStartZ(); z <= chunkPos.getEndZ(); z++) {
bpos.setZ(z);
if (chunk.getBlockState(bpos).getBlock() == config.target.getBlock()
&& rand.nextFloat() < config.chance) {
chunk.setBlockState(bpos, config.state, false);
}
}
}
}
return true;
}
}