2929/// needing to duplicate code across projects.
3030///
3131/// @param <T> The type of target
32- public abstract class EnhancedPlugin <T > implements Plugin <T >, EnhancedPluginAdditions {
32+ public non-sealed abstract class EnhancedPlugin <T > implements Plugin <T >, EnhancedPluginAdditions {
3333 private final String name ;
3434 private final String displayName ;
3535 private final @ Nullable String toolsExtName ;
@@ -87,8 +87,9 @@ protected EnhancedPlugin(String name, String displayName) {
8787 /// plugin's [global][#globalCaches()] and [local][#localCaches()] caches. Additionally, the name is used to
8888 /// create the cache folders (`minecraftforge/name`).
8989 ///
90- /// @param name The name for this plugin (must be machine-friendly)
91- /// @param displayName The display name for this plugin
90+ /// @param name The name for this plugin (must be machine-friendly)
91+ /// @param displayName The display name for this plugin
92+ /// @param toolsExtName The name for the tools extension to used, or `null` if it should not be created
9293 protected EnhancedPlugin (String name , String displayName , @ Nullable String toolsExtName ) {
9394 this .name = name ;
9495 this .displayName = displayName ;
@@ -104,8 +105,8 @@ protected EnhancedPlugin(String name, String displayName, @Nullable String tools
104105 public final void apply (T target ) {
105106 this .setup (this .target = target );
106107
107- if (this .toolsExtName != null && target instanceof ExtensionAware )
108- this .tools = (( ExtensionAware ) target ) .getExtensions ().create (this .toolsExtName , ToolsExtensionImpl .class , (Callable <? extends JavaToolchainService >) this ::toolchainsForTools );
108+ if (this .toolsExtName != null && target instanceof ExtensionAware extensionAware )
109+ this .tools = extensionAware .getExtensions ().create (this .toolsExtName , ToolsExtensionImpl .class , (Callable <? extends JavaToolchainService >) this ::toolchainsForTools );
109110// else
110111// this.tools = this.getObjects().newInstance(ToolsExtensionImpl.class, (Callable<? extends JavaToolchainService>) this::toolchainsForTools);
111112 }
@@ -164,8 +165,8 @@ public final DirectoryProperty globalCaches() {
164165
165166 private DirectoryProperty makeGlobalCaches () {
166167 try {
167- Gradle gradle = ((Gradle ) InvokerHelper .getProperty (this .target , "gradle" ));
168- DirectoryProperty gradleUserHomeDir = this .getObjects ().directoryProperty ().fileValue (gradle .getGradleUserHomeDir ());
168+ var gradle = ((Gradle ) InvokerHelper .getProperty (this .target , "gradle" ));
169+ var gradleUserHomeDir = this .getObjects ().directoryProperty ().fileValue (gradle .getGradleUserHomeDir ());
169170
170171 return this .getObjects ().directoryProperty ().convention (
171172 gradleUserHomeDir .dir ("caches/minecraftforge/" + this .name ).map (this .problemsInternal .ensureFileLocation ())
@@ -207,6 +208,32 @@ private DirectoryProperty makeLocalCaches() {
207208 }
208209 }
209210
211+ private final Lazy <DirectoryProperty > rootProjectDirectory = Lazy .simple (this ::makeRootProjectDirectory );
212+
213+ @ Override
214+ public final DirectoryProperty rootProjectDirectory () {
215+ return this .rootProjectDirectory .get ();
216+ }
217+
218+ private DirectoryProperty makeRootProjectDirectory () {
219+ var target = this .getTarget ();
220+ try {
221+ var rootProjectDirectory = this .getObjects ().directoryProperty ();
222+ if (target instanceof Project project ) {
223+ return rootProjectDirectory .value (project .getRootProject ().getLayout ().getProjectDirectory ());
224+ } else if (target instanceof Settings ) {
225+ return rootProjectDirectory .value (this .getBuildLayout ().getRootDirectory ());
226+ } else {
227+ throw new IllegalStateException ("Cannot get root project directory with an unsupported type (must be project or settings)" );
228+ }
229+ } catch (Exception e ) {
230+ throw this .problemsInternal .illegalPluginTarget (
231+ new IllegalArgumentException ("Failed to get %s root project directory for target: %s" .formatted (this .displayName , target ), e ),
232+ Project .class , Settings .class
233+ );
234+ }
235+ }
236+
210237 private final Lazy <DirectoryProperty > workingProjectDirectory = Lazy .simple (this ::makeWorkingProjectDirectory );
211238
212239 @ Override
@@ -215,18 +242,19 @@ public final DirectoryProperty workingProjectDirectory() {
215242 }
216243
217244 private DirectoryProperty makeWorkingProjectDirectory () {
245+ var target = this .getTarget ();
218246 try {
219- DirectoryProperty workingProjectDirectory = this .getObjects ().directoryProperty ();
220- if (this . target instanceof Project ) {
247+ var workingProjectDirectory = this .getObjects ().directoryProperty ();
248+ if (target instanceof Project ) {
221249 return workingProjectDirectory .value (this .getProjectLayout ().getProjectDirectory ());
222- } else if (this . target instanceof Settings ) {
250+ } else if (target instanceof Settings ) {
223251 return workingProjectDirectory .value (this .getBuildLayout ().getRootDirectory ());
224252 } else {
225253 throw new IllegalStateException ("Cannot get working project directory with an unsupported type (must be project or settings)" );
226254 }
227255 } catch (Exception e ) {
228256 throw this .problemsInternal .illegalPluginTarget (
229- new IllegalArgumentException (String . format ( "Failed to get %s working project directory for target: %s" , this .displayName , this . getTarget () ), e ),
257+ new IllegalArgumentException ("Failed to get %s working project directory for target: %s" . formatted ( this .displayName , target ), e ),
230258 Project .class , Settings .class
231259 );
232260 }
0 commit comments