Search in sources :

Example 1 with IInjectable

use of buildcraft.api.transport.IInjectable in project BuildCraft by BuildCraft.

the class TileChute method update.

@Override
public void update() {
    super.update();
    if (worldObj.isRemote || isEmpty || worldObj.getTotalWorldTime() % 2 != 0) {
        return;
    }
    TileEntity outputTile = getTile(EnumFacing.DOWN);
    ITransactor transactor = Transactor.getTransactorFor(outputTile, EnumFacing.UP);
    if (transactor == null) {
        if (outputTile instanceof IInjectable && getBattery().getEnergyStored() >= 10) {
            ItemStack stackToOutput = null;
            int internalSlot = 0;
            getBattery().useEnergy(10, 10, false);
            for (; internalSlot < inventory.getSizeInventory(); internalSlot++) {
                ItemStack stackInSlot = inventory.getStackInSlot(internalSlot);
                if (stackInSlot == null || stackInSlot.stackSize == 0) {
                    continue;
                }
                stackToOutput = stackInSlot.copy();
                stackToOutput.stackSize = 1;
                break;
            }
            if (stackToOutput != null) {
                int used = ((IInjectable) outputTile).injectItem(stackToOutput, true, EnumFacing.UP, null);
                if (used > 0) {
                    decrStackSize(internalSlot, 1);
                }
            }
        }
        return;
    }
    for (int internalSlot = 0; internalSlot < inventory.getSizeInventory(); internalSlot++) {
        ItemStack stackInSlot = inventory.getStackInSlot(internalSlot);
        if (stackInSlot == null || stackInSlot.stackSize == 0) {
            continue;
        }
        ItemStack clonedStack = stackInSlot.copy().splitStack(1);
        if (transactor.addNew(clonedStack, true).stackSize > 0) {
            inventory.decrStackSize(internalSlot, 1);
            return;
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IInjectable(buildcraft.api.transport.IInjectable) ItemStack(net.minecraft.item.ItemStack) ITransactor(buildcraft.core.lib.inventory.ITransactor)

Example 2 with IInjectable

use of buildcraft.api.transport.IInjectable in project BuildCraft by BuildCraft.

the class InventoryUtil method addToRandomInjectable.

/**
 * Look around the tile given in parameter in all 6 position, tries to add the items to a random injectable tile
 * around. Will make sure that the location from which the items are coming from (identified by the from parameter)
 * isn't used again so that entities doesn't go backwards. Returns true if successful, false otherwise.
 */
@Nonnull
public static ItemStack addToRandomInjectable(World world, BlockPos pos, EnumFacing ignore, @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) {
        if (face == ignore)
            continue;
        TileEntity tile = world.getTileEntity(pos.offset(face));
        IInjectable injectable = ItemTransactorHelper.getInjectable(tile, face.getOpposite());
        stack = injectable.injectItem(stack, true, face.getOpposite(), null, 0);
        if (stack.isEmpty()) {
            return StackUtil.EMPTY;
        }
    }
    return stack;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IInjectable(buildcraft.api.transport.IInjectable) EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) Nonnull(javax.annotation.Nonnull)

Example 3 with IInjectable

use of buildcraft.api.transport.IInjectable 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)

Example 4 with IInjectable

use of buildcraft.api.transport.IInjectable in project BuildCraft by BuildCraft.

the class Utils method addToRandomInjectableAround.

/**
 * Look around the tile given in parameter in all 6 position, tries to add the items to a random injectable tile
 * around. Will make sure that the location from which the items are coming from (identified by the from parameter)
 * isn't used again so that entities doesn't go backwards. Returns true if successful, false otherwise.
 */
public static int addToRandomInjectableAround(World world, BlockPos pos, EnumFacing from, ItemStack stack) {
    List<IInjectable> possiblePipes = new ArrayList<>();
    List<EnumFacing> pipeDirections = new ArrayList<>();
    for (EnumFacing side : EnumFacing.VALUES) {
        if (side.getOpposite() == from) {
            continue;
        }
        BlockPos newpos = pos.offset(side);
        TileEntity tile = world.getTileEntity(newpos);
        if (tile instanceof IInjectable) {
            if (!((IInjectable) tile).canInjectItems(side.getOpposite())) {
                continue;
            }
            possiblePipes.add((IInjectable) tile);
            pipeDirections.add(side.getOpposite());
        } else {
            IInjectable wrapper = CompatHooks.INSTANCE.getInjectableWrapper(tile, side);
            if (wrapper != null) {
                possiblePipes.add(wrapper);
                pipeDirections.add(side.getOpposite());
            }
        }
    }
    if (possiblePipes.size() > 0) {
        int choice = RANDOM.nextInt(possiblePipes.size());
        IInjectable pipeEntry = possiblePipes.get(choice);
        return pipeEntry.injectItem(stack, true, pipeDirections.get(choice), null);
    }
    return 0;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IInjectable(buildcraft.api.transport.IInjectable) EnumFacing(net.minecraft.util.EnumFacing)

Example 5 with IInjectable

use of buildcraft.api.transport.IInjectable in project BuildCraft by BuildCraft.

the class AIRobotUnload method unload.

public static boolean unload(EntityRobotBase robot, DockingStation station, boolean doUnload) {
    if (station == null) {
        return false;
    }
    IInjectable output = station.getItemOutput();
    if (output == null) {
        return false;
    }
    EnumFacing injectSide = station.getItemOutputSide().face;
    if (!output.canInjectItems(injectSide)) {
        return false;
    }
    for (IInvSlot robotSlot : InventoryIterator.getIterable(robot)) {
        if (robotSlot.getStackInSlot() == null) {
            continue;
        }
        if (!ActionRobotFilter.canInteractWithItem(station, new ArrayStackOrListFilter(robotSlot.getStackInSlot()), ActionStationAcceptItems.class)) {
            continue;
        }
        ItemStack stack = robotSlot.getStackInSlot();
        int used = output.injectItem(stack, doUnload, injectSide, null);
        if (used > 0) {
            if (doUnload) {
                robotSlot.decreaseStackInSlot(used);
            }
            return true;
        }
    }
    if (robot.getHeldItem() != null) {
        if (!ActionRobotFilter.canInteractWithItem(station, new ArrayStackOrListFilter(robot.getHeldItem()), ActionStationAcceptItems.class)) {
            return false;
        }
        ItemStack stack = robot.getHeldItem();
        int used = output.injectItem(stack, doUnload, injectSide, null);
        if (used > 0) {
            if (doUnload) {
                if (stack.stackSize <= used) {
                    robot.setItemInUse(null);
                } else {
                    stack.stackSize -= used;
                }
            }
            return true;
        }
    }
    return false;
}
Also used : ActionStationAcceptItems(buildcraft.robotics.statements.ActionStationAcceptItems) IInvSlot(buildcraft.api.core.IInvSlot) IInjectable(buildcraft.api.transport.IInjectable) ArrayStackOrListFilter(buildcraft.lib.inventory.filter.ArrayStackOrListFilter) EnumFacing(net.minecraft.util.EnumFacing) ItemStack(net.minecraft.item.ItemStack)

Aggregations

IInjectable (buildcraft.api.transport.IInjectable)5 TileEntity (net.minecraft.tileentity.TileEntity)4 EnumFacing (net.minecraft.util.EnumFacing)4 ItemStack (net.minecraft.item.ItemStack)3 IInvSlot (buildcraft.api.core.IInvSlot)1 IItemTransactor (buildcraft.api.inventory.IItemTransactor)1 IFlowItems (buildcraft.api.transport.pipe.IFlowItems)1 IPipe (buildcraft.api.transport.pipe.IPipe)1 ConnectedType (buildcraft.api.transport.pipe.IPipe.ConnectedType)1 IPipeHolder (buildcraft.api.transport.pipe.IPipeHolder)1 PipeEventItem (buildcraft.api.transport.pipe.PipeEventItem)1 PipeFlow (buildcraft.api.transport.pipe.PipeFlow)1 ITransactor (buildcraft.core.lib.inventory.ITransactor)1 ArrayStackOrListFilter (buildcraft.lib.inventory.filter.ArrayStackOrListFilter)1 ActionStationAcceptItems (buildcraft.robotics.statements.ActionStationAcceptItems)1 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1