use of com.simibubi.create.content.curiosities.tools.BlueprintEntity.BlueprintCraftingInventory in project Create by Creators-of-Create.
the class BlueprintOverlayRenderer method rebuild.
public static void rebuild(BlueprintSection sectionAt, boolean sneak) {
cachedRenderedFilters.clear();
ItemStackHandler items = sectionAt.getItems();
boolean empty = true;
for (int i = 0; i < 9; i++) {
if (!items.getStackInSlot(i).isEmpty()) {
empty = false;
break;
}
}
BlueprintOverlayRenderer.empty = empty;
BlueprintOverlayRenderer.result = ItemStack.EMPTY;
if (empty)
return;
boolean firstPass = true;
boolean success = true;
Minecraft mc = Minecraft.getInstance();
ItemStackHandler playerInv = new ItemStackHandler(mc.player.getInventory().getContainerSize());
for (int i = 0; i < playerInv.getSlots(); i++) playerInv.setStackInSlot(i, mc.player.getInventory().getItem(i).copy());
int amountCrafted = 0;
Optional<CraftingRecipe> recipe = Optional.empty();
Map<Integer, ItemStack> craftingGrid = new HashMap<>();
ingredients.clear();
ItemStackHandler missingItems = new ItemStackHandler(64);
ItemStackHandler availableItems = new ItemStackHandler(64);
List<ItemStack> newlyAdded = new ArrayList<>();
List<ItemStack> newlyMissing = new ArrayList<>();
boolean invalid = false;
do {
craftingGrid.clear();
newlyAdded.clear();
newlyMissing.clear();
Search: for (int i = 0; i < 9; i++) {
ItemStack requestedItem = items.getStackInSlot(i);
if (requestedItem.isEmpty()) {
craftingGrid.put(i, ItemStack.EMPTY);
continue;
}
for (int slot = 0; slot < playerInv.getSlots(); slot++) {
if (!FilterItem.test(mc.level, playerInv.getStackInSlot(slot), requestedItem))
continue;
ItemStack currentItem = playerInv.extractItem(slot, 1, false);
craftingGrid.put(i, currentItem);
newlyAdded.add(currentItem);
continue Search;
}
success = false;
newlyMissing.add(requestedItem);
}
if (success) {
CraftingContainer craftingInventory = new BlueprintCraftingInventory(craftingGrid);
if (!recipe.isPresent())
recipe = mc.level.getRecipeManager().getRecipeFor(RecipeType.CRAFTING, craftingInventory, mc.level);
ItemStack resultFromRecipe = recipe.filter(r -> r.matches(craftingInventory, mc.level)).map(r -> r.assemble(craftingInventory)).orElse(ItemStack.EMPTY);
if (resultFromRecipe.isEmpty()) {
if (!recipe.isPresent())
invalid = true;
success = false;
} else if (resultFromRecipe.getCount() + amountCrafted > 64) {
success = false;
} else {
amountCrafted += resultFromRecipe.getCount();
if (result.isEmpty())
result = resultFromRecipe.copy();
else
result.grow(resultFromRecipe.getCount());
resultCraftable = true;
firstPass = false;
}
}
if (success || firstPass) {
newlyAdded.forEach(s -> ItemHandlerHelper.insertItemStacked(availableItems, s, false));
newlyMissing.forEach(s -> ItemHandlerHelper.insertItemStacked(missingItems, s, false));
}
if (!success) {
if (firstPass) {
result = invalid ? ItemStack.EMPTY : items.getStackInSlot(9);
resultCraftable = false;
}
break;
}
if (!sneak)
break;
} while (success);
for (int i = 0; i < 9; i++) {
ItemStack available = availableItems.getStackInSlot(i);
if (available.isEmpty())
continue;
ingredients.add(Pair.of(available, true));
}
for (int i = 0; i < 9; i++) {
ItemStack missing = missingItems.getStackInSlot(i);
if (missing.isEmpty())
continue;
ingredients.add(Pair.of(missing, false));
}
}
Aggregations