Example usage for net.minecraftforge.fluids FluidStack copy

List of usage examples for net.minecraftforge.fluids FluidStack copy

Introduction

In this page you can find the example usage for net.minecraftforge.fluids FluidStack copy.

Prototype

public FluidStack copy() 

Source Link

Usage

From source file:additionalpipes.pipes.PipeFluidsAdvancedInsertion.java

License:Minecraft Mod Public

private void updateCache() {
    FluidStack pushStack = transport.internalTanks[ForgeDirection.UNKNOWN.ordinal()].getFluid();
    if (pushStack == null || pushStack.amount < 1) {
        return;/*ww  w .jav  a 2  s .  co  m*/
    }
    FluidStack testStack = pushStack.copy();
    testStack.amount = 1;

    outputOpenCache.clear();
    EnumSet<ForgeDirection> connectedPipes = !logic.isDisabled() ? EnumSet.noneOf(ForgeDirection.class)
            : outputOpenCache;

    for (ForgeDirection o : ForgeDirection.VALID_DIRECTIONS) {
        TileEntity tile = container.getTile(o);
        if (tile instanceof TileGenericPipe) {
            if (Utils.checkPipesConnections(tile, container)) {
                connectedPipes.add(o);
            }
        } else if (!logic.isDisabled() && tile instanceof IFluidHandler) {
            IFluidHandler tank = (IFluidHandler) tile;
            if (tank.fill(o, testStack, false) > 0) {
                outputOpenCache.add(o);
            }
        }
    }

    if (outputOpenCache.isEmpty()) {
        outputOpenCache = connectedPipes;
    }
}

From source file:additionalpipes.pipes.PipeFluidsTeleport.java

License:Minecraft Mod Public

@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
    if (CoreProxy.proxy.isRenderWorld(container.worldObj)) {
        return 0;
    }//from   ww  w  . j av  a2  s.  c  o  m

    FluidStack fluid = resource.copy();

    List<PipeFluidsTeleport> connectedPipes = logic.getConnectedPipes(true);
    connectedPipes = Lists.newArrayList(connectedPipes);
    Collections.shuffle(connectedPipes, container.worldObj.rand);
    OUTER: for (PipeFluidsTeleport destPipe : connectedPipes) {
        List<Pair<ForgeDirection, IFluidHandler>> fluidsList = destPipe.getPossibleFluidsMovements();
        Collections.shuffle(fluidsList, container.worldObj.rand);
        for (Pair<ForgeDirection, IFluidHandler> fluids : fluidsList) {
            ForgeDirection side = fluids.getLeft();
            IFluidHandler tank = fluids.getRight();

            int usedSingle = tank.fill(side, fluid, false);
            if (usedSingle > 0) {
                int energy = APUtils.divideAndCeil(usedSingle, AdditionalPipes.unitFluids);
                if (doFill ? logic.useEnergy(energy) : logic.canUseEnergy(energy)) {
                    if (doFill) {
                        tank.fill(side, fluid, doFill);
                    }
                    fluid.amount -= usedSingle;
                    if (fluid.amount <= 0) {
                        break OUTER;
                    }
                }
            }
        }
    }

    return resource.amount - fluid.amount;
}

From source file:appeng.parts.p2p.PartP2PFluids.java

License:Open Source License

