Skip to content

Commit 7e3c01c

Browse files
committed
Configure imperatively.
1 parent 197c94a commit 7e3c01c

File tree

9 files changed

+267
-169
lines changed

9 files changed

+267
-169
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.neoforged.moddevgradle.dsl;
2+
3+
import groovyjarjarantlr4.v4.runtime.misc.Nullable;
4+
5+
public record ImmutableModdingVersions(@Nullable String neoForgeVersion, @Nullable String neoFormVersion) {
6+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.neoforged.moddevgradle.dsl;
2+
3+
import org.gradle.api.provider.ListProperty;
4+
import org.gradle.api.tasks.SourceSet;
5+
import org.jetbrains.annotations.ApiStatus;
6+
import org.jetbrains.annotations.Nullable;
7+
8+
public abstract class ModdingVersionSettings {
9+
@Nullable
10+
private String neoForgeVersion;
11+
12+
@Nullable
13+
private String neoFormVersion;
14+
15+
/**
16+
* NeoForge version number. You have to set either this or {@link #setNeoFormVersion}.
17+
*/
18+
public void setNeoForgeVersion(String version) {
19+
this.neoForgeVersion = version;
20+
}
21+
22+
/**
23+
* You can set this property to a version of <a href="https://projects.neoforged.net/neoforged/neoform">NeoForm</a>
24+
* to either override the version used in the version of NeoForge you set, or to compile against
25+
* Vanilla artifacts that have no NeoForge code added.
26+
*/
27+
public void setNeoFormVersion(String version) {
28+
this.neoFormVersion = version;
29+
}
30+
31+
/**
32+
* Contains the list of source sets for which access to Minecraft classes should be configured.
33+
* Defaults to the main source set, but can also be set to an empty list.
34+
*/
35+
public abstract ListProperty<SourceSet> getEnabledSourceSets();
36+
37+
@ApiStatus.Internal
38+
public ImmutableModdingVersions toImmutable() {
39+
return new ImmutableModdingVersions(neoForgeVersion, neoFormVersion);
40+
}
41+
}

src/main/java/net/neoforged/moddevgradle/dsl/NeoForgeExtension.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import net.neoforged.moddevgradle.internal.ModDevPlugin;
44
import net.neoforged.moddevgradle.internal.utils.ExtensionUtils;
55
import org.gradle.api.Action;
6-
import org.gradle.api.GradleException;
6+
import org.gradle.api.InvalidUserCodeException;
77
import org.gradle.api.NamedDomainObjectContainer;
88
import org.gradle.api.Project;
99
import org.gradle.api.Task;
@@ -15,6 +15,7 @@
1515

1616
import javax.inject.Inject;
1717
import java.io.File;
18+
import java.util.List;
1819

1920
/**
2021
* This is the top-level {@code neoForge} extension, used to configure the moddev plugin.
@@ -44,34 +45,31 @@ public NeoForgeExtension(Project project, DataFileCollection accessTransformers,
4445
unitTest.getLoadedMods().convention(getMods());
4546
}
4647

47-
/**
48-
* Adds the necessary dependencies to develop a Minecraft mod to the given source set.
49-
* The plugin automatically adds these dependencies to the main source set.
50-
*/
51-
public void addModdingDependenciesTo(SourceSet sourceSet) {
52-
var configurations = project.getConfigurations();
53-
var sourceSets = ExtensionUtils.getSourceSets(project);
54-
if (!sourceSets.contains(sourceSet)) {
55-
throw new GradleException("Cannot add to the source set in another project.");
56-
}
57-
58-
configurations.getByName(sourceSet.getRuntimeClasspathConfigurationName())
59-
.extendsFrom(configurations.getByName(ModDevPlugin.CONFIGURATION_RUNTIME_DEPENDENCIES));
60-
configurations.getByName(sourceSet.getCompileClasspathConfigurationName())
61-
.extendsFrom(configurations.getByName(ModDevPlugin.CONFIGURATION_COMPILE_DEPENDENCIES));
48+
@Deprecated(forRemoval = true)
49+
public void setVersion(Object any) {
50+
throw new InvalidUserCodeException("Please use enableModding { neoForgeVersion = ... } instead of the version property.");
6251
}
6352

64-
/**
65-
* NeoForge version number. You have to set either this or {@link #getNeoFormVersion()}.
66-
*/
67-
public abstract Property<String> getVersion();
53+
public void enableModding(Action<ModdingVersionSettings> customizer) {
54+
var modDevPlugin = project.getPlugins().getPlugin(ModDevPlugin.class);
6855

69-
/**
70-
* You can set this property to a version of <a href="https://projects.neoforged.net/neoforged/neoform">NeoForm</a>
71-
* to either override the version used in the version of NeoForge you set, or to compile against
72-
* Vanilla artifacts that have no NeoForge code added.
73-
*/
74-
public abstract Property<String> getNeoFormVersion();
56+
var settings = project.getObjects().newInstance(ModdingVersionSettings.class);
57+
// By default enable modding deps only for the main source set
58+
settings.getEnabledSourceSets().convention(project.provider(() -> {
59+
var sourceSets = ExtensionUtils.getSourceSets(project);
60+
return List.of(sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME));
61+
}));
62+
customizer.execute(settings);
63+
64+
var versions = settings.toImmutable();
65+
66+
modDevPlugin.enableModding(
67+
project,
68+
settings.getEnabledSourceSets().get(),
69+
versions.neoForgeVersion(),
70+
versions.neoFormVersion()
71+
);
72+
}
7573

7674
/**
7775
* The list of additional access transformers that should be applied to the Minecraft source code.

0 commit comments

Comments
 (0)