Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ else if (path.equals("food_and_drinks") || path.equals("food_and_drink"))

public void onClose() {
releaseLock();
ChestConfigManager.setContainerColumns(9);
ChestConfigManager.getInstance().clearCurrentConfig();
ChestConfigManager.getInstance().getCurrentWhitelists().clear();

Expand Down Expand Up @@ -532,6 +533,52 @@ public static int slotKey(Slot slot) {
: realIndex(slot);
}

/**
* The open menu's own inventory: the container behind its first non-player slot, or null when the
* menu only shows the player inventory. Works for any menu type, not just vanilla's ChestMenu.
*/
public static net.minecraft.world.Container openContainer(AbstractContainerMenu menu) {
if (menu == null) return null;
for (Slot slot : menu.slots) {
if (!(slot.container instanceof net.minecraft.world.entity.player.Inventory)) return slot.container;
}
return null;
}

/**
* Grid width of a modded chest-like screen the editor can drive, or 0 when it can't. Vanilla screens
* are never matched here (they keep their explicit allow-list). A modded screen qualifies when every
* non-player slot belongs to one whitelist-capable container and those slots come first in the menu,
* in container order, laid out as one row-major grid of at least 27 cells: the editor addresses
* container slots by index (handler index == container index == grid cell), so anything else would
* draw and save lines on the wrong slots.
*/
public static int moddedGridColumns(AbstractContainerScreen<?> screen) {
if (screen.getClass().getName().startsWith("net.minecraft.")) return 0;
AbstractContainerMenu menu = screen.getMenu();
net.minecraft.world.Container container = openContainer(menu);
if (!(container instanceof IWhitelistProvider)) return 0;

List<Slot> gridSlots = new ArrayList<>();
for (Slot slot : menu.slots) {
if (slot.container instanceof net.minecraft.world.entity.player.Inventory) continue;
if (slot.container != container) return 0;
gridSlots.add(slot);
}
int n = gridSlots.size();
if (n < 27 || n >= ChestConfigManager.PLAYER_KEY_OFFSET) return 0;

int[] xs = new int[n];
int[] ys = new int[n];
for (int i = 0; i < n; i++) {
Slot slot = gridSlots.get(i);
if (slot.index != i || slot.getContainerSlot() != i) return 0;
xs[i] = slot.x;
ys[i] = slot.y;
}
return ChestConfigManager.detectGridColumns(xs, ys);
}

/** True if a slot belongs to the player inventory namespace (used for the mutual-exclusion dimming). */
public static boolean isPlayerSlot(Slot slot) {
return slot.container instanceof net.minecraft.world.entity.player.Inventory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public int calculateAction(Slot slot, double mouseX, double mouseY) {
public String calculateTraceStep(Slot hoverSlot, double mouseX, double mouseY) {
int guiX = accessor.getX();
int guiY = accessor.getY();
// Grid width of the hovered slot's namespace: 9 for the player inventory, the container's own
// width otherwise (wider than 9 for some modded chests).
int cols = ChestConfigManager.gridColumns(ChestSeparatorsEditor.slotKey(hoverSlot));

if (session.lockedTraceAxis == 1) {
// A pull of more than 7.5 px away from the locked horizontal axis transitions to vertical rail.
Expand All @@ -83,15 +86,15 @@ public String calculateTraceStep(Slot hoverSlot, double mouseX, double mouseY) {
session.lockedTraceAction =
(distLeft <= distRight) ? ChestConfigManager.ACTION_LEFT : ChestConfigManager.ACTION_RIGHT;
session.lockedLineCoord = (distLeft <= distRight) ? (guiX + hoverSlot.x) : (guiX + hoverSlot.x + 16);
session.lockedTraceRowCol = ChestSeparatorsEditor.realIndex(hoverSlot) % 9;
session.lockedTraceRowCol = ChestSeparatorsEditor.realIndex(hoverSlot) % cols;
return ChestSeparatorsEditor.slotKey(hoverSlot) + "_" + session.lockedTraceAction;
} else {
// Armor/offhand are isolated non-grid cells: keep the locked action but paint it on
// the hovered cell itself instead of projecting along a meaningless grid row/col.
if (ChestConfigManager.isNonGridInventoryKey(ChestSeparatorsEditor.slotKey(hoverSlot)))
return ChestSeparatorsEditor.slotKey(hoverSlot) + "_" + session.lockedTraceAction;
int col = ChestSeparatorsEditor.realIndex(hoverSlot) % 9;
int correctSlotIndex = (session.lockedTraceRowCol * 9) + col;
int col = ChestSeparatorsEditor.realIndex(hoverSlot) % cols;
int correctSlotIndex = (session.lockedTraceRowCol * cols) + col;
return (correctSlotIndex
+ (ChestSeparatorsEditor.isPlayerSlot(hoverSlot)
? ChestConfigManager.PLAYER_KEY_OFFSET
Expand All @@ -108,15 +111,15 @@ public String calculateTraceStep(Slot hoverSlot, double mouseX, double mouseY) {
session.lockedTraceAction =
(distTop <= distBottom) ? ChestConfigManager.ACTION_TOP : ChestConfigManager.ACTION_BOTTOM;
session.lockedLineCoord = (distTop <= distBottom) ? (guiY + hoverSlot.y) : (guiY + hoverSlot.y + 16);
session.lockedTraceRowCol = ChestSeparatorsEditor.realIndex(hoverSlot) / 9;
session.lockedTraceRowCol = ChestSeparatorsEditor.realIndex(hoverSlot) / cols;
return ChestSeparatorsEditor.slotKey(hoverSlot) + "_" + session.lockedTraceAction;
} else {
// Armor/offhand are isolated non-grid cells: keep the locked action but paint it on
// the hovered cell itself instead of projecting along a meaningless grid row/col.
if (ChestConfigManager.isNonGridInventoryKey(ChestSeparatorsEditor.slotKey(hoverSlot)))
return ChestSeparatorsEditor.slotKey(hoverSlot) + "_" + session.lockedTraceAction;
int row = ChestSeparatorsEditor.realIndex(hoverSlot) / 9;
int correctSlotIndex = (row * 9) + session.lockedTraceRowCol;
int row = ChestSeparatorsEditor.realIndex(hoverSlot) / cols;
int correctSlotIndex = (row * cols) + session.lockedTraceRowCol;
return (correctSlotIndex
+ (ChestSeparatorsEditor.isPlayerSlot(hoverSlot)
? ChestConfigManager.PLAYER_KEY_OFFSET
Expand All @@ -130,18 +133,20 @@ public String calculateTraceStep(Slot hoverSlot, double mouseX, double mouseY) {
}

public boolean isDraggingRectangle(double mouseX, double mouseY) {
// A drag stays inside its start slot's namespace, so one grid width covers both ends.
int cols = ChestConfigManager.gridColumns(ChestSeparatorsEditor.slotKey(session.dragStartSlot));
int minRow = Math.min(
ChestSeparatorsEditor.realIndex(session.dragStartSlot) / 9,
ChestSeparatorsEditor.realIndex(session.dragCurrentSlot) / 9);
ChestSeparatorsEditor.realIndex(session.dragStartSlot) / cols,
ChestSeparatorsEditor.realIndex(session.dragCurrentSlot) / cols);
int maxRow = Math.max(
ChestSeparatorsEditor.realIndex(session.dragStartSlot) / 9,
ChestSeparatorsEditor.realIndex(session.dragCurrentSlot) / 9);
ChestSeparatorsEditor.realIndex(session.dragStartSlot) / cols,
ChestSeparatorsEditor.realIndex(session.dragCurrentSlot) / cols);
int minCol = Math.min(
ChestSeparatorsEditor.realIndex(session.dragStartSlot) % 9,
ChestSeparatorsEditor.realIndex(session.dragCurrentSlot) % 9);
ChestSeparatorsEditor.realIndex(session.dragStartSlot) % cols,
ChestSeparatorsEditor.realIndex(session.dragCurrentSlot) % cols);
int maxCol = Math.max(
ChestSeparatorsEditor.realIndex(session.dragStartSlot) % 9,
ChestSeparatorsEditor.realIndex(session.dragCurrentSlot) % 9);
ChestSeparatorsEditor.realIndex(session.dragStartSlot) % cols,
ChestSeparatorsEditor.realIndex(session.dragCurrentSlot) % cols);

// Spanning multiple rows AND columns is always a 2-D rectangle.
if (minRow != maxRow && minCol != maxCol) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minecraft.client.input.KeyEvent;
import net.minecraft.world.item.Item;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.inventory.ChestMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.ChatFormatting;
import net.minecraft.resources.Identifier;
Expand Down Expand Up @@ -242,12 +241,10 @@ else if (input.key() == GLFW.GLFW_KEY_V) {
editor.sendWhitelistToServer();

if (Minecraft.getInstance().player != null
&& Minecraft.getInstance().player.containerMenu
instanceof ChestMenu genericHandler) {
if (genericHandler.getContainer() instanceof IWhitelistProvider provider) {
provider.setWhitelists(
ChestConfigManager.getInstance().getCurrentWhitelists());
}
&& ChestSeparatorsEditor.openContainer(
Minecraft.getInstance().player.containerMenu)
instanceof IWhitelistProvider provider) {
provider.setWhitelists(ChestConfigManager.getInstance().getCurrentWhitelists());
}
editor.showStatus(
Component.translatable("message.chestseparators.filters_pasted"), ChatFormatting.GREEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ public class EditorLayout {
public int bH = 22;
public int swatchSize = 12;

/**
* True when the toolbar has no room above the GUI (a GUI too tall for the window, e.g. a 9-row modded
* chest), so it stacks vertically in a column just left of the GUI at {@link #toolbarX} instead.
*/
public boolean toolbarBeside;
public int toolbarX;

// Thickness of the toolbar (20px icons + 4px padding each side) and its gap to the GUI; these mirror
// EditorRenderer#renderToolbar so the column reserved here is exactly the one it draws in.
private static final int TOOLBAR_THICKNESS = 28;
private static final int TOOLBAR_GAP_TO_GUI = 3;
// Narrowest the left dock may get when squeezed beside a wide GUI, and the room its rule tabs need.
private static final int MIN_LIST_W = 80;
private static final int LIST_TAB_ROOM = 22;

// Calculated X/Y positions for main panels
public int mainX;
public int mainY;
Expand Down Expand Up @@ -140,11 +155,29 @@ public void update(Screen screen, HandledScreenAccessor accessor, int customSide
.getActiveEffects()
.isEmpty();

// The toolbar normally sits above the GUI. Same test as EditorRenderer#renderToolbar: when that would
// start above the screen top, it moves into a column left of the GUI, which the left-side panels
// below then keep clear of.
int creativeLift =
screen instanceof net.minecraft.client.gui.screens.inventory.CreativeModeInventoryScreen ? 30 : 0;
this.toolbarBeside = io.github.marcsanzdev.chestseparators.config.GlobalChestConfig.instance.showEditButtons
&& this.guiY - TOOLBAR_THICKNESS - TOOLBAR_GAP_TO_GUI - creativeLift < 0;
this.toolbarX = this.guiX - TOOLBAR_GAP_TO_GUI - TOOLBAR_THICKNESS;
int leftEdge = this.toolbarBeside ? this.toolbarX : this.guiX;

// Mirror the exact separation the left dock leaves, so the right dock is not visually tighter.
int dockGap = this.guiX - this.mainX + this.gap;
this.listW = 120;
if (!bookOpen) {
this.listOnRight = false;
this.listX = this.mainX - this.listW - this.gap;
// Docks off the centre panel's left edge, unless the GUI (or the toolbar column) reaches further
// left: a modded chest wider than the centre panel would otherwise sit under the dock. When the
// window is too narrow for the full width there, narrow the panel (keeping its rule tabs on
// screen) rather than cover slots; below the minimum it runs off the screen edge instead.
int dockRight = Math.min(this.mainX, leftEdge);
int room = dockRight - this.gap - LIST_TAB_ROOM;
if (dockRight < this.mainX && room < this.listW) this.listW = Math.max(MIN_LIST_W, room);
this.listX = dockRight - this.listW - this.gap;
} else if (hasPotion) {
this.listOnRight = false;
this.listX = Math.max(22, this.guiX - RECIPE_BOOK_WIDTH - this.listW - this.gap);
Expand All @@ -154,7 +187,8 @@ public void update(Screen screen, HandledScreenAccessor accessor, int customSide
}
this.listY = this.mainY;
this.listH = this.mainH;
this.rightX = this.mainX + this.mainW + this.gap;
// Right of the centre panel, or of the GUI itself when a modded chest is wider than that panel.
this.rightX = Math.max(this.mainX + this.mainW, this.guiX + this.bgWidth) + this.gap;

// 3. Grid area
this.gridX = this.mainX + 10;
Expand All @@ -174,7 +208,7 @@ public void update(Screen screen, HandledScreenAccessor accessor, int customSide
this.lsbH = this.listViewH - 1;

// 6. Draw Lines Sidebar
this.sidebarX = this.guiX - this.sidebarWidth - 4;
this.sidebarX = leftEdge - this.sidebarWidth - 4;
this.paletteBoxY = this.guiY + customSidebarYOffset + 48;
int contentX = this.sidebarX + 7;
this.paletteY = this.paletteBoxY + 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,22 @@ private void renderToolbar(GuiGraphicsExtractor context, int hoverX, int hoverY,
panelY -= 30;
}

// A GUI too tall for the window leaves no room above it (the bar would start off the top of the
// screen), so the bar turns vertical in the column EditorLayout keeps free left of the GUI.
boolean vertical = layout.toolbarBeside;
if (vertical) {
panelH = panelW;
panelW = btn + 2 * pad;
panelX = layout.toolbarX;
panelY = accessor.getY();
}

UiTheme.panel(context, panelX, panelY, panelW, panelH);

for (int i = 0; i < n; i++) {
var b = vis.get(i);
b.x = panelX + pad + i * (btn + gap);
b.y = panelY + pad;
b.x = panelX + pad + (vertical ? 0 : i * (btn + gap));
b.y = panelY + pad + (vertical ? i * (btn + gap) : 0);
b.render(context, hoverX, hoverY, delta);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ boolean onMouseClicked(double mouseX, double mouseY, int button) {
session.tracePath.clear();
session.tracePath.add(ChestSeparatorsEditor.slotKey(slot) + "_" + action);
session.lockedTraceAction = action;
int cols = ChestConfigManager.gridColumns(ChestSeparatorsEditor.slotKey(slot));
if (action == ChestConfigManager.ACTION_TOP || action == ChestConfigManager.ACTION_BOTTOM) {
session.lockedTraceAxis = 1;
session.lockedTraceRowCol = slot.getContainerSlot() / 9;
session.lockedTraceRowCol = slot.getContainerSlot() / cols;
session.lockedLineCoord = (action == ChestConfigManager.ACTION_TOP)
? (layout.guiY + slot.y)
: (layout.guiY + slot.y + 16);
} else {
session.lockedTraceAxis = 2;
session.lockedTraceRowCol = slot.getContainerSlot() % 9;
session.lockedTraceRowCol = slot.getContainerSlot() % cols;
session.lockedLineCoord = (action == ChestConfigManager.ACTION_LEFT)
? (layout.guiX + slot.x)
: (layout.guiX + slot.x + 16);
Expand Down
Loading