From 8558596bb987d28cec0625788d3120fda5f8b8ea Mon Sep 17 00:00:00 2001 From: Matyrobbrt Date: Sat, 28 Dec 2024 16:47:22 +0200 Subject: [PATCH] Only fire gather data events for the mods enabled Previously, all mods would receive the event and register to the data generator and the cli args will simply decide which generator to run. This can cause issues when dependencies ship their datagen which requires other dependencies as you'll hit CNFEs. This PR changes it so that only the requested mods get the event posted. --- .../neoforge/data/event/GatherDataEvent.java | 9 +++++---- .../neoforge/data/loading/DatagenModLoader.java | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/neoforged/neoforge/data/event/GatherDataEvent.java b/src/main/java/net/neoforged/neoforge/data/event/GatherDataEvent.java index f35bb1768a..07c3aff9dc 100644 --- a/src/main/java/net/neoforged/neoforge/data/event/GatherDataEvent.java +++ b/src/main/java/net/neoforged/neoforge/data/event/GatherDataEvent.java @@ -29,6 +29,7 @@ import net.neoforged.fml.ModContainer; import net.neoforged.fml.event.IModBusEvent; import net.neoforged.neoforge.common.data.ExistingFileHelper; +import org.jetbrains.annotations.ApiStatus; public abstract class GatherDataEvent extends Event implements IModBusEvent { private final DataGenerator dataGenerator; @@ -87,6 +88,7 @@ public Client(ModContainer mc, DataGenerator dataGenerator, DataGeneratorConfig } } + @ApiStatus.Internal public static class DataGeneratorConfig { private final Set mods; private final Path path; @@ -125,10 +127,9 @@ public boolean isFlat() { return flat || getMods().size() == 1; } - public DataGenerator makeGenerator(final Function pathEnhancer, final boolean shouldExecute) { - final DataGenerator generator = new DataGenerator(pathEnhancer.apply(path), DetectedVersion.tryDetectVersion(), shouldExecute); - if (shouldExecute) - generators.add(generator); + public DataGenerator makeGenerator(final Function pathEnhancer) { + final DataGenerator generator = new DataGenerator(pathEnhancer.apply(path), DetectedVersion.tryDetectVersion(), true); + generators.add(generator); return generator; } diff --git a/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java b/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java index faa52ca084..ab13f2f96f 100644 --- a/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java +++ b/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java @@ -15,7 +15,8 @@ import net.minecraft.data.DataGenerator; import net.minecraft.data.registries.VanillaRegistries; import net.minecraft.server.Bootstrap; -import net.neoforged.fml.ModLoader; +import net.neoforged.fml.ModContainer; +import net.neoforged.fml.ModList; import net.neoforged.neoforge.common.data.ExistingFileHelper; import net.neoforged.neoforge.data.event.GatherDataEvent; import net.neoforged.neoforge.internal.CommonModLoader; @@ -55,8 +56,16 @@ public static void begin(final Set mods, final Path path, final Collecti } setup.run(); existingFileHelper = new ExistingFileHelper(existingPacks, existingMods, structureValidator, assetIndex, assetsDir); - ModLoader.runEventGenerator(mc -> eventGenerator.create(mc, dataGeneratorConfig.makeGenerator(p -> dataGeneratorConfig.isFlat() ? p : p.resolve(mc.getModId()), - dataGeneratorConfig.getMods().contains(mc.getModId())), dataGeneratorConfig, existingFileHelper)); + + // Only fire the event for mods that have their generators enabled + for (ModContainer mod : ModList.get().getSortedMods()) { + if (dataGeneratorConfig.getMods().contains(mod.getModId())) { + var generator = dataGeneratorConfig.makeGenerator(p -> dataGeneratorConfig.isFlat() ? p : p.resolve(mod.getModId())); + var event = eventGenerator.create(mod, generator, dataGeneratorConfig, existingFileHelper); + mod.acceptEvent(event); + } + } + dataGeneratorConfig.runAll(); } }