mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 19:15:32 +00:00
Fix DatapackBuiltinEntriesProvider issues with forge registries, Fixes #9874
This commit is contained in:
9 files changed
+208
-29
No files matched your search
@@ -12,13 +12,7 @@ import net.minecraft.resources.RegistryDataLoader;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ApiStatus.Internal
|
||||
@@ -26,8 +20,7 @@ public final class DataPackRegistriesHooks {
|
||||
private DataPackRegistriesHooks() {} // utility class
|
||||
|
||||
private static final Map<ResourceKey<? extends Registry<?>>, RegistrySynchronization.NetworkedRegistryData<?>> NETWORKABLE_REGISTRIES = new LinkedHashMap<>();
|
||||
private static final List<RegistryDataLoader.RegistryData<?>> DATA_PACK_REGISTRIES = new ArrayList<>(RegistryDataLoader.WORLDGEN_REGISTRIES);
|
||||
private static final List<RegistryDataLoader.RegistryData<?>> DATA_PACK_REGISTRIES_VIEW = Collections.unmodifiableList(DATA_PACK_REGISTRIES);
|
||||
private static final List<RegistryDataLoader.RegistryData<?>> WORLDGEN_REGISTRIES = new ArrayList<>();
|
||||
private static final Set<ResourceKey<? extends Registry<?>>> SYNCED_CUSTOM_REGISTRIES = new HashSet<>();
|
||||
private static final Set<ResourceKey<? extends Registry<?>>> SYNCED_CUSTOM_REGISTRIES_VIEW = Collections.unmodifiableSet(SYNCED_CUSTOM_REGISTRIES);
|
||||
|
||||
@@ -44,7 +37,7 @@ public final class DataPackRegistriesHooks {
|
||||
/* Internal forge method, registers a datapack registry codec and folder. */
|
||||
static <T> void addRegistryCodec(DataPackRegistryEvent.DataPackRegistryData<T> data) {
|
||||
RegistryDataLoader.RegistryData<T> loaderData = data.loaderData();
|
||||
DATA_PACK_REGISTRIES.add(loaderData);
|
||||
WORLDGEN_REGISTRIES.add(loaderData);
|
||||
if (data.networkCodec() != null) {
|
||||
SYNCED_CUSTOM_REGISTRIES.add(loaderData.key());
|
||||
NETWORKABLE_REGISTRIES.put(loaderData.key(), new RegistrySynchronization.NetworkedRegistryData<>(loaderData.key(), data.networkCodec()));
|
||||
@@ -55,12 +48,27 @@ public final class DataPackRegistriesHooks {
|
||||
* {@return An unmodifiable view of the list of datapack registries}.
|
||||
* These registries are loaded from per-world datapacks on server startup.
|
||||
*/
|
||||
@Deprecated // Use RegistryDataLoader#WORLDGEN_REGISTRIES instead
|
||||
public static List<RegistryDataLoader.RegistryData<?>> getDataPackRegistries() {
|
||||
return DATA_PACK_REGISTRIES_VIEW;
|
||||
return Collections.unmodifiableList(WORLDGEN_REGISTRIES);
|
||||
}
|
||||
|
||||
@Deprecated // Use RegistryDataLoader#WORLDGEN_REGISTRIES instead
|
||||
public static Stream<RegistryDataLoader.RegistryData<?>> getDataPackRegistriesWithDimensions() {
|
||||
return Stream.concat(DATA_PACK_REGISTRIES_VIEW.stream(), RegistryDataLoader.DIMENSION_REGISTRIES.stream());
|
||||
return WORLDGEN_REGISTRIES.stream();
|
||||
}
|
||||
|
||||
/**
|
||||
* Captures a mutable view of {@link RegistryDataLoader#WORLDGEN_REGISTRIES} so that we can register modded entries during the registry event.
|
||||
* All uses should use that field instead of this class.
|
||||
* @param vanilla The vanilla worldgen registries
|
||||
* @return An unmodifiable list of worldgen registries
|
||||
*/
|
||||
public static List<RegistryDataLoader.RegistryData<?>> grabWorldgenRegistries(RegistryDataLoader.RegistryData<?>... vanilla) {
|
||||
if (!StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass().equals(RegistryDataLoader.class))
|
||||
throw new IllegalCallerException("Attempted to call DataPackRegistriesHooks#grabWorldgenRegistries!");
|
||||
WORLDGEN_REGISTRIES.addAll(Arrays.asList(vanilla));
|
||||
return Collections.unmodifiableList(WORLDGEN_REGISTRIES);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"type": "forge:add_features",
|
||||
"biomes": "#minecraft:is_overworld",
|
||||
"features": "datapack_builtin_entries_provider_test:mossy_stone",
|
||||
"step": "underground_ores"
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"type": "minecraft:ore",
|
||||
"config": {
|
||||
"discard_chance_on_air_exposure": 0.0,
|
||||
"size": 5,
|
||||
"targets": [
|
||||
{
|
||||
"state": {
|
||||
"Name": "minecraft:mossy_cobblestone"
|
||||
},
|
||||
"target": {
|
||||
"predicate_type": "minecraft:tag_match",
|
||||
"tag": "minecraft:stone_ore_replaceables"
|
||||
}
|
||||
},
|
||||
{
|
||||
"state": {
|
||||
"Name": "minecraft:mossy_cobblestone"
|
||||
},
|
||||
"target": {
|
||||
"predicate_type": "minecraft:tag_match",
|
||||
"tag": "minecraft:deepslate_ore_replaceables"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"feature": "datapack_builtin_entries_provider_test:mossy_stone",
|
||||
"placement": [
|
||||
{
|
||||
"type": "minecraft:count",
|
||||
"count": 8
|
||||
},
|
||||
{
|
||||
"type": "minecraft:in_square"
|
||||
},
|
||||
{
|
||||
"type": "minecraft:height_range",
|
||||
"height": {
|
||||
"type": "minecraft:uniform",
|
||||
"max_inclusive": {
|
||||
"absolute": 64
|
||||
},
|
||||
"min_inclusive": {
|
||||
"absolute": -64
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "minecraft:biome"
|
||||
}
|
||||
]
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.debug.gameplay.data;
|
||||
|
||||
import net.minecraft.DetectedVersion;
|
||||
import net.minecraft.core.*;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.data.metadata.PackMetadataGenerator;
|
||||
import net.minecraft.data.worldgen.BootstapContext;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.packs.PackType;
|
||||
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
|
||||
import net.minecraft.tags.BiomeTags;
|
||||
import net.minecraft.tags.BlockTags;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.levelgen.GenerationStep;
|
||||
import net.minecraft.world.level.levelgen.VerticalAnchor;
|
||||
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
|
||||
import net.minecraft.world.level.levelgen.feature.Feature;
|
||||
import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration;
|
||||
import net.minecraft.world.level.levelgen.placement.*;
|
||||
import net.minecraft.world.level.levelgen.structure.templatesystem.TagMatchTest;
|
||||
import net.minecraftforge.common.data.DatapackBuiltinEntriesProvider;
|
||||
import net.minecraftforge.common.world.BiomeModifier;
|
||||
import net.minecraftforge.common.world.ForgeBiomeModifiers;
|
||||
import net.minecraftforge.data.event.GatherDataEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import net.minecraftforge.test.BaseTestMod;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Mod(DatapackBuiltinEntriesProviderTest.MOD_ID)
|
||||
public class DatapackBuiltinEntriesProviderTest extends BaseTestMod {
|
||||
|
||||
public static final String MOD_ID = "datapack_builtin_entries_provider_test";
|
||||
// Vanilla registry entries
|
||||
public static final ResourceKey<ConfiguredFeature<?, ?>> MOSSY_STONE_FEATURE = ResourceKey.create(Registries.CONFIGURED_FEATURE, new ResourceLocation(MOD_ID, "mossy_stone"));
|
||||
public static final ResourceKey<PlacedFeature> MOSSY_STONE_PLACEMENT = ResourceKey.create(Registries.PLACED_FEATURE, new ResourceLocation(MOD_ID, "mossy_stone"));
|
||||
// Forge registry entries
|
||||
public static final ResourceKey<BiomeModifier> MOSSY_STONE_MODIFIER = ResourceKey.create(ForgeRegistries.Keys.BIOME_MODIFIERS, new ResourceLocation(MOD_ID, "mossy_stone_modifier"));
|
||||
// The ore targets
|
||||
public static final Supplier<List<OreConfiguration.TargetBlockState>> MOSSY_STONE_TARGETS = () -> {
|
||||
return List.of(
|
||||
OreConfiguration.target(new TagMatchTest(BlockTags.STONE_ORE_REPLACEABLES), Blocks.MOSSY_COBBLESTONE.defaultBlockState()),
|
||||
OreConfiguration.target(new TagMatchTest(BlockTags.DEEPSLATE_ORE_REPLACEABLES), Blocks.MOSSY_COBBLESTONE.defaultBlockState())
|
||||
);
|
||||
};
|
||||
|
||||
@SubscribeEvent
|
||||
public void onDataGen(GatherDataEvent event) {
|
||||
var gen = event.getGenerator();
|
||||
var packOutput = gen.getPackOutput();
|
||||
/* Adds the DataPackBuiltinEntriesProvider to the data generator
|
||||
* If the registry is not correctly patched (it does only include the vanilla registries), the provider will fail with an exception
|
||||
* Reason: The RegistrySetBuilder creates a full patched registry including a lookup for all registries
|
||||
* For the lookup a cloner is needed, which is not available for forge registries
|
||||
*/
|
||||
gen.addProvider(event.includeServer(), new DatapackBuiltinEntriesProvider(packOutput, event.getLookupProvider(), this.createProvider(), Set.of(MOD_ID)));
|
||||
}
|
||||
|
||||
// Creates the registry builder for 2 vanilla and 1 forge registry
|
||||
private RegistrySetBuilder createProvider() {
|
||||
var builder = new RegistrySetBuilder();
|
||||
builder.add(Registries.CONFIGURED_FEATURE, c -> this.createFeature(c));
|
||||
builder.add(Registries.PLACED_FEATURE, this::createPlacement);
|
||||
builder.add(ForgeRegistries.Keys.BIOME_MODIFIERS, this::createModifier);
|
||||
return builder;
|
||||
}
|
||||
|
||||
// Registers the mossy stone feature
|
||||
private void createFeature(BootstapContext<ConfiguredFeature<?, ?>> context) {
|
||||
context.register(MOSSY_STONE_FEATURE, new ConfiguredFeature<>(
|
||||
Feature.ORE,
|
||||
new OreConfiguration(MOSSY_STONE_TARGETS.get(), 5)
|
||||
));
|
||||
}
|
||||
|
||||
// Registers the mossy stone placement
|
||||
private void createPlacement(BootstapContext<PlacedFeature> context) {
|
||||
HolderGetter<ConfiguredFeature<?, ?>> featureRegistry = context.lookup(Registries.CONFIGURED_FEATURE);
|
||||
context.register(MOSSY_STONE_PLACEMENT, new PlacedFeature(
|
||||
featureRegistry.getOrThrow(MOSSY_STONE_FEATURE),
|
||||
List.of(CountPlacement.of(8), InSquarePlacement.spread(), HeightRangePlacement.uniform(VerticalAnchor.absolute(-64), VerticalAnchor.absolute(64)), BiomeFilter.biome())
|
||||
));
|
||||
}
|
||||
|
||||
// Registers the mossy stone biome modifier
|
||||
private void createModifier(BootstapContext<BiomeModifier> context) {
|
||||
HolderGetter<Biome> biomeRegistry = context.lookup(Registries.BIOME);
|
||||
HolderGetter<PlacedFeature> placementRegistry = context.lookup(Registries.PLACED_FEATURE);
|
||||
context.register(MOSSY_STONE_MODIFIER, new ForgeBiomeModifiers.AddFeaturesBiomeModifier(
|
||||
biomeRegistry.getOrThrow(BiomeTags.IS_OVERWORLD),
|
||||
HolderSet.direct(placementRegistry.getOrThrow(MOSSY_STONE_PLACEMENT)),
|
||||
GenerationStep.Decoration.UNDERGROUND_ORES
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user