From dbac28252de411cb268f500c6472f26601e9f5e4 Mon Sep 17 00:00:00 2001 From: PseudoDistant Date: Thu, 28 Dec 2023 10:39:21 -0500 Subject: [PATCH] Cleanup, add very important info to mod menu screen. --- .../minifabric_api/impl/ModsDisplay.java | 163 ++---------------- .../mixin/SelectControlMenuMixin.java | 16 ++ 2 files changed, 34 insertions(+), 145 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 fc6973c..fbdc698 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 @@ -18,17 +18,10 @@ import java.util.Objects; public class ModsDisplay extends SelectControlMenu { - //private static final String[] defaultOptions = new String[]{"Highscore", "Load game", "Save game", "Start new game", "Resume game", "Respawn"}; - private static String[] defaultOptions; public static final int titleColor = Color.get(0, 8, 131, 551); - - private final Collection modsList = FabricLoader.getInstance().getAllMods(); - private static String modName = ""; - - public static String getModName() { return modName; } - private static ArrayList modNames; - private static final ArrayList modVersions = new ArrayList<>(); + private static ArrayList modDescriptions; + private static ArrayList modVersions; public ModsDisplay(SelectControlMenu parent) { super(parent); @@ -38,6 +31,8 @@ public ModsDisplay(SelectControlMenu parent) { private static ArrayList getModNames() { Collection modsList = FabricLoader.getInstance().getAllMods(); ArrayList modNames = new ArrayList<>(); + ArrayList modVersions = new ArrayList<>(); + ArrayList modDescs = new ArrayList<>(); if(ModsDisplay.modNames != null) { modNames.addAll(ModsDisplay.modNames); @@ -49,7 +44,7 @@ private static ArrayList getModNames() { return new ArrayList<>(); } - modVersions.clear(); + // Iterate between every mod loaded by Fabric. for (ModContainer container : modsList) { @@ -58,44 +53,37 @@ private static ArrayList getModNames() { if (!container.getMetadata().getId().equals("java")) { modNames.add(name); modVersions.add(container.getMetadata().getVersion().toString()); + modDescs.add(container.getMetadata().getDescription() == null ? "" : container.getMetadata().getDescription()); } } } - if(ModsDisplay.modNames == null) + if(ModsDisplay.modNames == null || ModsDisplay.modDescriptions == null || ModsDisplay.modVersions == null) { ModsDisplay.modNames = new ArrayList<>(); - else + ModsDisplay.modDescriptions = new ArrayList<>(); + ModsDisplay.modVersions = new ArrayList<>(); + } else { ModsDisplay.modNames.clear(); + ModsDisplay.modDescriptions.clear(); + ModsDisplay.modVersions.clear(); + } ModsDisplay.modNames.addAll(modNames); + ModsDisplay.modVersions.addAll(modVersions); + ModsDisplay.modDescriptions.addAll(modDescs); return modNames; } public void init(GameControl gameControl, InputHandler input) { - defaultOptions = getModNames().toArray(new String[0]); super.init(gameControl, input); - int[] new_selectable = new int[defaultOptions.length]; - int amount; - for(amount = 0; amount < defaultOptions.length; ++amount) { - new_selectable[amount] = 1; - } - - amount = defaultOptions.length; - - this.setOptions(Arrays.copyOf(defaultOptions, amount)); + this.setOptions(Arrays.copyOf(getModNames().toArray(new String[0]), modNames.size())); this.selected = this.options.length - 1; } public void optionSelected(int selected) { - String description = modsList.stream() - .filter(mod -> Objects.equals(mod.getMetadata().getName(), defaultOptions[selected])) - .findFirst().get().getMetadata().getDescription() == null ? - "" : modsList.stream() - .filter(mod -> Objects.equals(mod.getMetadata().getName(), defaultOptions[selected])) - .findFirst().get().getMetadata().getDescription(); - this.gameControl.setMenu(new SingleModDisplay(this, defaultOptions[selected], description, modVersions.get(selected))); + this.gameControl.setMenu(new SingleModDisplay(this, modNames.get(selected), modDescriptions.get(selected), modVersions.get(selected))); } @@ -111,120 +99,5 @@ public void render(Screen screen) { screen.renderSprite(xo, yo * SpriteSheet.spriteSize, 13, 28, titleColor, 0, 4 * SpriteSheet.spriteSize, SpriteSheet.spriteSize); this.menuTop = yo + h + 1; super.render(screen, false); - Font.draw(GameControl.VERSION, screen, screen.w - GameControl.VERSION.length() * SpriteSheet.spriteSize, screen.h - SpriteSheet.spriteSize, infoCol); - } -} -/* - 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<>(); - - ControlMenu parent; - - private static ArrayList getModNames() { - Collection modsList = FabricLoader.getInstance().getAllMods(); - ArrayList modNames = new ArrayList<>(); - - if(ModsDisplay.modNames != null) { - modNames.addAll(ModsDisplay.modNames); - return modNames; - } - - if(modsList.isEmpty()) { - System.err.println("ERROR: No mods are detected by Fabric Loader somehow..."); - 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()); - } - } - } - - if(ModsDisplay.modNames == null) - ModsDisplay.modNames = new ArrayList<>(); - else - ModsDisplay.modNames.clear(); - - ModsDisplay.modNames.addAll(modNames); - - return modNames; - } - - public ModsDisplay(ControlMenu parent) { - super(parent); - this.parent = parent; - } - - @Override - public void init(GameControl gameControl, InputHandler input) { - super.init(gameControl, input); - modName = ""; - - ArrayList modNames = getModNames(); - - int[] entries = new int[modNames.size()]; - - for(int i = 0; i < entries.length; i++) { - entries[i] = 1; - 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(); - - - entries[i] = new SelectEntry(modNames.get(i), () -> { - Game.setDisplay(new BookDisplay(description, false)); - }, false) {}; - } - - - - menus = new Menu[] { - new Menu.Builder(false, 0, RelPos.CENTER, entries) - .setDisplayLength(5) - .setScrollPolicies(1, true) - .createMenu() - }; - } - - @Override - public void render(Screen screen) { - super.render(screen); - - int sel = ((MenuDuck)menus[0]).minifabric_modmenu$selectionGetter(); - if(sel >= 0 && sel < modVersions.size()) { - String name = modNames.get(sel); - String version = modVersions.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(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); - - String title = Localization.getLocalized("Fabric Mod Menu"); - int color = Color.WHITE; - - int y = Screen.h - Font.textHeight(); - - Font.drawCentered(title, screen, 0, color); } -} -*/ \ No newline at end of file +} \ No newline at end of file diff --git a/src/main/java/io/github/minifabric/minifabric_api/mixin/SelectControlMenuMixin.java b/src/main/java/io/github/minifabric/minifabric_api/mixin/SelectControlMenuMixin.java index bbf165b..a135611 100644 --- a/src/main/java/io/github/minifabric/minifabric_api/mixin/SelectControlMenuMixin.java +++ b/src/main/java/io/github/minifabric/minifabric_api/mixin/SelectControlMenuMixin.java @@ -1,13 +1,20 @@ package io.github.minifabric.minifabric_api.mixin; +import com.mojang.ld22.gfx.Color; +import com.mojang.ld22.gfx.Font; +import com.mojang.ld22.gfx.Screen; +import com.mojang.ld22.gfx.SpriteSheet; import com.mojang.ld22.screen.controlmenu.ControlMenu; import com.mojang.ld22.screen.controlmenu.SelectControlMenu; import com.mojang.ld22.screen.controlmenu.TitleMenu; +import io.github.minifabric.minifabric_api.impl.ModsDisplay; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; +import static com.mojang.ld22.screen.controlmenu.SelectControlMenu.infoCol; + @Mixin(SelectControlMenu.class) public class SelectControlMenuMixin extends ControlMenu { @Shadow protected int selected; @@ -22,4 +29,13 @@ public void removeClose(SelectControlMenu instance, int i) { this.gameControl.setMenu(this.parent); } } + + @Redirect(method = "render(Lcom/mojang/ld22/gfx/Screen;Z)V", at = @At(value = "INVOKE", target = "Lcom/mojang/ld22/gfx/Font;draw(Ljava/lang/String;Lcom/mojang/ld22/gfx/Screen;III)V", ordinal = 2)) + private void addCredits(String msg, Screen screen, int x, int y, int col) { + if ((SelectControlMenu)(Object)this instanceof ModsDisplay) { + Font.draw("ModMenu by PseudoDistant!", screen, 0, screen.h - SpriteSheet.spriteSize, Color.get(0, 50, 50, 50)); + } else { + Font.draw("(§/°/[/], X and C)", screen, 0, screen.h - SpriteSheet.spriteSize, infoCol); + } + } }