mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 11:29:15 +00:00
Allow using PackOutputs in Forge-added datagen classes (#9182)
This commit is contained in:
1 parent
e541201129
commit
2ea16d89ca
13 files changed
+75
-72
No files matched your search
@@ -13,14 +13,12 @@ import com.google.gson.JsonObject;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
|
||||
import net.minecraft.data.CachedOutput;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.data.DataProvider;
|
||||
import net.minecraft.data.PackOutput;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.common.loot.IGlobalLootModifier;
|
||||
import net.minecraftforge.common.loot.LootModifier;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -37,14 +35,14 @@ import java.util.stream.Collectors;
|
||||
public abstract class GlobalLootModifierProvider implements DataProvider
|
||||
{
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
private final DataGenerator gen;
|
||||
private final PackOutput output;
|
||||
private final String modid;
|
||||
private final Map<String, JsonElement> toSerialize = new HashMap<>();
|
||||
private boolean replace = false;
|
||||
|
||||
public GlobalLootModifierProvider(DataGenerator gen, String modid)
|
||||
public GlobalLootModifierProvider(PackOutput output, String modid)
|
||||
{
|
||||
this.gen = gen;
|
||||
this.output = output;
|
||||
this.modid = modid;
|
||||
}
|
||||
|
||||
@@ -66,8 +64,8 @@ public abstract class GlobalLootModifierProvider implements DataProvider
|
||||
{
|
||||
start();
|
||||
|
||||
Path forgePath = this.gen.getPackOutput().getOutputFolder(PackOutput.Target.DATA_PACK).resolve("forge").resolve("loot_modifiers").resolve("global_loot_modifiers.json");
|
||||
Path modifierFolderPath = this.gen.getPackOutput().getOutputFolder(PackOutput.Target.DATA_PACK).resolve(this.modid).resolve("loot_modifiers");
|
||||
Path forgePath = this.output.getOutputFolder(PackOutput.Target.DATA_PACK).resolve("forge").resolve("loot_modifiers").resolve("global_loot_modifiers.json");
|
||||
Path modifierFolderPath = this.output.getOutputFolder(PackOutput.Target.DATA_PACK).resolve(this.modid).resolve("loot_modifiers");
|
||||
List<ResourceLocation> entries = new ArrayList<>();
|
||||
|
||||
ImmutableList.Builder<CompletableFuture<?>> futuresBuilder = new ImmutableList.Builder<>();
|
||||
|
||||
@@ -32,7 +32,6 @@ import net.minecraft.server.packs.PackType;
|
||||
import net.minecraftforge.common.crafting.CraftingHelper;
|
||||
import net.minecraftforge.common.crafting.conditions.ICondition;
|
||||
import net.minecraftforge.common.data.ExistingFileHelper.ResourceType;
|
||||
import net.minecraftforge.data.event.GatherDataEvent;
|
||||
import net.minecraftforge.registries.DataPackRegistriesHooks;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
@@ -48,7 +47,7 @@ import org.slf4j.Logger;
|
||||
public class JsonCodecProvider<T> implements DataProvider
|
||||
{
|
||||
private static final Logger LOGGER = LogUtils.getLogger();
|
||||
protected final DataGenerator dataGenerator;
|
||||
protected final PackOutput output;
|
||||
protected final ExistingFileHelper existingFileHelper;
|
||||
protected final String modid;
|
||||
protected final DynamicOps<JsonElement> dynamicOps;
|
||||
@@ -59,14 +58,14 @@ public class JsonCodecProvider<T> implements DataProvider
|
||||
protected Map<ResourceLocation, ICondition[]> conditions = Collections.emptyMap();
|
||||
|
||||
/**
|
||||
* @param dataGenerator DataGenerator provided by {@link GatherDataEvent}.
|
||||
* @param output {@linkplain PackOutput} provided by the {@link DataGenerator}.
|
||||
* @param dynamicOps DynamicOps to encode values to jsons with using the provided Codec, e.g. {@link JsonOps#INSTANCE}.
|
||||
* @param packType PackType specifying whether to generate entries in assets or data.
|
||||
* @param directory String representing the directory to generate jsons in, e.g. "dimension" or "cheesemod/cheese".
|
||||
* @param codec Codec to encode values to jsons with using the provided DynamicOps.
|
||||
* @param entries Map of named entries to serialize to jsons. Paths for values are derived from the ResourceLocation's entryid:entrypath as specified above.
|
||||
*/
|
||||
public JsonCodecProvider(DataGenerator dataGenerator, ExistingFileHelper existingFileHelper, String modid, DynamicOps<JsonElement> dynamicOps, PackType packType,
|
||||
public JsonCodecProvider(PackOutput output, ExistingFileHelper existingFileHelper, String modid, DynamicOps<JsonElement> dynamicOps, PackType packType,
|
||||
String directory, Codec<T> codec, Map<ResourceLocation, T> entries)
|
||||
{
|
||||
// Track generated data so other dataproviders can validate if needed.
|
||||
@@ -75,7 +74,7 @@ public class JsonCodecProvider<T> implements DataProvider
|
||||
{
|
||||
existingFileHelper.trackGenerated(id, resourceType);
|
||||
}
|
||||
this.dataGenerator = dataGenerator;
|
||||
this.output = output;
|
||||
this.existingFileHelper = existingFileHelper;
|
||||
this.modid = modid;
|
||||
this.dynamicOps = dynamicOps;
|
||||
@@ -92,14 +91,14 @@ public class JsonCodecProvider<T> implements DataProvider
|
||||
* be generated this way.
|
||||
*
|
||||
* @param <T> Registry element type, e.g. Biome
|
||||
* @param dataGenerator DataGenerator provided by {@link GatherDataEvent}.
|
||||
* @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(DataGenerator dataGenerator, ExistingFileHelper existingFileHelper, String modid,
|
||||
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();
|
||||
@@ -110,13 +109,13 @@ public class JsonCodecProvider<T> implements DataProvider
|
||||
: 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<>(dataGenerator, existingFileHelper, modid, registryOps, PackType.SERVER_DATA, registryFolder, codec, entries);
|
||||
return new JsonCodecProvider<>(output, existingFileHelper, modid, registryOps, PackType.SERVER_DATA, registryFolder, codec, entries);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<?> run(final CachedOutput cache)
|
||||
{
|
||||
final Path outputFolder = this.dataGenerator.getPackOutput().getOutputFolder(this.packType == PackType.CLIENT_RESOURCES
|
||||
final Path outputFolder = this.output.getOutputFolder(this.packType == PackType.CLIENT_RESOURCES
|
||||
? PackOutput.Target.RESOURCE_PACK
|
||||
: PackOutput.Target.DATA_PACK);
|
||||
ImmutableList.Builder<CompletableFuture<?>> futuresBuilder = new ImmutableList.Builder<>();
|
||||
|
||||
@@ -12,7 +12,6 @@ import java.util.TreeMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraft.data.CachedOutput;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.data.DataProvider;
|
||||
import net.minecraft.data.PackOutput;
|
||||
import net.minecraft.world.effect.MobEffect;
|
||||
@@ -24,12 +23,12 @@ import net.minecraft.world.level.block.Block;
|
||||
|
||||
public abstract class LanguageProvider implements DataProvider {
|
||||
private final Map<String, String> data = new TreeMap<>();
|
||||
private final DataGenerator gen;
|
||||
private final PackOutput output;
|
||||
private final String modid;
|
||||
private final String locale;
|
||||
|
||||
public LanguageProvider(DataGenerator gen, String modid, String locale) {
|
||||
this.gen = gen;
|
||||
public LanguageProvider(PackOutput output, String modid, String locale) {
|
||||
this.output = output;
|
||||
this.modid = modid;
|
||||
this.locale = locale;
|
||||
}
|
||||
@@ -41,7 +40,7 @@ public abstract class LanguageProvider implements DataProvider {
|
||||
addTranslations();
|
||||
|
||||
if (!data.isEmpty())
|
||||
return save(cache, this.gen.getPackOutput().getOutputFolder(PackOutput.Target.RESOURCE_PACK).resolve(this.modid).resolve("lang").resolve(this.locale + ".json"));
|
||||
return save(cache, this.output.getOutputFolder(PackOutput.Target.RESOURCE_PACK).resolve(this.modid).resolve("lang").resolve(this.locale + ".json"));
|
||||
|
||||
return CompletableFuture.allOf();
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraft.data.CachedOutput;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.data.DataProvider;
|
||||
import net.minecraft.data.PackOutput;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
@@ -30,7 +29,7 @@ import org.apache.logging.log4j.Logger;
|
||||
public abstract class SoundDefinitionsProvider implements DataProvider
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private final DataGenerator generator;
|
||||
private final PackOutput output;
|
||||
private final String modId;
|
||||
private final ExistingFileHelper helper;
|
||||
|
||||
@@ -39,13 +38,13 @@ public abstract class SoundDefinitionsProvider implements DataProvider
|
||||
/**
|
||||
* Creates a new instance of this data provider.
|
||||
*
|
||||
* @param generator The data generator instance provided by the event you are initializing this provider in.
|
||||
* @param output The {@linkplain PackOutput} instance provided by the data generator.
|
||||
* @param modId The mod ID of the current mod.
|
||||
* @param helper The existing file helper provided by the event you are initializing this provider in.
|
||||
*/
|
||||
protected SoundDefinitionsProvider(final DataGenerator generator, final String modId, final ExistingFileHelper helper)
|
||||
protected SoundDefinitionsProvider(final PackOutput output, final String modId, final ExistingFileHelper helper)
|
||||
{
|
||||
this.generator = generator;
|
||||
this.output = output;
|
||||
this.modId = modId;
|
||||
this.helper = helper;
|
||||
}
|
||||
@@ -63,7 +62,7 @@ public abstract class SoundDefinitionsProvider implements DataProvider
|
||||
this.validate();
|
||||
if (!this.sounds.isEmpty())
|
||||
{
|
||||
return this.save(cache, this.generator.getPackOutput().getOutputFolder(PackOutput.Target.RESOURCE_PACK).resolve(this.modId).resolve("sounds.json"));
|
||||
return this.save(cache, this.output.getOutputFolder(PackOutput.Target.RESOURCE_PACK).resolve(this.modId).resolve("sounds.json"));
|
||||
}
|
||||
|
||||
return CompletableFuture.allOf();
|
||||
|
||||
Reference in new issue
Block a user