}
// Create a (temporary) clone of the inventory to work with
final ItemStack[] items = inventory.getContents();
final int size = items.length;
final Inventory inventoryClone = new InventoryBaseImpl(items, true);
int amount, i;
// Craft items until the limit is reached, or crafting is impossible
// Below is the craftloop label, which is used to break out of crafting
craftloop:
for (amount = 0; amount < limit; amount++) {
// input item check
if (!this.containsInput(inventoryClone)) {
break;
}
// remove ingredients from inventory
for (ItemStack item : this.input) {
ItemUtil.removeItems(inventoryClone, item);
}
// add resulting items to inventory
for (ItemStack item : this.output) {
ItemStack cloned = ItemUtil.cloneItem(item);
ItemUtil.transfer(cloned, inventoryClone, Integer.MAX_VALUE);
// Could not add result (full), unsuccessful
if (!LogicUtil.nullOrEmpty(cloned)) {
break craftloop;
}
}
// Crafting was successful, transfer items over
// Be sure NOT to produce new ItemStack instances!
for (i = 0; i < size; i++) {
ItemStack newItem = inventoryClone.getItem(i);
if (LogicUtil.nullOrEmpty(newItem)) {
items[i] = null;
} else if (items[i] == null) {
items[i] = newItem.clone();
} else {