Skip to content

Commit

Permalink
Add CustomLayeredFeature & LayeredFeature
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Oct 25, 2024
1 parent aa6da87 commit 527dd78
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import com.mmodding.mmodding_lib.library.utils.MModdingIdentifier;
import com.mmodding.mmodding_lib.library.utils.RegistrationUtils;
import com.mmodding.mmodding_lib.library.worldgen.features.differeds.DifferedDripstoneClusterFeature;
import com.mmodding.mmodding_lib.library.worldgen.features.differeds.DifferedLargeDripstoneFeature;
import com.mmodding.mmodding_lib.library.worldgen.features.differeds.DifferedPointedDripstoneFeature;
import com.mmodding.mmodding_lib.library.worldgen.features.differeds.DifferedLiquidVegetationPatch;
import com.mmodding.mmodding_lib.library.worldgen.features.builtin.LayeredFeature;
import com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed.DifferedDripstoneClusterFeature;
import com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed.DifferedLargeDripstoneFeature;
import com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed.DifferedPointedDripstoneFeature;
import com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed.DifferedLiquidVegetationPatch;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.FeatureConfig;

public class MModdingFeatures {

public static final Feature<LayeredFeature.Config> LAYERED = MModdingFeatures.register(
"layered", new LayeredFeature(LayeredFeature.Config.CODEC)
);

public static final Feature<DifferedDripstoneClusterFeature.Config> DIFFERED_DRIPSTONE_CLUSTER = MModdingFeatures.register(
"differed_dripstone_cluster", new DifferedDripstoneClusterFeature(DifferedDripstoneClusterFeature.Config.CODEC)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.mmodding.mmodding_lib.library.worldgen.features.builtin;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.FeatureConfig;
import net.minecraft.world.gen.feature.PlacedFeature;
import net.minecraft.world.gen.feature.util.FeatureContext;

import java.util.List;

public class LayeredFeature extends Feature<LayeredFeature.Config> {

public LayeredFeature(Codec<LayeredFeature.Config> codec) {
super(codec);
}

@Override
public boolean place(FeatureContext<Config> context) {
context.getConfig().features.forEach(feature -> {
PlacedFeature entry = context.getWorld().getRegistryManager().get(Registry.PLACED_FEATURE_KEY).get(feature);
if (entry != null) {
entry.generate(context.getWorld(), context.getGenerator(), context.getRandom(), context.getOrigin());
}
else {
throw new IllegalArgumentException("PlacedFeature at " + feature + "does not exist");
}
});
return true;
}

public static class Config implements FeatureConfig {

private final List<RegistryKey<PlacedFeature>> features;

public static final Codec<Config> CODEC = RecordCodecBuilder.create(
instance -> instance
.group(Codec.list(RegistryKey.codec(Registry.PLACED_FEATURE_KEY)).fieldOf("features").forGetter(config -> config.features))
.apply(instance, Config::new)
);

public Config(List<RegistryKey<PlacedFeature>> features) {
this.features = features;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mmodding.mmodding_lib.library.worldgen.features.differeds;
package com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed;

import com.mmodding.mmodding_lib.library.helpers.CustomDripstoneHelper;
import com.mojang.serialization.Codec;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mmodding.mmodding_lib.library.worldgen.features.differeds;
package com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed;

import com.mmodding.mmodding_lib.library.helpers.CustomDripstoneHelper;
import com.mojang.serialization.Codec;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mmodding.mmodding_lib.library.worldgen.features.differeds;
package com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed;

import com.mojang.serialization.Codec;
import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mmodding.mmodding_lib.library.worldgen.features.differeds;
package com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.mmodding.mmodding_lib.library.utils.IdentifierUtils;
import com.mmodding.mmodding_lib.library.utils.ListUtils;
import com.mmodding.mmodding_lib.library.worldgen.MModdingFeatures;
import com.mmodding.mmodding_lib.library.worldgen.features.differeds.DifferedDripstoneClusterFeature;
import com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed.DifferedDripstoneClusterFeature;
import net.minecraft.block.Block;
import net.minecraft.util.Holder;
import net.minecraft.util.Identifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.PlacedFeature;

import java.util.List;

public interface CustomFeature {

Feature<?> getFeature();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.mmodding.mmodding_lib.library.utils.IdentifierUtils;
import com.mmodding.mmodding_lib.library.utils.ListUtils;
import com.mmodding.mmodding_lib.library.worldgen.MModdingFeatures;
import com.mmodding.mmodding_lib.library.worldgen.features.differeds.DifferedLargeDripstoneFeature;
import com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed.DifferedLargeDripstoneFeature;
import net.minecraft.block.Block;
import net.minecraft.util.Holder;
import net.minecraft.util.Identifier;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.mmodding.mmodding_lib.library.worldgen.features.defaults;

import com.mmodding.mmodding_lib.library.utils.BiArrayList;
import com.mmodding.mmodding_lib.library.utils.BiList;
import com.mmodding.mmodding_lib.library.utils.IdentifierUtils;
import com.mmodding.mmodding_lib.library.worldgen.MModdingFeatures;
import com.mmodding.mmodding_lib.library.worldgen.features.builtin.LayeredFeature;
import net.minecraft.util.Holder;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.gen.GenerationStep;
import net.minecraft.world.gen.feature.*;
import org.quiltmc.qsl.worldgen.biome.api.BiomeModifications;
import org.quiltmc.qsl.worldgen.biome.api.BiomeSelectionContext;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class CustomLayeredFeature implements CustomFeature, FeatureRegistrable {

private final AtomicBoolean registered = new AtomicBoolean();
private final AtomicReference<Identifier> identifier = new AtomicReference<>();
private final BiList<PlacedFeature, String> additionalPlacedFeatures = new BiArrayList<>();

private final Supplier<List<RegistryKey<PlacedFeature>>> features;
private final GenerationStep.Feature step;
private final List<PlacementModifier> defaultPlacementModifiers;

public CustomLayeredFeature(Supplier<List<RegistryKey<PlacedFeature>>> features, GenerationStep.Feature step, PlacementModifier... defaultPlacementModifiers) {
this.features = features;
this.step = step;
this.defaultPlacementModifiers = List.of(defaultPlacementModifiers);
}

@Override
public Feature<LayeredFeature.Config> getFeature() {
return MModdingFeatures.LAYERED;
}

@Override
public ConfiguredFeature<?, ?> getConfiguredFeature() {
return new ConfiguredFeature<>(this.getFeature(), new LayeredFeature.Config(this.features.get()));
}

public PlacedFeature createPlacedFeature(List<PlacementModifier> placementModifiers) {
return new PlacedFeature(Holder.createDirect(this.getConfiguredFeature()), placementModifiers);
}

@Override
public PlacedFeature getDefaultPlacedFeature() {
return this.createPlacedFeature(this.defaultPlacementModifiers);
}

public CustomLayeredFeature addPlacedFeature(List<PlacementModifier> placementModifiers, String idExt) {
this.additionalPlacedFeatures.add(this.createPlacedFeature(placementModifiers), idExt);
return this;
}

@Override
public BiList<PlacedFeature, String> getAdditionalPlacedFeatures() {
return this.additionalPlacedFeatures;
}

@Override
public void addDefaultToBiomes(Predicate<BiomeSelectionContext> ctx) {
this.addAdditionalToBiomes(ctx, "");
}

@Override
public void addAdditionalToBiomes(Predicate<BiomeSelectionContext> ctx, String idExt) {
if (this.registered.get()) {
BiomeModifications.addFeature(
ctx, this.step,
RegistryKey.of(Registry.PLACED_FEATURE_KEY, IdentifierUtils.extend(this.identifier.get(), idExt))
);
}
}

@Override
public void setIdentifier(Identifier identifier) {
this.identifier.set(identifier);
}

@Override
public boolean isNotRegistered() {
return !this.registered.get();
}

@Override
public void setRegistered() {
this.registered.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.mmodding.mmodding_lib.library.utils.IdentifierUtils;
import com.mmodding.mmodding_lib.library.utils.ListUtils;
import com.mmodding.mmodding_lib.library.worldgen.MModdingFeatures;
import com.mmodding.mmodding_lib.library.worldgen.features.differeds.DifferedLiquidVegetationPatch;
import com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed.DifferedLiquidVegetationPatch;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.tag.TagKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.mmodding.mmodding_lib.library.utils.IdentifierUtils;
import com.mmodding.mmodding_lib.library.utils.ListUtils;
import com.mmodding.mmodding_lib.library.worldgen.MModdingFeatures;
import com.mmodding.mmodding_lib.library.worldgen.features.differeds.DifferedPointedDripstoneFeature;
import com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed.DifferedPointedDripstoneFeature;
import net.minecraft.block.Block;
import net.minecraft.util.Holder;
import net.minecraft.util.HolderSet;
Expand Down

0 comments on commit 527dd78

Please sign in to comment.