Search in sources :

Example 6 with IPipeHolder

use of buildcraft.api.transport.pipe.IPipeHolder 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 7 with IPipeHolder

use of buildcraft.api.transport.pipe.IPipeHolder in project BuildCraft by BuildCraft.

the class PipeFlowItems method onItemReachCenter.

private void onItemReachCenter(TravellingItem item) {
    IPipeHolder holder = pipe.getHolder();
    PipeEventItem.ReachCenter reachCenter = new PipeEventItem.ReachCenter(holder, this, item.colour, item.stack, item.side);
    holder.fireEvent(reachCenter);
    if (reachCenter.getStack().isEmpty()) {
        return;
    }
    PipeEventItem.SideCheck sideCheck = new PipeEventItem.SideCheck(holder, this, reachCenter.colour, reachCenter.from, reachCenter.getStack());
    sideCheck.disallow(reachCenter.from);
    for (EnumFacing face : EnumFacing.VALUES) {
        if (item.tried.contains(face) || !pipe.isConnected(face)) {
            sideCheck.disallow(face);
        }
    }
    holder.fireEvent(sideCheck);
    List<EnumSet<EnumFacing>> order = sideCheck.getOrder();
    if (order.isEmpty()) {
        PipeEventItem.TryBounce tryBounce = new PipeEventItem.TryBounce(holder, this, reachCenter.colour, reachCenter.from, reachCenter.getStack());
        holder.fireEvent(tryBounce);
        if (tryBounce.canBounce) {
            order = ImmutableList.of(EnumSet.of(reachCenter.from));
        } else {
            dropItem(item.stack, null, item.side.getOpposite(), item.speed);
            return;
        }
    }
    PipeEventItem.ItemEntry entry = new PipeEventItem.ItemEntry(reachCenter.colour, reachCenter.getStack(), reachCenter.from);
    PipeEventItem.Split split = new PipeEventItem.Split(holder, this, order, entry);
    holder.fireEvent(split);
    ImmutableList<PipeEventItem.ItemEntry> entries = ImmutableList.copyOf(split.items);
    PipeEventItem.FindDest findDest = new PipeEventItem.FindDest(holder, this, order, entries);
    holder.fireEvent(findDest);
    World world = holder.getPipeWorld();
    long now = world.getTotalWorldTime();
    for (PipeEventItem.ItemEntry itemEntry : findDest.items) {
        if (itemEntry.stack.isEmpty()) {
            continue;
        }
        PipeEventItem.ModifySpeed modifySpeed = new PipeEventItem.ModifySpeed(holder, this, itemEntry, item.speed);
        modifySpeed.modifyTo(0.04, 0.01);
        holder.fireEvent(modifySpeed);
        double target = modifySpeed.targetSpeed;
        double maxDelta = modifySpeed.maxSpeedChange;
        double nSpeed = item.speed;
        if (nSpeed < target) {
            nSpeed += maxDelta;
            if (nSpeed > target) {
                nSpeed = target;
            }
        } else if (nSpeed > target) {
            nSpeed -= maxDelta;
            if (nSpeed < target) {
                nSpeed = target;
            }
        }
        List<EnumFacing> destinations = itemEntry.to;
        if (destinations == null || destinations.size() == 0) {
            destinations = findDest.generateRandomOrder();
        }
        if (destinations.size() == 0) {
            dropItem(itemEntry.stack, null, item.side.getOpposite(), nSpeed);
        } else {
            TravellingItem newItem = new TravellingItem(itemEntry.stack);
            newItem.tried.addAll(item.tried);
            newItem.toCenter = false;
            newItem.colour = itemEntry.colour;
            newItem.side = destinations.get(0);
            newItem.speed = nSpeed;
            newItem.genTimings(now, getPipeLength(newItem.side));
            items.add(newItem.timeToDest, newItem);
            sendItemDataToClient(newItem);
        }
    }
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) World(net.minecraft.world.World) IPipeHolder(buildcraft.api.transport.pipe.IPipeHolder) EnumSet(java.util.EnumSet) PipeEventItem(buildcraft.api.transport.pipe.PipeEventItem)

Example 8 with IPipeHolder

