Skip to content

Commit 0212260

Browse files
Copilotrubensworks
andcommitted
Implement shift-click support for Ender Chest slots
Add custom shift-click handling in handleClick method to move items between Ender Chest and player inventory. Implements proper item merging and empty slot filling logic. Co-authored-by: rubensworks <440384+rubensworks@users.noreply.github.com>
1 parent a93613d commit 0212260

1 file changed

Lines changed: 88 additions & 1 deletion

File tree

src/main/java/org/cyclops/integratedterminals/core/terminalstorage/TerminalStorageTabEnderChestClient.java

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import net.minecraft.world.inventory.Slot;
88
import net.minecraft.world.item.ItemStack;
99
import net.minecraft.world.level.block.Blocks;
10+
import org.apache.commons.lang3.tuple.Pair;
1011
import org.cyclops.integratedterminals.api.terminalstorage.ITerminalButton;
1112
import org.cyclops.integratedterminals.api.terminalstorage.ITerminalRowColumnProvider;
1213
import org.cyclops.integratedterminals.api.terminalstorage.ITerminalStorageSlot;
1314
import org.cyclops.integratedterminals.api.terminalstorage.ITerminalStorageTabClient;
15+
import org.cyclops.integratedterminals.api.terminalstorage.ITerminalStorageTabCommon;
1416
import org.cyclops.integratedterminals.inventory.container.ContainerTerminalStorageBase;
1517

1618
import java.util.Collections;
@@ -108,10 +110,95 @@ public void resetActiveSlot() {
108110
public boolean handleClick(AbstractContainerMenu container, int channel, int hoveringStorageSlot, int mouseButton,
109111
boolean hasClickedOutside, boolean hasClickedInStorage, int hoveredContainerSlot,
110112
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
112157
return false;
113158
}
114159

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+
115202
@Override
116203
public boolean handleScroll(AbstractContainerMenu container, int channel, int hoveringStorageSlot, double delta,
117204
boolean hasClickedOutside, boolean hasClickedInStorage, int hoveredContainerSlot) {

0 commit comments

Comments
 (0)