@Override
public int fill(FluidStack resource, boolean doFill) {
    final Deque<PartP2PFluids> stack = this.getDepth();

    for (final PartP2PFluids t : stack) {
        if (t == this) {
            return 0;
        }//from  w w w.j a va  2s  .  c  om
    }

    stack.push(this);

    final List<PartP2PFluids> list = this.getOutputs(resource.getFluid());
    int requestTotal = 0;

    Iterator<PartP2PFluids> i = list.iterator();

    while (i.hasNext()) {
        final PartP2PFluids l = i.next();
        final IFluidHandler tank = l.getTarget();
        if (tank != null) {
            l.tmpUsed = tank.fill(resource.copy(), false);
        } else {
            l.tmpUsed = 0;
        }

        if (l.tmpUsed <= 0) {
            i.remove();
        } else {
            requestTotal += l.tmpUsed;
        }
    }

    if (requestTotal <= 0) {
        if (stack.pop() != this) {
            throw new IllegalStateException("Invalid Recursion detected.");
        }

        return 0;
    }

    if (!doFill) {
        if (stack.pop() != this) {
            throw new IllegalStateException("Invalid Recursion detected.");
        }

        return Math.min(resource.amount, requestTotal);
    }

    int available = resource.amount;

    i = list.iterator();
    int used = 0;

    while (i.hasNext()) {
        final PartP2PFluids l = i.next();

        final FluidStack insert = resource.copy();
        insert.amount = (int) Math.ceil(insert.amount * ((double) l.tmpUsed / (double) requestTotal));
        if (insert.amount > available) {
            insert.amount = available;
        }

        final IFluidHandler tank = l.getTarget();
        if (tank != null) {
            l.tmpUsed = tank.fill(insert.copy(), true);
        } else {
            l.tmpUsed = 0;
        }

        available -= insert.amount;
        used += insert.amount;
    }

    if (stack.pop() != this) {
        throw new IllegalStateException("Invalid Recursion detected.");
    }

    return used;
}

From source file:appeng.parts.p2p.PartP2PLiquids.java

License:Open Source License

@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
    Stack<PartP2PLiquids> stack = this.getDepth();

    for (PartP2PLiquids t : stack) {
        if (t == this) {
            return 0;
        }// w  w  w  . ja va2s  .  c  om
    }

    stack.push(this);

    List<PartP2PLiquids> list = this.getOutputs(resource.getFluid());
    int requestTotal = 0;

    Iterator<PartP2PLiquids> i = list.iterator();
    while (i.hasNext()) {
        PartP2PLiquids l = i.next();
        IFluidHandler tank = l.getTarget();
        if (tank != null) {
            l.tmpUsed = tank.fill(l.side.getOpposite(), resource.copy(), false);
        } else {
            l.tmpUsed = 0;
        }

        if (l.tmpUsed <= 0) {
            i.remove();
        } else {
            requestTotal += l.tmpUsed;
        }
    }

    if (requestTotal <= 0) {
        if (stack.pop() != this) {
            throw new IllegalStateException("Invalid Recursion detected.");
        }

        return 0;
    }

    if (!doFill) {
        if (stack.pop() != this) {
            throw new IllegalStateException("Invalid Recursion detected.");
        }

        return Math.min(resource.amount, requestTotal);
    }

    int available = resource.amount;
    int used = 0;

    i = list.iterator();
    while (i.hasNext()) {
        PartP2PLiquids l = i.next();

        FluidStack insert = resource.copy();
        insert.amount = (int) Math.ceil(insert.amount * ((double) l.tmpUsed / (double) requestTotal));
        if (insert.amount > available) {
            insert.amount = available;
        }

        IFluidHandler tank = l.getTarget();
        if (tank != null) {
            l.tmpUsed = tank.fill(l.side.getOpposite(), insert.copy(), true);
        } else {
            l.tmpUsed = 0;
        }

        available -= insert.amount;
        used += insert.amount;
    }

    if (stack.pop() != this) {
        throw new IllegalStateException("Invalid Recursion detected.");
    }

    return used;
}

From source file:blusunrize.immersiveengineering.api.crafting.MultiblockRecipe.java

