2020import dev .terminalmc .clientsort .client .ClientSort ;
2121import dev .terminalmc .clientsort .client .compat .itemlocks .ItemLocksWrapper ;
2222import dev .terminalmc .clientsort .client .config .ClassPolicy ;
23+ import dev .terminalmc .clientsort .client .gui .TriggerButtonManager ;
2324import dev .terminalmc .clientsort .client .inventory .Scope ;
2425import dev .terminalmc .clientsort .client .inventory .helper .ContainerScreenHelper ;
2526import dev .terminalmc .clientsort .client .inventory .operator .client .ClientCreativeOperator ;
4243
4344import java .util .ArrayList ;
4445import java .util .List ;
46+ import java .util .Objects ;
4547import java .util .Set ;
4648
4749import static dev .terminalmc .clientsort .ClientSort .debug ;
@@ -62,6 +64,7 @@ public abstract class SingleUseOperator {
6264 private boolean hasOperated = false ;
6365 protected final AbstractContainerScreen <?> screen ;
6466 protected final ContainerScreenHelper <? extends AbstractContainerScreen <?>> screenHelper ;
67+ protected final Operation op ;
6568 /**
6669 * The slot that was hovered when sorting was triggered.
6770 */
@@ -94,13 +97,15 @@ public abstract class SingleUseOperator {
9497 */
9598 protected final ItemStack [] otherScopeStacks ;
9699
97- protected SingleUseOperator (AbstractContainerScreen <?> screen , Slot originSlot ) {
100+ protected SingleUseOperator (AbstractContainerScreen <?> screen , Slot originSlot , Operation op ) {
98101 this .screen = screen ;
99102 this .screenHelper = ContainerScreenHelper .of (screen );
100103 this .originSlot = originSlot ;
104+ this .op = op ;
105+
101106 // Collect slots in origin scope
102107 Scope originScope = screenHelper .getScope (originSlot );
103- originScopeSlots = collectSlots (originScope );
108+ originScopeSlots = collectSlots (originSlot );
104109 if (debug ()) {
105110 ClientSort .LOG .info (
106111 "Discovered {} slots in origin scope ({} - {}):" ,
@@ -116,13 +121,20 @@ protected SingleUseOperator(AbstractContainerScreen<?> screen, Slot originSlot)
116121 originScopeStacks [i ] = originScopeSlots [i ].getItem ().copy ();
117122 }
118123
124+ // Get the other container, if any
125+ LocalPlayer player = Objects .requireNonNull (Minecraft .getInstance ().player );
126+ @ Nullable Slot otherSlot = originSlot .container == player .getInventory ()
127+ ? TriggerButtonManager .getContainerRefSlot (op )
128+ : TriggerButtonManager .getPlayerRefSlot (op );
129+ if (otherSlot == null || op .equals (Operation .SORT )) {
130+ otherScopeSlots = new Slot []{};
131+ otherScopeStacks = new ItemStack []{};
132+ return ;
133+ }
134+
119135 // Collect slots in other container scope, if any
120- Scope otherScope = switch (originScope ) {
121- case PLAYER_INV -> Scope .CONTAINER_INV ;
122- case CONTAINER_INV -> Scope .PLAYER_INV ;
123- default -> Scope .INVALID ;
124- };
125- otherScopeSlots = collectSlots (otherScope );
136+ Scope otherScope = screenHelper .getScope (otherSlot );
137+ otherScopeSlots = collectSlots (otherSlot );
126138 if (debug ()) {
127139 ClientSort .LOG .info (
128140 "Discovered {} slots in other scope ({} - {}):" ,
@@ -142,8 +154,9 @@ protected SingleUseOperator(AbstractContainerScreen<?> screen, Slot originSlot)
142154 /**
143155 * Finds all the valid inventory menu slots that are in {@code scope}.
144156 */
145- private Slot [] collectSlots (Scope scope ) {
157+ private Slot [] collectSlots (Slot refSlot ) {
146158 LocalPlayer player = Minecraft .getInstance ().player ;
159+ Scope scope = screenHelper .getScope (refSlot );
147160 if (scope == Scope .INVALID )
148161 return new Slot [0 ];
149162
@@ -153,6 +166,12 @@ private Slot[] collectSlots(Scope scope) {
153166 int slotId = ((ISlot ) slot ).clientsort$getIndexInMenu ();
154167 int slotIdx = ((ISlot ) slot ).clientsort$getIndexInContainer ();
155168
169+ //noinspection ConstantValue
170+ if (slot .container == null )
171+ continue ;
172+ if (slot .container != refSlot .container )
173+ continue ;
174+
156175 // Ignore slots in different scope
157176 if (screenHelper .getScope (slot ) != scope )
158177 continue ;
@@ -295,7 +314,7 @@ public static boolean transfer(
295314 && ClientServices .PLATFORM .canSendToServer (operation .type )) {
296315 if (debug ())
297316 ClientSort .LOG .info ("Preparing server operator for {}" , operation .name ());
298- return new ServerOperator (screen , originSlot );
317+ return new ServerOperator (screen , originSlot , operation );
299318 }
300319
301320 // Check that there is not already an op running
@@ -313,11 +332,11 @@ public static boolean transfer(
313332 && screen instanceof CreativeModeInventoryScreen ) {
314333 if (debug ())
315334 ClientSort .LOG .info ("Preparing client-creative operator for {}" , operation .name ());
316- return new ClientCreativeOperator (screen , originSlot );
335+ return new ClientCreativeOperator (screen , originSlot , operation );
317336 } else {
318337 if (debug ())
319338 ClientSort .LOG .info ("Preparing client-survival operator for {}" , operation .name ());
320- return new ClientSurvivalOperator (screen , originSlot );
339+ return new ClientSurvivalOperator (screen , originSlot , operation );
321340 }
322341 }
323342
0 commit comments