Sitelet https://github.com/DeployedReject/murces/commit/566de97d4aece1859e2c3eb0b31435dd9fdc140d
Skip to content

Commit 566de97

Browse files
Updated Deprecated Paper API
1 parent df69446 commit 566de97

1 file changed

Lines changed: 82 additions & 48 deletions

File tree

‎java/orchestrator/src/main/java/org/codeberg/DeployedReject/device/ServerHandler.java‎

Lines changed: 82 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public void serverHandler() {
7272
case 0:
7373
try (FileWriter eulaWriter = new FileWriter("eula.txt")) {
7474
eulaWriter.write("eula=true\n");
75-
} catch (Exception ignored) {}
75+
} catch (Exception ignored) {
76+
}
7677
JsonObject comp = new JsonObject();
7778
comp.addProperty("status", 3);
7879
comp.addProperty("type", "server");
@@ -105,7 +106,8 @@ public void serverHandler() {
105106
try {
106107
String[] checkCmd = new String[] { "tmux", "has-session", "-t", "mcServer" };
107108
isRunning = (Shell.execute(checkCmd).waitFor() == 0);
108-
} catch (Exception ignored) {}
109+
} catch (Exception ignored) {
110+
}
109111
JsonObject statusResp = new JsonObject();
110112
statusResp.addProperty("status", 0);
111113
statusResp.addProperty("type", "server");
@@ -122,7 +124,8 @@ public void serverHandler() {
122124
stopServer();
123125
try {
124126
Thread.sleep(1000);
125-
} catch (Exception ignored) {}
127+
} catch (Exception ignored) {
128+
}
126129
if (loader.equals("forge"))
127130
spawnServer(true);
128131
else
@@ -189,14 +192,21 @@ private void vanilla() {
189192
}
190193

191194
private void fabric() {
195+
196+
if (!checkCommand("java")) {
197+
ErrorHelper.errorJson("java is not found in PATH. Please install a compatible Java. Recommended: SDKMAN!");
198+
return;
199+
}
200+
192201
if (job == 3) {
193202
list.add("fabric");
194203
return;
195204
}
196205

197206
String installerVersion = "1.1.2";
198207
try {
199-
HttpRequest vReq = HttpRequest.newBuilder().uri(URI.create("https://meta.fabricmc.net/v2/versions/installer")).GET().build();
208+
HttpRequest vReq = HttpRequest.newBuilder().uri(URI.create("https://meta.fabricmc.net/v2/versions/installer"))
209+
.GET().build();
200210
HttpResponse<String> vResp = NetworkUtils.attemptS(vReq);
201211
if (vResp != null && vResp.body() != null) {
202212
JsonArray arr = JsonParser.parseString(vResp.body()).getAsJsonArray();
@@ -208,10 +218,12 @@ private void fabric() {
208218
}
209219
}
210220
}
211-
} catch (Exception ignored) {}
221+
} catch (Exception ignored) {
222+
}
212223