public void setupJEI() {
    if (inputList != null) {
        this.jeiItemInputList = new ArrayList[inputList.size()];
        this.jeiTotalItemInputList = new ArrayList();
        for (int i = 0; i < inputList.size(); i++) {
            IngredientStack ingr = inputList.get(i);
            ArrayList list = new ArrayList();
            if (ingr.oreName != null)
                for (ItemStack stack : OreDictionary.getOres(ingr.oreName))
                    list.add(ApiUtils.copyStackWithAmount(stack, ingr.inputSize));
            else if (ingr.stackList != null)
                for (ItemStack stack : ingr.stackList)
                    list.add(ApiUtils.copyStackWithAmount(stack, ingr.inputSize));
            else/*from   w  ww  .j a  v  a  2s .c om*/
                list.add(ApiUtils.copyStackWithAmount(ingr.stack, ingr.inputSize));

            this.jeiItemInputList[i] = list;
            this.jeiTotalItemInputList.addAll(list);
        }
    } else
        this.jeiTotalItemInputList = Collections.emptyList();
    if (outputList != null) {
        this.jeiItemOutputList = new ArrayList[outputList.size()];
        this.jeiTotalItemOutputList = new ArrayList();
        for (int i = 0; i < outputList.size(); i++) {
            ItemStack s = outputList.get(i);
            ArrayList<ItemStack> list = Lists.newArrayList(!s.isEmpty() ? s.copy() : ItemStack.EMPTY);
            this.jeiItemOutputList[i] = list;
            this.jeiTotalItemOutputList.addAll(list);
        }
    } else
        this.jeiTotalItemOutputList = Collections.emptyList();
    if (fluidInputList != null) {
        this.jeiFluidInputList = new ArrayList();
        for (int i = 0; i < fluidInputList.size(); i++) {
            FluidStack fs = fluidInputList.get(i);
            if (fs != null)
                this.jeiFluidInputList.add(fs.copy());
        }
    } else
        this.jeiFluidInputList = Collections.emptyList();
    if (fluidOutputList != null) {
        this.jeiFluidOutputList = new ArrayList();
        for (int i = 0; i < fluidOutputList.size(); i++) {
            FluidStack fs = fluidOutputList.get(i);
            if (fs != null)
                this.jeiFluidOutputList.add(fs.copy());
        }
    } else
        this.jeiFluidOutputList = Collections.emptyList();
}

From source file:blusunrize.immersiveengineering.common.util.IEItemFluidHandler.java

@Override
public int fill(FluidStack resource, boolean doFill) {
    if (container.getCount() != 1 || resource == null || resource.amount <= 0 || !canFillFluidType(resource))
        return 0;

    FluidStack contained = getFluid();/*  w  ww. ja  v a2s . c om*/
    if (contained == null) {
        int fillAmount = Math.min(getCapacity(), resource.amount);
        if (doFill) {
            FluidStack filled = resource.copy();
            filled.amount = fillAmount;
            setFluid(filled);
        }
        return fillAmount;
    } else {
        if (contained.isFluidEqual(resource)) {
            int fillAmount = Math.min(getCapacity() - contained.amount, resource.amount);
            if (doFill && fillAmount > 0) {
                contained.amount += fillAmount;
                setFluid(contained);
            }
            return fillAmount;
        }
        return 0;
    }
}

From source file:buildcraft.additionalpipes.pipes.PipeBehaviorTeleportFluids.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
@PipeEventHandler//from  w  w  w. jav a 2 s.  c  o  m
public void onMoveCenter(PipeEventFluid.OnMoveToCentre event) {
    if (canSend()) {
        ArrayList<PipeBehaviorTeleportFluids> connectedPipes = (ArrayList) TeleportManager.instance
                .getConnectedPipes(this, false, true);

        Log.debug("[FluidTeleportPipe] Got " + event.fluid.amount + " MB of fluid");

        FluidStack remaining = event.fluid.copy();

        // loop until we're out of fluid, or until no pipes need it
        while (remaining.amount > 0 && connectedPipes.size() > 0) {

            // divide the fluid into apportionments for each pipe
            FluidStack maxPerIteration = remaining.copy();
            maxPerIteration.amount /= connectedPipes.size(); // it's OK if we have rounding errors, it will get resolved eventually

            // insert one allocation into each pipe that needs it
            Iterator<PipeBehaviorTeleportFluids> pipeIter = connectedPipes.iterator();
            while (pipeIter.hasNext()) {
                PipeBehaviorTeleportFluids pipe = pipeIter.next();

                int inserted = ((PipeFlowFluids) pipe.pipe.getFlow()).insertFluidsForce(maxPerIteration, null,
                        false);

                if (inserted == 0) {
                    // this pipe is done, remove it
                    pipeIter.remove();
                } else {
                    remaining.amount -= inserted;
                }

            }
        }

        if (remaining.amount > 0) {
            Log.debug(
                    "PipeLiquidsTeleport's PreMoveToCentre event handler requested more fluid than can be handled!  "
                            + remaining.amount + " MB is left and will be rejected.");
        }

        // update event data
        for (EnumFacing side : EnumFacing.VALUES) {
            // allow no fluid to enter the center, regardless
            event.fluidEnteringCentre[side.ordinal()] = 0;

            if (remaining.amount > 0) {
                // decrease the amount of fluid entering the side to match what was actually consumed
                int fluidBlockedFromEntering = Math.min(event.fluidLeavingSide[side.ordinal()],
                        remaining.amount);

                event.fluidLeavingSide[side.ordinal()] -= fluidBlockedFromEntering;
                remaining.amount -= fluidBlockedFromEntering;
            }
        }

    }
}

