@@ -53,7 +53,7 @@ public static void restock(ServerPlayer player, ItemStack item, int count, Consu
5353 if (cTHandler .getTargetGrid ().getStorageService () == null )
5454 return ;
5555
56- int toAdd = Math . max ( item .getMaxStackSize () / 2 , 1 ) - count ;
56+ int toAdd = item .getMaxStackSize () - count ;
5757 if (toAdd == 0 )
5858 return ;
5959
@@ -91,24 +91,12 @@ public static void insertStackInME(ItemEntity entity, Player player) {
9191 return ;
9292 if (player .level ().isClientSide ())
9393 return ;
94- if (player .isShiftKeyDown ())
95- return ;
96- CraftingTerminalHandler cTHandler = CraftingTerminalHandler .getCraftingTerminalHandler (player );
97- ItemStack terminal = cTHandler .getCraftingTerminal ();
98-
99- if (!(MagnetHandler .getMagnetMode (terminal ).pickupToME ()))
100- return ;
101- if (!cTHandler .inRange ())
102- return ;
10394
104- MagnetHost magnetHost = cTHandler .getMagnetHost ();
105- if (magnetHost == null )
95+ if (!canInsert (stack , player ))
10696 return ;
10797
108- // if the filter is empty, it will match anything, even in whitelist mode
109- if (magnetHost .getInsertFilter ().isEmpty () && magnetHost .getInsertMode () == IncludeExclude .WHITELIST )
110- return ;
111- if (!magnetHost .getInsertFilter ().matchesFilter (AEItemKey .of (stack ), magnetHost .getInsertMode ()))
98+ CraftingTerminalHandler cTHandler = CraftingTerminalHandler .getCraftingTerminalHandler (player );
99+ if (!cTHandler .inRange ())
112100 return ;
113101
114102 if (cTHandler .getTargetGrid () == null )
@@ -126,6 +114,47 @@ public static void insertStackInME(ItemEntity entity, Player player) {
126114 stack .setCount (leftover );
127115 }
128116
117+ private static boolean canInsert (ItemStack stack , Player player ) {
118+ CraftingTerminalHandler cTHandler = CraftingTerminalHandler .getCraftingTerminalHandler (player );
119+ ItemStack terminal = cTHandler .getCraftingTerminal ();
120+
121+ cTHandler .checkTerminal ();
122+ if (cTHandler .isRestockEnabled () && isRestocking (stack , player ))
123+ return true ;
124+
125+ if (player .isShiftKeyDown ())
126+ return false ;
127+
128+ if (!(MagnetHandler .getMagnetMode (terminal ).pickupToME ()))
129+ return false ;
130+
131+ MagnetHost magnetHost = cTHandler .getMagnetHost ();
132+ if (magnetHost == null )
133+ return false ;
134+
135+ // if the filter is empty, it will match anything, even in whitelist mode
136+ if (magnetHost .getInsertFilter ().isEmpty () && magnetHost .getInsertMode () == IncludeExclude .WHITELIST )
137+ return false ;
138+ if (!magnetHost .getInsertFilter ().matchesFilter (AEItemKey .of (stack ), magnetHost .getInsertMode ()))
139+ return false ;
140+
141+ return true ;
142+ }
143+
144+ private static boolean isRestocking (ItemStack stack , Player player ) {
145+ if (stack .is (AE2wtlibTags .NO_RESTOCK )) {
146+ return false ;
147+ }
148+ if (stack .getMaxStackSize () == 1 ) {
149+ return false ;
150+ }
151+ for (int i = 0 ; i < 9 ; i ++) {
152+ if (ItemStack .isSameItemSameComponents (stack , player .getInventory ().getItem (i )))
153+ return true ;
154+ }
155+ return false ;
156+ }
157+
129158 public static void pickBlock (ItemStack stack ) {
130159 PacketDistributor .sendToServer (new PickBlockPacket (stack ));
131160 }
@@ -152,9 +181,6 @@ public static void pickBlock(ServerPlayer player, ItemStack stack) {
152181 if (insert < toReplace .getCount ())
153182 return ;
154183 int targetAmount = stack .getMaxStackSize ();
155- if (!stack .is (AE2wtlibTags .NO_RESTOCK ) && targetAmount != 1 ) {
156- targetAmount /= 2 ;
157- }
158184 var extracted = networkInventory .extract (AEItemKey .of (stack ), targetAmount , Actionable .SIMULATE , playerSource );
159185 if (extracted == 0 )
160186 return ;
0 commit comments