213224
String installerJar = "fabric-installer.jar";
214-
String url = "https://maven.fabricmc.net/net/fabricmc/fabric-installer/" + installerVersion + "/fabric-installer-" + installerVersion + ".jar";
225+
String url = "https://maven.fabricmc.net/net/fabricmc/fabric-installer/" + installerVersion + "/fabric-installer-"
226+
+ installerVersion + ".jar";
215227
HttpRequest downloadRequest = HttpRequest.newBuilder().uri(URI.create(url)).GET().build();
216228
HttpResponse<InputStream> downloading = NetworkUtils.attemptI(downloadRequest);
217229
if (downloading == null) {
@@ -229,7 +241,8 @@ private void fabric() {
229241
commandList.add("server");
230242
commandList.add("-mcversion");
231243
commandList.add(gVersion);
232-
if (lVersion != null && !lVersion.isEmpty() && !lVersion.equalsIgnoreCase("none") && !lVersion.equalsIgnoreCase("latest")) {
244+
if (lVersion != null && !lVersion.isEmpty() && !lVersion.equalsIgnoreCase("none")
245+
&& !lVersion.equalsIgnoreCase("latest")) {
233246
commandList.add("-loader");
234247
commandList.add(lVersion);
235248
}
@@ -251,7 +264,8 @@ private void fabric() {
251264
} finally {
252265
try {
253266
Files.deleteIfExists(Paths.get(installerJar));
254-
} catch (Exception ignored) {}
267+
} catch (Exception ignored) {
268+
}
255269
}
256270
}
257271

@@ -316,24 +330,43 @@ private void paper() {
316330
return;
317331
}
318332

319-
String url = "https://api.papermc.io/v2/projects/paper/versions/" + gVersion;
320-
321-
HttpResponse<String> findingURL = NetworkUtils
322-
.attemptS(HttpRequest.newBuilder().uri(URI.create(url)).GET().build());
323-
int build = JsonParser.parseString(findingURL.body()).getAsJsonObject().get("builds").getAsJsonArray().size() - 1;
324-
325-
build = JsonParser.parseString(findingURL.body()).getAsJsonObject().get("builds").getAsJsonArray().get(build)
326-
.getAsInt();
333+
String url = "https://api.papermc.io/v3/projects/paper/versions/" + gVersion + "/builds/latest";
334+
HttpResponse<String> findingURL = NetworkUtils.attemptS(HttpRequest.newBuilder()
335+
.uri(URI.create(url))
336+
.GET()
337+
.build());
327338

328-
url += "/builds/" + build + "/downloads/paper-" + gVersion + "-" + build + ".jar";
339+
if (findingURL == null || findingURL.body() == null) {
340+
ErrorHelper.errorJson("Failed to fetch Paper build information");
341+
return;
342+
}
329343

330-
HttpResponse<InputStream> downloading = NetworkUtils
331-
.attemptI(HttpRequest.newBuilder().uri(URI.create(url)).GET().build());
344+
try {
345+
JsonObject jsonObj = JsonParser.parseString(findingURL.body()).getAsJsonObject();
346+
JsonObject downloads = jsonObj.getAsJsonObject("downloads");
347+
if (downloads == null || !downloads.has("server:default")) {
348+
ErrorHelper.errorJson("No server download found for Paper version " + gVersion);
349+
return;
350+
}
351+
JsonObject serverDefault = downloads.getAsJsonObject("server:default");
352+
String downloadUrl = serverDefault.get("url").getAsString();
332353

333-
long filesize = downloading.headers().firstValueAsLong("content-length").orElse(-1L);
354+
HttpRequest downloadRequest = HttpRequest.newBuilder()
355+
.uri(URI.create(downloadUrl))
356+
.GET()
357+
.build();
334358

335-
NetworkUtils.prog(downloading.body(), "server.jar", filesize);
359+
HttpResponse<InputStream> downloading = NetworkUtils.attemptI(downloadRequest);
360+
if (downloading == null) {
361+
ErrorHelper.errorJson("Failed to download Paper server");
362+
return;
363+
}
336364

365+
long filesize = downloading.headers().firstValueAsLong("content-length").orElse(-1L);
366+
NetworkUtils.prog(downloading.body(), "server.jar", filesize);
367+
} catch (Exception e) {
368+
ErrorHelper.errorJson("Error downloading Paper: " + e.getMessage());
369+
}
337370
}
338371

339372
private boolean checkCommand(String cmd) {
@@ -351,7 +384,7 @@ private void spawnServer() {
351384
return;
352385
}
353386
if (!checkCommand("java")) {
354-
ErrorHelper.errorJson("java is not found in PATH. Please install Java.");
387+
ErrorHelper.errorJson("java is not found in PATH. Please install Java. Recommended SDKMAN!");
355388
return;
356389
}
357390

@@ -423,33 +456,33 @@ private void spawnServer() {
423456

424457
private void stopServer() {
425458

426-
String[] checkCmd = new String[] { "tmux", "has-session", "-t", "mcServer" };
427-
try {
428-
if (Shell.execute(checkCmd).waitFor() != 0) {
459+
String[] checkCmd = new String[] { "tmux", "has-session", "-t", "mcServer" };
460+
try {
461+
if (Shell.execute(checkCmd).waitFor() != 0) {
429462

430-
ErrorHelper.errorJson("Server session not found; nothing to stop");
431-
return;
432-
}
433-
} catch (Exception e) {
434-
ErrorHelper.errorJson(e.toString());
435-
return;
436-
}
463+
ErrorHelper.errorJson("Server session not found; nothing to stop");
464+
return;
465+
}
466+
} catch (Exception e) {
467+
ErrorHelper.errorJson(e.toString());
468+
return;
469+
}
437470

438-
String[] command = new String[] {
439-
"tmux", "kill-session", "-t", "mcServer"
440-
};
441-
try {
442-
if (Shell.execute(command).waitFor() != 0) {
443-
ErrorHelper.errorJson("Could Not Stop Server");
444-
return;
445-
}
446-
JsonObject response = new JsonObject();
447-
response.addProperty("status", 0);
448-
response.addProperty("type", "server");
449-
Communicator.printer(response);
450-
} catch (Exception e) {
451-
ErrorHelper.errorJson(e.toString());
452-
}
471+
String[] command = new String[] {
472+
"tmux", "kill-session", "-t", "mcServer"
473+
};
474+
try {
475+
if (Shell.execute(command).waitFor() != 0) {
476+
ErrorHelper.errorJson("Could Not Stop Server");
477+
return;
478+
}
479+
JsonObject response = new JsonObject();
480+
response.addProperty("status", 0);
481+
response.addProperty("type", "server");
482+
Communicator.printer(response);
483+
} catch (Exception e) {
484+
ErrorHelper.errorJson(e.toString());
485+
}
453486

454487
}
455488

@@ -516,7 +549,8 @@ private void forge() {
516549
} finally {
517550
try {
518551
Files.deleteIfExists(Paths.get(installerName));
519-
} catch (Exception ignored) {}
552+
} catch (Exception ignored) {
553+
}
520554
}
521555

522556
}

0 commit comments

Comments
 (0)