Search in sources :

Example 1 with MessageRedirectTubeStack

use of com.bluepowermod.network.message.MessageRedirectTubeStack 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

IPneumaticTube (com.bluepowermod.api.tube.IPneumaticTube)1 ITubeConnection (com.bluepowermod.api.tube.ITubeConnection)1 MessageRedirectTubeStack (com.bluepowermod.network.message.MessageRedirectTubeStack)1 EntityItem (net.minecraft.entity.item.EntityItem)1 ItemStack (net.minecraft.item.ItemStack)1 TileEntity (net.minecraft.tileentity.TileEntity)1 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)1