Skip to content

Commit 19ebb9f

Browse files
MatyrobbrtshartteTechnici4n
authored
Add DevLogin support (#206)
Co-authored-by: shartte <[email protected]> Co-authored-by: Technici4n <[email protected]>
1 parent 77ffcc8 commit 19ebb9f

File tree

7 files changed

+77
-10
lines changed

7 files changed

+77
-10
lines changed

.github/renovate.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
"DEFAULT_NFRT_VERSION\\s*=\\s*\"(?<currentValue>[\\d\\.\\-a-z]+)\""
1616
],
1717
"datasourceTemplate": "maven"
18+
},
19+
{
20+
"customType": "regex",
21+
"fileMatch": [
22+
"RunUtils\\.java$"
23+
],
24+
"matchStrings": [
25+
"String\\s+[A-Z_]+\\s*=\\s*\"(?<depName>[\\w:\\.-]+):(?<currentValue>[\\d\\.]+)(:(?<depClassifier>[a-z]+))?\"\\s*;\\s*\\/\\/\\s*renovate"
26+
],
27+
"datasourceTemplate": "maven"
1828
}
1929
]
2030
}

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,26 @@ dependencies {
582582
}
583583
```
584584

585+
### Using Authenticated Minecraft Accounts
586+
Minecraft runs normally use an offline user profile in a development environment.
587+
If you want to run the game with your real user profile, you may do so using [DevLogin](https://github.com/covers1624/DevLogin) by setting
588+
the `devLogin` property of a client run to `true`:
589+
590+
```groovy
591+
neoForge {
592+
runs {
593+
// Add a second client run that is authenticated
594+
clientAuth {
595+
client()
596+
devLogin = true
597+
}
598+
}
599+
}
600+
```
601+
602+
The first time you launch the authenticated run you will be asked in the console to visit https://www.microsoft.com/link and enter
603+
the given code. More information is available on the [DevLogin readme](https://github.com/covers1624/DevLogin)
604+
585605
## Advanced Tips & Tricks
586606

587607
### Overriding Platform Libraries

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public RunModel(String name, Project project, Iterable<ModModel> defaultMods) {
5656
});
5757

5858
getLogLevel().convention(Level.INFO);
59+
getDevLogin().convention(false);
5960

6061
// Build a nicer name for the IDE run configuration
6162
boolean isSubProject = project.getRootProject() != project;
@@ -251,6 +252,12 @@ public Configuration getAdditionalRuntimeClasspathConfiguration() {
251252
*/
252253
public abstract Property<SourceSet> getSourceSet();
253254

255+
/**
256+
* Enables <a href="https://github.com/covers1624/DevLogin">DevLogin</a> which is used to log into an
257+
* official Minecraft account in development environments.
258+
*/
259+
public abstract Property<Boolean> getDevLogin();
260+
254261
@Override
255262
public String toString() {
256263
return "Run[" + getName() + "]";

src/main/java/net/neoforged/moddevgradle/internal/ModDevPlugin.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import java.util.Map;
5757
import java.util.function.Consumer;
5858
import java.util.function.Function;
59+
import java.util.stream.Collectors;
5960

6061
/**
6162
* The main plugin class.
@@ -510,10 +511,13 @@ static void setupRuns(Project project,
510511
) {
511512
var ideIntegration = IdeIntegration.of(project, branding);
512513

513-
// Create a configuration to resolve DevLaunch without leaking it to consumers
514+
// Create a configuration to resolve DevLaunch and DevLogin without leaking them to consumers
515+
var supplyDevLogin = project.provider(() -> runs.stream().anyMatch(model -> model.getDevLogin().get()));
514516
var devLaunchConfig = project.getConfigurations().create("devLaunchConfig", spec -> {
515-
spec.setDescription("This configuration is used to inject DevLaunch into the runtime classpaths of runs.");
517+
spec.setDescription("This configuration is used to inject DevLaunch and optionally DevLogin into the runtime classpaths of runs.");
516518
spec.getDependencies().add(project.getDependencyFactory().create(RunUtils.DEV_LAUNCH_GAV));
519+
spec.getDependencies().addAllLater(supplyDevLogin.map(
520+
supply -> supply ? List.of(project.getDependencyFactory().create(RunUtils.DEV_LOGIN_GAV)) : List.of()));
517521
});
518522

519523
// Create an empty task similar to "assemble" which can be used to generate all launch scripts at once
@@ -628,6 +632,7 @@ private static TaskProvider<PrepareRun> setupRunInGradle(
628632
task.getProgramArguments().set(run.getProgramArguments());
629633
task.getJvmArguments().set(run.getJvmArguments());
630634
task.getGameLogLevel().set(run.getLogLevel());
635+
task.getDevLogin().set(run.getDevLogin());
631636
task.getVersionCapabilities().set(versionCapabilities);
632637
});
633638
ideIntegration.runTaskOnProjectSync(prepareRunTask);

src/main/java/net/neoforged/moddevgradle/internal/PrepareRunOrTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,18 @@ abstract class PrepareRunOrTest extends DefaultTask {
104104
@Optional
105105
public abstract Property<VersionCapabilities> getVersionCapabilities();
106106

107+
/**
108+
* The property that decides whether DevLogin is enabled.
109+
*/
110+
@Input
111+
public abstract Property<Boolean> getDevLogin();
112+
107113
private final ProgramArgsFormat programArgsFormat;
108114

109115
protected PrepareRunOrTest(ProgramArgsFormat programArgsFormat) {
110116
this.programArgsFormat = programArgsFormat;
111117
getVersionCapabilities().convention(VersionCapabilities.latest());
118+
getDevLogin().convention(false);
112119
}
113120

114121
protected abstract UserDevRunType resolveRunType(UserDevConfig userDevConfig);
@@ -153,8 +160,19 @@ public void prepareRun() throws IOException {
153160
runConfig = resolveRunType(userDevConfig);
154161
}
155162

156-
writeJvmArguments(runConfig);
157-
writeProgramArguments(runConfig);
163+
var mainClass = resolveMainClass(runConfig);
164+
var devLogin = getDevLogin().get();
165+
166+
var sysProps = new LinkedHashMap<String, String>();
167+
168+
// When DevLogin is used, we swap out the main class with the DevLogin one, and add the actual main class as a system property
169+
if (devLogin && mainClass != null) {
170+
sysProps.put("devlogin.launch_target", mainClass);
171+
mainClass = RunUtils.DEV_LOGIN_MAIN_CLASS;
172+
}
173+
174+
writeJvmArguments(runConfig, sysProps);
175+
writeProgramArguments(runConfig, mainClass);
158176
}
159177

160178
private UserDevConfig loadUserDevConfig(File userDevFile) {
@@ -208,7 +226,7 @@ private UserDevConfig getSimulatedUserDevConfigForVanilla() {
208226
return new UserDevConfig(runTypes);
209227
}
210228

211-
private void writeJvmArguments(UserDevRunType runConfig) throws IOException {
229+
private void writeJvmArguments(UserDevRunType runConfig, Map<String, String> additionalProperties) throws IOException {
212230
var lines = new ArrayList<String>();
213231

214232
lines.addAll(getInterpolatedJvmArgs(runConfig));
@@ -238,7 +256,9 @@ private void writeJvmArguments(UserDevRunType runConfig) throws IOException {
238256
addSystemProp(prop.getKey(), propValue, lines);
239257
}
240258

241-
for (var entry : getSystemProperties().get().entrySet()) {
259+
additionalProperties.putAll(getSystemProperties().get());
260+
261+
for (var entry : additionalProperties.entrySet()) {
242262
addSystemProp(entry.getKey(), entry.getValue(), lines);
243263
}
244264

@@ -250,10 +270,9 @@ private void writeJvmArguments(UserDevRunType runConfig) throws IOException {
250270
);
251271
}
252272

253-
private void writeProgramArguments(UserDevRunType runConfig) throws IOException {
273+
private void writeProgramArguments(UserDevRunType runConfig, @Nullable String mainClass) throws IOException {
254274
var lines = new ArrayList<String>();
255275

256-
var mainClass = resolveMainClass(runConfig);
257276
if (mainClass != null) {
258277
lines.add("# Main Class");
259278
lines.add(mainClass);

src/main/java/net/neoforged/moddevgradle/internal/RunUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ final class RunUtils {
4444
private RunUtils() {
4545
}
4646

47-
public static String DEV_LAUNCH_GAV = "net.neoforged:DevLaunch:1.0.1";
48-
public static String DEV_LAUNCH_MAIN_CLASS = "net.neoforged.devlaunch.Main";
47+
public static final String DEV_LAUNCH_GAV = "net.neoforged:DevLaunch:1.0.1"; // renovate
48+
public static final String DEV_LAUNCH_MAIN_CLASS = "net.neoforged.devlaunch.Main";
49+
public static final String DEV_LOGIN_GAV = "net.covers1624:DevLogin:0.1.0.5"; // renovate
50+
public static final String DEV_LOGIN_MAIN_CLASS = "net.covers1624.devlogin.DevLogin";
4951

5052
public static String escapeJvmArg(String arg) {
5153
var escaped = arg.replace("\\", "\\\\").replace("\"", "\\\"");

testproject/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ neoForge {
4242
client()
4343
programArguments.addAll('--username', 'Dev2')
4444
}
45+
clientAuth {
46+
client()
47+
devLogin = true
48+
}
4549
gradleOnlyClient {
4650
client()
4751
programArguments.addAll('--username', 'Dev3')

0 commit comments

Comments
 (0)