From source file:buildcraft.core.robots.AIRobotLoadFluids.java

License:Minecraft Mod Public

private void doLoad() {
    if (robot.getDockingStation() != null) {
        DockingStation station = (DockingStation) robot.getDockingStation();

        if (!ActionRobotFilter.canInteractWithFluid(station, filter, ActionStationProvideFluids.class)) {
            return;
        }/*  w w w .  j  av a  2s.  c o m*/

        for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
            TileEntity nearbyTile = robot.worldObj.getTileEntity(station.x() + dir.offsetX,
                    station.y() + dir.offsetY, station.z() + dir.offsetZ);

            if (nearbyTile != null && nearbyTile instanceof IFluidHandler) {
                IFluidHandler handler = (IFluidHandler) nearbyTile;
                FluidStack drainable = handler.drain(station.side, FluidContainerRegistry.BUCKET_VOLUME, false);

                if (drainable != null && filter.matches(drainable.getFluid())) {

                    drainable = drainable.copy();

                    int filled = robot.fill(ForgeDirection.UNKNOWN, drainable, true);

                    if (filled > 0) {
                        drainable.amount = filled;
                        handler.drain(station.side, drainable, true);
                        loaded += filled;
                        return;
                    }
                }
            }
        }
    }
}

From source file:buildcraft.core.robots.AIRobotUnloadFluids.java

License:Minecraft Mod Public

private void doLoad() {
    if (robot.getDockingStation() != null) {
        DockingStation station = (DockingStation) robot.getDockingStation();

        if (!ActionRobotFilter.canInteractWithFluid(station,
                new SimpleFluidFilter(robot.getTankInfo(ForgeDirection.UNKNOWN)[0].fluid),
                ActionStationAcceptFluids.class)) {
            return;
        }//from   w  ww  . j av  a 2s  . co  m

        for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
            TileEntity nearbyTile = robot.worldObj.getTileEntity(station.x() + dir.offsetX,
                    station.y() + dir.offsetY, station.z() + dir.offsetZ);

            if (nearbyTile != null && nearbyTile instanceof IFluidHandler) {
                IFluidHandler handler = (IFluidHandler) nearbyTile;

                FluidStack drainable = robot.drain(ForgeDirection.UNKNOWN, FluidContainerRegistry.BUCKET_VOLUME,
                        false);

                if (drainable != null) {
                    drainable = drainable.copy();

                    int filled = handler.fill(station.side, drainable, true);

                    if (filled > 0) {
                        drainable.amount = filled;
                        robot.drain(ForgeDirection.UNKNOWN, drainable, true);
                        unloaded += filled;
                        return;
                    }
                }
            }
        }
    }
}

From source file:buildcraft.factory.TileRefinery.java

License:Minecraft Mod Public

@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
    int used = 0;
    FluidStack resourceUsing = resource.copy();

    used += tank1.fill(resourceUsing, doFill);
    resourceUsing.amount -= used;//  w  w w . ja v a 2  s. com
    used += tank2.fill(resourceUsing, doFill);

    return used;
}