Skip to content

Commit

Permalink
Use the appropriate Java version for the chosen Minecraft version (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte authored Dec 12, 2024
1 parent 600437d commit 511f517
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ public void adaptWithConfig(ComponentMetadataContext context, JsonObject config)
});
details.addVariant("modDevModulePath", variantMetadata -> {
variantMetadata.withDependencies(dependencies -> {
var modules = config.getAsJsonArray("modules");
for (JsonElement module : modules) {
dependencies.add(module.getAsString());
// Support versions that do not declare modules
if (config.has("modules")) {
var modules = config.getAsJsonArray("modules");
for (JsonElement module : modules) {
dependencies.add(module.getAsString());
}
}
});
variantMetadata.withCapabilities(capabilities -> {
Expand Down Expand Up @@ -96,9 +99,12 @@ public void adaptWithConfig(ComponentMetadataContext context, JsonObject config)
variantMetadata.withDependencies(vanillaDependencies);
variantMetadata.withFiles(MutableVariantFilesMetadata::removeAllFiles);
variantMetadata.withDependencies(dependencies -> {
var modules = config.getAsJsonArray("modules");
for (JsonElement module : modules) {
dependencies.add(module.getAsString());
// Support versions that do not declare modules
if (config.has("modules")) {
var modules = config.getAsJsonArray("modules");
for (JsonElement module : modules) {
dependencies.add(module.getAsString());
}
}
var libraries = config.getAsJsonArray("libraries");
for (JsonElement library : libraries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import net.neoforged.moddevgradle.dsl.NeoForgeExtension;
import net.neoforged.moddevgradle.internal.LegacyForgeFacade;
import net.neoforged.moddevgradle.internal.ModDevPlugin;
import net.neoforged.moddevgradle.internal.utils.ExtensionUtils;
import net.neoforged.moddevgradle.internal.utils.VersionCapabilities;
import net.neoforged.moddevgradle.legacyforge.dsl.MixinExtension;
import net.neoforged.moddevgradle.legacyforge.dsl.Obfuscation;
import net.neoforged.nfrtgradle.CreateMinecraftArtifacts;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.type.ArtifactTypeDefinition;
Expand All @@ -14,6 +17,9 @@
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.jvm.tasks.Jar;
import org.gradle.jvm.toolchain.JavaLanguageVersion;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.jvm.toolchain.JavaToolchainService;
import org.jetbrains.annotations.ApiStatus;

import java.net.URI;
Expand Down Expand Up @@ -133,6 +139,24 @@ public void apply(Project project) {
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE);
});

// Set the right Java version
project.getTasks().withType(CreateMinecraftArtifacts.class).configureEach(task -> {
var extension = ExtensionUtils.getExtension(project, NeoForgeExtension.NAME, NeoForgeExtension.class);
var toolchainService = ExtensionUtils.getExtension(project, "javaToolchains", JavaToolchainService.class);
task.getToolsJavaExecutable().set(
toolchainService.launcherFor(spec -> {
spec.getLanguageVersion().set(
extension.getVersion().map(VersionCapabilities::ofForgeVersion)
.orElse(extension.getNeoFormVersion().map(VersionCapabilities::ofNeoFormVersion))
.map(VersionCapabilities::javaVersion)
.map(JavaLanguageVersion::of)
);
})
.map(JavaLauncher::getExecutablePath)
.map(f -> f.getAsFile().getAbsolutePath())
);
});

obf.createRemappingConfiguration(project.getConfigurations().getByName(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME));
obf.createRemappingConfiguration(project.getConfigurations().getByName(JavaPlugin.RUNTIME_ONLY_CONFIGURATION_NAME));
obf.createRemappingConfiguration(project.getConfigurations().getByName(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ protected void adaptWithConfig(ComponentMetadataContext context, JsonObject conf

var zipDataName = id.getName() + "-" + id.getVersion() + ".zip";

var javaTarget = config.getAsJsonPrimitive("java_target").getAsInt();
// Very old versions did not specify this. Default to 8 in those cases.
var javaTarget = config.has("java_target")
? config.getAsJsonPrimitive("java_target").getAsInt()
: 8;

// a.k.a. "neoformData"
// Primarily pulled to use for NFRT manifest
Expand All @@ -61,7 +64,8 @@ protected void adaptWithConfig(ComponentMetadataContext context, JsonObject conf
});
});

dependencies(context, "mcpRuntimeElements", javaTarget, Usage.JAVA_RUNTIME, deps -> {});
dependencies(context, "mcpRuntimeElements", javaTarget, Usage.JAVA_RUNTIME, deps -> {
});

dependencies(context, "mcpApiElements", javaTarget, Usage.JAVA_API, dependencies -> {
var libraries = config.getAsJsonObject("libraries").getAsJsonArray("joined");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public void apply(Project project) {
ideIntegration.runTaskOnProjectSync(extension.getIdeSyncTasks());
var dependencyFactory = project.getDependencyFactory();

Provider<VersionCapabilities> versionCapabilities = extension.getVersion().map(VersionCapabilities::ofForgeVersion)
.orElse(extension.getNeoFormVersion().map(VersionCapabilities::ofNeoFormVersion))
.orElse(VersionCapabilities.latest());

// When a NeoForge version is specified, we use the dependencies published by that, and otherwise
// we fall back to a potentially specified NeoForm version, which allows us to run in "Vanilla" mode.
var neoForgeModDevLibrariesDependency = extension.getNeoForgeArtifact().map(artifactId -> {
Expand Down Expand Up @@ -275,7 +279,7 @@ public void apply(Project project) {
project.afterEvaluate(ignored -> {
var toolchainSpec = javaExtension.getToolchain();
try {
toolchainSpec.getLanguageVersion().convention(JavaLanguageVersion.of(21));
toolchainSpec.getLanguageVersion().convention(versionCapabilities.map(VersionCapabilities::javaVersion).map(JavaLanguageVersion::of));
} catch (IllegalStateException e) {
// We tried our best
}
Expand Down Expand Up @@ -325,7 +329,7 @@ public void apply(Project project) {
modulePath -> modulePath.getDependencies().addLater(modulePathDependency),
legacyClassPath -> legacyClassPath.extendsFrom(additionalClasspath),
downloadAssets.flatMap(DownloadAssets::getAssetPropertiesFile),
extension.getNeoFormVersion().map(VersionCapabilities::ofNeoFormVersion)
versionCapabilities
);

setupJarJar(project);
Expand Down Expand Up @@ -506,7 +510,7 @@ static void setupRuns(Project project,
});

// Create an empty task similar to "assemble" which can be used to generate all launch scripts at once
var createLaunchScriptsTask= project.getTasks().register("createLaunchScripts", Task.class, task -> {
var createLaunchScriptsTask = project.getTasks().register("createLaunchScripts", Task.class, task -> {
task.setGroup(branding.publicTaskGroup());
task.setDescription("Creates batch files/shell scripts to launch the game from outside of Gradle (i.e. Renderdoc, NVidia Nsight, etc.)");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public CreateMinecraftArtifacts() {
getParchmentEnabled().convention(false);
}

/**
* Path to the Java installation to use for running external tools with. This does not have to match
* NFRTs Java version. I.e. if running NFRT for MC 1.12, this would need to be Java 8, since the decompiler
* and other tools in that version were not updated to run with Java 21.
*/
@Input
@Optional
public abstract Property<String> getToolsJavaExecutable();

/**
* Files added to this collection will be passed to NFRT via the {@code --access-transformer}
* command line option.
Expand Down Expand Up @@ -169,6 +178,11 @@ public void createArtifacts() {
var args = new ArrayList<String>();
args.add("run");

if (getToolsJavaExecutable().isPresent()) {
args.add("--java-executable");
args.add(getToolsJavaExecutable().get());
}

for (var accessTransformer : getAccessTransformers().getFiles()) {
args.add("--access-transformer");
args.add(accessTransformer.getAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public abstract class NeoFormRuntimeExtension {
public static final String NAME = "neoFormRuntime";

private static final String DEFAULT_NFRT_VERSION = "1.0.12";
private static final String DEFAULT_NFRT_VERSION = "1.0.13";

@Inject
public NeoFormRuntimeExtension(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public NeoFormRuntimeTask() {
// Store temporary working directories in this projects build directory such that gradle clean removes them
getWorkDirectory().convention(project.getLayout().getBuildDirectory().dir("tmp/neoformruntime"));

// Default to J21 for NFRT
// Run NFRT itself with Java 21
getJavaExecutable().convention(getJavaToolchainService()
.launcherFor(spec -> spec.getLanguageVersion().set(JavaLanguageVersion.of(21)))
.map(javaLauncher -> javaLauncher.getExecutablePath().getAsFile().getAbsolutePath())
Expand Down

0 comments on commit 511f517

Please sign in to comment.