44import java .util .Objects ;
55import javax .inject .Inject ;
66import net .neoforged .moddevgradle .legacyforge .internal .MinecraftMappings ;
7+ import net .neoforged .moddevgradle .legacyforge .internal .SrgMappingsRule ;
78import net .neoforged .moddevgradle .legacyforge .tasks .RemapJar ;
89import net .neoforged .moddevgradle .legacyforge .tasks .RemapOperation ;
910import org .apache .commons .lang3 .StringUtils ;
@@ -32,6 +33,9 @@ public abstract class ObfuscationExtension {
3233 private final Configuration installerToolsRuntime ;
3334 private final FileCollection extraMixinMappings ;
3435
36+ private final MinecraftMappings namedMappings ;
37+ private final MinecraftMappings srgMappings ;
38+
3539 @ Inject
3640 public ObfuscationExtension (Project project ,
3741 Configuration autoRenamingToolRuntime ,
@@ -41,6 +45,9 @@ public ObfuscationExtension(Project project,
4145 this .autoRenamingToolRuntime = autoRenamingToolRuntime ;
4246 this .installerToolsRuntime = installerToolsRuntime ;
4347 this .extraMixinMappings = extraMixinMappings ;
48+
49+ this .namedMappings = project .getObjects ().named (MinecraftMappings .class , MinecraftMappings .NAMED );
50+ this .srgMappings = project .getObjects ().named (MinecraftMappings .class , MinecraftMappings .SRG );
4451 }
4552
4653 private <T > Provider <T > assertConfigured (Provider <T > provider ) {
@@ -123,7 +130,7 @@ public TaskProvider<RemapJar> reobfuscate(TaskProvider<? extends AbstractArchive
123130 var config = configurations .getByName (configurationName );
124131 // Mark the original configuration as NAMED to be able to disambiguate between it and the reobfuscated jar,
125132 // this is used for example by the JarJar configuration.
126- config .getAttributes ().attribute (MinecraftMappings .ATTRIBUTE , MinecraftMappings . NAMED );
133+ config .getAttributes ().attribute (MinecraftMappings .ATTRIBUTE , namedMappings );
127134
128135 // Now create a reobf configuration
129136 var reobfConfig = configurations .maybeCreate ("reobf" + StringUtils .capitalize (configurationName ));
@@ -132,7 +139,7 @@ public TaskProvider<RemapJar> reobfuscate(TaskProvider<? extends AbstractArchive
132139 for (var attribute : config .getAttributes ().keySet ()) {
133140 copyAttribute (project , attribute , config , reobfConfig );
134141 }
135- reobfConfig .getAttributes ().attribute (MinecraftMappings .ATTRIBUTE , MinecraftMappings . SRG );
142+ reobfConfig .getAttributes ().attribute (MinecraftMappings .ATTRIBUTE , srgMappings );
136143 project .getArtifacts ().add (reobfConfig .getName (), reobf );
137144
138145 // Publish the reobf configuration instead of the original one to Maven
@@ -156,11 +163,8 @@ private static <T> void copyAttribute(Project project, Attribute<T> attribute, C
156163 public Configuration createRemappingConfiguration (Configuration parent ) {
157164 var remappingConfig = project .getConfigurations ().create ("mod" + StringUtils .capitalize (parent .getName ()), spec -> {
158165 spec .setDescription ("Configuration for dependencies of " + parent .getName () + " that needs to be remapped" );
159- spec .attributes (attributeContainer -> {
160- attributeContainer .attribute (MinecraftMappings .ATTRIBUTE , MinecraftMappings .SRG );
161- });
162166 spec .setCanBeConsumed (false );
163- spec .setCanBeResolved (false );
167+ spec .setCanBeResolved (true );
164168 spec .setTransitive (false );
165169
166170 // Unfortunately, if we simply try to make the parent extend this config, transformations will not run because the parent doesn't request remapped deps
@@ -169,29 +173,39 @@ public Configuration createRemappingConfiguration(Configuration parent) {
169173 // Additionally, we force dependencies to be non-transitive since we cannot apply the attribute hack to transitive dependencies.
170174 spec .withDependencies (dependencies -> dependencies .forEach (dep -> {
171175 if (dep instanceof ExternalModuleDependency externalModuleDependency ) {
172- project .getDependencies ().constraints (constraints -> {
173- constraints .add (parent .getName (), externalModuleDependency .getGroup () + ":" + externalModuleDependency .getName () + ":" + externalModuleDependency .getVersion (), c -> {
174- c .attributes (a -> a .attribute (MinecraftMappings .ATTRIBUTE , MinecraftMappings .SRG ));
175- });
176- });
177176 externalModuleDependency .setTransitive (false );
177+
178+ // This rule ensures that this external module will be enriched with the attribute MAPPINGS=SRG
179+ project .getDependencies ().getComponents ().withModule (
180+ dep .getGroup () + ":" + dep .getName (), SrgMappingsRule .class , cfg -> {
181+ cfg .params (srgMappings );
182+ });
178183 } else if (dep instanceof FileCollectionDependency fileCollectionDependency ) {
179184 project .getDependencies ().constraints (constraints -> {
180185 constraints .add (parent .getName (), fileCollectionDependency .getFiles (), c -> {
181- c .attributes (a -> a .attribute (MinecraftMappings .ATTRIBUTE , MinecraftMappings . SRG ));
186+ c .attributes (a -> a .attribute (MinecraftMappings .ATTRIBUTE , namedMappings ));
182187 });
183188 });
184189 } else if (dep instanceof ProjectDependency projectDependency ) {
185190 project .getDependencies ().constraints (constraints -> {
186191 constraints .add (parent .getName (), projectDependency .getDependencyProject (), c -> {
187- c .attributes (a -> a .attribute (MinecraftMappings .ATTRIBUTE , MinecraftMappings . SRG ));
192+ c .attributes (a -> a .attribute (MinecraftMappings .ATTRIBUTE , namedMappings ));
188193 });
189194 });
190195 projectDependency .setTransitive (false );
191196 }
192197 }));
193198 });
194- parent .extendsFrom (remappingConfig );
199+
200+ var remappedDep = project .getDependencyFactory ().create (
201+ remappingConfig .getIncoming ().artifactView (view -> {
202+ view .attributes (a -> a .attribute (MinecraftMappings .ATTRIBUTE , namedMappings ));
203+ }).getFiles ());
204+ remappedDep .because ("Remapped mods from " + remappingConfig .getName ());
205+
206+ parent .getDependencies ().add (
207+ remappedDep );
208+
195209 return remappingConfig ;
196210 }
197211}
0 commit comments