mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 11:05:40 +00:00
[1.19.x] Add stock biome modifier types for adding features and spawns (#8697)
This commit is contained in:
1 parent
2a05c7c01d
commit
683bf99fe4
10 files changed
+391
-84
No files matched your search
@@ -8,12 +8,8 @@ package net.minecraftforge.debug.world;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.DataResult;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
|
||||
@@ -22,12 +18,12 @@ import net.minecraft.core.HolderSet;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.RegistryAccess;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.data.DataProvider;
|
||||
import net.minecraft.data.worldgen.features.NetherFeatures;
|
||||
import net.minecraft.resources.RegistryOps;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.tags.BiomeTags;
|
||||
import net.minecraft.tags.EntityTypeTags;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.biome.Biome.Precipitation;
|
||||
@@ -39,8 +35,11 @@ import net.minecraft.world.level.levelgen.placement.CountOnEveryLayerPlacement;
|
||||
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
||||
import net.minecraftforge.common.data.ExistingFileHelper;
|
||||
import net.minecraftforge.common.data.JsonCodecProvider;
|
||||
import net.minecraftforge.common.world.BiomeGenerationSettingsBuilder;
|
||||
import net.minecraftforge.common.world.BiomeModifier;
|
||||
import net.minecraftforge.common.world.ForgeBiomeModifiers.AddFeaturesBiomeModifier;
|
||||
import net.minecraftforge.common.world.ForgeBiomeModifiers.AddSpawnsBiomeModifier;
|
||||
import net.minecraftforge.common.world.ForgeBiomeModifiers.RemoveFeaturesBiomeModifier;
|
||||
import net.minecraftforge.common.world.ForgeBiomeModifiers.RemoveSpawnsBiomeModifier;
|
||||
import net.minecraftforge.common.world.ModifiableBiomeInfo.BiomeInfo.Builder;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
@@ -53,28 +52,42 @@ import net.minecraftforge.registries.RegistryObject;
|
||||
/**
|
||||
* <p>This tests the following features and requirements of biome modifier jsons::</p>
|
||||
* <ul>
|
||||
* <li>A biome modifier json is created via datagen.</li>
|
||||
* <li>The biome modifier modifies all four modifiable fields in biomes, to ensure patches and coremods apply correctly (generation, spawns, climate, and client effects).</li>
|
||||
* <li>The biome modifier uses a biome tag to determine which biomes to modify.</li>
|
||||
* <li>The biome modifier adds json feature to modified biomes, to ensure json features are usable in biome modifiers.</li>
|
||||
* <li>Biome modifier jsons are created via datagen.</li>
|
||||
* <li>Biome modifiers modify all four modifiable fields in biomes, to ensure patches and coremods apply correctly (generation, spawns, climate, and client effects).</li>
|
||||
* <li>Biome modifiers use biome tags to determine which biomes to modify.</li>
|
||||
* <li>Biome modifiers add a json feature to modified biomes, to ensure json features are usable in biome modifiers.</li>
|
||||
* </ul>
|
||||
* <p>If the biome modifier is applied correctly, then badlands biomes should generate large basalt columns,
|
||||
* spawn magma cubes, have red-colored water, and be snowy.</p>
|
||||
* <p>If the biome modifiers are applied correctly, then badlands biomes should generate large basalt columns,
|
||||
* spawn magma cubes, have red-colored water, and be snowy. Additionally, biomes in the is_forest tag are missing
|
||||
* oak trees, pine trees, and skeletons.</p>
|
||||
*/
|
||||
@Mod(BiomeModifierTest.MODID)
|
||||
public class BiomeModifierTest
|
||||
{
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final String MODID = "biome_modifiers_test";
|
||||
public static final boolean ENABLED = true;
|
||||
public static final String BASALT_PILLARS = "large_basalt_columns";
|
||||
public static final ResourceLocation BASALT_PILLARS_RL = new ResourceLocation(MODID, BASALT_PILLARS);
|
||||
public static final ResourceKey<PlacedFeature> BASALT_PILLARS_KEY = ResourceKey.create(Registry.PLACED_FEATURE_REGISTRY, BASALT_PILLARS_RL);
|
||||
public static final String TEST = "test";
|
||||
public static final ResourceLocation ADD_FEATURES_TO_BIOMES_RL = new ResourceLocation(MODID, TEST);
|
||||
public static final String MODIFY_BADLANDS = "modify_badlands";
|
||||
public static final ResourceLocation MODIFY_BADLANDS_RL = new ResourceLocation(MODID, MODIFY_BADLANDS);
|
||||
public static final ResourceKey<BiomeModifier> MODIFY_BADLANDS_KEY = ResourceKey.create(ForgeRegistries.Keys.BIOME_MODIFIERS, MODIFY_BADLANDS_RL);
|
||||
private static final boolean ENABLED = true;
|
||||
|
||||
private static final String LARGE_BASALT_COLUMNS = "large_basalt_columns";
|
||||
private static final ResourceLocation LARGE_BASALT_COLUMNS_RL = new ResourceLocation(MODID, LARGE_BASALT_COLUMNS);
|
||||
private static final ResourceKey<PlacedFeature> LARGE_BASALT_COLUMNS_KEY = ResourceKey.create(Registry.PLACED_FEATURE_REGISTRY, LARGE_BASALT_COLUMNS_RL);
|
||||
|
||||
private static final String MODIFY_BIOMES = "modify_biomes";
|
||||
private static final ResourceLocation MODIFY_BIOMES_RL = new ResourceLocation(MODID, MODIFY_BIOMES);
|
||||
|
||||
private static final String ADD_BASALT = "add_basalt";
|
||||
private static final ResourceLocation ADD_BASALT_RL = new ResourceLocation(MODID, ADD_BASALT);
|
||||
|
||||
private static final String ADD_MAGMA_CUBES = "add_magma_cubes";
|
||||
private static final ResourceLocation ADD_MAGMA_CUBES_RL = new ResourceLocation(MODID, ADD_MAGMA_CUBES);
|
||||
|
||||
private static final String MODIFY_BADLANDS = "modify_badlands";
|
||||
private static final ResourceLocation MODIFY_BADLANDS_RL = new ResourceLocation(MODID, MODIFY_BADLANDS);
|
||||
|
||||
private static final String REMOVE_FOREST_TREES = "remove_forest_trees";
|
||||
private static final ResourceLocation REMOVE_FOREST_TREES_RL = new ResourceLocation(MODID, REMOVE_FOREST_TREES);
|
||||
|
||||
private static final String REMOVE_FOREST_SKELETONS = "remove_forest_skeletons";
|
||||
private static final ResourceLocation REMOVE_FOREST_SKELETONS_RL = new ResourceLocation(MODID, REMOVE_FOREST_SKELETONS);
|
||||
|
||||
public BiomeModifierTest()
|
||||
{
|
||||
@@ -86,7 +99,7 @@ public class BiomeModifierTest
|
||||
// Serializer types can be registered via deferred register.
|
||||
final DeferredRegister<Codec<? extends BiomeModifier>> serializers = DeferredRegister.create(ForgeRegistries.Keys.BIOME_MODIFIER_SERIALIZERS, MODID);
|
||||
serializers.register(modBus);
|
||||
serializers.register(TEST, TestModifier::makeCodec);
|
||||
serializers.register(MODIFY_BIOMES, TestModifier::makeCodec);
|
||||
|
||||
modBus.addListener(this::onGatherData);
|
||||
}
|
||||
@@ -96,58 +109,66 @@ public class BiomeModifierTest
|
||||
// Example of how to datagen datapack registry objects.
|
||||
final DataGenerator generator = event.getGenerator();
|
||||
final ExistingFileHelper existingFileHelper = event.getExistingFileHelper();
|
||||
|
||||
// Any reference holders our objects have must come from this same RegistryAccess instance,
|
||||
// or encoding our objects will fail.
|
||||
final RegistryAccess registries = RegistryAccess.builtinCopy();
|
||||
final RegistryOps<JsonElement> ops = RegistryOps.create(JsonOps.INSTANCE, registries);
|
||||
final Registry<ConfiguredFeature<?,?>> configuredFeatures = ops.registry(Registry.CONFIGURED_FEATURE_REGISTRY).get();
|
||||
final Registry<PlacedFeature> placedFeatures = ops.registry(Registry.PLACED_FEATURE_REGISTRY).get();
|
||||
final RegistryOps<JsonElement> ops = RegistryOps.create(JsonOps.INSTANCE, RegistryAccess.builtinCopy());
|
||||
|
||||
// Create our PlacedFeature so we can generate it.
|
||||
// Create our placed feature and biome modifiers.
|
||||
final ResourceKey<ConfiguredFeature<?,?>> configuredFeatureKey = NetherFeatures.LARGE_BASALT_COLUMNS.unwrapKey().get().cast(Registry.CONFIGURED_FEATURE_REGISTRY).get();
|
||||
final Holder<ConfiguredFeature<?,?>> configuredFeatureHolder = configuredFeatures.getOrCreateHolderOrThrow(configuredFeatureKey);
|
||||
final PlacedFeature placedFeature = new PlacedFeature(configuredFeatureHolder, List.of(CountOnEveryLayerPlacement.of(1), BiomeFilter.biome()));
|
||||
// Make sure we're using the holder from the registryaccess/registryops, the static ones won't work.
|
||||
final Holder<ConfiguredFeature<?,?>> configuredFeatureHolder = ops.registry(Registry.CONFIGURED_FEATURE_REGISTRY).get().getOrCreateHolderOrThrow(configuredFeatureKey);
|
||||
final PlacedFeature basaltFeature = new PlacedFeature(
|
||||
configuredFeatureHolder,
|
||||
List.of(CountOnEveryLayerPlacement.of(1), BiomeFilter.biome()));
|
||||
|
||||
final HolderSet.Named<Biome> badlandsTag = new HolderSet.Named<>(ops.registry(Registry.BIOME_REGISTRY).get(), BiomeTags.IS_BADLANDS);
|
||||
final HolderSet.Named<Biome> forestsTag = new HolderSet.Named<>(ops.registry(Registry.BIOME_REGISTRY).get(), BiomeTags.IS_FOREST);
|
||||
|
||||
final BiomeModifier addBasaltFeature = new AddFeaturesBiomeModifier(
|
||||
badlandsTag,
|
||||
HolderSet.direct(ops.registry(Registry.PLACED_FEATURE_REGISTRY).get().getOrCreateHolderOrThrow(ResourceKey.create(Registry.PLACED_FEATURE_REGISTRY, LARGE_BASALT_COLUMNS_KEY.location()))),
|
||||
Decoration.TOP_LAYER_MODIFICATION);
|
||||
|
||||
final BiomeModifier addSpawn = AddSpawnsBiomeModifier.singleSpawn(
|
||||
badlandsTag,
|
||||
new SpawnerData(EntityType.MAGMA_CUBE, 100, 1, 4));
|
||||
|
||||
// Create our BiomeModifier so we can generate it.
|
||||
final HolderSet<PlacedFeature> placedFeatureHolderSet =
|
||||
HolderSet.direct(placedFeatures.getOrCreateHolderOrThrow(BASALT_PILLARS_KEY));
|
||||
final BiomeModifier biomeModifier = new TestModifier(
|
||||
new HolderSet.Named<>(ops.registry(Registry.BIOME_REGISTRY).get(), BiomeTags.IS_BADLANDS),
|
||||
Decoration.TOP_LAYER_MODIFICATION,
|
||||
placedFeatureHolderSet,
|
||||
new SpawnerData(EntityType.MAGMA_CUBE, 100, 1, 4),
|
||||
Precipitation.SNOW,
|
||||
0xFF0000
|
||||
);
|
||||
|
||||
// Create and add our data providers.
|
||||
final DataProvider placedFeatureProvider =
|
||||
JsonCodecProvider.forDatapackRegistry(generator, existingFileHelper, MODID, ops, Registry.PLACED_FEATURE_REGISTRY,
|
||||
Map.of(BASALT_PILLARS_RL, placedFeature));
|
||||
generator.addProvider(event.includeServer(), placedFeatureProvider);
|
||||
|
||||
final DataProvider biomeModifierProvider =
|
||||
JsonCodecProvider.forDatapackRegistry(generator, existingFileHelper, MODID, ops, ForgeRegistries.Keys.BIOME_MODIFIERS,
|
||||
Map.of(MODIFY_BADLANDS_RL, biomeModifier));
|
||||
generator.addProvider(event.includeServer(), biomeModifierProvider);
|
||||
final BiomeModifier removeFeature = RemoveFeaturesBiomeModifier.allSteps(
|
||||
forestsTag,
|
||||
HolderSet.direct(ops.registry(Registry.PLACED_FEATURE_REGISTRY).get().getOrCreateHolderOrThrow(ResourceKey.create(Registry.PLACED_FEATURE_REGISTRY, new ResourceLocation("trees_birch_and_oak"))))
|
||||
);
|
||||
|
||||
final BiomeModifier removeSpawn = new RemoveSpawnsBiomeModifier(
|
||||
forestsTag,
|
||||
new HolderSet.Named<>(ops.registry(Registry.ENTITY_TYPE_REGISTRY).get(), EntityTypeTags.SKELETONS)
|
||||
);
|
||||
|
||||
// Create and add dataproviders.
|
||||
generator.addProvider(event.includeServer(), JsonCodecProvider.forDatapackRegistry(
|
||||
generator, existingFileHelper, MODID, ops, Registry.PLACED_FEATURE_REGISTRY, Map.of(
|
||||
LARGE_BASALT_COLUMNS_RL, basaltFeature)));
|
||||
|
||||
generator.addProvider(event.includeServer(), JsonCodecProvider.forDatapackRegistry(
|
||||
generator, existingFileHelper, MODID, ops, ForgeRegistries.Keys.BIOME_MODIFIERS, Map.of(
|
||||
MODIFY_BADLANDS_RL, biomeModifier,
|
||||
ADD_BASALT_RL, addBasaltFeature,
|
||||
ADD_MAGMA_CUBES_RL, addSpawn,
|
||||
REMOVE_FOREST_TREES_RL, removeFeature,
|
||||
REMOVE_FOREST_SKELETONS_RL, removeSpawn)));
|
||||
}
|
||||
|
||||
public record TestModifier(HolderSet<Biome> biomes, Decoration generationStage, HolderSet<PlacedFeature> features, SpawnerData spawn, Precipitation precipitation, int waterColor)
|
||||
implements BiomeModifier
|
||||
public record TestModifier(HolderSet<Biome> biomes, Precipitation precipitation, int waterColor) implements BiomeModifier
|
||||
{
|
||||
private static final RegistryObject<Codec<? extends BiomeModifier>> SERIALIZER = RegistryObject.create(ADD_FEATURES_TO_BIOMES_RL, ForgeRegistries.Keys.BIOME_MODIFIER_SERIALIZERS, MODID);
|
||||
private static final RegistryObject<Codec<? extends BiomeModifier>> SERIALIZER = RegistryObject.create(MODIFY_BIOMES_RL, ForgeRegistries.Keys.BIOME_MODIFIER_SERIALIZERS, MODID);
|
||||
|
||||
@Override
|
||||
public void modify(Holder<Biome> biome, Phase phase, Builder builder)
|
||||
{
|
||||
if (phase == Phase.ADD && this.biomes.contains(biome))
|
||||
{
|
||||
BiomeGenerationSettingsBuilder generation = builder.getGenerationSettings();
|
||||
this.features.forEach(holder -> generation.addFeature(this.generationStage, holder));
|
||||
builder.getMobSpawnSettings().addSpawn(this.spawn.type.getCategory(), this.spawn);
|
||||
}
|
||||
else if (phase == Phase.MODIFY && this.biomes.contains(biome))
|
||||
if (phase == Phase.MODIFY && this.biomes.contains(biome))
|
||||
{
|
||||
builder.getClimateSettings().setPrecipitation(this.precipitation);
|
||||
builder.getEffects().waterColor(this.waterColor);
|
||||
@@ -166,24 +187,9 @@ public class BiomeModifierTest
|
||||
{
|
||||
return RecordCodecBuilder.create(builder -> builder.group(
|
||||
Biome.LIST_CODEC.fieldOf("biomes").forGetter(TestModifier::biomes),
|
||||
Codec.STRING.comapFlatMap(TestModifier::generationStageFromString, Decoration::toString).fieldOf("generation_stage").forGetter(TestModifier::generationStage),
|
||||
PlacedFeature.LIST_CODEC.fieldOf("features").forGetter(TestModifier::features),
|
||||
SpawnerData.CODEC.fieldOf("spawn").forGetter(TestModifier::spawn),
|
||||
Precipitation.CODEC.fieldOf("precipitation").forGetter(TestModifier::precipitation),
|
||||
Codec.INT.fieldOf("water_color").forGetter(TestModifier::waterColor)
|
||||
).apply(builder, TestModifier::new));
|
||||
}
|
||||
|
||||
private static DataResult<Decoration> generationStageFromString(String name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return DataResult.success(Decoration.valueOf(name));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return DataResult.error("Not a decoration stage: " + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user