Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #17 from RacoonDog/master
Browse files Browse the repository at this point in the history
Improve automatic meteor installation. Thanks Crosby
  • Loading branch information
DAMcraft authored Nov 7, 2023
2 parents ab6b7d3 + cbda8d8 commit cb53acc
Showing 1 changed file with 83 additions and 115 deletions.
198 changes: 83 additions & 115 deletions src/main/java/de/damcraft/serverseeker/gui/InstallMeteorScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

import java.io.InputStream;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class InstallMeteorScreen extends Screen {
public InstallMeteorScreen() {
Expand All @@ -24,129 +26,95 @@ public InstallMeteorScreen() {

public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
context.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, this.height / 4 - 60 + 20, 16777215);
context.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, this.height / 4 - 60 + 20, -1);
}

protected void init() {
super.init();
ButtonWidget quitButton = this.addDrawableChild(ButtonWidget.builder(Text.translatable("menu.quit"), (button) -> {
this.client.scheduleStop();
}).dimensions(this.width / 2 + 2, this.height / 4 + 100 + 25, 148, 20).build());

this.addDrawableChild(ButtonWidget.builder(Text.of("Automatically install Meteor (§arecommended§r)"), (button) -> {
String result = SmallHttp.get("https://meteorclient.com/api/stats");
if (result == null) {
this.clearChildren();
// Render error
this.addDrawableChild(new TextWidget(
this.width / 2 - 250,
this.height / 4,
500,
20,
Text.of("Failed to get install meteor automatically! Please install it manually."),
this.textRenderer
));
this.addDrawableChild(ButtonWidget.builder(Text.of("Open install FAQ"), (button2) -> {
Util.getOperatingSystem().open("https://meteorclient.com/faq/installation");
}).dimensions(this.width / 2 - 150, this.height / 4 + 100, 300, 20).build());
}
Gson gson = new Gson();
JsonObject json = gson.fromJson(result, JsonObject.class);
String currentVersion = SharedConstants.getGameVersion().getName();
String stableVersion = json.get("mc_version").getAsString();
String devBuildVersion = json.get("dev_build_mc_version").getAsString();
String url;
if (currentVersion.equals(stableVersion)) {
url = "https://meteorclient.com/api/download";
} else if (currentVersion.equals(devBuildVersion)) {
url = "https://meteorclient.com/api/download?devBuild=latest";
} else {
this.clearChildren();
// Render error
this.addDrawableChild(new TextWidget(
this.width / 2 - 250,
this.height / 4,
500,
20,
Text.of("Failed to find Meteor for your current version."),
this.textRenderer
));
this.addDrawableChild(ButtonWidget.builder(Text.of("Open install FAQ"), (button2) -> {
Util.getOperatingSystem().open("https://meteorclient.com/faq/installation");
}).dimensions(this.width / 2 - 150, this.height / 4 + 100, 300, 20).build());
return;
}
HttpResponse<InputStream> file = SmallHttp.download(url);
if (file == null) {
this.clearChildren();
// Render error
this.addDrawableChild(new TextWidget(
this.width / 2 - 250,
this.height / 4,
500,
20,
Text.of("Failed to download Meteor! Please install it manually."),
this.textRenderer
));
this.addDrawableChild(ButtonWidget.builder(Text.of("Open install FAQ"), (button2) -> {
Util.getOperatingSystem().open("https://meteorclient.com/faq/installation");
}).dimensions(this.width / 2 - 150, this.height / 4 + 100, 300, 20).build());
return;
}
Optional<String> filenameT= file.headers().firstValue("Content-Disposition");
String filename = "meteor-client.jar";
if (filenameT.isPresent()) {
filename = filenameT.get().split("filename=")[1];
}
System.out.println(filename);
// Get the mods folder
Path modsFolder = FabricLoader.getInstance().getGameDir().resolve("mods");
if (!modsFolder.toFile().exists()) {
this.clearChildren();
// Render error
this.addDrawableChild(new TextWidget(
this.width / 2 - 250,
this.height / 4,
500,
20,
Text.of("Failed to find mods folder! Please install Meteor manually."),
this.textRenderer
));
this.addDrawableChild(ButtonWidget.builder(Text.of("Open install FAQ"), (button2) -> {
Util.getOperatingSystem().open("https://meteorclient.com/faq/installation");
}).dimensions(this.width / 2 - 150, this.height / 4 + 100, 300, 20).build());
}
// Save the file
try {
java.nio.file.Files.copy(file.body(), modsFolder.resolve(filename));
} catch (Exception e) {
this.clearChildren();
// Render error
this.addDrawableChild(new TextWidget(
this.width / 2 - 250,
this.height / 4,
500,
20,
Text.of("Failed to save Meteor! Please install it manually."),
this.textRenderer
));
this.addDrawableChild(ButtonWidget.builder(Text.of("Open install FAQ"), (button2) -> {
Util.getOperatingSystem().open("https://meteorclient.com/faq/installation");
}).dimensions(this.width / 2 - 150, this.height / 4 + 100, 300, 20).build());
return;
}
// Success message
this.clearChildren();
this.addDrawableChild(new TextWidget(
this.width / 2 - 250,
this.height / 4,
500,
20,
Text.of("Successfully installed Meteor! Please restart your game."),
this.textRenderer
));
quitButton.active = false;
CompletableFuture.runAsync(() -> {
install();
quitButton.active = true;
});
}).dimensions(this.width / 2 - 150, this.height / 4 + 100, 300, 20).build());

this.addDrawableChild(ButtonWidget.builder(Text.of("Manual installation"), (button) -> {
Util.getOperatingSystem().open("https://meteorclient.com/faq/installation");
}).dimensions(this.width / 2 - 150, this.height / 4 + 100 + 25, 148, 20).build());
this.addDrawableChild(ButtonWidget.builder(Text.translatable("menu.quit"), (button) -> {
this.client.scheduleStop();
}).dimensions(this.width / 2 + 2, this.height / 4 + 100 + 25, 148, 20).build());
}

