Skip to content

Commit d125888

Browse files
committed
Ask for a valid mc version instead of crashing
1 parent d287d4a commit d125888

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

src/main/java/org/kettingpowered/launcher/KettingLauncher.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public class KettingLauncher {
8585
private final ParsedArgs args;
8686
private final BetterUI ui;
8787
private final Libraries libs = new Libraries();
88+
89+
private final List<String> serverVersions = new ArrayList<>();
90+
private final List<String> availableMcVersions = new ArrayList<>();
8891

8992
KettingLauncher(String[] str_args) throws Exception {
9093
ui = new BetterUI(KettingConstants.NAME);
@@ -98,7 +101,21 @@ public class KettingLauncher {
98101
public void init() throws Exception {
99102
final String mc_version;
100103
if (Main.DEBUG && Bundled) System.out.println("We are in a bundled jar. Read Version information from manifest.");
101-
if (!Bundled) mc_version = MCVersion.getMc(args);
104+
105+
//Cache ketting versions
106+
final List<String> depVersions = new MavenManifest(KettingConstants.KETTINGSERVER_GROUP, Main.FORGE_SERVER_ARTIFACT_ID).getDepVersions();
107+
serverVersions.addAll(depVersions);
108+
109+
//Strip the minecraft version from the ketting version, by splitting by - and getting the first entry
110+
availableMcVersions.addAll(depVersions.stream().map(version -> {
111+
try {
112+
return version.split("-")[0];
113+
} catch (Exception e) { //Make sure that we don't crash, if the version is invalid
114+
return null;
115+
}
116+
}).filter(Objects::nonNull).distinct().sorted().toList());
117+
118+
if (!Bundled) mc_version = MCVersion.getMc(args, availableMcVersions);
102119
else mc_version = Bundled_McVersion;
103120
//This cannot get moved past the ensureOneServerAndUpdate call.
104121
//It will cause the just downloaded server to be removed, which causes issues.
@@ -259,7 +276,6 @@ else if(versions.size() > 1) {
259276
if (needsDownload) System.out.println("Downloading Server, since there is none currently present. Using determined Minecraft version: "+ mc_version);
260277
// get the newest version
261278
if (args.enableServerUpdator() || needsDownload) {
262-
final List<String> serverVersions = new MavenManifest(KettingConstants.KETTINGSERVER_GROUP, Main.FORGE_SERVER_ARTIFACT_ID).getDepVersions();
263279
final List<Tuple<MajorMinorPatchVersion<Integer>, MajorMinorPatchVersion<Integer>>> parsedServerVersions = MajorMinorPatchVersion.parseKettingServerVersionList(serverVersions.stream()).getOrDefault(mc_mmp, new ArrayList<>());
264280
if (Main.DEBUG) {
265281
System.out.println("Available Ketting versions");

src/main/java/org/kettingpowered/launcher/info/MCVersion.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@
88
import org.kettingpowered.launcher.utils.FileUtils;
99

1010
import java.io.*;
11-
import java.util.ArrayList;
12-
import java.util.Arrays;
13-
import java.util.Comparator;
14-
import java.util.HashMap;
15-
import java.util.List;
16-
import java.util.Objects;
11+
import java.util.*;
1712

1813
/**
1914
* @author C0D3 M4513R
2015
*/
2116
public class MCVersion {
2217
private static String mc;
23-
public static String getMc(ParsedArgs args){
18+
public static String getMc(ParsedArgs args, List<String> supportedMcVersions){
2419
if (mc != null) return mc;
2520

2621
//Check for a mcversion.txt file, this has priority
@@ -88,16 +83,35 @@ public static String getMc(ParsedArgs args){
8883
System.out.println("There are multiple mapping configurations for multiple minecraft versions present in the libraries folder.");
8984
}
9085
}
91-
//and now we are out of options.
92-
System.err.println("""
93-
Could not determine the active server minecraft version. Please specify it, by specifying one of the following:
94-
- creating a file named 'mcversion.txt' with the desired minecraft version (e.g. 1.20.4).
95-
- the '--minecraftVersion' argument. E.g.: add ' --minecraftVersion 1.20.4 ' after the '-jar' argument
96-
- the java property 'kettinglauncher.minecraftVersion' E.g.: ' -Dkettinglauncher.minecraftVersion=1.20.4 ' before the '-jar' argument
97-
- the environment variable 'kettinglauncher_minecraftVersion' E.g. ' kettinglauncher_minecraftVersion=1.20.4 ' before the java executable.
98-
""");
99-
System.exit(1);
100-
throw new RuntimeException();//bogus, but has to be there to stop the compiler from complaining, that there is no return value here.
86+
//Ask for a version
87+
{
88+
System.out.println("Could not automatically determine the minecraft version.");
89+
System.out.println("Please enter the minecraft version you want to use");
90+
System.out.println("The following versions are supported: " + String.join(", ", supportedMcVersions));
91+
System.out.print("Minecraft version: ");
92+
93+
int wrong = 0;
94+
95+
Scanner console = new Scanner(System.in);
96+
while (true) {
97+
String answer = console.nextLine();
98+
if (answer == null || answer.isBlank()) {
99+
if (wrong++ >= 2) {
100+
System.err.println("You have typed the wrong answer too many times. Exiting.");
101+
System.exit(1);
102+
}
103+
System.err.println("Please enter a valid version.");
104+
System.out.print("Minecraft version: ");
105+
continue;
106+
}
107+
if (supportedMcVersions.contains(answer.trim())) {
108+
mc = answer;
109+
return mc;
110+
}
111+
System.out.println("The version you entered is not supported.");
112+
System.out.print("Minecraft version: ");
113+
}
114+
}
101115
}
102116

103117
private static String readFromIS(@Nullable InputStream versionStream) {

0 commit comments

Comments
 (0)