Search in sources :

Example 6 with IItemTransactor

use of buildcraft.api.inventory.IItemTransactor in project BuildCraft by BuildCraft.

the class ItemTransactorTester method testSimpleMoving.

@Test
public void testSimpleMoving() {
    IItemTransactor trans = new ItemHandlerSimple(2, null);
    Assert.assertTrue(trans.extract(null, 1, 1, false).isEmpty());
    ItemStack insert = new ItemStack(Items.APPLE);
    ItemStack leftOver = trans.insert(insert.copy(), false, false);
    Assert.assertTrue(leftOver.isEmpty());
    ItemStack extracted = trans.extract(null, 1, 1, false);
    Assert.assertTrue(ItemStack.areItemStacksEqual(insert, extracted));
    extracted = trans.extract(null, 1, 1, false);
    Assert.assertTrue(extracted.isEmpty());
}
Also used : IItemTransactor(buildcraft.api.inventory.IItemTransactor) ItemHandlerSimple(buildcraft.lib.tile.item.ItemHandlerSimple) ItemStack(net.minecraft.item.ItemStack) Test(org.junit.Test)

Example 7 with IItemTransactor

use of buildcraft.api.inventory.IItemTransactor in project BuildCraft by BuildCraft.

the class PipeFlowItems method tryExtractItems.

// IFlowItems
@Override
public int tryExtractItems(int count, EnumFacing from, EnumDyeColor colour, IStackFilter filter, boolean simulate) {
    if (pipe.getHolder().getPipeWorld().isRemote) {
        throw new IllegalStateException("Cannot extract items on the client side!");
    }
    if (from == null) {
        return 0;
    }
    TileEntity tile = pipe.getConnectedTile(from);
    IItemTransactor trans = ItemTransactorHelper.getTransactor(tile, from.getOpposite());
    ItemStack possible = trans.extract(filter, 1, count, true);
    if (possible.isEmpty()) {
        return 0;
    }
    if (possible.getCount() > possible.getMaxStackSize()) {
        possible.setCount(possible.getMaxStackSize());
        count = possible.getMaxStackSize();
    }
    IPipeHolder holder = pipe.getHolder();
    PipeEventItem.TryInsert tryInsert = new PipeEventItem.TryInsert(holder, this, colour, from, possible);
    holder.fireEvent(tryInsert);
    if (tryInsert.isCanceled() || tryInsert.accepted <= 0) {
        return 0;
    }
    count = Math.min(count, tryInsert.accepted);
    ItemStack stack = trans.extract(filter, count, count, simulate);
    if (stack.isEmpty()) {
        throw new IllegalStateException("The transactor " + trans + " returned an empty itemstack from a known good request!");
    }
    if (!simulate) {
        insertItemEvents(stack, colour, EXTRACT_SPEED, from);
    }
    return count;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IItemTransactor(buildcraft.api.inventory.IItemTransactor) IPipeHolder(buildcraft.api.transport.pipe.IPipeHolder) ItemStack(net.minecraft.item.ItemStack) PipeEventItem(buildcraft.api.transport.pipe.PipeEventItem)

Example 8 with IItemTransactor

use of buildcraft.api.inventory.IItemTransactor in project BuildCraft by BuildCraft.

the class PipeFlowItems method onItemReachEnd.

private void onItemReachEnd(TravellingItem item) {
    IPipeHolder holder = pipe.getHolder();
    PipeEventItem.ReachEnd reachEnd = new PipeEventItem.ReachEnd(holder, this, item.colour, item.stack, item.side);
    holder.fireEvent(reachEnd);
    item.colour = reachEnd.colour;
    item.stack = reachEnd.getStack();
    ItemStack excess = item.stack;
    if (excess.isEmpty()) {
        return;
    }
    if (pipe.isConnected(item.side)) {
        ConnectedType type = pipe.getConnectedType(item.side);
        EnumFacing oppositeSide = item.side.getOpposite();
        switch(type) {
            case PIPE:
                {
                    IPipe oPipe = pipe.getConnectedPipe(item.side);
                    if (oPipe == null) {
                        break;
                    }
                    PipeFlow flow = oPipe.getFlow();
                    if (flow instanceof IFlowItems) {
                        IFlowItems oFlow = (IFlowItems) flow;
                        ItemStack before = excess;
                        excess = oFlow.injectItem(excess.copy(), true, oppositeSide, item.colour, item.speed);
                        if (!excess.isEmpty()) {
                            before.shrink(excess.getCount());
                        }
                        excess = fireEventEjectIntoPipe(oFlow, item.side, before, excess);
                    }
                    break;
                }
            case TILE:
                {
                    TileEntity tile = pipe.getConnectedTile(item.side);
                    IInjectable injectable = ItemTransactorHelper.getInjectable(tile, oppositeSide);
                    ItemStack before = excess;
                    excess = injectable.injectItem(excess.copy(), true, oppositeSide, item.colour, item.speed);
                    if (!excess.isEmpty()) {
                        IItemTransactor transactor = ItemTransactorHelper.getTransactor(tile, oppositeSide);
                        excess = transactor.insert(excess, false, false);
                    }
                    excess = fireEventEjectIntoTile(tile, item.side, before, excess);
                    break;
                }
        }
    }
    if (excess.isEmpty()) {
        return;
    }
    item.tried.add(item.side);
    item.toCenter = true;
    item.stack = excess;
    item.genTimings(holder.getPipeWorld().getTotalWorldTime(), getPipeLength(item.side));
    items.add(item.timeToDest, item);
    sendItemDataToClient(item);
}
Also used : IPipeHolder(buildcraft.api.transport.pipe.IPipeHolder) EnumFacing(net.minecraft.util.EnumFacing) PipeFlow(buildcraft.api.transport.pipe.PipeFlow) IFlowItems(buildcraft.api.transport.pipe.IFlowItems) ConnectedType(buildcraft.api.transport.pipe.IPipe.ConnectedType) TileEntity(net.minecraft.tileentity.TileEntity) IItemTransactor(buildcraft.api.inventory.IItemTransactor) IInjectable(buildcraft.api.transport.IInjectable) PipeEventItem(buildcraft.api.transport.pipe.PipeEventItem) ItemStack(net.minecraft.item.ItemStack) IPipe(buildcraft.api.transport.pipe.IPipe)

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