Created
October 28, 2024 11:44
-
-
Save Ensamisten/b561637f8f63948fd58a517bca2a1182 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package io.github.ensamisten.util.mcfiles; | |
| import com.google.gson.JsonObject; | |
| import com.google.gson.JsonParser; | |
| import net.fabricmc.loader.api.FabricLoader; | |
| import net.minecraft.GameVersion; | |
| import net.minecraft.SharedConstants; | |
| import net.minecraft.client.font.TextRenderer; | |
| import net.minecraft.client.gui.DrawContext; | |
| import net.minecraft.client.gui.screen.Screen; | |
| import net.minecraft.text.Text; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.nio.file.Files; | |
| import java.nio.file.Path; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.zip.ZipEntry; | |
| import java.util.zip.ZipFile; | |
| public class McFilesScreen extends Screen { | |
| private final List<ModInfo> modInfos; | |
| public McFilesScreen() { | |
| super(Text.literal("McFiles")); | |
| this.modInfos = new ArrayList<>(); | |
| loadModInfos(); | |
| } | |
| private void loadModInfos() { | |
| // Define the mods folder path | |
| Path modsFolderPath = FabricLoader.getInstance().getGameDir().resolve("mods"); | |
| // Check if the folder exists | |
| if (!Files.exists(modsFolderPath) || !Files.isDirectory(modsFolderPath)) { | |
| System.out.println("Mods folder not found!"); | |
| return; | |
| } | |
| // Iterate over files in the mods folder | |
| File modsFolder = modsFolderPath.toFile(); | |
| File[] files = modsFolder.listFiles((dir, name) -> name.endsWith(".jar")); | |
| if (files != null) { | |
| for (File file : files) { | |
| try (ZipFile zipFile = new ZipFile(file)) { | |
| ZipEntry entry = zipFile.getEntry("fabric.mod.json"); | |
| if (entry != null) { | |
| try (InputStream inputStream = zipFile.getInputStream(entry); | |
| InputStreamReader reader = new InputStreamReader(inputStream)) { | |
| JsonObject modJson = JsonParser.parseReader(reader).getAsJsonObject(); | |
| String modId = modJson.get("id").getAsString(); | |
| String modName = modJson.has("name") ? modJson.get("name").getAsString() : modId; | |
| String modVersion = modJson.get("version").getAsString(); | |
| ModMetadata modMetadata = new ModMetadata( | |
| modId, | |
| modVersion, | |
| modName, | |
| getDependencyVersion(modJson, "minecraft"), | |
| getDependencyVersion(modJson, "fabricloader") | |
| ); | |
| modInfos.add(new ModInfo(file.getName(), modMetadata)); | |
| } | |
| } | |
| } catch (IOException e) { | |
| System.out.println("Failed to read mod file: " + file.getName()); | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| } | |
| private String getDependencyVersion(JsonObject modJson, String modId) { | |
| if (modJson.has("depends")) { | |
| JsonObject dependencies = modJson.getAsJsonObject("depends"); | |
| if (dependencies.has(modId)) { | |
| return dependencies.get(modId).getAsString(); | |
| } | |
| } | |
| return null; | |
| } | |
| @Override | |
| protected void init() { | |
| super.init(); | |
| } | |
| @Override | |
| public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) { | |
| this.renderBackground(drawContext, mouseX, mouseY, delta); | |
| TextRenderer textRenderer = this.client.textRenderer; | |
| // Display the Minecraft version in the top-right corner | |
| GameVersion minecraftVersion = SharedConstants.getGameVersion(); | |
| String versionText = "Minecraft Version: " + minecraftVersion.getName(); | |
| int versionWidth = textRenderer.getWidth(versionText); | |
| drawContext.drawText(textRenderer, versionText, this.width - versionWidth - 10, 10, 0xFFFFFF, false); | |
| // Display the list of mods with their metadata | |
| int yPosition = 30; | |
| for (ModInfo modInfo : modInfos) { | |
| String displayText = modInfo.fileName; | |
| if (modInfo.metadata != null) { | |
| displayText += " - " + (modInfo.metadata.name != null ? modInfo.metadata.name : modInfo.metadata.id); | |
| displayText += " v" + modInfo.metadata.version; | |
| if (modInfo.metadata.minecraftVersion != null) { | |
| displayText += " [MC " + modInfo.metadata.minecraftVersion + "]"; | |
| } | |
| if (modInfo.metadata.loaderVersion != null) { | |
| displayText += " [Loader " + modInfo.metadata.loaderVersion + "]"; | |
| } | |
| } else { | |
| displayText += " - Metadata Unavailable"; | |
| } | |
| drawContext.drawText(textRenderer, displayText, 10, yPosition, 0xFFFFFF, false); | |
| yPosition += 12; | |
| } | |
| super.render(drawContext, mouseX, mouseY, delta); | |
| } | |
| private static class ModInfo { | |
| public final String fileName; | |
| public final ModMetadata metadata; | |
| public ModInfo(String fileName, ModMetadata metadata) { | |
| this.fileName = fileName; | |
| this.metadata = metadata; | |
| } | |
| } | |
| private static class ModMetadata { | |
| public final String id; | |
| public final String version; | |
| public final String name; | |
| public final String minecraftVersion; | |
| public final String loaderVersion; | |
| public ModMetadata(String id, String version, String name, String minecraftVersion, String loaderVersion) { | |
| this.id = id; | |
| this.version = version; | |
| this.name = name; | |
| this.minecraftVersion = minecraftVersion; | |
| this.loaderVersion = loaderVersion; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment