Skip to content

Commit

Permalink
Begin implementing ore nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rearth committed Apr 19, 2024
1 parent 5755d71 commit 79c852c
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/main/java/rearth/oritech/init/world/FeatureContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
import net.minecraft.util.Identifier;
import net.minecraft.world.gen.GenerationStep;
import rearth.oritech.Oritech;
import rearth.oritech.init.world.features.oil.OilSpringFeature;
import rearth.oritech.init.world.features.oil.OilSpringFeatureConfig;
import rearth.oritech.init.world.features.resourcenode.ResourceNodeFeature;
import rearth.oritech.init.world.features.resourcenode.ResourceNodeFeatureConfig;

public class FeatureContent {

public static final OilSpringFeature OIL_SPRING_FEATURE = new OilSpringFeature(OilSpringFeatureConfig.OIL_FEATURE_ENDEC.codec());
public static final ResourceNodeFeature RESOURCE_NODE_FEATURE = new ResourceNodeFeature(ResourceNodeFeatureConfig.NODE_FEATURE_ENDEC.codec());

public static void initialize() {
Registry.register(Registries.FEATURE, new Identifier(Oritech.MOD_ID, "oil_spring"), OIL_SPRING_FEATURE);
Registry.register(Registries.FEATURE, new Identifier(Oritech.MOD_ID, "resource_node"), RESOURCE_NODE_FEATURE);

BiomeModifications.addFeature(
BiomeSelectors.foundInOverworld(),
Expand All @@ -27,6 +33,20 @@ public static void initialize() {
BiomeSelectors.tag(BiomeTags.VILLAGE_DESERT_HAS_STRUCTURE),
GenerationStep.Feature.LAKES,
RegistryKey.of(RegistryKeys.PLACED_FEATURE, new Identifier(Oritech.MOD_ID, "oil_spring_desert")));

BiomeModifications.addFeature(
BiomeSelectors.foundInOverworld(),
GenerationStep.Feature.TOP_LAYER_MODIFICATION,
RegistryKey.of(RegistryKeys.PLACED_FEATURE, new Identifier(Oritech.MOD_ID, "resource_node_common")));

BiomeModifications.addFeature(
BiomeSelectors.foundInOverworld(),
GenerationStep.Feature.TOP_LAYER_MODIFICATION,
RegistryKey.of(RegistryKeys.PLACED_FEATURE, new Identifier(Oritech.MOD_ID, "resource_node_rare")));
BiomeModifications.addFeature(
BiomeSelectors.foundInOverworld(),
GenerationStep.Feature.TOP_LAYER_MODIFICATION,
RegistryKey.of(RegistryKeys.PLACED_FEATURE, new Identifier(Oritech.MOD_ID, "resource_node_other")));
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rearth.oritech.init.world;
package rearth.oritech.init.world.features.oil;

import com.mojang.serialization.Codec;
import net.minecraft.block.Blocks;
Expand Down Expand Up @@ -26,7 +26,7 @@ public boolean generate(FeatureContext<OilSpringFeatureConfig> context) {
for (int y = 0; y < world.getHeight(); y++) {
testPos = testPos.up();

if (world.getBlockState(testPos).isIn(BlockTags.DIRT)) {
if (world.getBlockState(testPos).isIn(BlockTags.DIRT) || world.getBlockState(testPos).isIn(BlockTags.SAND)) {
if (world.getBlockState(testPos.up()).isOf(Blocks.AIR)) {
placeStructure(testPos, context);
return true;
Expand All @@ -40,8 +40,6 @@ public boolean generate(FeatureContext<OilSpringFeatureConfig> context) {

private void placeStructure(BlockPos surfacePos, FeatureContext<OilSpringFeatureConfig> context) {

System.out.println("placing at" + surfacePos);

var random = context.getRandom();
var config = context.getConfig();
var state = Registries.BLOCK.get(config.blockId()).getDefaultState();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rearth.oritech.init.world;
package rearth.oritech.init.world.features.oil;

import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.endec.BuiltInEndecs;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package rearth.oritech.init.world.features.resourcenode;

import com.mojang.serialization.Codec;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.registry.Registries;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.util.FeatureContext;

import java.util.List;

public class ResourceNodeFeature extends Feature<ResourceNodeFeatureConfig> {

public ResourceNodeFeature(Codec<ResourceNodeFeatureConfig> configCodec) {
super(configCodec);
}

@Override
public boolean generate(FeatureContext<ResourceNodeFeatureConfig> context) {

var world = context.getWorld();
var origin = context.getOrigin();

if (world.isClient()) return false;

var bedrockFound = false;
var testPos = new BlockPos(origin);
var deepNodePos = testPos;
for (int y = world.getBottomY(); y < world.getHeight(); y++) {
testPos = testPos.up();
var testState = world.getBlockState(testPos);

if (!testState.isOf(Blocks.BEDROCK) && !bedrockFound) {
deepNodePos = testPos;
bedrockFound = true;
} else if (bedrockFound && testState.isOf(Blocks.BEDROCK)) {
bedrockFound = false;
// reset if another bedrock layer occurs
}

if (testState.isIn(BlockTags.DIRT) || testState.isIn(BlockTags.SAND)) {
if (world.getBlockState(testPos.up()).isOf(Blocks.AIR)) {
placeSurfaceBoulder(testPos, context);
placeBedrockNode(deepNodePos, context);
System.out.println("placing resource node at " + testPos + " with deep " + deepNodePos);
return true;
}
}

}

return false;
}

private BlockState getRandomBlockFromList(List<Identifier> list, Random random) {
return Registries.BLOCK.get(getRandomFromList(list, random)).getDefaultState();
}

private Identifier getRandomFromList(List<Identifier> list, Random random) {
return list.get(random.nextInt(list.size()));
}

private void placeBedrockNode(BlockPos startPos, FeatureContext<ResourceNodeFeatureConfig> context) {

var world = context.getWorld();
var random = context.getRandom();
var ores = context.getConfig().nodeOres();

var radius = context.getConfig().nodeSize();
var overlayBlock = Registries.BLOCK.get(context.getConfig().overlayBlock()).getDefaultState();

for (int x = 0; x < radius; x++) {
for (int y = 0; y < radius; y++) {
var pos = startPos.add(x, 0, y);
world.setBlockState(pos, getRandomBlockFromList(ores, random), 0x10);
}
}

// overlay it with something
for (int x = -1; x <= radius; x++) {
for (int y = -1; y <= radius; y++) {
var pos = startPos.add(x, 0, y);
for (int i = 0; i < context.getConfig().overlayHeight(); i++) {
world.setBlockState(pos.up(1 + i), overlayBlock, 0x10);
}
}
}
}

private void placeSurfaceBoulder(BlockPos startPos, FeatureContext<ResourceNodeFeatureConfig> context) {

var world = context.getWorld();
var random = context.getRandom();
var movedCenter = new Vec3d(startPos.getX() - random.nextFloat(), startPos.getY() - random.nextFloat(), startPos.getZ() - random.nextFloat());
var radius = context.getConfig().boulderRadius();
var ores = context.getConfig().boulderOres();

for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
for (int z = -radius; z <= radius; z++) {
var pos = startPos.add(x, y, z);
var distance = movedCenter.distanceTo(pos.toCenterPos());
if (distance > radius) continue;
world.setBlockState(pos, getRandomBlockFromList(ores, random), 0x10);
}
}
}

for (int i = 8; i < 20; i++) {
world.setBlockState(startPos.up(i), Blocks.DIAMOND_BLOCK.getDefaultState(), 0x10);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package rearth.oritech.init.world.features.resourcenode;

import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.endec.BuiltInEndecs;
import io.wispforest.owo.serialization.endec.StructEndecBuilder;
import net.minecraft.util.Identifier;
import net.minecraft.world.gen.feature.FeatureConfig;

import java.util.List;

public record ResourceNodeFeatureConfig(int nodeSize, int boulderRadius, List<Identifier> nodeOres, List<Identifier> boulderOres, Identifier overlayBlock, int overlayHeight) implements FeatureConfig {

public static final Endec<ResourceNodeFeatureConfig> NODE_FEATURE_ENDEC = StructEndecBuilder.of(
Endec.INT.fieldOf("nodeSize", ResourceNodeFeatureConfig::nodeSize),
Endec.INT.fieldOf("boulderRadius", ResourceNodeFeatureConfig::boulderRadius),
BuiltInEndecs.IDENTIFIER.listOf().fieldOf("nodeOres", ResourceNodeFeatureConfig::nodeOres),
BuiltInEndecs.IDENTIFIER.listOf().fieldOf("boulderOres", ResourceNodeFeatureConfig::boulderOres),
BuiltInEndecs.IDENTIFIER.fieldOf("overlayBlock", ResourceNodeFeatureConfig::overlayBlock),
Endec.INT.fieldOf("overlayHeight", ResourceNodeFeatureConfig::overlayHeight),
ResourceNodeFeatureConfig::new
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type": "oritech:resource_node",
"config": {
"nodeSize": 5,
"boulderRadius": 3,
"nodeOres": [
"minecraft:deepslate_iron_ore",
"minecraft:deepslate_iron_ore",
"minecraft:deepslate_iron_ore",
"minecraft:deepslate_copper_ore",
"minecraft:deepslate_copper_ore",
"minecraft:deepslate_coal_ore",
"minecraft:deepslate_gold_ore",
"minecraft:deepslate"
],
"boulderOres": [
"minecraft:deepslate_iron_ore",
"minecraft:deepslate_copper_ore",
"minecraft:deepslate_iron_ore",
"minecraft:deepslate",
"minecraft:deepslate"
],
"overlayBlock": "oritech:oil_fluid_block",
"overlayHeight": 3
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "oritech:resource_node",
"config": {
"nodeSize": 5,
"boulderRadius": 3,
"nodeOres": [
"minecraft:deepslate_gold_ore",
"minecraft:deepslate_lapis_ore",
"minecraft:deepslate_redstone_ore",
"minecraft:deepslate_redstone_ore",
"minecraft:deepslate",
"minecraft:deepslate"
],
"boulderOres": [
"minecraft:deepslate_gold_ore",
"minecraft:deepslate_lapis_ore",
"minecraft:deepslate_redstone_ore",
"minecraft:deepslate",
"minecraft:deepslate"
],
"overlayBlock": "oritech:oil_fluid_block",
"overlayHeight": 3
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"type": "oritech:resource_node",
"config": {
"nodeSize": 5,
"boulderRadius": 3,
"nodeOres": [
"minecraft:deepslate_diamond_ore",
"minecraft:deepslate_diamond_ore",
"minecraft:deepslate_gold_ore",
"minecraft:deepslate_gold_ore",
"minecraft:deepslate_emerald_ore",
"minecraft:deepslate",
"minecraft:deepslate"
],
"boulderOres": [
"minecraft:deepslate_diamond_ore",
"minecraft:deepslate_gold_ore",
"minecraft:deepslate_emerald_ore",
"minecraft:deepslate",
"minecraft:deepslate"
],
"overlayBlock": "oritech:oil_fluid_block",
"overlayHeight": 3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"placement": [
{
"type": "minecraft:rarity_filter",
"chance": 25
"chance": 50
},
{
"type": "minecraft:in_square"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"feature": "oritech:resource_node_common",
"placement": [
{
"type": "minecraft:rarity_filter",
"chance": 250
},
{
"type": "minecraft:in_square"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"feature": "oritech:resource_node_other",
"placement": [
{
"type": "minecraft:rarity_filter",
"chance": 300
},
{
"type": "minecraft:in_square"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"feature": "oritech:resource_node_rare",
"placement": [
{
"type": "minecraft:rarity_filter",
"chance": 350
},
{
"type": "minecraft:in_square"
}
]
}

0 comments on commit 79c852c

Please # to comment.