mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 11:05:40 +00:00
Replace StructureSpawnListGatherEvent with StructureModifiers (#8717)
This commit is contained in:
1 parent
59b8ec5625
commit
e386229b66
21 files changed
+863
-378
No files matched your search
@@ -5,16 +5,12 @@
|
||||
|
||||
package net.minecraftforge.debug.world;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.data.CachedOutput;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.DataResult;
|
||||
@@ -31,7 +27,6 @@ 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.server.packs.PackType;
|
||||
import net.minecraft.tags.BiomeTags;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
@@ -42,6 +37,8 @@ import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
|
||||
import net.minecraft.world.level.levelgen.placement.BiomeFilter;
|
||||
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.ModifiableBiomeInfo.BiomeInfo.Builder;
|
||||
@@ -72,11 +69,13 @@ public class BiomeModifierTest
|
||||
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);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public BiomeModifierTest()
|
||||
{
|
||||
if (!ENABLED)
|
||||
@@ -89,86 +88,49 @@ public class BiomeModifierTest
|
||||
serializers.register(modBus);
|
||||
serializers.register(TEST, TestModifier::makeCodec);
|
||||
|
||||
// Biome modifiers don't need to be registered when defined in json, but they do need to be registered if we are to datagenerate the jsons.
|
||||
// We'll also datagenerate a placedfeature json (using our own placedfeature avoids feature cycle problems when we add it to biomes).
|
||||
final DeferredRegister<PlacedFeature> placedFeatures = DeferredRegister.create(Registry.PLACED_FEATURE_REGISTRY, MODID);
|
||||
placedFeatures.register(modBus);
|
||||
placedFeatures.register(BASALT_PILLARS,
|
||||
() -> new PlacedFeature((Holder<ConfiguredFeature<?,?>>) (Holder<? extends ConfiguredFeature<?,?>>)NetherFeatures.LARGE_BASALT_COLUMNS,
|
||||
List.of(CountOnEveryLayerPlacement.of(1), BiomeFilter.biome())));
|
||||
|
||||
modBus.addListener(this::onGatherData);
|
||||
}
|
||||
|
||||
private void onGatherData(GatherDataEvent event)
|
||||
{
|
||||
// Example of how to datagen datapack registry objects.
|
||||
// Datapack registry objects referred to by other datapack registry objects must be registered first.
|
||||
DataGenerator generator = event.getGenerator();
|
||||
final Path outputFolder = generator.getOutputFolder();
|
||||
final RegistryOps<JsonElement> ops = RegistryOps.create(JsonOps.INSTANCE, RegistryAccess.BUILTIN.get());
|
||||
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
final String directory = PackType.SERVER_DATA.getDirectory();
|
||||
Registry<PlacedFeature> placedFeatures = ops.registry(Registry.PLACED_FEATURE_REGISTRY).get();
|
||||
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();
|
||||
|
||||
// prepare to datagenerate our placed feature
|
||||
final String featurePathString = String.join("/", directory, MODID, Registry.PLACED_FEATURE_REGISTRY.location().getPath(), BASALT_PILLARS + ".json");
|
||||
final Path featurePath = outputFolder.resolve(featurePathString);
|
||||
final PlacedFeature feature = placedFeatures.get(BASALT_PILLARS_RL);
|
||||
// Create our PlacedFeature so we can generate it.
|
||||
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()));
|
||||
|
||||
// prepare to datagenerate our biome modifier
|
||||
final ResourceLocation biomeModifiersRegistryID = ForgeRegistries.Keys.BIOME_MODIFIERS.location();
|
||||
final String biomeModifierPathString = String.join("/", directory, MODID, biomeModifiersRegistryID.getNamespace(), biomeModifiersRegistryID.getPath(), MODIFY_BADLANDS + ".json");
|
||||
final Path biomeModifierPath = outputFolder.resolve(biomeModifierPathString);
|
||||
// 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,
|
||||
HolderSet.direct(placedFeatures.getOrCreateHolderOrThrow(ResourceKey.create(Registry.PLACED_FEATURE_REGISTRY, BASALT_PILLARS_RL))),
|
||||
placedFeatureHolderSet,
|
||||
new SpawnerData(EntityType.MAGMA_CUBE, 100, 1, 4),
|
||||
Precipitation.SNOW,
|
||||
0xFF0000
|
||||
);
|
||||
|
||||
generator.addProvider(event.includeServer(), new DataProvider()
|
||||
{
|
||||
@Override
|
||||
public void run(final CachedOutput cache) throws IOException
|
||||
{
|
||||
PlacedFeature.DIRECT_CODEC.encodeStart(ops, feature)
|
||||
.resultOrPartial(msg -> LOGGER.error("Failed to encode {}: {}", featurePathString, msg)) // Log error on encode failure.
|
||||
.ifPresent(json -> // Output to file on encode success.
|
||||
{
|
||||
try
|
||||
{
|
||||
DataProvider.saveStable(cache, json, featurePath);
|
||||
}
|
||||
catch (IOException e) // The throws can't deal with this exception, because we're inside the ifPresent.
|
||||
{
|
||||
LOGGER.error("Failed to save " + featurePathString, e);
|
||||
}
|
||||
});
|
||||
|
||||
BiomeModifier.DIRECT_CODEC.encodeStart(ops, biomeModifier)
|
||||
.resultOrPartial(msg -> LOGGER.error("Failed to encode {}: {}", biomeModifierPathString, msg)) // Log error on encode failure.
|
||||
.ifPresent(json -> // Output to file on encode success.
|
||||
{
|
||||
try
|
||||
{
|
||||
DataProvider.saveStable(cache, json, biomeModifierPath);
|
||||
}
|
||||
catch (IOException e) // The throws can't deal with this exception, because we're inside the ifPresent.
|
||||
{
|
||||
LOGGER.error("Failed to save " + biomeModifierPathString, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return MODID + " data provider";
|
||||
}
|
||||
});
|
||||
// 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);
|
||||
}
|
||||
|
||||
public record TestModifier(HolderSet<Biome> biomes, Decoration generationStage, HolderSet<PlacedFeature> features, SpawnerData spawn, Precipitation precipitation, int waterColor)
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.debug.world;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import java.util.Map;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.HolderSet;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.RegistryAccess;
|
||||
import net.minecraft.core.RegistryCodecs;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.data.DataProvider;
|
||||
import net.minecraft.resources.RegistryOps;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.MobCategory;
|
||||
import net.minecraft.world.level.biome.MobSpawnSettings;
|
||||
import net.minecraft.world.level.levelgen.structure.BuiltinStructures;
|
||||
import net.minecraft.world.level.levelgen.structure.Structure;
|
||||
import net.minecraftforge.common.data.JsonCodecProvider;
|
||||
import net.minecraftforge.common.world.ModifiableStructureInfo.StructureInfo.Builder;
|
||||
import net.minecraftforge.common.world.StructureModifier;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import net.minecraftforge.forge.event.lifecycle.GatherDataEvent;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import net.minecraftforge.registries.ForgeRegistries.Keys;
|
||||
import net.minecraftforge.registries.RegistryObject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
/**
|
||||
* <p>This tests the requirements of structure modifier jsons::</p>
|
||||
* <ul>
|
||||
* <li>A structure modifier json is created via datagen.</li>
|
||||
* <li>The structure modifier adds a mob spawn of the given category to the given structures.</li>
|
||||
* </ul>
|
||||
* <p>If the structure modifier is applied correctly, then strongholds should have wither skeletons spawn in them.</p>
|
||||
*/
|
||||
@Mod(StructureModifierTest.MODID)
|
||||
public class StructureModifierTest
|
||||
{
|
||||
private static final Codec<HolderSet<Structure>> STRUCTURE_LIST_CODEC = RegistryCodecs.homogeneousList(Registry.STRUCTURE_REGISTRY, Structure.DIRECT_CODEC);
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final String MODID = "structure_modifiers_test";
|
||||
public static final boolean ENABLED = true;
|
||||
public static final String TEST = "test";
|
||||
public static final ResourceLocation ADD_SPAWNS_TO_STRUCTURE_RL = new ResourceLocation(MODID, TEST);
|
||||
public static final String MODIFY_STRONGHOLD = "modify_stronghold";
|
||||
public static final ResourceLocation MODIFY_STRONGHOLD_RL = new ResourceLocation(MODID, MODIFY_STRONGHOLD);
|
||||
|
||||
public StructureModifierTest()
|
||||
{
|
||||
if (!ENABLED)
|
||||
return;
|
||||
|
||||
IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
|
||||
// Serializer types can be registered via deferred register.
|
||||
final DeferredRegister<Codec<? extends StructureModifier>> serializers = DeferredRegister.create(Keys.STRUCTURE_MODIFIER_SERIALIZERS, MODID);
|
||||
serializers.register(modBus);
|
||||
serializers.register(TEST, TestModifier::makeCodec);
|
||||
|
||||
// Structure modifiers don't need to be registered when defined in json, but they do need to be registered if we are to datagenerate the jsons.
|
||||
|
||||
modBus.addListener(this::onGatherData);
|
||||
}
|
||||
|
||||
private void onGatherData(GatherDataEvent event)
|
||||
{
|
||||
// Example of how to datagen datapack registry objects.
|
||||
DataGenerator generator = event.getGenerator();
|
||||
final RegistryOps<JsonElement> ops = RegistryOps.create(JsonOps.INSTANCE, RegistryAccess.BUILTIN.get());
|
||||
|
||||
// prepare to datagenerate our structure modifier
|
||||
final StructureModifier structureModifier = new TestModifier(
|
||||
HolderSet.direct(ops.registry(Registry.STRUCTURE_REGISTRY).get().getHolder(BuiltinStructures.STRONGHOLD).orElseThrow()),
|
||||
MobCategory.MONSTER,
|
||||
new MobSpawnSettings.SpawnerData(EntityType.WITHER_SKELETON, 100, 5, 15)
|
||||
);
|
||||
|
||||
DataProvider structureModifierProvider =
|
||||
JsonCodecProvider.forDatapackRegistry(generator, event.getExistingFileHelper(), MODID, ops, ForgeRegistries.Keys.STRUCTURE_MODIFIERS,
|
||||
Map.of(MODIFY_STRONGHOLD_RL, structureModifier));
|
||||
generator.addProvider(event.includeServer(), structureModifierProvider);
|
||||
}
|
||||
|
||||
public record TestModifier(HolderSet<Structure> structures, MobCategory category, MobSpawnSettings.SpawnerData spawn)
|
||||
implements StructureModifier
|
||||
{
|
||||
private static final RegistryObject<Codec<? extends StructureModifier>> SERIALIZER = RegistryObject.create(ADD_SPAWNS_TO_STRUCTURE_RL, ForgeRegistries.Keys.STRUCTURE_MODIFIER_SERIALIZERS, MODID);
|
||||
|
||||
@Override
|
||||
public void modify(Holder<Structure> structure, Phase phase, Builder builder)
|
||||
{
|
||||
if (phase == Phase.ADD && this.structures.contains(structure))
|
||||
{
|
||||
builder.getStructureSettings()
|
||||
.getOrAddSpawnOverrides(category)
|
||||
.addSpawn(spawn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Codec<? extends StructureModifier> codec()
|
||||
{
|
||||
return SERIALIZER.get();
|
||||
}
|
||||
|
||||
private static Codec<TestModifier> makeCodec()
|
||||
{
|
||||
return RecordCodecBuilder.create(builder -> builder.group(
|
||||
STRUCTURE_LIST_CODEC.fieldOf("structures").forGetter(TestModifier::structures),
|
||||
MobCategory.CODEC.fieldOf("category").forGetter(TestModifier::category),
|
||||
MobSpawnSettings.SpawnerData.CODEC.fieldOf("spawn").forGetter(TestModifier::spawn)
|
||||
).apply(builder, TestModifier::new));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.debug.world;
|
||||
|
||||
import net.minecraft.data.worldgen.Structures;
|
||||
import net.minecraft.tags.StructureTags;
|
||||
import net.minecraft.world.entity.MobCategory;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.level.biome.MobSpawnSettings;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.world.StructureSpawnListGatherEvent;
|
||||
import net.minecraftforge.eventbus.api.EventPriority;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@Mod(StructureSpawnListGatherEventTest.MODID)
|
||||
public class StructureSpawnListGatherEventTest
|
||||
{
|
||||
|
||||
public static final String MODID = "structure_spawn_list_event_test";
|
||||
private static final Logger LOGGER = LogManager.getLogger(MODID);
|
||||
|
||||
public StructureSpawnListGatherEventTest()
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.addListener(EventPriority.HIGH, this::onStructureSpawnListGather);
|
||||
}
|
||||
|
||||
private void onStructureSpawnListGather(StructureSpawnListGatherEvent event)
|
||||
{
|
||||
// TODO-PATCHING: we likely want to get registry access here, so we can resolve tags instead of hardcoding structure objects
|
||||
if (Structures.STRONGHOLD.get() == event.getStructure())
|
||||
{
|
||||
event.addEntitySpawn(MobCategory.MONSTER, new MobSpawnSettings.SpawnerData(EntityType.WITHER_SKELETON, 100, 5, 15));
|
||||
LOGGER.info("Adding wither skeleton spawns to strong holds");
|
||||
}
|
||||
else if (Structures.SHIPWRECK.get() == event.getStructure())
|
||||
{
|
||||
event.setInsideOnly(false);
|
||||
event.addEntitySpawn(MobCategory.MONSTER, new MobSpawnSettings.SpawnerData(EntityType.GUARDIAN, 100, 5, 15));
|
||||
LOGGER.info("Adding guardians spawns to shipwrecks");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user