@@ -1368,10 +1368,45 @@ private void sendIt() {
1368
1368
for (CraftableBigItemStack cbis : recipesToOrder ) {
1369
1369
if (!(cbis .recipe instanceof CraftingRecipe cr ))
1370
1370
continue ;
1371
- PackageOrder pattern =
1372
- new PackageOrder (FactoryPanelScreen .convertRecipeToPackageOrderContext (cr , itemsToOrder ));
1373
- int count = cbis .count / cbis .getOutputCount (blockEntity .getLevel ());
1374
- craftList .add (new CraftingEntry (pattern , count ));
1371
+ int craftedCount = 0 ;
1372
+ int targetCount = cbis .count / cbis .getOutputCount (blockEntity .getLevel ());
1373
+ List <BigItemStack > mutableOrder = BigItemStack .duplicateWrappers (itemsToOrder );
1374
+
1375
+ while (craftedCount < targetCount ) {
1376
+ // Carefully split the ordered recipes based on what exactly will be used to craft them
1377
+ PackageOrder pattern = new PackageOrder (FactoryPanelScreen .convertRecipeToPackageOrderContext (cr , mutableOrder , true ));
1378
+ int maxCrafts = targetCount - craftedCount ;
1379
+ int availableCrafts = 0 ;
1380
+
1381
+ boolean itemsExhausted = false ;
1382
+ Outer : while (availableCrafts < maxCrafts && !itemsExhausted ) {
1383
+ List <BigItemStack > previousSnapshot = BigItemStack .duplicateWrappers (mutableOrder );
1384
+ itemsExhausted = true ;
1385
+ Pattern : for (BigItemStack patternStack : pattern .stacks ()) {
1386
+ if (patternStack .stack .isEmpty ())
1387
+ continue ;
1388
+ for (BigItemStack ordered : mutableOrder ) {
1389
+ if (!ItemHandlerHelper .canItemStacksStack (ordered .stack , patternStack .stack ))
1390
+ continue ;
1391
+ if (ordered .count == 0 )
1392
+ continue ;
1393
+ ordered .count -= 1 ;
1394
+ itemsExhausted = false ;
1395
+ continue Pattern ;
1396
+ }
1397
+ mutableOrder = previousSnapshot ;
1398
+ break Outer ;
1399
+ }
1400
+ availableCrafts ++;
1401
+ }
1402
+
1403
+ if (availableCrafts == 0 )
1404
+ break ;
1405
+
1406
+ craftList .add (new CraftingEntry (pattern , availableCrafts ));
1407
+ craftedCount += availableCrafts ;
1408
+ }
1409
+
1375
1410
}
1376
1411
order = new PackageOrderWithCrafts (order .orderedStacks (), craftList );
1377
1412
}
@@ -1517,7 +1552,7 @@ private Pair<Integer, List<List<BigItemStack>>> maxCraftable(CraftableBigItemSta
1517
1552
Function <ItemStack , Integer > countModifier , int newTypeLimit ) {
1518
1553
List <Ingredient > ingredients = cbis .getIngredients ();
1519
1554
List <List <BigItemStack >> validEntriesByIngredient = new ArrayList <>();
1520
- List <ItemStack > visited = new ArrayList <>();
1555
+ List <BigItemStack > alreadyCreated = new ArrayList <>();
1521
1556
1522
1557
for (Ingredient ingredient : ingredients ) {
1523
1558
if (ingredient .isEmpty ())
@@ -1528,17 +1563,18 @@ private Pair<Integer, List<List<BigItemStack>>> maxCraftable(CraftableBigItemSta
1528
1563
Entries :for (BigItemStack entry : list ) {
1529
1564
if (!ingredient .test (entry .stack ))
1530
1565
continue ;
1566
+ for (BigItemStack visitedStack : alreadyCreated ) {
1567
+ if (!ItemHandlerHelper .canItemStacksStack (visitedStack .stack , entry .stack ))
1568
+ continue ;
1569
+ valid .add (visitedStack );
1570
+ continue Entries ;
1571
+ }
1531
1572
BigItemStack asBis = new BigItemStack (entry .stack ,
1532
1573
summary .getCountOf (entry .stack ) + countModifier .apply (entry .stack ));
1533
- if (asBis .count > 0 )
1574
+ if (asBis .count > 0 ) {
1534
1575
valid .add (asBis );
1535
- for (ItemStack visitedStack : visited ) {
1536
- if (!ItemHandlerHelper .canItemStacksStack (visitedStack , entry .stack ))
1537
- continue ;
1538
- visitedStack .grow (1 );
1539
- continue Entries ;
1576
+ alreadyCreated .add (asBis );
1540
1577
}
1541
- visited .add (entry .stack .copyWithCount (1 ));
1542
1578
}
1543
1579
1544
1580
if (valid .isEmpty ())
@@ -1562,15 +1598,7 @@ private Pair<Integer, List<List<BigItemStack>>> maxCraftable(CraftableBigItemSta
1562
1598
}
1563
1599
1564
1600
// Ingredients with shared items must divide counts
1565
- for (ItemStack visitedItem : visited ) {
1566
- for (List <BigItemStack > list : validEntriesByIngredient ) {
1567
- for (BigItemStack entry : list ) {
1568
- if (!ItemHandlerHelper .canItemStacksStack (entry .stack , visitedItem ))
1569
- continue ;
1570
- entry .count = entry .count / visitedItem .getCount ();
1571
- }
1572
- }
1573
- }
1601
+ validEntriesByIngredient = resolveIngredientAmounts (validEntriesByIngredient );
1574
1602
1575
1603
// Determine the bottlenecking ingredient
1576
1604
int minCount = Integer .MAX_VALUE ;
@@ -1616,6 +1644,40 @@ private void removeLeastEssentialItemStack(List<List<BigItemStack>> validIngredi
1616
1644
for (List <BigItemStack > list : validIngredients )
1617
1645
list .remove (chosen );
1618
1646
}
1647
+
1648
+ private List <List <BigItemStack >> resolveIngredientAmounts (List <List <BigItemStack >> validIngredients ) {
1649
+ List <List <BigItemStack >> resolvedIngredients = new ArrayList <>();
1650
+ for (int i = 0 ; i < validIngredients .size (); i ++)
1651
+ resolvedIngredients .add (new ArrayList <>());
1652
+
1653
+ boolean everythingTaken = false ;
1654
+ while (!everythingTaken ) {
1655
+ everythingTaken = true ;
1656
+ Ingredients : for (int i = 0 ; i < validIngredients .size (); i ++) {
1657
+ List <BigItemStack > list = validIngredients .get (i );
1658
+ List <BigItemStack > resolvedList = resolvedIngredients .get (i );
1659
+ for (BigItemStack bigItemStack : list ) {
1660
+ if (bigItemStack .count == 0 )
1661
+ continue ;
1662
+
1663
+ bigItemStack .count -= 1 ;
1664
+ everythingTaken = false ;
1665
+
1666
+ for (BigItemStack resolvedItemStack : resolvedList ) {
1667
+ if (resolvedItemStack .stack == bigItemStack .stack ) {
1668
+ resolvedItemStack .count ++;
1669
+ continue Ingredients ;
1670
+ }
1671
+ }
1672
+
1673
+ resolvedList .add (new BigItemStack (bigItemStack .stack , 1 ));
1674
+ continue Ingredients ;
1675
+ }
1676
+ }
1677
+ }
1678
+
1679
+ return resolvedIngredients ;
1680
+ }
1619
1681
1620
1682
private void syncJEI () {
1621
1683
if (Mods .JEI .isLoaded () && AllConfigs .client ().syncJeiSearch .get ())
0 commit comments