use of buildcraft.api.transport.pipe.IPipeHolder in project BuildCraft by BuildCraft.

the class PipeFlowItems method insertItemEvents.

/**
 * Used internally to split up manual insertions from controlled extractions.
 */
private void insertItemEvents(@Nonnull ItemStack toInsert, EnumDyeColor colour, double speed, EnumFacing from) {
    IPipeHolder holder = pipe.getHolder();
    PipeEventItem.OnInsert onInsert = new PipeEventItem.OnInsert(holder, this, colour, toInsert, from);
    holder.fireEvent(onInsert);
    if (onInsert.getStack().isEmpty()) {
        return;
    }
    World world = pipe.getHolder().getPipeWorld();
    long now = world.getTotalWorldTime();
    TravellingItem item = new TravellingItem(toInsert);
    item.side = from;
    item.toCenter = true;
    item.speed = speed;
    item.colour = onInsert.colour;
    item.stack = onInsert.getStack();
    item.genTimings(now, getPipeLength(from));
    item.tried.add(from);
    addItemTryMerge(item);
}
Also used : IPipeHolder(buildcraft.api.transport.pipe.IPipeHolder) PipeEventItem(buildcraft.api.transport.pipe.PipeEventItem) World(net.minecraft.world.World)

Example 9 with IPipeHolder

use of buildcraft.api.transport.pipe.IPipeHolder 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 10 with IPipeHolder

use of buildcraft.api.transport.pipe.IPipeHolder in project BuildCraft by BuildCraft.

the class PipeBehaviourStripes method onDrop.

@PipeEventHandler
public void onDrop(PipeEventItem.Drop event) {
    if (direction == null) {
        return;
    }
    IPipeHolder holder = pipe.getHolder();
    World world = holder.getPipeWorld();
    BlockPos pos = holder.getPipePos();
    FakePlayer player = BuildCraftAPI.fakePlayerProvider.getFakePlayer((WorldServer) world, holder.getOwner(), pos);
    player.inventory.clear();
    // set the main hand of the fake player to the stack
    player.inventory.setInventorySlotContents(player.inventory.currentItem, event.getStack());
    if (PipeApi.stripeRegistry.handleItem(world, pos, direction, event.getStack(), player, this)) {
        event.setStack(StackUtil.EMPTY);
        for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
            ItemStack stack = player.inventory.removeStackFromSlot(i);
            if (!stack.isEmpty()) {
                sendItem(stack, direction);
            }
        }
    }
}
Also used : IPipeHolder(buildcraft.api.transport.pipe.IPipeHolder) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) FakePlayer(net.minecraftforge.common.util.FakePlayer) PipeEventHandler(buildcraft.api.transport.pipe.PipeEventHandler)

Aggregations

IPipeHolder (buildcraft.api.transport.pipe.IPipeHolder)10 PipeEventItem (buildcraft.api.transport.pipe.PipeEventItem)5 TileEntity (net.minecraft.tileentity.TileEntity)5 EnumFacing (net.minecraft.util.EnumFacing)4 World (net.minecraft.world.World)4 IPipe (buildcraft.api.transport.pipe.IPipe)3 ItemStack (net.minecraft.item.ItemStack)3 BlockPos (net.minecraft.util.math.BlockPos)3 IItemTransactor (buildcraft.api.inventory.IItemTransactor)2 PipePluggable (buildcraft.api.transport.pluggable.PipePluggable)2 PluggableGate (buildcraft.transport.plug.PluggableGate)2 IGate (buildcraft.api.gates.IGate)1 EnumWirePart (buildcraft.api.transport.EnumWirePart)1 IInjectable (buildcraft.api.transport.IInjectable)1 IWireManager (buildcraft.api.transport.IWireManager)1 IFlowItems (buildcraft.api.transport.pipe.IFlowItems)1 ConnectedType (buildcraft.api.transport.pipe.IPipe.ConnectedType)1 PipeBehaviour (buildcraft.api.transport.pipe.PipeBehaviour)1 PipeEventHandler (buildcraft.api.transport.pipe.PipeEventHandler)1 PipeFlow (buildcraft.api.transport.pipe.PipeFlow)1