From 527dd782a87d78742d6900f472571c35ec8c22a7 Mon Sep 17 00:00:00 2001 From: FirstMegaGame4 <84094287+FirstMegaGame4@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:21:24 +0200 Subject: [PATCH] Add CustomLayeredFeature & LayeredFeature --- .../library/worldgen/MModdingFeatures.java | 13 ++- .../features/builtin/LayeredFeature.java | 48 +++++++++ .../DifferedDripstoneClusterFeature.java | 2 +- .../DifferedLargeDripstoneFeature.java | 2 +- .../DifferedLiquidVegetationPatch.java | 2 +- .../DifferedPointedDripstoneFeature.java | 2 +- .../CustomDripstoneClusterFeature.java | 2 +- .../features/defaults/CustomFeature.java | 2 - .../defaults/CustomLargeDripstoneFeature.java | 2 +- .../defaults/CustomLayeredFeature.java | 97 +++++++++++++++++++ .../CustomLiquidVegetationPatchFeature.java | 2 +- .../CustomPointedDripstoneFeature.java | 2 +- 12 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/LayeredFeature.java rename src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/{differeds => builtin/differed}/DifferedDripstoneClusterFeature.java (99%) rename src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/{differeds => builtin/differed}/DifferedLargeDripstoneFeature.java (99%) rename src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/{differeds => builtin/differed}/DifferedLiquidVegetationPatch.java (99%) rename src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/{differeds => builtin/differed}/DifferedPointedDripstoneFeature.java (98%) create mode 100644 src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLayeredFeature.java diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/MModdingFeatures.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/MModdingFeatures.java index 7d85045..db28f31 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/MModdingFeatures.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/MModdingFeatures.java @@ -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 LAYERED = MModdingFeatures.register( + "layered", new LayeredFeature(LayeredFeature.Config.CODEC) + ); + public static final Feature DIFFERED_DRIPSTONE_CLUSTER = MModdingFeatures.register( "differed_dripstone_cluster", new DifferedDripstoneClusterFeature(DifferedDripstoneClusterFeature.Config.CODEC) ); diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/LayeredFeature.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/LayeredFeature.java new file mode 100644 index 0000000..42b6927 --- /dev/null +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/LayeredFeature.java @@ -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 { + + public LayeredFeature(Codec codec) { + super(codec); + } + + @Override + public boolean place(FeatureContext 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> features; + + public static final Codec 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> features) { + this.features = features; + } + } +} diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedDripstoneClusterFeature.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedDripstoneClusterFeature.java similarity index 99% rename from src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedDripstoneClusterFeature.java rename to src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedDripstoneClusterFeature.java index 7641285..962f62f 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedDripstoneClusterFeature.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedDripstoneClusterFeature.java @@ -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; diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedLargeDripstoneFeature.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedLargeDripstoneFeature.java similarity index 99% rename from src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedLargeDripstoneFeature.java rename to src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedLargeDripstoneFeature.java index e1c1b56..18e321b 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedLargeDripstoneFeature.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedLargeDripstoneFeature.java @@ -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; diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedLiquidVegetationPatch.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedLiquidVegetationPatch.java similarity index 99% rename from src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedLiquidVegetationPatch.java rename to src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedLiquidVegetationPatch.java index 204a5d8..bb8e670 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedLiquidVegetationPatch.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedLiquidVegetationPatch.java @@ -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; diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedPointedDripstoneFeature.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedPointedDripstoneFeature.java similarity index 98% rename from src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedPointedDripstoneFeature.java rename to src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedPointedDripstoneFeature.java index f8ff434..2ac59a2 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/differeds/DifferedPointedDripstoneFeature.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/builtin/differed/DifferedPointedDripstoneFeature.java @@ -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; diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomDripstoneClusterFeature.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomDripstoneClusterFeature.java index 1f930d3..87f4a89 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomDripstoneClusterFeature.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomDripstoneClusterFeature.java @@ -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; diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomFeature.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomFeature.java index 257f99e..823d5fa 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomFeature.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomFeature.java @@ -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(); diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLargeDripstoneFeature.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLargeDripstoneFeature.java index 541a71d..f56f245 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLargeDripstoneFeature.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLargeDripstoneFeature.java @@ -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; diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLayeredFeature.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLayeredFeature.java new file mode 100644 index 0000000..bae53a6 --- /dev/null +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLayeredFeature.java @@ -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 = new AtomicReference<>(); + private final BiList additionalPlacedFeatures = new BiArrayList<>(); + + private final Supplier>> features; + private final GenerationStep.Feature step; + private final List defaultPlacementModifiers; + + public CustomLayeredFeature(Supplier>> features, GenerationStep.Feature step, PlacementModifier... defaultPlacementModifiers) { + this.features = features; + this.step = step; + this.defaultPlacementModifiers = List.of(defaultPlacementModifiers); + } + + @Override + public Feature getFeature() { + return MModdingFeatures.LAYERED; + } + + @Override + public ConfiguredFeature getConfiguredFeature() { + return new ConfiguredFeature<>(this.getFeature(), new LayeredFeature.Config(this.features.get())); + } + + public PlacedFeature createPlacedFeature(List placementModifiers) { + return new PlacedFeature(Holder.createDirect(this.getConfiguredFeature()), placementModifiers); + } + + @Override + public PlacedFeature getDefaultPlacedFeature() { + return this.createPlacedFeature(this.defaultPlacementModifiers); + } + + public CustomLayeredFeature addPlacedFeature(List placementModifiers, String idExt) { + this.additionalPlacedFeatures.add(this.createPlacedFeature(placementModifiers), idExt); + return this; + } + + @Override + public BiList getAdditionalPlacedFeatures() { + return this.additionalPlacedFeatures; + } + + @Override + public void addDefaultToBiomes(Predicate ctx) { + this.addAdditionalToBiomes(ctx, ""); + } + + @Override + public void addAdditionalToBiomes(Predicate 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); + } +} diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLiquidVegetationPatchFeature.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLiquidVegetationPatchFeature.java index 5ca72e0..8e23818 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLiquidVegetationPatchFeature.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomLiquidVegetationPatchFeature.java @@ -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; diff --git a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomPointedDripstoneFeature.java b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomPointedDripstoneFeature.java index 012875d..df5cb6b 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomPointedDripstoneFeature.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/worldgen/features/defaults/CustomPointedDripstoneFeature.java @@ -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;