mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 08:17:35 +00:00
[1.19.3] Fix Datagen Tests and Providers (#9212)
This commit is contained in:
1 parent
cc393818e4
commit
58d6f072e4
9 files changed
+229
-200
No files matched your search
@@ -5,29 +5,66 @@
|
||||
|
||||
package net.minecraftforge.common.data;
|
||||
|
||||
import net.minecraft.Util;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.core.RegistryAccess;
|
||||
import net.minecraft.core.RegistrySetBuilder;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.data.PackOutput;
|
||||
import net.minecraft.data.registries.RegistriesDatapackGenerator;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraftforge.registries.DataPackRegistriesHooks;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* An extension of the {@link RegistriesDatapackGenerator} which properly handles
|
||||
* referencing existing dynamic registry objects within another dynamic registry
|
||||
* object.
|
||||
*/
|
||||
public class DatapackBuiltinEntriesProvider extends RegistriesDatapackGenerator
|
||||
{
|
||||
public DatapackBuiltinEntriesProvider(PackOutput output, Supplier<HolderLookup.Provider> registriesSupplier)
|
||||
|
||||
/**
|
||||
* Constructs a new datapack provider which generates all registry objects
|
||||
* from the provided mods using the holder.
|
||||
*
|
||||
* @param output the target directory of the data generator
|
||||
* @param registries a future of a lookup for registries and their objects
|
||||
* @param modIds a set of mod ids to generate the dynamic registry objects of
|
||||
*/
|
||||
public DatapackBuiltinEntriesProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> registries, Set<String> modIds)
|
||||
{
|
||||
super(output, CompletableFuture.supplyAsync(registriesSupplier, Util.backgroundExecutor()));
|
||||
super(output, registries, modIds);
|
||||
}
|
||||
|
||||
public DatapackBuiltinEntriesProvider(PackOutput output, RegistrySetBuilder datapackEntriesBuilder)
|
||||
/**
|
||||
* Constructs a new datapack provider which generates all registry objects
|
||||
* from the provided mods using the holder. All entries that need to be
|
||||
* bootstrapped are provided within the {@link RegistrySetBuilder}.
|
||||
*
|
||||
* @param output the target directory of the data generator
|
||||
* @param registries a future of a lookup for registries and their objects
|
||||
* @param datapackEntriesBuilder a builder containing the dynamic registry objects added by this provider
|
||||
* @param modIds a set of mod ids to generate the dynamic registry objects of
|
||||
*/
|
||||
public DatapackBuiltinEntriesProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> registries, RegistrySetBuilder datapackEntriesBuilder, Set<String> modIds)
|
||||
{
|
||||
this(output, () -> createLookup(datapackEntriesBuilder));
|
||||
this(output, registries.thenApply(r -> constructRegistries(r, datapackEntriesBuilder)), modIds);
|
||||
}
|
||||
|
||||
public static HolderLookup.Provider createLookup(RegistrySetBuilder datapackEntriesBuilder) {
|
||||
return datapackEntriesBuilder.build(RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY));
|
||||
/**
|
||||
* A method used to construct empty bootstraps for all registries this provider
|
||||
* did not touch such that existing dynamic registry objects do not get inlined.
|
||||
*
|
||||
* @param original a future of a lookup for registries and their objects
|
||||
* @param datapackEntriesBuilder a builder containing the dynamic registry objects added by this provider
|
||||
* @return a new lookup containing the existing and to be generated registries and their objects
|
||||
*/
|
||||
private static HolderLookup.Provider constructRegistries(HolderLookup.Provider original, RegistrySetBuilder datapackEntriesBuilder)
|
||||
{
|
||||
var builderKeys = new HashSet<>(datapackEntriesBuilder.getEntryKeys());
|
||||
DataPackRegistriesHooks.getDataPackRegistries().stream().filter(data -> !builderKeys.contains(data.key())).forEach(data -> datapackEntriesBuilder.add(data.key(), context -> {}));
|
||||
return datapackEntriesBuilder.buildPatch(RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY), original);
|
||||
}
|
||||
}
|
||||
@@ -20,10 +20,11 @@ import java.util.function.Consumer;
|
||||
* An extension of the {@link AdvancementProvider} to provide a feature-complete
|
||||
* experience to generate modded advancements.
|
||||
*/
|
||||
public abstract class ForgeAdvancementProvider extends AdvancementProvider
|
||||
public class ForgeAdvancementProvider extends AdvancementProvider
|
||||
{
|
||||
/**
|
||||
* Default constructor.
|
||||
* Constructs an advancement provider using the generators to write the
|
||||
* advancements to a file.
|
||||
*
|
||||
* @param output the target directory of the data generator
|
||||
* @param registries a future of a lookup for registries and their objects
|
||||
|
||||
@@ -84,34 +84,6 @@ public class JsonCodecProvider<T> implements DataProvider
|
||||
this.entries = entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return DatapackRegistryProvider that encodes using the registered loading codec for the provided registry key}
|
||||
* Ensures the correct directory and codec are used.
|
||||
* Only vanilla datapack registries enumerated in {@link RegistryAccess} and custom forge datapack registries can
|
||||
* be generated this way.
|
||||
*
|
||||
* @param <T> Registry element type, e.g. Biome
|
||||
* @param output {@linkplain PackOutput} provided by the {@link DataGenerator}.
|
||||
* @param modid namespace of the mod adding this DataProvider, for logging purposes.
|
||||
* @param registryOps RegistryOps to encode values to json with.
|
||||
* @param registryKey ResourceKey identifying the registry and its directory.
|
||||
* @param entries Map of entries to encode and their ResourceLocations. Paths for values are derived from the ResourceLocation's entryid:entrypath.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> JsonCodecProvider<T> forDatapackRegistry(PackOutput output, ExistingFileHelper existingFileHelper, String modid,
|
||||
RegistryOps<JsonElement> registryOps, ResourceKey<Registry<T>> registryKey, Map<ResourceLocation, T> entries)
|
||||
{
|
||||
final ResourceLocation registryId = registryKey.location();
|
||||
// Minecraft datapack registry folders are in data/json-namespace/registry-name/
|
||||
// Non-vanilla registry folders are data/json-namespace/registry-namespace/registry-name/
|
||||
final String registryFolder = registryId.getNamespace().equals("minecraft")
|
||||
? registryId.getPath()
|
||||
: registryId.getNamespace() + "/" + registryId.getPath();
|
||||
RegistryDataLoader.RegistryData<?> registryData = DataPackRegistriesHooks.getDataPackRegistries().stream().filter(data -> data.key() == registryKey).findAny().orElseThrow();
|
||||
final Codec<T> codec = (Codec<T>) registryData.elementCodec();
|
||||
return new JsonCodecProvider<>(output, existingFileHelper, modid, registryOps, PackType.SERVER_DATA, registryFolder, codec, entries);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<?> run(final CachedOutput cache)
|
||||
{
|
||||
@@ -164,4 +136,4 @@ public class JsonCodecProvider<T> implements DataProvider
|
||||
this.conditions = conditions;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user