Search in sources :

Example 1 with IItemTransactor

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);
}
Also used : ISidedInventory(net.minecraft.inventory.ISidedInventory) IInventory(net.minecraft.inventory.IInventory) IItemTransactor(buildcraft.api.inventory.IItemTransactor) IItemHandler(net.minecraftforge.items.IItemHandler) Nonnull(javax.annotation.Nonnull)

Example 2 with IItemTransactor

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;
}
Also used : EntityItem(net.minecraft.entity.item.EntityItem) Entity(net.minecraft.entity.Entity) IItemHandler(net.minecraftforge.items.IItemHandler) InventoryUtil(buildcraft.lib.misc.InventoryUtil) ICapabilityProvider(net.minecraftforge.common.capabilities.ICapabilityProvider) World(net.minecraft.world.World) IItemInsertable(buildcraft.api.inventory.IItemTransactor.IItemInsertable) PipeApi(buildcraft.api.transport.pipe.PipeApi) EnumFacing(net.minecraft.util.EnumFacing) IInjectable(buildcraft.api.transport.IInjectable) InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) EntityArrow(net.minecraft.entity.projectile.EntityArrow) ItemStack(net.minecraft.item.ItemStack) IStackFilter(buildcraft.api.core.IStackFilter) ISidedInventory(net.minecraft.inventory.ISidedInventory) Vec3d(net.minecraft.util.math.Vec3d) IInventory(net.minecraft.inventory.IInventory) StackUtil(buildcraft.lib.misc.StackUtil) CapUtil(buildcraft.lib.misc.CapUtil) NonNullList(net.minecraft.util.NonNullList) Nonnull(javax.annotation.Nonnull) IItemTransactor(buildcraft.api.inventory.IItemTransactor) IStackFilter(buildcraft.api.core.IStackFilter) ItemStack(net.minecraft.item.ItemStack)

Example 3 with IItemTransactor

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;
}
Also used : IItemTransactor(buildcraft.api.inventory.IItemTransactor) PipeFlow(buildcraft.api.transport.pipe.PipeFlow) EntityLivingBase(net.minecraft.entity.EntityLivingBase) IFlowFluid(buildcraft.api.transport.pipe.IFlowFluid) IFlowItems(buildcraft.api.transport.pipe.IFlowItems) ItemStack(net.minecraft.item.ItemStack)

Example 4 with IItemTransactor

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;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IItemTransactor(buildcraft.api.inventory.IItemTransactor) EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) Nonnull(javax.annotation.Nonnull)

Example 5 with IItemTransactor

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));
}
Also used : IItemTransactor(buildcraft.api.inventory.IItemTransactor) ItemHandlerSimple(buildcraft.lib.tile.item.ItemHandlerSimple) ItemStack(net.minecraft.item.ItemStack) Test(org.junit.Test)

Aggregations

IItemTransactor (buildcraft.api.inventory.IItemTransactor)8 ItemStack (net.minecraft.item.ItemStack)6 Nonnull (javax.annotation.Nonnull)3 TileEntity (net.minecraft.tileentity.TileEntity)3 EnumFacing (net.minecraft.util.EnumFacing)3 IInjectable (buildcraft.api.transport.IInjectable)2 IFlowItems (buildcraft.api.transport.pipe.IFlowItems)2 IPipeHolder (buildcraft.api.transport.pipe.IPipeHolder)2 PipeEventItem (buildcraft.api.transport.pipe.PipeEventItem)2 PipeFlow (buildcraft.api.transport.pipe.PipeFlow)2 ItemHandlerSimple (buildcraft.lib.tile.item.ItemHandlerSimple)2 IInventory (net.minecraft.inventory.IInventory)2 ISidedInventory (net.minecraft.inventory.ISidedInventory)2 IItemHandler (net.minecraftforge.items.IItemHandler)2 Test (org.junit.Test)2 IStackFilter (buildcraft.api.core.IStackFilter)1 IItemInsertable (buildcraft.api.inventory.IItemTransactor.IItemInsertable)1 IFlowFluid (buildcraft.api.transport.pipe.IFlowFluid)1 IPipe (buildcraft.api.transport.pipe.IPipe)1 ConnectedType (buildcraft.api.transport.pipe.IPipe.ConnectedType)1