Skip to content

Commit

Permalink
Expose version, minecraft version and version capabilities on the ext…
Browse files Browse the repository at this point in the history
…ension (#210)
  • Loading branch information
shartte authored Jan 1, 2025
1 parent 5640aa0 commit 383ac6a
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import net.neoforged.moddevgradle.internal.RepositoriesPlugin;
import net.neoforged.moddevgradle.internal.WorkflowArtifact;
import net.neoforged.moddevgradle.internal.utils.ExtensionUtils;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilities;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilitiesInternal;
import net.neoforged.moddevgradle.legacyforge.dsl.LegacyForgeExtension;
import net.neoforged.moddevgradle.legacyforge.dsl.LegacyForgeModdingSettings;
import net.neoforged.moddevgradle.legacyforge.dsl.MixinExtension;
Expand Down Expand Up @@ -118,7 +118,7 @@ public void enable(Project project, LegacyForgeModdingSettings settings, LegacyF

ModdingDependencies dependencies;
ArtifactNamingStrategy artifactNamingStrategy;
VersionCapabilities versionCapabilities;
VersionCapabilitiesInternal versionCapabilities;
if (forgeVersion != null || neoForgeVersion != null) {
// All settings are mutually exclusive
if (forgeVersion != null && neoForgeVersion != null || mcpVersion != null) {
Expand All @@ -136,15 +136,15 @@ public void enable(Project project, LegacyForgeModdingSettings settings, LegacyF
}
};

versionCapabilities = VersionCapabilities.ofForgeVersion(forgeVersion);
versionCapabilities = VersionCapabilitiesInternal.ofForgeVersion(forgeVersion);

String groupId = forgeVersion != null ? "net.minecraftforge" : "net.neoforged";
var neoForge = depFactory.create(groupId + ":forge:" + forgeVersion);
var neoForgeNotation = groupId + ":forge:" + forgeVersion + ":userdev";
dependencies = ModdingDependencies.create(neoForge, neoForgeNotation, null, null, versionCapabilities);
} else if (mcpVersion != null) {
artifactNamingStrategy = ArtifactNamingStrategy.createDefault("vanilla-" + mcpVersion);
versionCapabilities = VersionCapabilities.ofMinecraftVersion(mcpVersion);
versionCapabilities = VersionCapabilitiesInternal.ofMinecraftVersion(mcpVersion);

var neoForm = depFactory.create("de.oceanlabs.mcp:mcp_config:" + mcpVersion);
var neoFormNotation = "de.oceanlabs.mcp:mcp_config:" + mcpVersion + "@zip";
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/net/neoforged/moddevgradle/dsl/ModDevExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import net.neoforged.moddevgradle.internal.Branding;
import net.neoforged.moddevgradle.internal.IdeIntegration;
import net.neoforged.moddevgradle.internal.ModDevArtifactsWorkflow;
import net.neoforged.moddevgradle.internal.utils.ExtensionUtils;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.InvalidUserCodeException;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Project;
import org.gradle.api.Task;
Expand Down Expand Up @@ -158,4 +157,30 @@ public void ideSyncTask(Task task) {
public void addModdingDependenciesTo(SourceSet sourceSet) {
ModDevArtifactsWorkflow.get(project).addToSourceSet(sourceSet);
}

/**
* After enabling modding, you can retrieve the version of the modding platform you picked using this getter.
* I.e. the NeoForge or Forge version. If you chose to enable vanilla-only mode, this getter returns null.
*/
public String getVersion() {
var dependencies = ModDevArtifactsWorkflow.get(project).dependencies();
if (dependencies.neoForgeDependency() == null) {
throw new InvalidUserCodeException("You cannot retrieve the enabled version if you are in vanilla-only mode.");
}
return dependencies.neoForgeDependency().getVersion();
}

/**
* After enabling modding, you can retrieve the effective Minecraft version using this getter.
*/
public String getMinecraftVersion() {
return ModDevArtifactsWorkflow.get(project).versionCapabilities().minecraftVersion();
}

/**
* After enabling modding, you can retrieve the capabilities of the version you picked using this getter.
*/
public VersionCapabilities getVersionCapabilities() {
return ModDevArtifactsWorkflow.get(project).versionCapabilities();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.neoforged.moddevgradle.dsl;

/**
* Describes the capabilities of the currently chosen version of Minecraft.
*/
public interface VersionCapabilities {
/**
* The Java version used by this version of Minecraft.
*/
int javaVersion();

/**
* Whether this version of Minecraft uses separate data-generation runs for client and server data.
*/
boolean splitDataRuns();

/**
* Whether the NeoForge version for this version of Minecraft supports mod-loading in unit tests.
*/
boolean testFixtures();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.neoforged.minecraftdependencies.MinecraftDistribution;
import net.neoforged.moddevgradle.dsl.ModDevExtension;
import net.neoforged.moddevgradle.internal.utils.ExtensionUtils;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilities;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilitiesInternal;
import net.neoforged.nfrtgradle.CreateMinecraftArtifacts;
import net.neoforged.nfrtgradle.DownloadAssets;
import org.gradle.api.GradleException;
Expand Down Expand Up @@ -42,21 +42,20 @@
public record ModDevArtifactsWorkflow(
Project project,
ModdingDependencies dependencies,
VersionCapabilities versionCapabilities,
VersionCapabilitiesInternal versionCapabilities,
TaskProvider<CreateMinecraftArtifacts> createArtifacts,
TaskProvider<DownloadAssets> downloadAssets,
Configuration runtimeDependencies,
Configuration compileDependencies,
Provider<Directory> modDevBuildDir,
Provider<Directory> artifactsBuildDir
) {

private static final String EXTENSION_NAME = "__internal_modDevArtifactsWorkflow";

public static ModDevArtifactsWorkflow get(Project project) {
var result = ExtensionUtils.findExtension(project, EXTENSION_NAME, ModDevArtifactsWorkflow.class);
if (result == null) {
throw new IllegalStateException("Mod development has not been enabled yet for project " + project);
throw new InvalidUserCodeException("Mod development has not been enabled yet for project " + project);
}
return result;
}
Expand All @@ -69,7 +68,7 @@ public static ModDevArtifactsWorkflow create(Project project,
ArtifactNamingStrategy artifactNamingStrategy,
Configuration accessTransformers,
Configuration interfaceInjectionData,
VersionCapabilities versionCapabilities
VersionCapabilitiesInternal versionCapabilities
) {
if (project.getExtensions().findByName(EXTENSION_NAME) != null) {
throw new InvalidUserCodeException("You cannot enable modding in the same project twice.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.neoforged.moddevgradle.dsl.ModdingVersionSettings;
import net.neoforged.moddevgradle.dsl.NeoForgeExtension;
import net.neoforged.moddevgradle.internal.jarjar.JarJarPlugin;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilities;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilitiesInternal;
import net.neoforged.nfrtgradle.NeoFormRuntimePlugin;
import org.gradle.api.InvalidUserCodeException;
import org.gradle.api.Plugin;
Expand Down Expand Up @@ -83,8 +83,8 @@ public void enable(

var configurations = project.getConfigurations();

var versionCapabilities = neoForgeVersion != null ? VersionCapabilities.ofNeoForgeVersion(neoForgeVersion)
: VersionCapabilities.ofNeoFormVersion(neoFormVersion);
var versionCapabilities = neoForgeVersion != null ? VersionCapabilitiesInternal.ofNeoForgeVersion(neoForgeVersion)
: VersionCapabilitiesInternal.ofNeoFormVersion(neoFormVersion);

var dependencies = neoForge != null ? ModdingDependencies.create(neoForge, neoForgeNotation, neoForm, neoFormNotation, versionCapabilities)
: ModdingDependencies.createVanillaOnly(neoForm, neoFormNotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.neoforged.moddevgradle.dsl.ModModel;
import net.neoforged.moddevgradle.dsl.RunModel;
import net.neoforged.moddevgradle.internal.utils.ExtensionUtils;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilities;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilitiesInternal;
import net.neoforged.nfrtgradle.CreateMinecraftArtifacts;
import net.neoforged.nfrtgradle.DownloadAssets;
import org.gradle.api.DomainObjectCollection;
Expand Down Expand Up @@ -77,7 +77,7 @@ private ModDevRunWorkflow(Project project,
@Nullable ModuleDependency testFixturesDependency,
ModuleDependency gameLibrariesDependency,
DomainObjectCollection<RunModel> runs,
VersionCapabilities versionCapabilities) {
VersionCapabilitiesInternal versionCapabilities) {
this.project = project;
this.branding = branding;
this.modulePathDependency = modulePathDependency;
Expand Down Expand Up @@ -220,7 +220,7 @@ public static void setupRuns(
Consumer<Configuration> configureModulePath,
Consumer<Configuration> configureLegacyClasspath,
Provider<RegularFile> assetPropertiesFile,
VersionCapabilities versionCapabilities
VersionCapabilitiesInternal versionCapabilities
) {
var dependencyFactory = project.getDependencyFactory();
var ideIntegration = IdeIntegration.of(project, branding);
Expand Down Expand Up @@ -276,7 +276,7 @@ private static TaskProvider<PrepareRun> setupRunInGradle(
Consumer<Configuration> configureLegacyClasspath, // TODO: can be removed in favor of directly passing a configuration for the moddev libraries
Provider<RegularFile> assetPropertiesFile,
Configuration devLaunchConfig,
VersionCapabilities versionCapabilities,
VersionCapabilitiesInternal versionCapabilities,
TaskProvider<Task> createLaunchScriptsTask) {
var ideIntegration = IdeIntegration.of(project, branding);
var configurations = project.getConfigurations();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.neoforged.moddevgradle.internal;

import net.neoforged.moddevgradle.internal.utils.VersionCapabilities;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilitiesInternal;
import org.gradle.api.artifacts.ModuleDependency;
import org.jetbrains.annotations.Nullable;

Expand All @@ -21,7 +21,7 @@ public static ModdingDependencies create(ModuleDependency neoForge,
String neoForgeNotation,
@Nullable ModuleDependency neoForm,
@Nullable String neoFormNotation,
VersionCapabilities versionCapabilities) {
VersionCapabilitiesInternal versionCapabilities) {
var runTypesDataDependency = neoForge.copy()
.capabilities(caps -> caps.requireCapability("net.neoforged:neoforge-moddev-config"));
var modulePathDependency = neoForge.copy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.neoforged.moddevgradle.dsl.ModModel;
import net.neoforged.moddevgradle.dsl.RunModel;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilities;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilitiesInternal;
import org.gradle.api.DomainObjectCollection;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
Expand Down Expand Up @@ -44,7 +44,7 @@ public static void setupRuns(Project project,
configureAdditionalClasspath,
assetPropertiesFile,
// This overload of the method was only used by NeoForge 1.21.3
VersionCapabilities.ofMinecraftVersion("1.21.3")
VersionCapabilitiesInternal.ofMinecraftVersion("1.21.3")
);
}

Expand All @@ -66,7 +66,7 @@ public static void setupRuns(Project project,
configureModulePath,
configureAdditionalClasspath,
assetPropertiesFile,
neoFormVersion.map(VersionCapabilities::ofNeoFormVersion).getOrElse(VersionCapabilities.latest())
neoFormVersion.map(VersionCapabilitiesInternal::ofNeoFormVersion).getOrElse(VersionCapabilitiesInternal.latest())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.neoforged.moddevgradle.internal.utils.FileUtils;
import net.neoforged.moddevgradle.internal.utils.OperatingSystem;
import net.neoforged.moddevgradle.internal.utils.StringUtils;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilities;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilitiesInternal;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.file.ConfigurableFileCollection;
Expand Down Expand Up @@ -103,7 +103,7 @@ abstract class PrepareRunOrTest extends DefaultTask {
*/
@Input
@Optional
public abstract Property<VersionCapabilities> getVersionCapabilities();
public abstract Property<VersionCapabilitiesInternal> getVersionCapabilities();

/**
* The property that decides whether DevLogin is enabled.
Expand All @@ -115,7 +115,7 @@ abstract class PrepareRunOrTest extends DefaultTask {

protected PrepareRunOrTest(ProgramArgsFormat programArgsFormat) {
this.programArgsFormat = programArgsFormat;
getVersionCapabilities().convention(VersionCapabilities.latest());
getVersionCapabilities().convention(VersionCapabilitiesInternal.latest());
getDevLogin().convention(false);
}

Expand Down Expand Up @@ -211,7 +211,7 @@ private UserDevConfig getSimulatedUserDevConfigForVanilla() {
true, "net.minecraft.server.Main", commonArgs, List.of(), Map.of(), Map.of()
));

if (getVersionCapabilities().getOrElse(VersionCapabilities.latest()).splitDataRuns()) {
if (getVersionCapabilities().getOrElse(VersionCapabilitiesInternal.latest()).splitDataRuns()) {
runTypes.put("clientData", new UserDevRunType(
true, "net.minecraft.client.data.Main", commonArgs, List.of(), Map.of(), Map.of()
));
Expand Down
Loading

0 comments on commit 383ac6a

Please sign in to comment.