private void install() {
String result = SmallHttp.get("https://meteorclient.com/api/stats");
if (result == null) {
this.displayError("Failed to get install meteor automatically! Please install it manually.");
return;
}
Gson gson = new Gson();
JsonObject json = gson.fromJson(result, JsonObject.class);
String currentVersion = SharedConstants.getGameVersion().getName();
String stableVersion = json.get("mc_version").getAsString();
String devBuildVersion = json.get("dev_build_mc_version").getAsString();
String url;
if (currentVersion.equals(stableVersion)) {
url = "https://meteorclient.com/api/download";
} else if (currentVersion.equals(devBuildVersion)) {
url = "https://meteorclient.com/api/download?devBuild=latest";
} else {
this.displayError("Failed to find Meteor for your current version.");
return;
}
HttpResponse<InputStream> file = SmallHttp.download(url);
if (file == null) {
this.displayError("Failed to download Meteor! Please install it manually.");
return;
}
Optional<String> filenameT = file.headers().firstValue("Content-Disposition");
String filename = "meteor-client.jar";
if (filenameT.isPresent()) {
filename = filenameT.get().split("filename=")[1];
}

// Get the mods folder
Path modsFolder = FabricLoader.getInstance().getGameDir().resolve("mods");
if (!Files.exists(modsFolder)) {
this.displayError("Failed to find mods folder! Please install Meteor manually.");
return;
}

// Save the file
try {
Files.copy(file.body(), modsFolder.resolve(filename));
} catch (Exception e) {
this.displayError("Failed to save Meteor! Please install it manually.");
return;
}

// Success message
this.displayNotice("Successfully installed Meteor! Please restart your game.");
}

private void displayError(String errorMessage) {
this.displayNotice(errorMessage);

this.addDrawableChild(ButtonWidget.builder(Text.of("Open install FAQ"), (button2) -> {
Util.getOperatingSystem().open("https://meteorclient.com/faq/installation");
}).dimensions(this.width / 2 - 150, this.height / 4 + 100, 300, 20).build());
}

private void displayNotice(String noticeMessage) {
this.clearChildren();
this.addDrawableChild(new TextWidget(
this.width / 2 - 250,
this.height / 4,
500,
20,
Text.of(noticeMessage),
this.textRenderer
));
}
}

0 comments on commit cb53acc

Please sign in to comment.