This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now prompts users to install Meteor in case it isn't installed
- Loading branch information
Showing
5 changed files
with
212 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
src/main/java/de/damcraft/serverseeker/gui/InstallMeteorScreen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package de.damcraft.serverseeker.gui; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonObject; | ||
import de.damcraft.serverseeker.SmallHttp; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.minecraft.SharedConstants; | ||
import net.minecraft.client.gui.DrawContext; | ||
import net.minecraft.client.gui.screen.Screen; | ||
import net.minecraft.client.gui.widget.ButtonWidget; | ||
import net.minecraft.client.gui.widget.TextWidget; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Util; | ||
|
||
import java.io.InputStream; | ||
import java.net.http.HttpResponse; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
public class InstallMeteorScreen extends Screen { | ||
public InstallMeteorScreen() { | ||
super(Text.of("Meteor Client is not installed!")); | ||
} | ||
|
||
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); | ||
} | ||
|
||
protected void init() { | ||
super.init(); | ||
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 | ||
)); | ||
}).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()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/de/damcraft/serverseeker/mixin/TitleScreenMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package de.damcraft.serverseeker.mixin; | ||
|
||
import de.damcraft.serverseeker.gui.InstallMeteorScreen; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.gui.screen.TitleScreen; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(TitleScreen.class) | ||
public class TitleScreenMixin { | ||
@Inject(at = @At("HEAD"), method = "init()V", cancellable = true) | ||
private void init(CallbackInfo info) { | ||
// Check if meteor-client is installed | ||
if (!FabricLoader.getInstance().isModLoaded("meteor-client")) { | ||
info.cancel(); | ||
MinecraftClient.getInstance().setScreen(new InstallMeteorScreen()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters