@@ -144,11 +144,12 @@ public boolean craft(boolean simulate) {
144144 // Determine output
145145 if (chosenPossibility != null && !itemStack .isEmpty ()
146146 && addItemStackForOutput (level , targetPos , targetSide , outputProviders , itemStack , true )) {
147- if (!simulate ) {
148- addItemStackForOutput (level , targetPos , targetSide , outputProviders , itemStack , simulate );
147+ if (chosenPossibility .handleRemainingItems (level , inputSide , simulate )) {
148+ if (!simulate ) {
149+ addItemStackForOutput (level , targetPos , targetSide , outputProviders , itemStack , simulate );
150+ }
151+ return true ;
149152 }
150- chosenPossibility .handleRemainingItems (level , inputSide , simulate );
151- return true ;
152153 }
153154 return false ;
154155 }
@@ -208,15 +209,33 @@ public ItemStack getOutput(Level level) {
208209 * @param inputSide The crafting side.
209210 * @param simulate If the crafting should be simulated.
210211 */
211- public void handleRemainingItems (Level level , Direction inputSide , boolean simulate ) {
212+ public boolean handleRemainingItems (Level level , Direction inputSide , boolean simulate ) {
212213 Recipe recipe = getRecipe (level );
213214 NonNullList <ItemStack > remainingStacks = recipe .getRemainingItems (inventoryCrafting );
214215 for (int i = 0 ; i < remainingStacks .size (); i ++) {
215216 ItemStack originalStack = inventoryCrafting .getItem (i );
216217 ItemStack remainingStack = remainingStacks .get (i );
217218 if (originalStack != null && !originalStack .isEmpty ()) {
218219 if (providers [i ] != null ) {
219- providers [i ].reduceItemStack (level , positions [i ], inputSide , simulate );
220+ // Consume one item from input
221+ boolean success = providers [i ].reduceItemStack (level , positions [i ], inputSide , simulate );
222+
223+ // If consumption failed, consider the whole crafting job failed
224+ if (!success ) {
225+ // Restore all previous slots if not simulating
226+ if (!simulate ) {
227+ for (int j = 0 ; j < i ; j ++) {
228+ ItemStack stackToRestore = inventoryCrafting .getItem (j );
229+ if (stackToRestore != null && !stackToRestore .isEmpty () && providers [j ] != null ) {
230+ providers [j ].addItemStack (level , positions [j ], inputSide , stackToRestore , false );
231+ }
232+ }
233+ }
234+
235+ return false ;
236+ }
237+
238+ // Add a possibly remaining stack to the slot
220239 if (!remainingStack .isEmpty () && remainingStack .getCount () > 0 ) {
221240 providers [i ].addItemStack (level , positions [i ], inputSide , remainingStack , simulate );
222241 }
@@ -226,6 +245,7 @@ public void handleRemainingItems(Level level, Direction inputSide, boolean simul
226245 }
227246 }
228247 }
248+ return true ;
229249 }
230250
231251 public CraftingPossibility clone () {
0 commit comments