diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/ChestSeparatorsEditor.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/ChestSeparatorsEditor.java index f20ce9a..b2426d5 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/ChestSeparatorsEditor.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/ChestSeparatorsEditor.java @@ -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(); @@ -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 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; diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorGeometry.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorGeometry.java index 97fdcf4..e7ec164 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorGeometry.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorGeometry.java @@ -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. @@ -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 @@ -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 @@ -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; diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorInputHandler.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorInputHandler.java index cfb5df5..211eb19 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorInputHandler.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorInputHandler.java @@ -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; @@ -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); diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorLayout.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorLayout.java index 16478e6..c49ec1f 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorLayout.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorLayout.java @@ -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; @@ -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); @@ -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; @@ -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; diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorRenderer.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorRenderer.java index f6f1c80..711816e 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorRenderer.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/EditorRenderer.java @@ -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); } } diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/DrawLinesClickHandler.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/DrawLinesClickHandler.java index f4472f3..42fe8ad 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/DrawLinesClickHandler.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/DrawLinesClickHandler.java @@ -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); diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorDragCommitter.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorDragCommitter.java index 6a8ee68..084656e 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorDragCommitter.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorDragCommitter.java @@ -31,6 +31,8 @@ void commit(double mouseX, double mouseY) { boolean playerNs = ChestSeparatorsEditor.isPlayerSlot(session.dragStartSlot); int off = playerNs ? ChestConfigManager.PLAYER_KEY_OFFSET : 0; int nsCount = editor.geometry.getNamespaceSlotCount(playerNs); + // The namespace's grid width: 9 for the inventory, the container's own width otherwise. + int cols = ChestConfigManager.gridColumns(off); if (session.currentTab == EditorSessionData.TAB_COMBO) { int colorToPaint = screen.getCurrentSelectedComboColorValue(); @@ -88,17 +90,17 @@ void commit(double mouseX, double mouseY) { // non-grid cell always gets its full box; grid cells ignore non-grid neighbours. boolean nonGrid = ChestConfigManager.isNonGridInventoryKey(slotIdx); boolean hasTop = !nonGrid - && traceSlots.contains(slotIdx - 9) - && !ChestConfigManager.isNonGridInventoryKey(slotIdx - 9); + && traceSlots.contains(slotIdx - cols) + && !ChestConfigManager.isNonGridInventoryKey(slotIdx - cols); boolean hasBottom = !nonGrid - && traceSlots.contains(slotIdx + 9) - && !ChestConfigManager.isNonGridInventoryKey(slotIdx + 9); + && traceSlots.contains(slotIdx + cols) + && !ChestConfigManager.isNonGridInventoryKey(slotIdx + cols); boolean hasLeft = !nonGrid - && (slotIdx % 9 != 0) + && (ChestConfigManager.gridCol(slotIdx) != 0) && traceSlots.contains(slotIdx - 1) && !ChestConfigManager.isNonGridInventoryKey(slotIdx - 1); boolean hasRight = !nonGrid - && (slotIdx % 9 != 8) + && (ChestConfigManager.gridCol(slotIdx) != cols - 1) && traceSlots.contains(slotIdx + 1) && !ChestConfigManager.isNonGridInventoryKey(slotIdx + 1); @@ -148,17 +150,17 @@ void commit(double mouseX, double mouseY) { if (session.lineToolMode == 0) { // Pencil Area boolean erase = explicitEraser || session.isDragModeErasing; - int sRow = session.dragStartSlot.getContainerSlot() / 9; - int sCol = session.dragStartSlot.getContainerSlot() % 9; - int cRow = session.dragCurrentSlot.getContainerSlot() / 9; - int cCol = session.dragCurrentSlot.getContainerSlot() % 9; + int sRow = session.dragStartSlot.getContainerSlot() / cols; + int sCol = session.dragStartSlot.getContainerSlot() % cols; + int cRow = session.dragCurrentSlot.getContainerSlot() / cols; + int cCol = session.dragCurrentSlot.getContainerSlot() % cols; int minRow = Math.min(sRow, cRow); int maxRow = Math.max(sRow, cRow); int minCol = Math.min(sCol, cCol); int maxCol = Math.max(sCol, cCol); - // Armor/offhand are isolated (non-grid) cells outside the 9-wide grid: the grid + // Armor/offhand are isolated (non-grid) cells outside the inventory grid: the grid // rederivation below can't address them (their partial row is clamped back into the // main inventory), which is why dragging lines over armor drew nothing on the armor // and painted phantom lines mid-inventory. Give each covered non-grid slot its full @@ -178,8 +180,8 @@ void commit(double mouseX, double mouseY) { if (ChestSeparatorsEditor.isPlayerSlot(slot) != playerNs) continue; int key = ChestSeparatorsEditor.slotKey(slot); if (!ChestConfigManager.isNonGridInventoryKey(key)) continue; - int r = slot.getContainerSlot() / 9; - int c = slot.getContainerSlot() % 9; + int r = slot.getContainerSlot() / cols; + int c = slot.getContainerSlot() % cols; if (r >= minRow && r <= maxRow && c >= minCol && c <= maxCol) { if (!isRect && key == startKey) { if (erase) manager.removeAction(key, session.currentDragAction); @@ -196,12 +198,12 @@ void commit(double mouseX, double mouseY) { } } - // Grid rederivation only covers the true 9-wide grid: the player namespace's grid is + // Grid rederivation only covers the true slot grid: the player namespace's grid is // its first 36 slots (everything past that is the non-grid row handled above), while // the chest namespace is fully grid. Skip entirely when the drag lies wholly in the // non-grid row. int gridSlotCount = playerNs ? 36 : nsCount; - int gridRows = gridSlotCount / 9; + int gridRows = gridSlotCount / cols; if (minRow < gridRows) { int yTopRaw = minRow * 2; int yBotRaw = maxRow * 2 + 1; @@ -238,8 +240,8 @@ else if (session.currentDragAction == ChestConfigManager.ACTION_LEFT && cCol < s int maxRows = gridRows; yTopExp = Math.max(0, Math.min(maxRows * 2 - 1, yTopExp)); yBotExp = Math.max(0, Math.min(maxRows * 2 - 1, yBotExp)); - xLeftExp = Math.max(0, Math.min(17, xLeftExp)); - xRightExp = Math.max(0, Math.min(17, xRightExp)); + xLeftExp = Math.max(0, Math.min(cols * 2 - 1, xLeftExp)); + xRightExp = Math.max(0, Math.min(cols * 2 - 1, xRightExp)); boolean isRectangle = editor.geometry.isDraggingRectangle(mouseX, mouseY); @@ -247,7 +249,7 @@ else if (session.currentDragAction == ChestConfigManager.ACTION_LEFT && cCol < s if (erase) { for (int r = Math.max(0, minRow - 1); r <= maxRow + 1; r++) { for (int c = Math.max(0, minCol - 1); c <= maxCol + 1; c++) { - int slotIdx = r * 9 + c; + int slotIdx = r * cols + c; if (slotIdx >= gridSlotCount) continue; int key = slotIdx + off; @@ -280,10 +282,10 @@ else if (session.currentDragAction == ChestConfigManager.ACTION_LEFT && cCol < s int botRow = yBotExp / 2; for (int c = fillMinCol; c <= fillMaxCol; c++) { - if (topRow * 9 + c < gridSlotCount) - manager.paintAction(topRow * 9 + c + off, topAction, colorToPaint); - if (botRow * 9 + c < gridSlotCount) - manager.paintAction(botRow * 9 + c + off, botAction, colorToPaint); + if (topRow * cols + c < gridSlotCount) + manager.paintAction(topRow * cols + c + off, topAction, colorToPaint); + if (botRow * cols + c < gridSlotCount) + manager.paintAction(botRow * cols + c + off, botAction, colorToPaint); } int fillMinRow = (yTopExp + 1) / 2; @@ -298,18 +300,18 @@ else if (session.currentDragAction == ChestConfigManager.ACTION_LEFT && cCol < s int rightCol = xRightExp / 2; for (int r = fillMinRow; r <= fillMaxRow; r++) { - if (r * 9 + leftCol < gridSlotCount) - manager.paintAction(r * 9 + leftCol + off, leftAction, colorToPaint); - if (r * 9 + rightCol < gridSlotCount) - manager.paintAction(r * 9 + rightCol + off, rightAction, colorToPaint); + if (r * cols + leftCol < gridSlotCount) + manager.paintAction(r * cols + leftCol + off, leftAction, colorToPaint); + if (r * cols + rightCol < gridSlotCount) + manager.paintAction(r * cols + rightCol + off, rightAction, colorToPaint); } } changeMade = true; } else { // 1D Line for (int r = minRow; r <= Math.min(maxRow, gridRows - 1); r++) { for (int c = minCol; c <= maxCol; c++) { - if (erase) manager.removeAction(r * 9 + c + off, session.currentDragAction); - else manager.paintAction(r * 9 + c + off, session.currentDragAction, colorToPaint); + if (erase) manager.removeAction(r * cols + c + off, session.currentDragAction); + else manager.paintAction(r * cols + c + off, session.currentDragAction, colorToPaint); } } changeMade = true; diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorDragInput.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorDragInput.java index 74ce551..f98036d 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorDragInput.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorDragInput.java @@ -48,7 +48,7 @@ boolean onDrag(double mouseX, double mouseY) { if (toolMode == 1) { // Armor/offhand are isolated non-grid cells with no grid neighbours. The // vertex-connection logic below projects segments along rows/columns via grid - // math (index%9, index/9, row*9+col), which is meaningless for them. But we + // math (column, row, row*width+col), which is meaningless for them. But we // still honour the axis lock via calculateTraceStep (patched to paint the // locked edge on the hovered cell itself, without grid projection) so a // vertical drag paints only the left/right edge and a horizontal one only @@ -81,7 +81,7 @@ boolean onDrag(double mouseX, double mouseY) { double cRelY = mouseY - (guiY + currSlot.y); // Calculate vertices (P1 and P2) of the previous line - int c1 = prevSlotIdx % 9, r1 = prevSlotIdx / 9; + int c1 = ChestConfigManager.gridCol(prevSlotIdx), r1 = ChestConfigManager.gridRow(prevSlotIdx); int v1x1 = c1, v1y1 = r1, v1x2 = c1, v1y2 = r1; if (prevAction == ChestConfigManager.ACTION_TOP) { v1x2 = c1 + 1; @@ -98,7 +98,7 @@ boolean onDrag(double mouseX, double mouseY) { } // Calculate vertices of the current line - int c2 = currSlotIdx % 9, r2 = currSlotIdx / 9; + int c2 = ChestConfigManager.gridCol(currSlotIdx), r2 = ChestConfigManager.gridRow(currSlotIdx); int v2x1 = c2, v2y1 = r2, v2x2 = c2, v2y2 = r2; if (currAction == ChestConfigManager.ACTION_TOP) { v2x2 = c2 + 1; @@ -136,13 +136,13 @@ boolean onDrag(double mouseX, double mouseY) { if (prevAction == ChestConfigManager.ACTION_TOP || prevAction == ChestConfigManager.ACTION_BOTTOM) { session.lockedTraceAxis = 1; - session.lockedTraceRowCol = prevSlotIdx / 9; + session.lockedTraceRowCol = ChestConfigManager.gridRow(prevSlotIdx); session.lockedLineCoord = (prevAction == ChestConfigManager.ACTION_TOP) ? (guiY + prevSlot.y) : (guiY + prevSlot.y + 16); } else { session.lockedTraceAxis = 2; - session.lockedTraceRowCol = prevSlotIdx % 9; + session.lockedTraceRowCol = ChestConfigManager.gridCol(prevSlotIdx); session.lockedLineCoord = (prevAction == ChestConfigManager.ACTION_LEFT) ? (guiX + prevSlot.x) : (guiX + prevSlot.x + 16); @@ -168,7 +168,7 @@ boolean onDrag(double mouseX, double mouseY) { double rRelX = mouseX - (guiX + rawSlot.x); double rRelY = mouseY - (guiY + rawSlot.y); - int c1 = lastSlotIdx % 9, r1 = lastSlotIdx / 9; + int c1 = ChestConfigManager.gridCol(lastSlotIdx), r1 = ChestConfigManager.gridRow(lastSlotIdx); int v1x1 = c1, v1y1 = r1, v1x2 = c1, v1y2 = r1; if (lastAction == ChestConfigManager.ACTION_TOP) { v1x2 = c1 + 1; @@ -184,7 +184,7 @@ boolean onDrag(double mouseX, double mouseY) { v1y2 = r1 + 1; } - int c2 = rawSlotIdx % 9, r2 = rawSlotIdx / 9; + int c2 = ChestConfigManager.gridCol(rawSlotIdx), r2 = ChestConfigManager.gridRow(rawSlotIdx); int v2x1 = c2, v2y1 = r2, v2x2 = c2, v2y2 = r2; if (rawAction == ChestConfigManager.ACTION_TOP) { v2x2 = c2 + 1; diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorPreviewRenderer.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorPreviewRenderer.java index e0c9486..a628092 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorPreviewRenderer.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/client/ui/screens/SeparatorPreviewRenderer.java @@ -137,6 +137,9 @@ void renderDragPreview(GuiGraphicsExtractor context, int mouseX, int mouseY) { int guiX = layout.guiX; int guiY = layout.guiY; int tabMode = session.currentTab; + // Chest namespace from here on (key 0 is its first slot), so the grid width is the container's, + // which is wider than 9 for some modded chests. + int cols = ChestConfigManager.gridColumns(0); int colorVal = 0; boolean explicitEraser = false; @@ -163,10 +166,10 @@ void renderDragPreview(GuiGraphicsExtractor context, int mouseX, int mouseY) { if (tabMode == 1) { if (tMode == 0) { - int startRow = session.dragStartSlot.getContainerSlot() / 9; - int startCol = session.dragStartSlot.getContainerSlot() % 9; - int currRow = session.dragCurrentSlot.getContainerSlot() / 9; - int currCol = session.dragCurrentSlot.getContainerSlot() % 9; + int startRow = session.dragStartSlot.getContainerSlot() / cols; + int startCol = session.dragStartSlot.getContainerSlot() % cols; + int currRow = session.dragCurrentSlot.getContainerSlot() / cols; + int currCol = session.dragCurrentSlot.getContainerSlot() % cols; int minRow = Math.min(startRow, currRow); int maxRow = Math.max(startRow, currRow); int minCol = Math.min(startCol, currCol); @@ -177,8 +180,8 @@ void renderDragPreview(GuiGraphicsExtractor context, int mouseX, int mouseY) { // Without this, raw getIndex() of player slots (0-35) collides with chest indices and the // chest preview gets mirrored onto the inventory. if (ChestSeparatorsEditor.isPlayerSlot(slot)) continue; - int r = slot.getContainerSlot() / 9; - int c = slot.getContainerSlot() % 9; + int r = slot.getContainerSlot() / cols; + int c = slot.getContainerSlot() % cols; if (r >= minRow && r <= maxRow && c >= minCol && c <= maxCol) { if (erase && ChestConfigManager.getInstance() @@ -200,10 +203,10 @@ void renderDragPreview(GuiGraphicsExtractor context, int mouseX, int mouseY) { } } else if (tabMode == 2) { if (tMode == 0) { - int startRow = session.dragStartSlot.getContainerSlot() / 9; - int startCol = session.dragStartSlot.getContainerSlot() % 9; - int currRow = session.dragCurrentSlot.getContainerSlot() / 9; - int currCol = session.dragCurrentSlot.getContainerSlot() % 9; + int startRow = session.dragStartSlot.getContainerSlot() / cols; + int startCol = session.dragStartSlot.getContainerSlot() % cols; + int currRow = session.dragCurrentSlot.getContainerSlot() / cols; + int currCol = session.dragCurrentSlot.getContainerSlot() % cols; int minRow = Math.min(startRow, currRow); int maxRow = Math.max(startRow, currRow); int minCol = Math.min(startCol, currCol); @@ -213,8 +216,8 @@ void renderDragPreview(GuiGraphicsExtractor context, int mouseX, int mouseY) { // Chest namespace only; player drags go through renderPlayerDragPreview. if (ChestSeparatorsEditor.isPlayerSlot(slot)) continue; int slotIdx = slot.getContainerSlot(); - int r = slotIdx / 9; - int c = slotIdx % 9; + int r = slotIdx / cols; + int c = slotIdx % cols; if (r >= minRow && r <= maxRow && c >= minCol && c <= maxCol) { int x = guiX + slot.x; int y = guiY + slot.y; @@ -252,10 +255,10 @@ void renderDragPreview(GuiGraphicsExtractor context, int mouseX, int mouseY) { Slot slot = editor.accessor.getHandler().getSlot(slotIdx); int x = guiX + slot.x; int y = guiY + slot.y; - boolean hasTop = traceSlots.contains(slotIdx - 9); - boolean hasBottom = traceSlots.contains(slotIdx + 9); - boolean hasLeft = (slotIdx % 9 != 0) && traceSlots.contains(slotIdx - 1); - boolean hasRight = (slotIdx % 9 != 8) && traceSlots.contains(slotIdx + 1); + boolean hasTop = traceSlots.contains(slotIdx - cols); + boolean hasBottom = traceSlots.contains(slotIdx + cols); + boolean hasLeft = (slotIdx % cols != 0) && traceSlots.contains(slotIdx - 1); + boolean hasRight = (slotIdx % cols != cols - 1) && traceSlots.contains(slotIdx + 1); if (!erase || ChestConfigManager.getInstance().getColor(slotIdx, ChestConfigManager.ACTION_BG) != 0) context.fill(x, y, x + 16, y + 16, colorBg); @@ -282,10 +285,10 @@ void renderDragPreview(GuiGraphicsExtractor context, int mouseX, int mouseY) { } } else { if (tMode == 0) { - int sRow = session.dragStartSlot.getContainerSlot() / 9; - int sCol = session.dragStartSlot.getContainerSlot() % 9; - int cRow = session.dragCurrentSlot.getContainerSlot() / 9; - int cCol = session.dragCurrentSlot.getContainerSlot() % 9; + int sRow = session.dragStartSlot.getContainerSlot() / cols; + int sCol = session.dragStartSlot.getContainerSlot() % cols; + int cRow = session.dragCurrentSlot.getContainerSlot() / cols; + int cCol = session.dragCurrentSlot.getContainerSlot() % cols; int minRow = Math.min(sRow, cRow); int maxRow = Math.max(sRow, cRow); int minCol = Math.min(sCol, cCol); @@ -321,16 +324,16 @@ else if (session.currentDragAction == ChestConfigManager.ACTION_LEFT && cCol < s if (xRightRaw % 2 != 0) xRightExp++; } - int maxRows = geometry.getContainerSlotCount() / 9; + int maxRows = geometry.getContainerSlotCount() / cols; yTopExp = Math.max(0, Math.min(maxRows * 2 - 1, yTopExp)); yBotExp = Math.max(0, Math.min(maxRows * 2 - 1, yBotExp)); xLeftExp = Math.max(0, xLeftExp); - xRightExp = Math.max(0, Math.min(17, xRightExp)); + xRightExp = Math.max(0, Math.min(cols * 2 - 1, xRightExp)); if (geometry.isDraggingRectangle(mouseX, mouseY)) { - Slot startRowSlot = editor.accessor.getHandler().getSlot((yTopExp / 2) * 9); + Slot startRowSlot = editor.accessor.getHandler().getSlot((yTopExp / 2) * cols); int topY = guiY + startRowSlot.y + (yTopExp % 2 == 0 ? -1 : 16); - Slot endRowSlot = editor.accessor.getHandler().getSlot((yBotExp / 2) * 9); + Slot endRowSlot = editor.accessor.getHandler().getSlot((yBotExp / 2) * cols); int botY = guiY + endRowSlot.y + (yBotExp % 2 == 0 ? 0 : 17); Slot startColSlot = editor.accessor.getHandler().getSlot(xLeftExp / 2); int leftX = guiX + startColSlot.x + (xLeftExp % 2 == 0 ? -1 : 16); @@ -345,7 +348,7 @@ else if (session.currentDragAction == ChestConfigManager.ACTION_LEFT && cCol < s if (erase) { for (int r = Math.max(0, minRow - 1); r <= maxRow + 1; r++) { for (int c = Math.max(0, minCol - 1); c <= maxCol + 1; c++) { - int slotIdx = r * 9 + c; + int slotIdx = r * cols + c; if (slotIdx >= geometry.getContainerSlotCount()) continue; Slot s = editor.accessor.getHandler().getSlot(slotIdx); int x = guiX + s.x; @@ -384,7 +387,7 @@ else if (session.currentDragAction == ChestConfigManager.ACTION_LEFT && cCol < s } else { for (int r = minRow; r <= maxRow; r++) { for (int c = minCol; c <= maxCol; c++) { - int slotIdx = r * 9 + c; + int slotIdx = r * cols + c; Slot s = editor.accessor.getHandler().getSlot(slotIdx); int x = guiX + s.x; int y = guiY + s.y; diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/data/ChestConfigManager.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/data/ChestConfigManager.java index 3b3d99e..1adf294 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/data/ChestConfigManager.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/data/ChestConfigManager.java @@ -877,13 +877,15 @@ public void setCurrentWhitelists( /** * Flood-fills the contiguous region of slots reachable from {@code startSlot} without crossing a * separator, confined to the key window {@code [lo, hi)} (one namespace: the container starts at 0, - * the player inventory at {@link #PLAYER_KEY_OFFSET}). {@code lo} must be a multiple of 9. + * the player inventory at {@link #PLAYER_KEY_OFFSET}). {@code lo} must be that namespace's base key; + * the grid width comes from the namespace (see {@link #gridColumns(int)}). */ public Set getContiguousSlots(int startSlot, int lo, int hi) { Set result = new HashSet<>(); if (startSlot < lo || startSlot >= hi) return result; - int rowCount = (hi - lo + 8) / 9; + int cols = gridColumns(lo); + int rowCount = (hi - lo + cols - 1) / cols; java.util.Queue queue = new java.util.LinkedList<>(); queue.add(startSlot); result.add(startSlot); @@ -891,10 +893,10 @@ public Set getContiguousSlots(int startSlot, int lo, int hi) { while (!queue.isEmpty()) { int current = queue.poll(); int rel = current - lo; - int row = rel / 9; - int col = rel % 9; + int row = rel / cols; + int col = rel % cols; - int up = current - 9; + int up = current - cols; if (row > 0 && up >= lo && !result.contains(up)) { if (getColor(current, ACTION_TOP) == 0 && getColor(up, ACTION_BOTTOM) == 0) { result.add(up); @@ -902,7 +904,7 @@ public Set getContiguousSlots(int startSlot, int lo, int hi) { } } - int down = current + 9; + int down = current + cols; if (row < rowCount - 1 && down < hi && !result.contains(down)) { if (getColor(current, ACTION_BOTTOM) == 0 && getColor(down, ACTION_TOP) == 0) { result.add(down); @@ -919,7 +921,7 @@ public Set getContiguousSlots(int startSlot, int lo, int hi) { } int right = current + 1; - if (col < 8 && right < hi && !result.contains(right)) { + if (col < cols - 1 && right < hi && !result.contains(right)) { if (getColor(current, ACTION_RIGHT) == 0 && getColor(right, ACTION_LEFT) == 0) { result.add(right); queue.add(right); @@ -1146,13 +1148,63 @@ public void seedDefaultPresetsIfNeeded() { * so chest slots (raw indices) and inventory slots (offset indices) — which would otherwise share * the same index — coexist while editing both at once. Must exceed any container's slot count and * be a multiple of 9 so the 9-wide grid math stays aligned within the inventory namespace. + * The offset keys only ever live in memory (every save and every payload re-keys or drops them), + * so the value is free to change. It used to be 117, which modded chests with more slots (a + * 126-slot Iron Chests netherite chest) overlapped: their top slots read as inventory keys. */ - public static final int PLAYER_KEY_OFFSET = 117; + public static final int PLAYER_KEY_OFFSET = 1008; public static boolean isInventoryKey(int key) { return key >= PLAYER_KEY_OFFSET; } + /** + * Width of the open container's slot grid. 9 for every vanilla container; a modded chest screen + * (e.g. Iron Chests' 12- and 14-wide chests) sets its own width when the editor opens on it. + */ + private static int containerColumns = 9; + + public static void setContainerColumns(int cols) { + containerColumns = cols > 0 ? cols : 9; + } + + /** Grid width for a working-map key: the player inventory is always 9 wide, the container varies. */ + public static int gridColumns(int key) { + return isInventoryKey(key) ? 9 : containerColumns; + } + + /** Row of a key inside its own namespace's grid (row 0 is the namespace's first slot). */ + public static int gridRow(int key) { + return (key - namespaceBase(key)) / gridColumns(key); + } + + /** Column of a key inside its own namespace's grid. */ + public static int gridCol(int key) { + return (key - namespaceBase(key)) % gridColumns(key); + } + + private static int namespaceBase(int key) { + return isInventoryKey(key) ? PLAYER_KEY_OFFSET : 0; + } + + /** + * Column count of slots laid out as one row-major grid of 18 px cells (slot i at column i % cols, + * row i / cols), or 0 when the positions are not such a grid. The width is the run of slots sharing + * the first slot's row. Used to accept a modded chest screen only when the editor's index-based grid + * math will line up with what is drawn. + */ + public static int detectGridColumns(int[] xs, int[] ys) { + int n = xs.length; + if (n == 0 || ys.length != n) return 0; + int cols = 1; + while (cols < n && ys[cols] == ys[0]) cols++; + if (n % cols != 0) return 0; + for (int i = 0; i < n; i++) { + if (xs[i] != xs[0] + (i % cols) * 18 || ys[i] != ys[0] + (i / cols) * 18) return 0; + } + return cols; + } + /** * True for player-inventory keys that fall outside the 9-wide grid: armor and offhand (their * PlayerInventory index is >= MAIN_SIZE = 36). They are isolated cells with no grid neighbours, so @@ -1272,9 +1324,12 @@ public boolean loadInventoryPreset(int index) { * File backing chest preset slot {@code index} for a container of {@code size} slots (27 vs 54). * Presets are split by container size so a single chest and a double chest keep independent sets, * shown only when a container of that size is open. Global reusable template across worlds. + * A grid that is not 9 wide also carries its width ("108x12"), so two modded containers with the same + * slot count but a different shape never share presets; 9-wide containers keep their original names. */ private Path getChestPresetFile(int size, int index) { - return getGlobalConfigDir().resolve(CHEST_PRESET_PREFIX + size + "_" + index + ".json"); + String shape = containerColumns == 9 ? String.valueOf(size) : size + "x" + containerColumns; + return getGlobalConfigDir().resolve(CHEST_PRESET_PREFIX + shape + "_" + index + ".json"); } /** Whether chest preset slot {@code index} of the {@code size}-slot set has been saved. */ diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/mixin/client/GenericContainerScreenMixin.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/mixin/client/GenericContainerScreenMixin.java index 05fe75b..80f940c 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/mixin/client/GenericContainerScreenMixin.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/mixin/client/GenericContainerScreenMixin.java @@ -4,6 +4,7 @@ import io.github.marcsanzdev.chestseparators.client.ui.ChestSeparatorsEditor; import io.github.marcsanzdev.chestseparators.client.ui.ModKeyBindings; import io.github.marcsanzdev.chestseparators.config.GlobalChestConfig; +import io.github.marcsanzdev.chestseparators.data.ChestConfigManager; import net.minecraft.client.gui.GuiGraphicsExtractor; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; @@ -48,8 +49,18 @@ protected void init(CallbackInfo ci) { || (Object) this instanceof net.minecraft.client.gui.screens.inventory.CreativeModeInventoryScreen || isHorseScreenWithCargo(); + // Modded chest screens (e.g. Iron Chests) are not ContainerScreens, so they are matched by shape + // instead: one whitelist-capable container laid out as a plain slot grid, possibly wider than 9. + int gridColumns = 9; + if (!isValidScreen) { + gridColumns = ChestSeparatorsEditor.moddedGridColumns((AbstractContainerScreen) (Object) this); + isValidScreen = gridColumns > 0; + } + if (!isValidScreen) return; + // Set before the editor builds its geometry: every container grid computation reads this width. + ChestConfigManager.setContainerColumns(gridColumns); this.editor = new ChestSeparatorsEditor((AbstractContainerScreen) (Object) this); this.editor.init(); } diff --git a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/network/ModClientNetworking.java b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/network/ModClientNetworking.java index 4b80b6b..0a8d6f7 100644 --- a/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/network/ModClientNetworking.java +++ b/multiloader-26.2/common/src/main/java/io/github/marcsanzdev/chestseparators/network/ModClientNetworking.java @@ -10,7 +10,6 @@ import net.minecraft.ChatFormatting; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.inventory.ChestMenu; import net.minecraft.world.inventory.PlayerEnderChestContainer; import net.minecraft.world.inventory.Slot; import net.minecraft.world.level.block.entity.BlockEntity; @@ -120,9 +119,10 @@ public static void register() { provider.setWhitelists(payload.whitelists()); } - // 3. THE FLICKER KILLER: inject into the client's dummy GUI inventory. - if (player.containerMenu instanceof ChestMenu genericHandler - && genericHandler.getContainer() instanceof IWhitelistProvider provider) { + // 3. THE FLICKER KILLER: inject into the client's dummy GUI inventory. Looked up from + // the menu's slots rather than ChestMenu#getContainer so modded chest menus get it too. + if (ChestSeparatorsEditor.openContainer(player.containerMenu) + instanceof IWhitelistProvider provider) { provider.setWhitelists(payload.whitelists()); } }); @@ -147,8 +147,8 @@ public static void register() { // Inject into the client's open container inventory (the minecart) to keep client-side // prediction consistent and avoid a flicker. - if (player.containerMenu instanceof ChestMenu genericHandler - && genericHandler.getContainer() instanceof IWhitelistProvider provider) { + if (ChestSeparatorsEditor.openContainer(player.containerMenu) + instanceof IWhitelistProvider provider) { provider.setWhitelists(payload.whitelists()); } diff --git a/multiloader-26.2/common/src/test/java/io/github/marcsanzdev/chestseparators/data/ContiguousSlotsTest.java b/multiloader-26.2/common/src/test/java/io/github/marcsanzdev/chestseparators/data/ContiguousSlotsTest.java index edae3eb..a471f86 100644 --- a/multiloader-26.2/common/src/test/java/io/github/marcsanzdev/chestseparators/data/ContiguousSlotsTest.java +++ b/multiloader-26.2/common/src/test/java/io/github/marcsanzdev/chestseparators/data/ContiguousSlotsTest.java @@ -9,6 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Set; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -16,7 +17,8 @@ * Tests {@link ChestConfigManager#getContiguousSlots} — the BFS that finds all slots reachable * from a starting slot without crossing a painted separator line. * - *

Uses a 27-slot (3×9) single chest layout for all cases. + *

Uses a 27-slot (3×9) single chest layout, plus a 108-slot (9×12) modded chest for the wide-grid + * cases. */ class ContiguousSlotsTest { @@ -30,6 +32,11 @@ void reset() { manager.clearCurrentConfig(); } + @AfterEach + void restoreColumns() { + ChestConfigManager.setContainerColumns(9); + } + @Test void withNoSeparators_allSlotsAreContiguous() { Set result = manager.getContiguousSlots(0, 0, MAX_SLOTS); @@ -92,4 +99,42 @@ void outOfBoundsStart_returnsEmptySet() { result = manager.getContiguousSlots(MAX_SLOTS, 0, MAX_SLOTS); assertTrue(result.isEmpty()); } + + @Test + void wideChest_rowsAreTwelveSlotsLong() { + // A 108-slot chest drawn 12 wide (Iron Chests diamond/obsidian/crystal). + ChestConfigManager.setContainerColumns(12); + for (int col = 0; col < 12; col++) { + manager.paintAction(12 + col, ACTION_TOP, RED); + } + + Set topRow = manager.getContiguousSlots(0, 0, 108); + assertEquals(12, topRow.size()); + for (int i = 0; i < 12; i++) assertTrue(topRow.contains(i)); + assertFalse(topRow.contains(12)); + } + + @Test + void wideChest_lastColumnDoesNotWrapIntoNextRow() { + // With 12 columns, slot 11 ends row 0 and slot 12 starts row 1. Walling slot 11 off from the + // slot below it must isolate it: a 9-wide step would wrongly reach 12 as a right-hand neighbour. + ChestConfigManager.setContainerColumns(12); + manager.paintAction(11, ACTION_LEFT | ACTION_BOTTOM, RED); + + Set region = manager.getContiguousSlots(11, 0, 108); + assertEquals(1, region.size()); + } + + @Test + void wideChest_doesNotChangeTheInventoryGrid() { + // The player inventory stays 9 wide whatever the open container's width is. + ChestConfigManager.setContainerColumns(14); + int lo = ChestConfigManager.PLAYER_KEY_OFFSET; + for (int col = 0; col < 9; col++) { + manager.paintAction(lo + 9 + col, ACTION_TOP, RED); + } + + Set hotbar = manager.getContiguousSlots(lo, lo, lo + 36); + assertEquals(9, hotbar.size()); + } } diff --git a/multiloader-26.2/common/src/test/java/io/github/marcsanzdev/chestseparators/data/GridColumnsTest.java b/multiloader-26.2/common/src/test/java/io/github/marcsanzdev/chestseparators/data/GridColumnsTest.java new file mode 100644 index 0000000..f54226e --- /dev/null +++ b/multiloader-26.2/common/src/test/java/io/github/marcsanzdev/chestseparators/data/GridColumnsTest.java @@ -0,0 +1,103 @@ +package io.github.marcsanzdev.chestseparators.data; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +/** + * Tests the container grid-width helpers in {@link ChestConfigManager}: per-namespace columns, row/column + * of a key, grid detection from slot positions, and that the player-key offset clears every chest size. + */ +class GridColumnsTest { + + @AfterEach + void restoreColumns() { + ChestConfigManager.setContainerColumns(9); + } + + @Test + void defaultsToNineColumns() { + assertEquals(9, ChestConfigManager.gridColumns(0)); + ChestConfigManager.setContainerColumns(0); + assertEquals(9, ChestConfigManager.gridColumns(0)); + } + + @Test + void containerWidthNeverAppliesToInventoryKeys() { + ChestConfigManager.setContainerColumns(14); + int inv = ChestConfigManager.PLAYER_KEY_OFFSET; + assertEquals(14, ChestConfigManager.gridColumns(125)); + assertEquals(9, ChestConfigManager.gridColumns(inv)); + assertEquals(9, ChestConfigManager.gridColumns(inv + 40)); + } + + @Test + void rowAndColumnAreRelativeToTheNamespace() { + ChestConfigManager.setContainerColumns(14); + assertEquals(1, ChestConfigManager.gridRow(14)); + assertEquals(0, ChestConfigManager.gridCol(14)); + assertEquals(8, ChestConfigManager.gridRow(125)); + assertEquals(13, ChestConfigManager.gridCol(125)); + + int inv = ChestConfigManager.PLAYER_KEY_OFFSET; + assertEquals(0, ChestConfigManager.gridRow(inv + 8)); + assertEquals(8, ChestConfigManager.gridCol(inv + 8)); + assertEquals(3, ChestConfigManager.gridRow(inv + 35)); + assertEquals(8, ChestConfigManager.gridCol(inv + 35)); + } + + @Test + void playerKeyOffsetClearsTheLargestChestAndKeepsTheNineWideGridAligned() { + // Netherite (Iron Chests) is the largest known container at 126 slots. + assertFalse(ChestConfigManager.isInventoryKey(125)); + assertTrue(ChestConfigManager.isInventoryKey(ChestConfigManager.PLAYER_KEY_OFFSET)); + assertEquals(0, ChestConfigManager.PLAYER_KEY_OFFSET % 9); + } + + @Test + void detectsRowMajorGrids() { + assertEquals(9, ChestConfigManager.detectGridColumns(xs(9, 3, 8), ys(9, 3, 18))); + assertEquals(12, ChestConfigManager.detectGridColumns(xs(12, 9, 8), ys(12, 9, 18))); + assertEquals(14, ChestConfigManager.detectGridColumns(xs(14, 9, 8), ys(14, 9, 18))); + } + + @Test + void rejectsLayoutsThatAreNotOneGrid() { + // Ragged last row. + int[] x = xs(9, 3, 8); + int[] y = ys(9, 3, 18); + assertEquals(0, ChestConfigManager.detectGridColumns( + java.util.Arrays.copyOf(x, 26), java.util.Arrays.copyOf(y, 26))); + + // A gap between two columns. + int[] gapX = xs(9, 3, 8); + for (int i = 0; i < gapX.length; i++) if (i % 9 >= 5) gapX[i] += 4; + assertEquals(0, ChestConfigManager.detectGridColumns(gapX, y)); + + // Slots stored column-major instead of row-major. + int[] colX = new int[27]; + int[] colY = new int[27]; + for (int i = 0; i < 27; i++) { + colX[i] = 8 + (i / 3) * 18; + colY[i] = 18 + (i % 3) * 18; + } + assertEquals(0, ChestConfigManager.detectGridColumns(colX, colY)); + + assertEquals(0, ChestConfigManager.detectGridColumns(new int[0], new int[0])); + } + + private static int[] xs(int cols, int rows, int x0) { + int[] out = new int[cols * rows]; + for (int i = 0; i < out.length; i++) out[i] = x0 + (i % cols) * 18; + return out; + } + + private static int[] ys(int cols, int rows, int y0) { + int[] out = new int[cols * rows]; + for (int i = 0; i < out.length; i++) out[i] = y0 + (i / cols) * 18; + return out; + } +}