personthecat.catlib.client.gui.LibErrorMenu Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of catlib-quilt Show documentation
Show all versions of catlib-quilt Show documentation
Utilities for serialization, commands, noise generation, IO, and some new data types.
The newest version!
package personthecat.catlib.client.gui;
import net.minecraft.class_124;
import net.minecraft.class_2554;
import net.minecraft.class_2561;
import net.minecraft.class_2583;
import net.minecraft.class_2585;
import net.minecraft.class_2588;
import net.minecraft.class_310;
import net.minecraft.class_339;
import net.minecraft.class_364;
import net.minecraft.class_4185;
import net.minecraft.class_437;
import net.minecraft.class_4587;
import net.minecraft.network.chat.*;
import org.jetbrains.annotations.Nullable;
import personthecat.catlib.data.ModDescriptor;
import personthecat.catlib.data.collections.MultiValueHashMap;
import personthecat.catlib.data.collections.MultiValueMap;
import personthecat.catlib.event.error.LibErrorContext;
import personthecat.catlib.exception.FormattedException;
import java.util.*;
public class LibErrorMenu extends LibMenu {
private static final int PADDING = 6;
private static final int MAX_SIZE = 756;
private static final int MID_SIZE = 596;
private final Map options;
private final MultiValueMap errors;
private final Set fatal;
private final List keys;
private final int[] screens;
private @Nullable CategorizedList current;
private @Nullable CategorizedList mods;
private int page;
public LibErrorMenu(@Nullable final class_437 parent) {
super(parent, new class_2588("catlib.errorMenu.numErrors", LibErrorContext.numMods()));
this.options = new HashMap<>();
this.errors = new MultiValueHashMap<>();
this.fatal = new HashSet<>();
this.screens = new int[LibErrorContext.numMods()];
this.keys = LibErrorContext.getMods();
this.current = null;
this.mods = null;
this.page = 0;
this.loadErrors();
}
private void loadErrors() {
this.errors.putAll(LibErrorContext.getCommon());
LibErrorContext.getFatal().forEach((m, l) -> l.forEach(e -> {
this.errors.add(m, e);
this.fatal.add(e);
}));
Arrays.fill(this.screens, 0);
}
@Override
protected void method_25426() {
super.method_25426();
this.updateButtons();
this.rebuildOptions();
this.current = this.options.get(this.keys.get(this.page));
this.mods = new CategorizedList(this, this.getModMenuLeft(), this.getModMenuRight(), this.createModButtons());
this.mods.deselectAll();
this.mods.selectButton(this.page);
if (this.current != null) {
this.method_37063(this.current);
}
this.method_37063(this.mods);
this.method_25395(this.current);
}
private void updateButtons() {
if (this.keys.size() == 1) {
this.previous.field_22763 = false;
this.next.field_22763 = false;
}
}
private void rebuildOptions() {
this.options.clear();
int page = 0;
for (final Map.Entry> entry : this.errors.entrySet()) {
final MultiValueMap sorted = sortExceptions(entry.getValue());
final CategorizedList list = new CategorizedList(this, this.getErrorMenuLeft(), this.getErrorMenuRight(), createErrorButtons(page, sorted));
this.options.put(entry.getKey(), list);
page++;
}
}
private int getModMenuLeft() {
return this.field_22789 < MAX_SIZE ? PADDING : this.field_22789 / 2 - (MID_SIZE / 2);
}
private int getModMenuRight() {
return this.field_22789 / 2;
}
private int getErrorMenuLeft() {
return this.field_22789 / 2 - PADDING;
}
private int getErrorMenuRight() {
return this.field_22789 < MAX_SIZE ? this.field_22789 : this.field_22789 / 2 + (MID_SIZE / 2);
}
private MultiValueMap createModButtons() {
final MultiValueMap buttons = new MultiValueHashMap<>();
final List widgets = new ArrayList<>();
for (final ModDescriptor mod : this.keys) {
widgets.add(CategorizedList.createButton(new class_2585(mod.getName()), b -> this.setMod(mod)));
}
buttons.put("catlib.errorMenu.mods", widgets);
return buttons;
}
private MultiValueMap sortExceptions(List exceptions) {
final MultiValueMap sorted = new MultiValueHashMap<>();
for (final FormattedException exception : exceptions) {
sorted.add(exception.getCategory(), exception);
}
return sorted;
}
private MultiValueMap createErrorButtons(int page, MultiValueMap sorted) {
final MultiValueMap buttons = new MultiValueHashMap<>();
for (final Map.Entry> entry : sorted.entrySet()) {
final List widgets = new ArrayList<>();
for (int j = 0; j < entry.getValue().size(); j++) {
final FormattedException e = entry.getValue().get(j);
final int screen = j;
class_2561 display = e.getDisplayMessage();
if (display instanceof class_2554 && this.fatal.contains(e)) {
display = ((class_2554) display).method_27696(class_2583.field_24360.method_10977(class_124.field_1061));
}
final class_2561 tooltip = e.getTooltip();
final class_4185.class_4241 onPress = b -> {
this.screens[page] = screen;
class_310.method_1551().method_1507(e.getDetailsScreen(this));
};
final class_4185.class_5316 onTooltip = tooltip == null ? class_4185.field_25035 :
(b, stack, x, y) -> this.method_25424(stack, tooltip, x, y);
widgets.add(CategorizedList.createButton(display, onPress, onTooltip));
}
buttons.put(entry.getKey(), widgets);
}
return buttons;
}
@Override
@SuppressWarnings("ConstantConditions")
protected void renderMenu(class_4587 stack, int x, int y, float partial) {
this.current.method_25394(stack, x, y, partial);
this.mods.method_25394(stack, x, y, partial);
}
@Override
@SuppressWarnings("ConstantConditions")
protected void renderDetails(class_4587 stack, int x, int y, float partial) {
super.renderDetails(stack, x, y, partial);
this.mods.renderTooltips(stack, x, y);
this.current.renderTooltips(stack, x, y);
}
public boolean hasPreviousError() {
final List list = this.getCurrentScreens();
int screen = this.screens[this.page];
return screen != decrementWrapping(screen, list.size());
}
public boolean hasNextError() {
final List list = this.getCurrentScreens();
int screen = this.screens[this.page];
return screen != incrementWrapping(screen, list.size());
}
@Nullable
public class_437 previousError() {
final List list = this.getCurrentScreens();
int screen = this.screens[this.page];
int previous = decrementWrapping(screen, list.size());
if (screen != previous) {
this.screens[this.page] = previous;
final FormattedException e = list.get(previous);
return e.getDetailsScreen(this);
}
return null;
}
@Nullable
public class_437 nextError() {
final List list = this.getCurrentScreens();
int screen = this.screens[this.page];
int next = incrementWrapping(screen, list.size());
if (screen != next) {
this.screens[this.page] = next;
final FormattedException e = list.get(next);
return e.getDetailsScreen(this);
}
return null;
}
private List getCurrentScreens() {
return this.errors.get(this.keys.get(this.page));
}
@Override
protected void onPrevious() {
this.page = decrementWrapping(this.page, this.keys.size());
this.updateMod(this.keys.get(this.page));
}
@Override
protected void onNext() {
this.page = incrementWrapping(this.page, this.keys.size());
this.updateMod(this.keys.get(this.page));
}
@Override
public void method_25419() {
if (LibErrorContext.isFatal()) {
class_310.method_1551().close();
}
LibErrorContext.dispose();
super.method_25419();
}
private static int decrementWrapping(int num, int max) {
return num <= 0 ? max - 1 : num - 1;
}
private static int incrementWrapping(int num, int max) {
return num >= max - 1 ? 0 : num + 1;
}
private void setMod(ModDescriptor mod) {
this.page = this.keys.indexOf(mod);
this.updateMod(mod);
}
@SuppressWarnings("unchecked")
private void updateMod(ModDescriptor mod) {
this.method_25396().remove(this.current);
this.current = this.options.get(mod);
((List) this.method_25396()).add(this.current);
this.method_25395(this.current);
if (this.mods != null) {
this.mods.deselectAll();
this.mods.selectButton(this.page);
}
}
}