use of buildcraft.lib.inventory.filter.ArrayStackFilter in project BuildCraft by BuildCraft.
the class FlexibleRecipe method craft.
@Override
public CraftingResult<T> craft(IFlexibleCrafter baseCrafter, boolean preview) {
if (output == null) {
return null;
}
IFlexibleCrafter crafter = baseCrafter;
if (preview) {
crafter = new FakeFlexibleCrafter(baseCrafter);
}
CraftingResult<T> result = new CraftingResult<>();
result.recipe = this;
result.powerCost = powerCost;
result.craftingTime = craftingTime;
for (ItemStack requirement : inputItems) {
IStackFilter filter = new ArrayStackFilter(requirement);
int amount = requirement.stackSize;
if (consumeItems(crafter, result, filter, amount) != 0) {
return null;
}
}
for (List<ItemStack> requirements : inputItemsWithAlternatives) {
IStackFilter filter = new ArrayStackFilter(requirements.toArray(new ItemStack[requirements.size()]));
int amount = requirements.get(0).stackSize;
if (consumeItems(crafter, result, filter, amount) != 0) {
return null;
}
}
for (FluidStack requirement : inputFluids) {
int amount = requirement.amount;
for (int tankid = 0; tankid < crafter.getCraftingFluidStackSize(); tankid++) {
FluidStack fluid = crafter.getCraftingFluidStack(tankid);
if (fluid != null && fluid.isFluidEqual(requirement)) {
int amountUsed;
if (fluid.amount > amount) {
amountUsed = amount;
if (!preview) {
crafter.decrCraftingFluidStack(tankid, amount);
}
amount = 0;
} else {
amountUsed = fluid.amount;
if (!preview) {
crafter.decrCraftingFluidStack(tankid, fluid.amount);
}
amount -= fluid.amount;
}
result.usedFluids.add(new FluidStack(requirement.getFluid(), amountUsed));
}
if (amount == 0) {
break;
}
}
if (amount != 0) {
return null;
}
}
// Output generation
result.crafted = output;
return result;
}
use of buildcraft.lib.inventory.filter.ArrayStackFilter in project BuildCraft by BuildCraft.
the class WorkbenchCrafting method hasExactStacks.
private boolean hasExactStacks() {
TObjectIntMap<ItemStackKey> required = new TObjectIntHashMap<>(getSizeInventory());
for (int s = 0; s < getSizeInventory(); s++) {
ItemStack req = invBlueprint.getStackInSlot(s);
if (!req.isEmpty()) {
int count = req.getCount();
if (count != 1) {
req = req.copy();
req.setCount(1);
}
ItemStackKey key = new ItemStackKey(req);
required.adjustOrPutValue(key, count, count);
}
}
return required.forEachEntry((stack, count) -> {
ArrayStackFilter filter = new ArrayStackFilter(stack.baseStack);
ItemStack inInventory = invMaterials.extract(filter, count, count, true);
return !inInventory.isEmpty() && inInventory.getCount() == count;
});
}
use of buildcraft.lib.inventory.filter.ArrayStackFilter in project BuildCraft by BuildCraft.
the class WorkbenchCrafting method craftExact.
/**
* Implementation of {@link #craft()}, assuming nothing about the current recipe.
*/
private boolean craftExact() {
// 4 steps:
// - Move everything out of this inventory (Just to check: state correction operation)
// - Attempt to move every exact item from invMaterials to this inventory
// - Call normal crafting stuffs
// - Move everything from the inventory back to materials
// Step 1
clearInventory();
// Step 2
for (int s = 0; s < getSizeInventory(); s++) {
ItemStack bpt = invBlueprint.getStackInSlot(s);
if (!bpt.isEmpty()) {
ItemStack stack = invMaterials.extract(new ArrayStackFilter(bpt), 1, 1, false);
if (stack.isEmpty()) {
clearInventory();
return false;
}
setInventorySlotContents(s, stack);
}
}
// result of matches for getCraftingResult and getResult.
if (!currentRecipe.matches(this, tile.getWorld())) {
return false;
}
ItemStack result = currentRecipe.getCraftingResult(this);
if (result.isEmpty()) {
// what?
clearInventory();
return false;
}
ItemStack leftover = invResult.insert(result, false, false);
if (!leftover.isEmpty()) {
InventoryUtil.addToBestAcceptor(tile.getWorld(), tile.getPos(), null, leftover);
}
NonNullList<ItemStack> remainingStacks = currentRecipe.getRemainingItems(this);
for (int s = 0; s < remainingStacks.size(); s++) {
ItemStack inSlot = getStackInSlot(s);
ItemStack remaining = remainingStacks.get(s);
if (!inSlot.isEmpty()) {
decrStackSize(s, 1);
inSlot = getStackInSlot(s);
}
if (!remaining.isEmpty()) {
if (inSlot.isEmpty()) {
setInventorySlotContents(s, remaining);
} else if (ItemStack.areItemsEqual(inSlot, remaining) && ItemStack.areItemStackTagsEqual(inSlot, remaining)) {
remaining.grow(inSlot.getCount());
setInventorySlotContents(s, remaining);
} else {
leftover = invMaterials.insert(remaining, false, false);
if (!leftover.isEmpty()) {
InventoryUtil.addToBestAcceptor(tile.getWorld(), tile.getPos(), null, leftover);
}
}
}
}
// Some ingredients really need to be removed (like empty buckets)
for (int s = 0; s < getSizeInventory(); s++) {
ItemStack inSlot = super.getStackInSlot(s);
if (!inSlot.isEmpty()) {
leftover = invMaterials.insert(inSlot, false, false);
decrStackSize(s, inSlot.getCount() - (leftover.isEmpty() ? 0 : leftover.getCount()));
if (!leftover.isEmpty()) {
InventoryUtil.addToBestAcceptor(tile.getWorld(), tile.getPos(), null, leftover);
}
}
}
return true;
}
use of buildcraft.lib.inventory.filter.ArrayStackFilter in project BuildCraft by BuildCraft.
the class BoardRobotBuilder method update.
@Override
public void update() {
if (launchingDelay > 0) {
launchingDelay--;
return;
}
if (markerToBuild == null) {
markerToBuild = findClosestMarker();
if (markerToBuild == null) {
if (robot.containsItems()) {
startDelegateAI(new AIRobotDisposeItems(robot));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
}
return;
}
}
if (!markerToBuild.needsToBuild()) {
markerToBuild = null;
currentBuildingSlot = null;
return;
}
if (currentBuildingSlot == null) {
currentBuildingSlot = markerToBuild.bluePrintBuilder.reserveNextSlot(robot.worldObj);
if (currentBuildingSlot == null) {
// No slots available yet
launchingDelay = 40;
return;
}
}
if (requirementsToLookFor == null) {
if (robot.containsItems()) {
startDelegateAI(new AIRobotDisposeItems(robot));
}
if (robot.worldObj.getWorldInfo().getGameType() != WorldSettings.GameType.CREATIVE) {
requirementsToLookFor = currentBuildingSlot.getRequirements(markerToBuild.getContext());
} else {
requirementsToLookFor = new LinkedList<>();
}
if (requirementsToLookFor == null) {
launchingDelay = 40;
return;
}
if (requirementsToLookFor.size() > 4) {
currentBuildingSlot.built = true;
currentBuildingSlot = null;
requirementsToLookFor = null;
return;
}
}
if (requirementsToLookFor.size() > 0) {
startDelegateAI(new AIRobotGotoStationAndLoad(robot, new ArrayStackFilter(requirementsToLookFor.get(0)), requirementsToLookFor.get(0).stackSize));
return;
}
if (requirementsToLookFor.size() == 0) {
if (currentBuildingSlot.stackConsumed == null) {
// Once all the element are in, if not already, use them to
// prepare the slot.
markerToBuild.bluePrintBuilder.useRequirements(robot, currentBuildingSlot);
}
if (!hasEnoughEnergy()) {
startDelegateAI(new AIRobotRecharge(robot));
} else {
startDelegateAI(new AIRobotGotoBlock(robot, Utils.convertFloor(currentBuildingSlot.getDestination()), 8));
}
// TODO: take into account cases where the robot can't reach the
// destination - go to work on another block
}
}
Aggregations