Search in sources :

Example 1 with ITubeConnection

use of com.bluepowermod.api.tube.ITubeConnection in project BluePower by Qmunity.

the class PneumaticTube method updateConnections.

private void updateConnections() {
    if (getWorld() != null && !getWorld().isRemote) {
        int connectionCount = 0;
        boolean clearedCache = false;
        clearCache();
        for (int i = 0; i < 6; i++) {
            boolean oldState = connections[i];
            ForgeDirection d = ForgeDirection.getOrientation(i);
            TileEntity neighbor = getTileCache(d);
            connections[i] = IOHelper.canInterfaceWith(neighbor, d.getOpposite(), this, canConnectToInventories());
            if (!connections[i])
                connections[i] = neighbor instanceof ITubeConnection && ((ITubeConnection) neighbor).isConnectedTo(d.getOpposite());
            if (connections[i]) {
                connections[i] = isConnected(d, null);
            }
            if (connections[i])
                connectionCount++;
            if (!clearedCache && oldState != connections[i]) {
                if (Config.enableTubeCaching)
                    getLogic().clearNodeCaches();
                clearedCache = true;
            }
        }
        isCrossOver = connectionCount != 2;
        sendUpdatePacket();
    }
    initialized = true;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ITubeConnection(com.bluepowermod.api.tube.ITubeConnection) ForgeDirection(net.minecraftforge.common.util.ForgeDirection)

Example 2 with ITubeConnection

use of com.bluepowermod.api.tube.ITubeConnection in project BluePower by Qmunity.

the class IOHelper method insert.

public static ItemStack insert(TileEntity tile, ItemStack itemStack, ForgeDirection direction, TubeColor color, boolean simulate) {
    if (tile == null || itemStack == null)
        return itemStack;
    if (tile instanceof ITubeConnection) {
        TubeStack tubeStack = ((ITubeConnection) tile).acceptItemFromTube(new TubeStack(itemStack, direction.getOpposite(), color), direction, simulate);
        if (tubeStack == null)
            return null;
        return tubeStack.stack;
    }
    IInventory inv = getInventoryForTE(tile);
    if (inv != null)
        return insert(inv, itemStack, direction.ordinal(), simulate);
    PneumaticTube tube = MultipartCompatibility.getPart(tile.getWorldObj(), tile.xCoord, tile.yCoord, tile.zCoord, PneumaticTube.class);
    if (tube != null) {
        // we don't need to check connections, that's catched earlier.
        TubeLogic logic = tube.getLogic();
        return logic.injectStack(itemStack, direction.getOpposite(), color, simulate) ? null : itemStack;
    }
    return itemStack;
}
Also used : ITubeConnection(com.bluepowermod.api.tube.ITubeConnection) IInventory(net.minecraft.inventory.IInventory) PneumaticTube(com.bluepowermod.part.tube.PneumaticTube) TubeStack(com.bluepowermod.part.tube.TubeStack) TubeLogic(com.bluepowermod.part.tube.TubeLogic)

Example 3 with ITubeConnection

use of com.bluepowermod.api.tube.ITubeConnection in project BluePower by Qmunity.

the class TubeLogic method update.

public void update() {
    if (!Config.enableTubeCaching)
        clearNodeCache();
    Iterator<TubeStack> iterator = tubeStacks.iterator();
    while (iterator.hasNext()) {
        TubeStack tubeStack = iterator.next();
        if (tubeStack.update(tube.getWorld())) {
            if (!tube.isCrossOver) {
                for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
                    if (tube.connections[dir.ordinal()] && dir != tubeStack.heading.getOpposite()) {
                        tubeStack.heading = dir;
                        break;
                    }
                }
            } else {
                // when we are at an intersection
                if (!tube.getWorld().isRemote) {
                    Pair<ForgeDirection, TileEntity> heading = getHeadingForItem(tubeStack, false);
                    if (heading == null) {
                        // if no valid destination
                        for (int i = 0; i < 6; i++) {
                            TubeEdge edge = getNode().edges[i];
                            if (edge != null) {
                                // this will allow the item to ignore the color mask when
                                tubeStack.heading = ForgeDirection.getOrientation(i);
                                // there's really no option left.
                                if (canPassThroughMask(tubeStack.color, edge.colorMask)) {
                                    // just a specific direction for now.
                                    tubeStack.heading = ForgeDirection.getOrientation(i);
                                    break;
                                }
                            }
                        }
                    } else {
                        tubeStack.heading = heading.getKey();
                    }
                    BPNetworkHandler.INSTANCE.sendToAllAround(new MessageRedirectTubeStack(tube, tubeStack), tube.getWorld());
                } else {
                    tubeStack.enabled = false;
                }
            }
        } else if (tubeStack.progress >= 1) {
            // when the item reached the end of the tube.
            TileEntity output = tube.getTileCache(tubeStack.heading);
            PneumaticTube tube = this.tube.getPartCache(tubeStack.heading);
            if (tube != null) {
                // we don't need to check connections, that's catched earlier.
                TubeLogic logic = tube.getLogic();
                tubeStack.progress = 0;
                tubeStack.oldProgress = -tubeStack.getSpeed() * TubeStack.tickTimeMultiplier;
                // transfer to another tube.
                logic.tubeStacks.add(tubeStack);
                iterator.remove();
            } else if (!this.tube.getWorld().isRemote) {
                ItemStack remainder = tubeStack.stack;
                if (output instanceof ITubeConnection && ((ITubeConnection) output).isConnectedTo(tubeStack.heading.getOpposite())) {
                    TubeStack rem = ((ITubeConnection) output).acceptItemFromTube(tubeStack, tubeStack.heading.getOpposite(), false);
                    if (rem != null)
                        remainder = rem.stack;
                    else
                        remainder = null;
                }
                if (remainder != null)
                    remainder = IOHelper.insert(output, remainder, tubeStack.heading.getOpposite(), tubeStack.color, false);
                if (remainder != null) {
                    if (injectStack(remainder, tubeStack.heading.getOpposite(), tubeStack.color, true)) {
                        tubeStack.stack = remainder;
                        tubeStack.progress = 0;
                        tubeStack.oldProgress = 0;
                        tubeStack.heading = tubeStack.heading.getOpposite();
                        this.tube.sendUpdatePacket();
                    } else {
                        EntityItem entity = new EntityItem(this.tube.getWorld(), this.tube.getX() + 0.5 + tubeStack.heading.offsetX * tubeStack.progress * 0.5, this.tube.getY() + 0.5 + tubeStack.heading.offsetY * tubeStack.progress * 0.5, this.tube.getZ() + 0.5 + tubeStack.heading.offsetX * tubeStack.progress * 0.5, remainder);
                        this.tube.getWorld().spawnEntityInWorld(entity);
                        iterator.remove();
                    }
                } else {
                    iterator.remove();
                }
            } else {
                iterator.remove();
            }
        } else if (tubeStack.idleCounter > 100) {
            iterator.remove();
        }
    }
}
Also used : ITubeConnection(com.bluepowermod.api.tube.ITubeConnection) MessageRedirectTubeStack(com.bluepowermod.network.message.MessageRedirectTubeStack) MessageRedirectTubeStack(com.bluepowermod.network.message.MessageRedirectTubeStack) TileEntity(net.minecraft.tileentity.TileEntity) IPneumaticTube(com.bluepowermod.api.tube.IPneumaticTube) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Aggregations

ITubeConnection (com.bluepowermod.api.tube.ITubeConnection)3 TileEntity (net.minecraft.tileentity.TileEntity)2 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)2 IPneumaticTube (com.bluepowermod.api.tube.IPneumaticTube)1 MessageRedirectTubeStack (com.bluepowermod.network.message.MessageRedirectTubeStack)1 PneumaticTube (com.bluepowermod.part.tube.PneumaticTube)1 TubeLogic (com.bluepowermod.part.tube.TubeLogic)1 TubeStack (com.bluepowermod.part.tube.TubeStack)1 EntityItem (net.minecraft.entity.item.EntityItem)1 IInventory (net.minecraft.inventory.IInventory)1 ItemStack (net.minecraft.item.ItemStack)1