Skip to content

Commit

Permalink
Fix crash when attempting to close title menu
Browse files Browse the repository at this point in the history
  • Loading branch information
PseudoDistant committed Dec 28, 2023
1 parent 78b4a12 commit 5ca7c01
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 241 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {
}

dependencies {
implementation files("libs/minicraft_delux.jar")
implementation files("libs/ld22.jar")
implementation 'io.github.pseudodistant:MinicraftGameProvider:1.2.0'
implementation "net.fabricmc:fabric-loader:${project.loader_version}"
implementation "net.fabricmc:sponge-mixin:0.11.0+mixin.0.8.5"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.15.3

# Mod Properties
mod_version = 1.2.0-delux
mod_version = 1.2.0-ld22
maven_group = io.github.minifabric
archives_base_name = minifabric-modmenu

This file was deleted.

233 changes: 79 additions & 154 deletions src/main/java/io/github/minifabric/minifabric_api/impl/ModsDisplay.java
Original file line number Diff line number Diff line change
@@ -1,133 +1,75 @@
package io.github.minifabric.minifabric_api.impl;

import com.mojang.ld22.GameControl;
import com.mojang.ld22.GameState;
import com.mojang.ld22.InputHandler;
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.*;
import com.mojang.ld22.screen.AboutMenu;
import com.mojang.ld22.screen.InstructionsMenu;
import com.mojang.ld22.screen.Menu;
import com.mojang.ld22.sound.Sound;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
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"};
public class ModsDisplay extends Menu {
private static String[] defaultOptions;

private int selected = 0;
public static final int titleColor = Color.get(0, 8, 131, 551);
private Menu parent;

private final Collection<ModContainer> modsList = FabricLoader.getInstance().getAllMods();
private static String modName = "";

public static String getModName() { return modName; }

private static ArrayList<String> modNames;
private static final ArrayList<String> modVersions = new ArrayList<>();
private static ArrayList<String> modVersions;

private static ArrayList<String> modDescriptions;

public ModsDisplay(SelectControlMenu parent) {
super(parent);
public ModsDisplay(Menu parent) {
this.parent = parent;
}

private static ArrayList<String> getModNames() {
Collection<ModContainer> modsList = FabricLoader.getInstance().getAllMods();
ArrayList<String> modNames = new ArrayList<>();
//TODO: Scrolling

if(ModsDisplay.modNames != null) {
modNames.addAll(ModsDisplay.modNames);
return modNames;
@Override
public void tick() {
if (this.input.up.clicked) {
--this.selected;
}

if(modsList.isEmpty()) {
System.err.println("ERROR: No mods are detected by Fabric Loader somehow...");
return new ArrayList<>();
if (this.input.down.clicked) {
++this.selected;
}

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());
}
}
int len = modNames.size();
if (this.selected < 0) {
this.selected += len;
}

if(ModsDisplay.modNames == null)
ModsDisplay.modNames = new ArrayList<>();
else
ModsDisplay.modNames.clear();

ModsDisplay.modNames.addAll(modNames);

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;
if (this.selected >= len) {
this.selected -= len;
}

amount = defaultOptions.length;

this.setOptions(Arrays.copyOf(defaultOptions, amount));
this.selected = this.options.length - 1;
}
if (this.input.attack.clicked || this.input.menu.clicked) {
if (this.selected == modNames.size() - 1) {
this.game.setMenu(parent);
} else {
String description = modDescriptions.get(selected) == null ?
"" : modDescriptions.get(selected);
this.game.setMenu(new SingleModDisplay(this, modNames.get(selected), description, modVersions.get(selected)));

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)));

}

public void render(Screen screen) {
screen.clear(0);
int h = 2;
int w = 13;
int xo = (screen.w - w * SpriteSheet.spriteSize) / 2;
int yo = 1;
screen.renderSprite(xo, yo * SpriteSheet.spriteSize, 0, 28, titleColor, 0, w * SpriteSheet.spriteSize, h * SpriteSheet.spriteSize);
xo = (screen.w - 4 * SpriteSheet.spriteSize) / 2;
yo += h;
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<ModContainer> modsList = FabricLoader.getInstance().getAllMods();
private static String modName = "";
public static String getModName() { return modName; }
private static ArrayList<String> modNames = null;
private static final ArrayList<String> modVersions = new ArrayList<>();
ControlMenu parent;

private static ArrayList<String> getModNames() {
Collection<ModContainer> modsList = FabricLoader.getInstance().getAllMods();
ArrayList<String> modNames = new ArrayList<>();
ArrayList<String> modDescs = new ArrayList<>();
ArrayList<String> versions = new ArrayList<>();

if(ModsDisplay.modNames != null) {
modNames.addAll(ModsDisplay.modNames);
Expand All @@ -139,92 +81,75 @@ private static ArrayList<String> 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());
versions.add(container.getMetadata().getVersion().toString());
modDescs.add(container.getMetadata().getDescription());
}
}
}
modNames.add("Back to menu");
versions.add("");
modDescs.add("");

