From cbda8d81cc986db495b74b2354601f6655246e70 Mon Sep 17 00:00:00 2001 From: RacoonDog <32882447+RacoonDog@users.noreply.github.com> Date: Tue, 7 Nov 2023 01:13:18 -0500 Subject: [PATCH] Update InstallMeteorScreen.java --- .../serverseeker/gui/InstallMeteorScreen.java | 198 ++++++++---------- 1 file changed, 83 insertions(+), 115 deletions(-) diff --git a/src/main/java/de/damcraft/serverseeker/gui/InstallMeteorScreen.java b/src/main/java/de/damcraft/serverseeker/gui/InstallMeteorScreen.java index 3c59ed1..ee3a807 100644 --- a/src/main/java/de/damcraft/serverseeker/gui/InstallMeteorScreen.java +++ b/src/main/java/de/damcraft/serverseeker/gui/InstallMeteorScreen.java @@ -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() { @@ -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 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 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 file = SmallHttp.download(url); + if (file == null) { + this.displayError("Failed to download Meteor! Please install it manually."); + return; + } + Optional 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 + )); } }