From dd4fd39044ab15baef0bfea2dfe3ad10db5318fb Mon Sep 17 00:00:00 2001
From: modmuss50 <modmuss50@gmail.com>
Date: Sun, 25 Sep 2022 14:45:43 +0100
Subject: [PATCH] Add DataGeneratorEntrypoint.getEffectiveModId (#2534)

---
 .../api/datagen/v1/DataGeneratorEntrypoint.java    | 14 ++++++++++++++
 .../fabric/api/datagen/v1/FabricDataGenerator.java |  3 ++-
 .../fabric/impl/datagen/FabricDataGenHelper.java   | 13 +++++++++++--
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/DataGeneratorEntrypoint.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/DataGeneratorEntrypoint.java
index ae7c93d6f..b2927e4f4 100644
--- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/DataGeneratorEntrypoint.java
+++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/DataGeneratorEntrypoint.java
@@ -16,6 +16,8 @@
 
 package net.fabricmc.fabric.api.datagen.v1;
 
+import org.jetbrains.annotations.Nullable;
+
 /**
  * An entry point for data generation.
  *
@@ -31,4 +33,16 @@ public interface DataGeneratorEntrypoint {
 	 * @param fabricDataGenerator The {@link FabricDataGenerator} instance
 	 */
 	void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator);
+
+	/**
+	 * Returns the mod ID of the mod the data is being generated for.
+	 * A {@code null} return will run the data generator using the mod ID that registered the current entrypoint.
+	 *
+	 * @return a {@link String} or {@code null}
+	 * @throws RuntimeException If the mod ID does not exist.
+	 */
+	@Nullable
+	default String getEffectiveModId() {
+		return null;
+	}
 }
diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricDataGenerator.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricDataGenerator.java
index 8ec109945..65eca6e33 100644
--- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricDataGenerator.java
+++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricDataGenerator.java
@@ -18,6 +18,7 @@ package net.fabricmc.fabric.api.datagen.v1;
 
 import java.nio.file.Path;
 import java.util.Collections;
+import java.util.Objects;
 import java.util.function.Function;
 
 import org.jetbrains.annotations.ApiStatus;
@@ -37,7 +38,7 @@ public final class FabricDataGenerator extends DataGenerator {
 	@ApiStatus.Internal
 	public FabricDataGenerator(Path output, ModContainer mod, boolean strictValidation) {
 		super(output, Collections.emptyList());
-		this.modContainer = mod;
+		this.modContainer = Objects.requireNonNull(mod);
 		this.strictValidation = strictValidation;
 	}
 
diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/impl/datagen/FabricDataGenHelper.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/impl/datagen/FabricDataGenHelper.java
index f41b2cc66..9abec7081 100644
--- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/impl/datagen/FabricDataGenHelper.java
+++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/impl/datagen/FabricDataGenHelper.java
@@ -42,6 +42,7 @@ import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
 import net.fabricmc.fabric.api.datagen.v1.provider.FabricTagProvider.DynamicRegistryTagProvider;
 import net.fabricmc.fabric.api.resource.conditions.v1.ConditionJsonProvider;
 import net.fabricmc.loader.api.FabricLoader;
+import net.fabricmc.loader.api.ModContainer;
 import net.fabricmc.loader.api.entrypoint.EntrypointContainer;
 
 @ApiStatus.Internal
@@ -126,8 +127,16 @@ public final class FabricDataGenHelper {
 			LOGGER.info("Running data generator for {}", id);
 
 			try {
-				FabricDataGenerator dataGenerator = new FabricDataGenerator(outputDir, entrypointContainer.getProvider(), STRICT_VALIDATION);
-				entrypointContainer.getEntrypoint().onInitializeDataGenerator(dataGenerator);
+				final DataGeneratorEntrypoint entrypoint = entrypointContainer.getEntrypoint();
+				final String effectiveModId = entrypoint.getEffectiveModId();
+				ModContainer modContainer = entrypointContainer.getProvider();
+
+				if (effectiveModId != null) {
+					modContainer = FabricLoader.getInstance().getModContainer(effectiveModId).orElseThrow(() -> new RuntimeException("Failed to find effective mod container for mod id (%s)".formatted(effectiveModId)));
+				}
+
+				FabricDataGenerator dataGenerator = new FabricDataGenerator(outputDir, modContainer, STRICT_VALIDATION);
+				entrypoint.onInitializeDataGenerator(dataGenerator);
 				dataGenerator.run();
 			} catch (Throwable t) {
 				throw new RuntimeException("Failed to run data generator from mod (%s)".formatted(id), t);