From 06492ff27f12c2d3fd4aec3c83f47349073c9f91 Mon Sep 17 00:00:00 2001 From: PseudoDistant Date: Thu, 28 Dec 2023 11:23:04 -0500 Subject: [PATCH] Cleanup, add Authors line to menu. --- .../minifabric_api/impl/ModsDisplay.java | 81 ++++++++++++------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/src/main/java/io/github/minifabric/minifabric_api/impl/ModsDisplay.java b/src/main/java/io/github/minifabric/minifabric_api/impl/ModsDisplay.java index b8ffcef..e8e0750 100644 --- a/src/main/java/io/github/minifabric/minifabric_api/impl/ModsDisplay.java +++ b/src/main/java/io/github/minifabric/minifabric_api/impl/ModsDisplay.java @@ -10,6 +10,7 @@ import minicraft.screen.entry.SelectEntry; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; +import net.fabricmc.loader.api.metadata.Person; import java.util.ArrayList; import java.util.Collection; @@ -18,22 +19,24 @@ public class ModsDisplay extends Display { private final Collection modsList = FabricLoader.getInstance().getAllMods(); - private static String modName = ""; - public static String getModName() { return modName; } - - private static ArrayList modNames = null; - private static final ArrayList modVersions = new ArrayList<>(); + private static ArrayList modNames; + private static ArrayList modDescriptions; + private static ArrayList modVersions; + private static ArrayList modAuthors; Display parent; private static ArrayList getModNames() { Collection modsList = FabricLoader.getInstance().getAllMods(); - ArrayList modNames = new ArrayList<>(); + ArrayList names = new ArrayList<>(); + ArrayList descs = new ArrayList<>(); + ArrayList versions = new ArrayList<>(); + ArrayList authors = new ArrayList<>(); if(ModsDisplay.modNames != null) { - modNames.addAll(ModsDisplay.modNames); - return modNames; + names.addAll(ModsDisplay.modNames); + return names; } if(modsList.isEmpty()) { @@ -41,27 +44,50 @@ private static ArrayList getModNames() { return new ArrayList<>(); } - modVersions.clear(); - // Iterate between every mod loaded by Fabric. for (ModContainer container : modsList) { if (container != null) { String name = container.getMetadata().getName(); if (!container.getMetadata().getId().equals("java")) { - modNames.add(name); - modVersions.add(container.getMetadata().getVersion().toString()); + names.add(name); + descs.add(container.getMetadata().getDescription() == null ? "" : container.getMetadata().getDescription()); + versions.add(container.getMetadata().getVersion().toString()); + String author; + if (container.getMetadata().getAuthors() != null) { + StringBuilder authorNames = new StringBuilder(); + for (Person person : container.getMetadata().getAuthors()) { + if (!authorNames.isEmpty()) { + authorNames.append(", "); + } + authorNames.append(person.getName()); + } + author = authorNames.toString(); + } else { + author = "Unknown"; + } + authors.add(author); } } } - if(ModsDisplay.modNames == null) + if(ModsDisplay.modNames == null) { ModsDisplay.modNames = new ArrayList<>(); - else + ModsDisplay.modDescriptions = new ArrayList<>(); + ModsDisplay.modVersions = new ArrayList<>(); + ModsDisplay.modAuthors = new ArrayList<>(); + } else { ModsDisplay.modNames.clear(); + ModsDisplay.modDescriptions.clear(); + ModsDisplay.modVersions.clear(); + ModsDisplay.modAuthors.clear(); + } - ModsDisplay.modNames.addAll(modNames); + ModsDisplay.modNames.addAll(names); + ModsDisplay.modDescriptions.addAll(descs); + ModsDisplay.modVersions.addAll(versions); + ModsDisplay.modAuthors.addAll(authors); - return modNames; + return names; } public ModsDisplay() { @@ -78,23 +104,14 @@ public void tick(InputHandler input) { @Override public void init(Display parent) { - modName = ""; this.parent = parent; ArrayList modNames = getModNames(); - SelectEntry[] entries = new SelectEntry[modNames.size()]; + SelectEntry[] entries = new SelectEntry[ModsDisplay.modNames.size()]; for(int i = 0; i < entries.length; i++) { - String name = modNames.get(i); - final String version = modVersions.get(i); - String description = modsList.stream() - .filter(mod -> Objects.equals(mod.getMetadata().getName(), name)) - .findFirst().get().getMetadata().getDescription() == null ? - "" : modsList.stream() - .filter(mod -> Objects.equals(mod.getMetadata().getName(), name)) - .findFirst().get().getMetadata().getDescription(); - + String description = modDescriptions.get(i); entries[i] = new SelectEntry(modNames.get(i), () -> { Game.setDisplay(new BookDisplay(description, false)); @@ -105,7 +122,7 @@ public void init(Display parent) { menus = new Menu[] { new Menu.Builder(false, 0, RelPos.CENTER, entries) - .setDisplayLength(5) + .setDisplayLength(6) .setScrollPolicies(1, true) .createMenu() }; @@ -119,13 +136,15 @@ public void render(Screen screen) { if(sel >= 0 && sel < modVersions.size()) { String name = modNames.get(sel); String version = modVersions.get(sel); + String author = modAuthors.get(sel); int col = Color.WHITE; Font.drawCentered(Localization.getLocalized("Name:") + " " + name, screen, Font.textHeight() * 2, col); - Font.drawCentered(Localization.getLocalized("Mod version:") + " " + version, screen, Font.textHeight() * 7/2, col); + Font.drawCentered(Localization.getLocalized("Version:") + " " + version, screen, Font.textHeight() * 7/2, col); + Font.drawCentered(Localization.getLocalized("Authors:") + " " + author, screen, Font.textHeight() * 5, col); } - Font.drawCentered(Game.input.getMapping("select") + Localization.getLocalized(" to confirm"), screen, Screen.h - 60, Color.GRAY); - Font.drawCentered(Game.input.getMapping("exit") + Localization.getLocalized(" to return"), screen, Screen.h - 40, Color.GRAY); + Font.drawCentered(Game.input.getMapping("select") + Localization.getLocalized(" to confirm"), screen, Screen.h - 40, Color.GRAY); + Font.drawCentered(Game.input.getMapping("exit") + Localization.getLocalized(" to return"), screen, Screen.h - 20, Color.GRAY); String title = Localization.getLocalized("Fabric Mod Menu"); int color = Color.WHITE;