Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
14 419
src/client/java/com/kodari/titlelayout/client/MenuLayout.java
package com.kodari.titlelayout.client;
import com.kodari.titlelayout.mixin.client.ScreenAccessor;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.screens.PauseScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.TitleScreen;
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.client.gui.components.events.GuiEventListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class MenuLayout {
private static final int UTILITY_BUTTON_SIZE = 20;
private static final int BUTTON_HEIGHT = 20;
static {
MenuLayoutConfig.load();
}
private MenuLayout() {
}
public static void applyTitleScreen(TitleScreen screen) {
if (MenuLayoutConfig.get().removeRealmsButton) {
AbstractWidget realmsButton = null;
for (GuiEventListener child : screen.children()) {
if (child instanceof AbstractWidget widget
&& widget.getMessage().getContents() instanceof TranslatableContents contents
&& contents.getKey().equals("menu.online")) {
realmsButton = widget;
break;
}
}
if (realmsButton != null) {
int realmsY = realmsButton.getY();
int rowSpacing = findNextActionRowSpacing(screen, realmsButton);
((ScreenAccessor) (Object) screen).titlelayout$invokeRemoveWidget(realmsButton);
if (rowSpacing > 0) {
shiftWidgetsBelow(screen, realmsY, rowSpacing);
}
}
}
if (MenuLayoutConfig.get().repositionUtilityButtons) {
moveUtilityRowBelowActions(screen);
}
}
public static void applyPauseScreen(PauseScreen screen) {
if (MenuLayoutConfig.get().repositionUtilityButtons) {
compactPauseActions(screen);
moveUtilityRowBelowActions(screen);
}
}
private static int findNextActionRowSpacing(Screen screen, AbstractWidget button) {
int nextActionY = Integer.MAX_VALUE;
for (GuiEventListener child : screen.children()) {
if (child instanceof AbstractWidget widget
&& widget != button
&& widget.getWidth() > UTILITY_BUTTON_SIZE
&& widget.getHeight() == BUTTON_HEIGHT
&& widget.getY() > button.getY()) {
nextActionY = Math.min(nextActionY, widget.getY());
}
}
return nextActionY == Integer.MAX_VALUE ? 0 : nextActionY - button.getY();
}
private static void shiftWidgetsBelow(Screen screen, int y, int amount) {
for (GuiEventListener child : screen.children()) {
if (child instanceof AbstractWidget widget
&& widget.getHeight() == BUTTON_HEIGHT
&& widget.getY() > y) {
widget.setY(widget.getY() - amount);
}
}
}
private static void compactPauseActions(Screen screen) {
List<AbstractWidget> utilityRow = findUtilityRow(screen);
if (utilityRow.isEmpty()) {
return;
}
int utilityRowY = utilityRow.getFirst().getY();
int firstLowerActionY = Integer.MAX_VALUE;
for (GuiEventListener child : screen.children()) {
if (child instanceof AbstractWidget widget
&& widget.getWidth() > UTILITY_BUTTON_SIZE
&& widget.getHeight() == BUTTON_HEIGHT
&& widget.getY() > utilityRowY) {
firstLowerActionY = Math.min(firstLowerActionY, widget.getY());
}
}
if (firstLowerActionY == Integer.MAX_VALUE) {
return;
}
int shift = firstLowerActionY - utilityRowY;
shiftWidgetsAtOrBelow(screen, firstLowerActionY, shift);
}
private static void shiftWidgetsAtOrBelow(Screen screen, int y, int amount) {
for (GuiEventListener child : screen.children()) {
if (child instanceof AbstractWidget widget
&& widget.getWidth() > UTILITY_BUTTON_SIZE
&& widget.getHeight() == BUTTON_HEIGHT
&& widget.getY() >= y) {
widget.setY(widget.getY() - amount);
}
}
}
private static void moveUtilityRowBelowActions(Screen screen) {
List<AbstractWidget> utilityRow = findUtilityRow(screen);
int actionBottom = findLastActionBottom(screen);
if (utilityRow.isEmpty() || actionBottom < 0) {
return;
}
int targetY = actionBottom + 4;
for (AbstractWidget widget : utilityRow) {
widget.setY(targetY);
}
}
private static List<AbstractWidget> findUtilityRow(Screen screen) {
Map<Integer, List<AbstractWidget>> rows = new HashMap<>();
for (GuiEventListener child : screen.children()) {
if (child instanceof AbstractWidget widget
&& widget.getWidth() == UTILITY_BUTTON_SIZE
&& widget.getHeight() == BUTTON_HEIGHT) {
rows.computeIfAbsent(widget.getY(), ignored -> new ArrayList<>()).add(widget);
}
}
List<AbstractWidget> largestRow = List.of();
for (List<AbstractWidget> row : rows.values()) {
if (row.size() > largestRow.size()) {
largestRow = row;
}
}
return largestRow.size() >= 2 ? largestRow : List.of();
}
private static int findLastActionBottom(Screen screen) {
int bottom = -1;
for (GuiEventListener child : screen.children()) {
if (child instanceof AbstractWidget widget
&& widget.getWidth() > UTILITY_BUTTON_SIZE
&& widget.getHeight() == BUTTON_HEIGHT) {
bottom = Math.max(bottom, widget.getY() + widget.getHeight());
}
}
return bottom;
}
}