Skip to content

Commit

Permalink
SALT_CAVES ADDED
Browse files Browse the repository at this point in the history
  • Loading branch information
MEGATREX4 committed Jun 20, 2024
1 parent f363bfb commit b21e4cb
Show file tree
Hide file tree
Showing 15 changed files with 510 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.megatrex4.ukrainian_dlight.item.ModItemGroups;
import com.megatrex4.ukrainian_dlight.item.ModItems;
import com.megatrex4.ukrainian_dlight.item.ToolTipHelper;
import com.megatrex4.ukrainian_dlight.util.Config;
import net.fabricmc.api.ModInitializer;

import net.minecraft.entity.damage.DamageType;
Expand All @@ -14,13 +15,18 @@
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Objects;

public class UkrainianDelight implements ModInitializer {
public static final String MOD_ID = "ukrainian_delight";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

public static final RegistryKey<DamageType> GLASS_DAMAGE = RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier("ukrainian_delight", "glass_damage"));


public static Identifier id(String id) {
return new Identifier(MOD_ID, id);
}

@Override
public void onInitialize() {
RegistryKey<DamageType> GLASS_DAMAGE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.megatrex4.ukrainian_dlight.features;

import com.megatrex4.ukrainian_dlight.UkrainianDelight;
import com.megatrex4.ukrainian_dlight.features.features.SaltCavesFeature;
import com.megatrex4.ukrainian_dlight.features.features.config.SaltCavesFeatureConfig;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.FeatureConfig;

public class Features {

private static <C extends FeatureConfig, F extends Feature<C>> F register(String id, F feature) {
return (F) Registry.register(Registries.FEATURE, UkrainianDelight.id(id), feature);
}

public final static Feature<SaltCavesFeatureConfig> SALT_CAVES_FEATURE;

static {
SALT_CAVES_FEATURE = register("salt_caves_feature", new SaltCavesFeature(SaltCavesFeatureConfig.CODEC));
}

public static void init() {
new Features();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.megatrex4.ukrainian_dlight.features.features;


import com.megatrex4.ukrainian_dlight.features.features.config.SaltCavesFeatureConfig;
import com.mojang.serialization.Codec;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.util.FeatureContext;

public class SaltCavesFeature extends Feature<SaltCavesFeatureConfig> {
public SaltCavesFeature(Codec<SaltCavesFeatureConfig> configCodec) {
super(configCodec);
}

private static final Direction[] DIRECTIONS = {Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST};

@Override
public boolean generate(FeatureContext<SaltCavesFeatureConfig> context) {
BlockPos origin = context.getOrigin();
StructureWorldAccess world = context.getWorld();
Random random = context.getRandom();

SaltCavesFeatureConfig config = context.getConfig();
int minSize = config.size().getMin();
int size = config.size().get(random);
int amount = config.amount().get(random);
int spread = config.spread().get(random) / 2;
BlockState innerState = config.innerState();
BlockState outerState = config.outerState();

spread = spread < 16 ? spread : 16;

if (!world.isAir(origin) || world.isAir(origin.up())) {
return false;
}

makeOneIcicle(world, origin, size, innerState, outerState, random);
amount -= 1;

while (amount > 0) {
amount -= 1;
BlockPos.Mutable mutable = origin.mutableCopy();
mutable = mutable.add(random.nextInt(1 + spread * 2) - spread, 0, random.nextInt(1 + spread * 2) - spread).mutableCopy();

boolean bl = false;

if (world.isAir(mutable) && world.isAir(mutable.up())) {
for (int i = 0; i < 3 && !bl; i += 1) {
mutable = mutable.add(0, 1, 0).mutableCopy();

if (world.isAir(mutable) && !world.isAir(mutable.up()) && !world.isAir(mutable.up(2))) {
bl = true;
}
}
} else if (!world.isAir(mutable)) {
for (int i = 0; i < 3 && !bl; i += 1) {
mutable = mutable.add(0, -1, 0).mutableCopy();

if (world.isAir(mutable) && !world.isAir(mutable.up()) && !world.isAir(mutable.up(2))) {
bl = true;
}
}
}

int nextSize = random.nextInt(size) + 1;
nextSize = nextSize > minSize ? nextSize : minSize;

if (!world.isAir(mutable.up()) && !world.isAir(mutable.up(2))) makeOneIcicle(world, mutable, nextSize, innerState, outerState, random);
}

return true;
}

public static void makeOneIcicle(StructureWorldAccess world, BlockPos origin, int size, BlockState innerState, BlockState outerState, Random random) {
BlockPos.Mutable cursor = origin.mutableCopy();

for (int i = 0; i < -1 + (2 * size / 3); i += 1) {
if (world.isAir(cursor)) {
world.setBlockState(cursor, innerState, 0);
} else {
break;
}

cursor = cursor.down().mutableCopy();
}

for (int i = 0; i < 1 + (size / 3); i += 1) {
if (world.isAir(cursor)) {
world.setBlockState(cursor, outerState, 0);
} else {
break;
}

cursor = cursor.down().mutableCopy();
}

if (size > 3) {
for (Direction d : DIRECTIONS) {
if (world.isAir(origin.offset(d)) && !world.isAir(origin.offset(d).up())) makeOneIcicle(world, origin.offset(d), random.nextInt(2 + (size / 3)), innerState, outerState, random);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.megatrex4.ukrainian_dlight.features.features.config;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.minecraft.block.BlockState;
import net.minecraft.util.math.intprovider.IntProvider;
import net.minecraft.world.gen.feature.FeatureConfig;

public record SaltCavesFeatureConfig(IntProvider size, IntProvider amount, IntProvider spread, BlockState innerState,
BlockState outerState) implements FeatureConfig
{
public static final Codec<SaltCavesFeatureConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
IntProvider.VALUE_CODEC.fieldOf("size").forGetter(SaltCavesFeatureConfig::size),
IntProvider.VALUE_CODEC.fieldOf("amount").forGetter(SaltCavesFeatureConfig::amount),
IntProvider.VALUE_CODEC.fieldOf("spread").forGetter(SaltCavesFeatureConfig::spread),
BlockState.CODEC.fieldOf("inner_state").forGetter(SaltCavesFeatureConfig::innerState),
BlockState.CODEC.fieldOf("outer_state").forGetter(SaltCavesFeatureConfig::outerState)
).apply(instance, instance.stable(SaltCavesFeatureConfig::new)));
}
16 changes: 16 additions & 0 deletions src/main/java/com/megatrex4/ukrainian_dlight/gen/BiomeKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.megatrex4.ukrainian_dlight.gen;

import com.megatrex4.ukrainian_dlight.UkrainianDelight;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.world.biome.Biome;

public class BiomeKeys {

private static RegistryKey<Biome> register(String name) {
return RegistryKey.of(RegistryKeys.BIOME, UkrainianDelight.id(name));
}

public static final RegistryKey<Biome> SALT_CAVES = register("salt_caves");

}
51 changes: 51 additions & 0 deletions src/main/java/com/megatrex4/ukrainian_dlight/gen/CaveBiomes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.megatrex4.ukrainian_dlight.gen;

import com.google.common.collect.ImmutableList;
import net.minecraft.registry.RegistryKey;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.source.util.MultiNoiseUtil;
import net.minecraft.world.biome.source.util.MultiNoiseUtil.ParameterRange;

public class CaveBiomes {

public static class CaveBiome {
public final ParameterRange temperature, humidity, continentalness, erosion, depth, weirdness;
public final float offset;
public final RegistryKey<Biome> biome;

public CaveBiome(ParameterRange temperature, ParameterRange humidity, ParameterRange continentalness, ParameterRange erosion, ParameterRange depth, ParameterRange weirdness, float offset, RegistryKey<Biome> biome) {
this.temperature = temperature;
this.humidity = humidity;
this.continentalness = continentalness;
this.erosion = erosion;
this.depth = depth;
this.weirdness = weirdness;
this.offset = offset;
this.biome = biome;
}

public static CaveBiome of(ParameterRange temperature, ParameterRange humidity, ParameterRange continentalness, ParameterRange erosion, ParameterRange depth, ParameterRange weirdness, float offset, RegistryKey<Biome> biome) {
return new CaveBiome(temperature, humidity, continentalness, erosion, depth, weirdness, offset, biome);
}
}

private static final MultiNoiseUtil.ParameterRange DEFAULT_PARAMETER = MultiNoiseUtil.ParameterRange.of(-1.0f, 1.0f);
static final ParameterRange ALL_HEIGHT_RANGE = ParameterRange.of(0.2f, 0.9f);
static final ParameterRange ALL_HEIGHT_RANGE_DEEPER = ParameterRange.of(-0.2f, 0.9f);
static final ParameterRange HIGH_RANGE = ParameterRange.of(0.55f, 0.9f);

public final static ImmutableList<CaveBiome> DEFAULT_CAVE_BIOMES = ImmutableList.of(
// Existing biomes
CaveBiome.of(
ParameterRange.of(-1.0f, -0.6f),
DEFAULT_PARAMETER,
DEFAULT_PARAMETER,
DEFAULT_PARAMETER,
ALL_HEIGHT_RANGE,
ParameterRange.of(0.8f, 1.0f),
0f,
BiomeKeys.SALT_CAVES
)
);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.megatrex4.ukrainian_dlight.mixin;

import com.megatrex4.ukrainian_dlight.gen.CaveBiomes;
import com.megatrex4.ukrainian_dlight.gen.CaveBiomes.CaveBiome;
import com.megatrex4.ukrainian_dlight.util.VanillaBiomeParametersHelper;
import com.mojang.datafixers.util.Pair;
import net.minecraft.registry.RegistryKey;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.source.util.MultiNoiseUtil;
import net.minecraft.world.biome.source.util.VanillaBiomeParameters;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.function.Consumer;

@Mixin(VanillaBiomeParameters.class)
public class VanillaBiomeParametersMixin {
@Inject(at = @At("RETURN"), method = "writeCaveBiomes")
private void init(Consumer<Pair<MultiNoiseUtil.NoiseHypercube, RegistryKey<Biome>>> parameters, CallbackInfo info) {
for (CaveBiome b : CaveBiomes.DEFAULT_CAVE_BIOMES) {
VanillaBiomeParametersHelper.writeCaveBiomeParameters(parameters, b.temperature, b.humidity, b.continentalness, b.erosion, b.depth, b.weirdness, b.offset, b.biome);
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/megatrex4/ukrainian_dlight/util/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.megatrex4.ukrainian_dlight.util;

public class Config {

public boolean generateSaltCaves = true;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.megatrex4.ukrainian_dlight.util;

import com.megatrex4.ukrainian_dlight.gen.BiomeKeys;
import com.mojang.datafixers.util.Pair;
import net.minecraft.registry.RegistryKey;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.source.util.MultiNoiseUtil;

public class VanillaBiomeParametersHelper {
public static void writeCaveBiomeParameters(
java.util.function.Consumer<Pair<MultiNoiseUtil.NoiseHypercube, RegistryKey<Biome>>> parameters,
MultiNoiseUtil.ParameterRange temperature,
MultiNoiseUtil.ParameterRange humidity,
MultiNoiseUtil.ParameterRange continentalness,
MultiNoiseUtil.ParameterRange erosion,
MultiNoiseUtil.ParameterRange depth,
MultiNoiseUtil.ParameterRange weirdness,
float offset,
RegistryKey<Biome> biome) {

parameters.accept(Pair.of(
MultiNoiseUtil.createNoiseHypercube(temperature, humidity, continentalness, erosion, depth, weirdness, offset),
biome
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"_comment8": "misc",
"death.attack.glass_damage": "%s was not careful with glass",
"tooltip.ukrainian_delight.no_effects": "No effects",
"biome.ukrainian_delight.salt_caves": "Salt Caves",

"amplifier.none": "",
"amplifier.1": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"ukrainian_delight:salt_caves"
]
}
Loading

0 comments on commit b21e4cb

Please sign in to comment.