use of buildcraft.api.inventory.IItemTransactor in project BuildCraft by BuildCraft.
the class ItemTransactorHelper method getTransactor.
@Nonnull
public static IItemTransactor getTransactor(ICapabilityProvider provider, EnumFacing face) {
if (provider == null) {
return NoSpaceTransactor.INSTANCE;
}
IItemTransactor trans = provider.getCapability(CapUtil.CAP_ITEM_TRANSACTOR, face);
if (trans != null) {
return trans;
}
IItemHandler handler = provider.getCapability(CapUtil.CAP_ITEMS, face);
if (handler == null) {
if (provider instanceof ISidedInventory) {
return new SidedInventoryWrapper((ISidedInventory) provider, face);
}
if (provider instanceof IInventory) {
return new InventoryWrapper((IInventory) provider);
}
return NoSpaceTransactor.INSTANCE;
}
if (handler instanceof IItemTransactor) {
return (IItemTransactor) handler;
}
return new ItemHandlerWrapper(handler);
}
use of buildcraft.api.inventory.IItemTransactor in project BuildCraft by BuildCraft.
the class ItemTransactorHelper method moveSingle0.
private static int moveSingle0(IItemTransactor src, IItemTransactor dst, IStackFilter filter, int maxItems, boolean simulateSrc, boolean simulateDst) {
ItemStack potential = src.extract(filter, 1, maxItems, true);
if (potential.isEmpty())
return 0;
ItemStack leftOver = dst.insert(potential, false, simulateDst);
int toTake = potential.getCount() - leftOver.getCount();
IStackFilter exactFilter = (stack) -> StackUtil.canMerge(stack, potential);
ItemStack taken = src.extract(exactFilter, toTake, toTake, simulateSrc);
if (taken.getCount() != toTake) {
String msg = "One of the two transactors (either src = ";
msg += src.getClass() + " or dst = " + dst.getClass() + ")";
msg += " didn't respect the movement flags! ( potential = " + potential;
msg += ", leftOver = " + leftOver + ", taken = " + taken;
msg += ", count = " + toTake + " )";
throw new IllegalStateException(msg);
}
return toTake;
}
use of buildcraft.api.inventory.IItemTransactor in project BuildCraft by BuildCraft.
the class PipeBehaviourObsidian method trySuckEntity.
/**
* @return The left over power
*/
protected long trySuckEntity(Entity entity, EnumFacing faceFrom, long power, boolean simulate) {
if (entity.isDead || entity instanceof EntityLivingBase) {
return power;
}
Long tickPickupObj = entityDropTime.get(entity);
if (tickPickupObj != null) {
long tickPickup = tickPickupObj;
long tickNow = pipe.getHolder().getPipeWorld().getTotalWorldTime();
if (tickNow < tickPickup) {
return power;
} else {
entityDropTime.remove(entity);
}
}
PipeFlow flow = pipe.getFlow();
IFlowItems flowItem = flow instanceof IFlowItems ? (IFlowItems) flow : null;
IFlowFluid flowFluid = flow instanceof IFlowFluid ? (IFlowFluid) flow : null;
IItemTransactor transactor = ItemTransactorHelper.getTransactorForEntity(entity, faceFrom.getOpposite());
if (flowItem != null) {
double distance = Math.sqrt(entity.getDistanceSqToCenter(pipe.getHolder().getPipePos()));
long powerReqPerItem = (long) (distance * POWER_PER_METRE + POWER_PER_ITEM);
int max = power == 0 ? 1 : (int) (power / powerReqPerItem);
ItemStack extracted = transactor.extract(StackFilter.ALL, 1, max, simulate);
if (!extracted.isEmpty()) {
if (!simulate) {
flowItem.insertItemsForce(extracted, faceFrom, null, INSERT_SPEED);
}
return power - powerReqPerItem * extracted.getCount();
}
}
if (flowFluid != null) {
// TODO: Fluid extraction!
}
return power;
}
use of buildcraft.api.inventory.IItemTransactor in project BuildCraft by BuildCraft.
the class InventoryUtil method addToRandomInventory.
// Sending items around
/**
* @return The leftover stack
*/
@Nonnull
public static ItemStack addToRandomInventory(World world, BlockPos pos, @Nonnull ItemStack stack) {
if (stack.isEmpty()) {
return StackUtil.EMPTY;
}
List<EnumFacing> toTry = new ArrayList<>(6);
Collections.addAll(toTry, EnumFacing.VALUES);
Collections.shuffle(toTry);
for (EnumFacing face : toTry) {
TileEntity tile = world.getTileEntity(pos.offset(face));
IItemTransactor transactor = ItemTransactorHelper.getTransactor(tile, face.getOpposite());
stack = transactor.insert(stack, false, false);
if (stack.isEmpty()) {
return StackUtil.EMPTY;
}
}
return stack;
}
use of buildcraft.api.inventory.IItemTransactor in project BuildCraft by BuildCraft.
the class ItemTransactorTester method testLimitedInventory.
@Test
public void testLimitedInventory() {
IItemTransactor limited = new ItemHandlerSimple(2, (i, s) -> true, StackInsertionFunction.getInsertionFunction(4), null);
ItemStack toInsert = new ItemStack(Items.APPLE, 9);
ItemStack toInsertCopy = toInsert.copy();
ItemStack supposedLeftOver = new ItemStack(Items.APPLE);
ItemStack actuallyLeftOver = limited.insert(toInsert, false, false);
Assert.assertTrue(ItemStack.areItemStacksEqual(toInsert, toInsertCopy));
Assert.assertTrue(ItemStack.areItemStacksEqual(supposedLeftOver, actuallyLeftOver));
}
Aggregations