if(ModsDisplay.modNames == null)
if(ModsDisplay.modNames == null || ModsDisplay.modVersions == null || ModsDisplay.modDescriptions == 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(versions);
ModsDisplay.modDescriptions.addAll(modDescs);

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<String> 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();
public void init() {
defaultOptions = getModNames().toArray(new String[0]);
int[] new_selectable = new int[defaultOptions.length];

entries[i] = new SelectEntry(modNames.get(i), () -> {
Game.setDisplay(new BookDisplay(description, false));
}, false) {};
int amount;
amount = modNames.size();
for(amount = 0; amount < defaultOptions.length; ++amount) {
new_selectable[amount] = 1;
}
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);
screen.clear(0);
int h = 2;
int w = 13;
int titleColor = Color.get(0, 8, 131, 551);
int xo = (screen.w - w * 8) / 2;
int yo = 24;

int i;
for(i = 0; i < h; ++i) {
for(int x = 0; x < w; ++x) {
screen.render(xo + x * 8, yo + i * 8, x + (i + 6) * 32, titleColor, 0);
}
}

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();
for(i = 0; i < getModNames().size(); ++i) {
String msg = getModNames().get(i);
int col = Color.get(0, 222, 222, 222);
//if (i == this.selected) {
if (i == selected) {
msg = "> " + msg + " <";
col = Color.get(0, 555, 555, 555);
}

Font.drawCentered(title, screen, 0, color);
Font.draw(msg, screen, (screen.w - msg.length() * 8) / 2, (8 + i) * 8, col);
}
}
}
*/
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
package io.github.minifabric.minifabric_api.impl;

import com.mojang.ld22.screen.controlmenu.ControlMenu;
import com.mojang.ld22.screen.controlmenu.TextControlMenu;
import com.mojang.ld22.gfx.Font;
import com.mojang.ld22.gfx.Screen;
import com.mojang.ld22.screen.Menu;
import com.mojang.ld22.gfx.Color;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SingleModDisplay extends TextControlMenu {
public SingleModDisplay(ControlMenu parent, String mod_name, String content, String version) {
super(parent);
public class SingleModDisplay extends Menu {
private Menu parent;
private String title;
private String version;
ArrayList<String> desc = new ArrayList<>();


@Override
public void tick() {
if (this.input.attack.clicked || this.input.menu.clicked) {
this.game.setMenu(this.parent);
}

}
public SingleModDisplay(Menu parent, String mod_name, String content, String version) {
this.parent = parent;
this.title = mod_name;
List matchList = new ArrayList();
Pattern regex = Pattern.compile("(.{1,24}(?:\\s|$))|(.{0,10})", Pattern.DOTALL);
this.version = version;

Pattern regex = Pattern.compile("(.{1,20}(?:\\s|$))|(.{0,10})", Pattern.DOTALL);
Matcher regexMatcher = regex.matcher(content);
while (regexMatcher.find()) {
matchList.add(regexMatcher.group());
desc.add(regexMatcher.group());
}
}

@Override
public void render(Screen screen) {
int screenPos = 24;
screen.clear(0);
Font.draw(title, screen, 4, 8, Color.get(0, 333, 333, 333));
for (String s : desc) {
Font.draw(s, screen, 0, screenPos, Color.get(0, 333, 333, 333));
screenPos += 8;
}
matchList.forEach(phrase -> {
this.addTextLine((String) phrase, Color.get(0, 333, 333, 333));
});

this.addTextLine(version, Color.get(0, 50, 50, 50));
Font.draw(version, screen, 4, screenPos, Color.get(0, 50, 50, 50));
}
}
Loading

0 comments on commit 5ca7c01

Please sign in to comment.