Skip to content

Commit

Permalink
refactor: Cleanups and preparing for mod options
Browse files Browse the repository at this point in the history
  • Loading branch information
magicus committed Apr 27, 2024
1 parent 7fdf730 commit 7106c41
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/main/java/se/icus/mag/modsettings/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

public class Main implements ClientModInitializer {
public static final Logger LOGGER = LogManager.getLogger("modsettings");
public static final Options OPTIONS = new Options();

@Override
public void onInitializeClient() {
Expand All @@ -25,4 +26,9 @@ public void onInitializeClient() {
}
});
}

public static class Options {
public String filterText = "";
public boolean showIndirect = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,25 @@ protected void init() {

// Put list between 32 pixels from top and bottom
this.list = new ModListWidget(this.client, this.width, this.height - 64, 32, 25);
this.list.addAll(getAllModConfigOptions());

this.addSelectableChild(this.list);
this.addDrawableChild(this.list);
this.addDrawableChild(new Button(this.width / 2 - FULL_BUTTON_WIDTH / 2, this.height - 27,
FULL_BUTTON_WIDTH, BUTTON_HEIGHT, ScreenTexts.DONE,
button -> this.client.setScreen(this.previous)));

updateModButtons();
initIsProcessing = false;
}

private ModConfigInfo[] getAllModConfigOptions() {
private void updateModButtons() {
List<String> visibleModIds = ModRegistry.getInstance().getVisibleModIds(Main.OPTIONS.showIndirect, Main.OPTIONS.filterText);
this.list.setModButtons(getModConfigInfo(visibleModIds));
}

private List<ModConfigInfo> getModConfigInfo(List<String> modIds) {
List<ModConfigInfo> options = new LinkedList<>();
for (String modId : ModRegistry.getInstance().getVisibleModIds(true, "")) {
for (String modId : modIds) {
try {
Screen configScreen = ModRegistry.getInstance().getConfigScreen(modId, this);
if (configScreen != null) {
Expand All @@ -54,6 +60,6 @@ private ModConfigInfo[] getAllModConfigOptions() {
Main.LOGGER.error("Error creating Settings screen from mod " + modId, e);
}
}
return options.toArray(new ModConfigInfo[0]);
return options;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package se.icus.mag.modsettings.gui.widget;

import com.google.common.collect.ImmutableList;
import java.util.List;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.Selectable;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.ElementListWidget;
import net.minecraft.text.Text;

import java.util.List;
import se.icus.mag.modsettings.gui.ModConfigInfo;

public class ModListWidget extends ElementListWidget<ModListWidget.Entry> {
Expand All @@ -30,9 +29,10 @@ protected int getScrollbarPositionX() {
return super.getScrollbarPositionX() + 32;
}

public void addAll(ModConfigInfo[] options) {
for (int i = 0; i < options.length; i += 2) {
addEntry(new ModEntry(options[i], i < options.length - 1 ? options[i + 1] : null));
public void setModButtons(List<ModConfigInfo> options) {
clearEntries();
for (int i = 0; i < options.size(); i += 2) {
addEntry(new ModEntry(options.get(i), i < options.size() - 1 ? options.get(i + 1) : null));
}
}

Expand Down Expand Up @@ -70,6 +70,6 @@ public void render(DrawContext context, int index, int y, int x, int entryWidth,
}
}

public static abstract class Entry extends ElementListWidget.Entry<Entry> {
public abstract static class Entry extends ElementListWidget.Entry<Entry> {
}
}

0 comments on commit 7106c41

Please sign in to comment.