use of mcjty.rftoolscontrol.blocks.workbench.WorkbenchTileEntity in project RFToolsControl by McJty.
the class ProcessorTileEntity method pushItemsWorkbench.
public boolean pushItemsWorkbench(IProgram program, @Nonnull BlockSide workbench, ItemStack item, int slot1, int slot2) {
if (item.isEmpty()) {
item = getCraftResult(program);
}
if (item.isEmpty()) {
throw new ProgException(EXCEPT_MISSINGCRAFTRESULT);
}
TileEntity te = getTileEntityAt(workbench);
if (!(te instanceof WorkbenchTileEntity)) {
throw new ProgException(EXCEPT_NOTAWORKBENCH);
}
IItemHandler cardHandler = getItemHandlerAt(te, EnumFacing.EAST);
ItemStack card = findCraftingCard(cardHandler, item);
if (card.isEmpty()) {
throw new ProgException(EXCEPT_MISSINGCRAFTINGCARD);
}
if (!CraftingCardItem.fitsGrid(card)) {
throw new ProgException(EXCEPT_NOTAGRID);
}
CardInfo info = this.cardInfo[((RunningProgram) program).getCardIndex()];
IItemHandler itemHandler = getItemHandler();
IItemHandler gridHandler = getItemHandlerAt(te, EnumFacing.UP);
List<ItemStack> ingredients = CraftingCardItem.getIngredientsGrid(card);
boolean success = true;
for (int i = 0; i < 9; i++) {
ItemStack stackInWorkbench = gridHandler.getStackInSlot(i);
ItemStack stackInIngredient = ingredients.get(i);
if (!stackInWorkbench.isEmpty() && stackInIngredient.isEmpty()) {
// Can't work. There is already something in the workbench that doesn't belong
success = false;
} else if (stackInWorkbench.isEmpty() && !stackInIngredient.isEmpty()) {
// Let's see if we can find the needed ingredient
boolean found = false;
for (int slot = slot1; slot <= slot2; slot++) {
int realSlot = info.getRealSlot(slot);
ItemStack localStack = itemHandler.getStackInSlot(realSlot);
if (stackInIngredient.isItemEqual(localStack)) {
localStack = itemHandler.extractItem(realSlot, stackInIngredient.getCount(), false);
gridHandler.insertItem(i, localStack, false);
found = true;
break;
}
}
if (!found) {
success = false;
}
} else if (!stackInWorkbench.isEmpty() && !stackInIngredient.isEmpty()) {
// See if the item matches and we have enough
if (!stackInIngredient.isItemEqual(stackInWorkbench)) {
success = false;
} else if (stackInIngredient.getCount() > stackInWorkbench.getCount()) {
success = false;
}
}
}
return success;
}
Aggregations