From 22e111bf12bf0d78e6f62bd048dc1ef27665f211 Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Tue, 7 Jan 2025 03:32:59 +0100 Subject: [PATCH 1/2] Add a getNeoFormVersion getter to make Kotlin happy. --- .../moddevgradle/dsl/ModDevExtension.java | 2 +- .../moddevgradle/dsl/NeoForgeExtension.java | 15 +++++++++++++++ .../moddevgradle/internal/ModDevPluginTest.java | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/neoforged/moddevgradle/dsl/ModDevExtension.java b/src/main/java/net/neoforged/moddevgradle/dsl/ModDevExtension.java index d6fa353..021719a 100644 --- a/src/main/java/net/neoforged/moddevgradle/dsl/ModDevExtension.java +++ b/src/main/java/net/neoforged/moddevgradle/dsl/ModDevExtension.java @@ -159,7 +159,7 @@ public void addModdingDependenciesTo(SourceSet 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. + * I.e. the NeoForge or Forge version. If you chose to enable vanilla-only mode, this getter will throw. */ public String getVersion() { var dependencies = ModDevArtifactsWorkflow.get(project).dependencies(); diff --git a/src/main/java/net/neoforged/moddevgradle/dsl/NeoForgeExtension.java b/src/main/java/net/neoforged/moddevgradle/dsl/NeoForgeExtension.java index 5684ad1..c090517 100644 --- a/src/main/java/net/neoforged/moddevgradle/dsl/NeoForgeExtension.java +++ b/src/main/java/net/neoforged/moddevgradle/dsl/NeoForgeExtension.java @@ -1,8 +1,10 @@ package net.neoforged.moddevgradle.dsl; import javax.inject.Inject; +import net.neoforged.moddevgradle.internal.ModDevArtifactsWorkflow; import net.neoforged.moddevgradle.internal.ModDevPlugin; import org.gradle.api.Action; +import org.gradle.api.InvalidUserCodeException; import org.gradle.api.Project; /** @@ -50,6 +52,19 @@ public void setNeoFormVersion(String version) { }); } + /** + * After enabling modding, you can retrieve the version of NeoForm you picked using this getter. + * This is only meaningful if you have enabled vanilla-only mode or if you have overridden NeoForm for NeoForge. + * This getter throws if no NeoForm version was set. + */ + public String getNeoFormVersion() { + var dependencies = ModDevArtifactsWorkflow.get(project).dependencies(); + if (dependencies.neoFormDependency() == null) { + throw new InvalidUserCodeException("You cannot retrieve the NeoForm version without setting it first."); + } + return dependencies.neoFormDependency().getVersion(); + } + public void enable(Action customizer) { var modDevPlugin = project.getPlugins().getPlugin(ModDevPlugin.class); diff --git a/src/test/java/net/neoforged/moddevgradle/internal/ModDevPluginTest.java b/src/test/java/net/neoforged/moddevgradle/internal/ModDevPluginTest.java index b42067b..e372c45 100644 --- a/src/test/java/net/neoforged/moddevgradle/internal/ModDevPluginTest.java +++ b/src/test/java/net/neoforged/moddevgradle/internal/ModDevPluginTest.java @@ -119,7 +119,7 @@ void testEnable() { @Test void testGetVersion() { extension.setNeoFormVersion(VERSION); - assertEquals(VersionCapabilitiesInternal.ofNeoFormVersion(VERSION), extension.getVersionCapabilities()); + assertEquals(VERSION, extension.getNeoFormVersion()); } @Test From 6709540a430e5e89b369c880cc3ff40f864ee95c Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Tue, 7 Jan 2025 20:51:07 +0100 Subject: [PATCH 2/2] Add getMcpVersion and test --- .../legacyforge/dsl/LegacyForgeExtension.java | 14 ++++++++++++++ .../legacyforge/LegacyModDevPluginTest.java | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/src/legacy/java/net/neoforged/moddevgradle/legacyforge/dsl/LegacyForgeExtension.java b/src/legacy/java/net/neoforged/moddevgradle/legacyforge/dsl/LegacyForgeExtension.java index 07ba603..bd207a2 100644 --- a/src/legacy/java/net/neoforged/moddevgradle/legacyforge/dsl/LegacyForgeExtension.java +++ b/src/legacy/java/net/neoforged/moddevgradle/legacyforge/dsl/LegacyForgeExtension.java @@ -3,8 +3,10 @@ import javax.inject.Inject; import net.neoforged.moddevgradle.dsl.DataFileCollection; import net.neoforged.moddevgradle.dsl.ModDevExtension; +import net.neoforged.moddevgradle.internal.ModDevArtifactsWorkflow; import net.neoforged.moddevgradle.legacyforge.internal.LegacyForgeModDevPlugin; import org.gradle.api.Action; +import org.gradle.api.InvalidUserCodeException; import org.gradle.api.Project; /** @@ -49,6 +51,18 @@ public void setMcpVersion(String version) { }); } + /** + * After enabling modding, you can retrieve the version of MCP you picked using this getter. + * This getter will throw if you have not enabled vanilla-mode yet. + */ + public String getMcpVersion() { + var dependencies = ModDevArtifactsWorkflow.get(project).dependencies(); + if (dependencies.neoFormDependency() == null) { + throw new InvalidUserCodeException("You cannot retrieve the MCP version without setting it first."); + } + return dependencies.neoFormDependency().getVersion(); + } + public void enable(Action customizer) { var plugin = project.getPlugins().getPlugin(LegacyForgeModDevPlugin.class); diff --git a/src/test/java/net/neoforged/moddevgradle/legacyforge/LegacyModDevPluginTest.java b/src/test/java/net/neoforged/moddevgradle/legacyforge/LegacyModDevPluginTest.java index fb439ed..3679d1f 100644 --- a/src/test/java/net/neoforged/moddevgradle/legacyforge/LegacyModDevPluginTest.java +++ b/src/test/java/net/neoforged/moddevgradle/legacyforge/LegacyModDevPluginTest.java @@ -1,6 +1,7 @@ package net.neoforged.moddevgradle.legacyforge; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.Set; @@ -57,6 +58,12 @@ void testEnableVanillaOnlyMode() { "build/moddev/artifacts/vanilla-1.17.1-client-extra-aka-minecraft-resources.jar", "de.oceanlabs.mcp:mcp_config:1.17.1[net.neoforged:neoform-dependencies]", "build/moddev/artifacts/intermediateToNamed.zip"); + assertEquals("1.17.1", extension.getMcpVersion()); + } + + @Test + void testGetMcpVersionThrowsBeforeEnabling() { + assertThrows(InvalidUserCodeException.class, extension::getMcpVersion); } @Test