|
7 | 7 | import net.minecraft.world.inventory.Slot; |
8 | 8 | import net.minecraft.world.item.ItemStack; |
9 | 9 | import net.minecraft.world.level.block.Blocks; |
| 10 | +import org.apache.commons.lang3.tuple.Pair; |
10 | 11 | import org.cyclops.integratedterminals.api.terminalstorage.ITerminalButton; |
11 | 12 | import org.cyclops.integratedterminals.api.terminalstorage.ITerminalRowColumnProvider; |
12 | 13 | import org.cyclops.integratedterminals.api.terminalstorage.ITerminalStorageSlot; |
13 | 14 | import org.cyclops.integratedterminals.api.terminalstorage.ITerminalStorageTabClient; |
| 15 | +import org.cyclops.integratedterminals.api.terminalstorage.ITerminalStorageTabCommon; |
14 | 16 | import org.cyclops.integratedterminals.inventory.container.ContainerTerminalStorageBase; |
15 | 17 |
|
16 | 18 | import java.util.Collections; |
@@ -108,10 +110,95 @@ public void resetActiveSlot() { |
108 | 110 | public boolean handleClick(AbstractContainerMenu container, int channel, int hoveringStorageSlot, int mouseButton, |
109 | 111 | boolean hasClickedOutside, boolean hasClickedInStorage, int hoveredContainerSlot, |
110 | 112 | boolean isQuickMove) { |
111 | | - // Let default container handling take care of clicks |
| 113 | + // Handle shift-clicking for Ender Chest slots |
| 114 | + if (isQuickMove && hoveredContainerSlot >= 0 && container instanceof ContainerTerminalStorageBase) { |
| 115 | + ContainerTerminalStorageBase<?> terminalContainer = (ContainerTerminalStorageBase<?>) container; |
| 116 | + |
| 117 | + // Get the Ender Chest slots for this tab |
| 118 | + List<Pair<Slot, ITerminalStorageTabCommon.ISlotPositionCallback>> enderSlots = |
| 119 | + terminalContainer.getTabSlots(getName().toString()); |
| 120 | + |
| 121 | + if (!enderSlots.isEmpty()) { |
| 122 | + // Find the start and end indices of Ender Chest slots |
| 123 | + int startIndex = Integer.MAX_VALUE; |
| 124 | + int endIndex = Integer.MIN_VALUE; |
| 125 | + for (Pair<Slot, ITerminalStorageTabCommon.ISlotPositionCallback> slotPair : enderSlots) { |
| 126 | + int index = slotPair.getLeft().index; |
| 127 | + startIndex = Math.min(startIndex, index); |
| 128 | + endIndex = Math.max(endIndex, index); |
| 129 | + } |
| 130 | + |
| 131 | + boolean isEnderSlot = hoveredContainerSlot >= startIndex && hoveredContainerSlot <= endIndex; |
| 132 | + Slot slot = container.getSlot(hoveredContainerSlot); |
| 133 | + ItemStack stackInSlot = slot.getItem().copy(); |
| 134 | + |
| 135 | + if (!stackInSlot.isEmpty()) { |
| 136 | + // Try to move items |
| 137 | + if (isEnderSlot) { |
| 138 | + // Moving from Ender Chest to player inventory (slots 0-35) |
| 139 | + if (moveItems(container, stackInSlot, 0, 36)) { |
| 140 | + slot.set(stackInSlot.isEmpty() ? ItemStack.EMPTY : stackInSlot); |
| 141 | + slot.setChanged(); |
| 142 | + return true; |
| 143 | + } |
| 144 | + } else { |
| 145 | + // Moving from player inventory to Ender Chest |
| 146 | + if (moveItems(container, stackInSlot, startIndex, endIndex + 1)) { |
| 147 | + slot.set(stackInSlot.isEmpty() ? ItemStack.EMPTY : stackInSlot); |
| 148 | + slot.setChanged(); |
| 149 | + return true; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // Let default container handling take care of other clicks |
112 | 157 | return false; |
113 | 158 | } |
114 | 159 |
|
| 160 | + private boolean moveItems(AbstractContainerMenu container, ItemStack stack, int startIndex, int endIndex) { |
| 161 | + boolean moved = false; |
| 162 | + int originalCount = stack.getCount(); |
| 163 | + |
| 164 | + // Try to merge with existing stacks first |
| 165 | + for (int i = startIndex; i < endIndex && !stack.isEmpty(); i++) { |
| 166 | + Slot targetSlot = container.getSlot(i); |
| 167 | + ItemStack targetStack = targetSlot.getItem(); |
| 168 | + |
| 169 | + if (!targetStack.isEmpty() && ItemStack.isSameItemSameTags(stack, targetStack)) { |
| 170 | + int maxSize = Math.min(targetSlot.getMaxStackSize(), targetStack.getMaxStackSize()); |
| 171 | + int toTransfer = Math.min(stack.getCount(), maxSize - targetStack.getCount()); |
| 172 | + |
| 173 | + if (toTransfer > 0) { |
| 174 | + targetStack.grow(toTransfer); |
| 175 | + stack.shrink(toTransfer); |
| 176 | + targetSlot.setChanged(); |
| 177 | + moved = true; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + // Then try to put in empty slots |
| 183 | + for (int i = startIndex; i < endIndex && !stack.isEmpty(); i++) { |
| 184 | + Slot targetSlot = container.getSlot(i); |
| 185 | + |
| 186 | + if (!targetSlot.mayPlace(stack)) { |
| 187 | + continue; |
| 188 | + } |
| 189 | + |
| 190 | + ItemStack targetStack = targetSlot.getItem(); |
| 191 | + if (targetStack.isEmpty()) { |
| 192 | + int toTransfer = Math.min(stack.getCount(), targetSlot.getMaxStackSize()); |
| 193 | + targetSlot.set(stack.split(toTransfer)); |
| 194 | + targetSlot.setChanged(); |
| 195 | + moved = true; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return moved && stack.getCount() < originalCount; |
| 200 | + } |
| 201 | + |
115 | 202 | @Override |
116 | 203 | public boolean handleScroll(AbstractContainerMenu container, int channel, int hoveringStorageSlot, double delta, |
117 | 204 | boolean hasClickedOutside, boolean hasClickedInStorage, int hoveredContainerSlot) { |
|
0 commit comments