Search in sources :

Example 26 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.

the class TileEntityMixer method update.

@Override
public void update() {
    super.update();
    if (isDummy() || isRSDisabled())
        return;
    if (worldObj.isRemote) {
        if (shouldRenderAsActive()) {
            if (worldObj.rand.nextInt(8) == 0) {
                FluidStack fs = !tank.fluids.isEmpty() ? tank.fluids.get(0) : null;
                if (fs != null) {
                    float amount = tank.getFluidAmount() / (float) tank.getCapacity() * 1.125f;
                    Vec3d partPos = new Vec3d(getPos().getX() + .5f + facing.getFrontOffsetX() * .5f + (mirrored ? facing.rotateYCCW() : facing.rotateY()).getFrontOffsetX() * .5f, getPos().getY() - .0625f + amount, getPos().getZ() + .5f + facing.getFrontOffsetZ() * .5f + (mirrored ? facing.rotateYCCW() : facing.rotateY()).getFrontOffsetZ() * .5f);
                    float r = worldObj.rand.nextFloat() * .8125f;
                    float angleRad = (float) Math.toRadians(animation_agitator);
                    partPos = partPos.addVector(r * Math.cos(angleRad), 0, r * Math.sin(angleRad));
                    if (worldObj.rand.nextBoolean())
                        ImmersiveEngineering.proxy.spawnBubbleFX(worldObj, fs, partPos.xCoord, partPos.yCoord, partPos.zCoord, 0, 0, 0);
                    else
                        ImmersiveEngineering.proxy.spawnFluidSplashFX(worldObj, fs, partPos.xCoord, partPos.yCoord, partPos.zCoord, 0, 0, 0);
                }
            }
            animation_agitator = (animation_agitator + 9) % 360;
        }
    } else {
        boolean update = false;
        boolean foundRecipe = false;
        if (energyStorage.getEnergyStored() > 0 && processQueue.size() < this.getProcessQueueMaxLength()) {
            int tankAmount = tank.getFluidAmount();
            if (tankAmount > 0) {
                Set<Integer> usedInvSlots = new HashSet<Integer>();
                for (MultiblockProcess<MixerRecipe> process : processQueue) if (process instanceof MultiblockProcessInMachine)
                    for (int i : ((MultiblockProcessInMachine<MixerRecipe>) process).inputSlots) usedInvSlots.add(i);
                ItemStack[] components = new ItemStack[this.inventory.length];
                for (int i = 0; i < components.length; i++) if (!usedInvSlots.contains(i))
                    components[i] = inventory[i];
                for (FluidStack fs : tank.fluids) {
                    MixerRecipe recipe = MixerRecipe.findRecipe(fs, components);
                    if (recipe != null) {
                        foundRecipe = true;
                        MultiblockProcessInMachine process = new MultiblockProcessMixer(recipe, recipe.getUsedSlots(fs, components)).setInputTanks(0);
                        if (this.addProcessToQueue(process, true)) {
                            this.addProcessToQueue(process, false);
                            update = true;
                        }
                    }
                }
            }
        }
        if (this.tank.getFluidTypes() > 1 || !foundRecipe || outputAll) {
            BlockPos outputPos = this.getPos().down().offset(facing.getOpposite(), 2);
            IFluidHandler output = FluidUtil.getFluidHandler(worldObj, outputPos, facing);
            if (output != null) {
                if (!outputAll) {
                    FluidStack inTank = this.tank.getFluid();
                    if (inTank != null) {
                        FluidStack out = Utils.copyFluidStackWithAmount(inTank, Math.min(inTank.amount, 80), false);
                        int accepted = output.fill(out, false);
                        if (accepted > 0) {
                            int drained = output.fill(Utils.copyFluidStackWithAmount(out, Math.min(out.amount, accepted), false), true);
                            this.tank.drain(drained, true);
                            update = true;
                        }
                    }
                } else {
                    int totalOut = 0;
                    Iterator<FluidStack> it = this.tank.fluids.iterator();
                    while (it.hasNext()) {
                        FluidStack fs = it.next();
                        if (fs != null) {
                            FluidStack out = Utils.copyFluidStackWithAmount(fs, Math.min(fs.amount, 80 - totalOut), false);
                            int accepted = output.fill(out, false);
                            if (accepted > 0) {
                                int drained = output.fill(Utils.copyFluidStackWithAmount(out, Math.min(out.amount, accepted), false), true);
                                MultiFluidTank.drain(drained, fs, it, true);
                                totalOut += drained;
                                update = true;
                            }
                            if (totalOut >= 80)
                                break;
                        }
                    }
                }
            }
        }
        if (update) {
            this.markDirty();
            this.markContainingBlockForUpdate(null);
        }
    }
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) Vec3d(net.minecraft.util.math.Vec3d) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) MixerRecipe(blusunrize.immersiveengineering.api.crafting.MixerRecipe) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 27 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.

the class TileEntityFermenter method update.

@Override
public void update() {
    super.update();
    if (isDummy() || isRSDisabled())
        return;
    if (!worldObj.isRemote) {
        boolean update = false;
        if (energyStorage.getEnergyStored() > 0 && processQueue.size() < this.getProcessQueueMaxLength()) {
            final int[] usedInvSlots = new int[8];
            for (MultiblockProcess process : processQueue) if (process instanceof MultiblockProcessInMachine)
                for (int i : ((MultiblockProcessInMachine) process).inputSlots) usedInvSlots[i]++;
            Integer[] preferredSlots = new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7 };
            Arrays.sort(preferredSlots, 0, 8, new Comparator<Integer>() {

                @Override
                public int compare(Integer arg0, Integer arg1) {
                    return Integer.compare(usedInvSlots[arg0], usedInvSlots[arg1]);
                }
            });
            for (int slot : preferredSlots) {
                ItemStack stack = this.getInventory()[slot];
                if (stack != null) {
                    stack = stack.copy();
                    stack.stackSize -= usedInvSlots[slot];
                }
                if (stack != null && stack.stackSize > 0) {
                    FermenterRecipe recipe = this.findRecipeForInsertion(stack);
                    if (recipe != null) {
                        MultiblockProcessInMachine<FermenterRecipe> process = new MultiblockProcessInMachine(recipe, slot);
                        if (this.addProcessToQueue(process, true)) {
                            this.addProcessToQueue(process, false);
                            update = true;
                        }
                    }
                }
            }
        }
        EnumFacing fw = mirrored ? facing.rotateYCCW() : facing.rotateY();
        if (this.tanks[0].getFluidAmount() > 0) {
            FluidStack out = Utils.copyFluidStackWithAmount(this.tanks[0].getFluid(), Math.min(this.tanks[0].getFluidAmount(), 80), false);
            BlockPos outputPos = this.getPos().add(0, -1, 0).offset(fw, 2);
            IFluidHandler output = FluidUtil.getFluidHandler(worldObj, outputPos, fw.getOpposite());
            if (output != null) {
                int accepted = output.fill(out, false);
                if (accepted > 0) {
                    int drained = output.fill(Utils.copyFluidStackWithAmount(out, Math.min(out.amount, accepted), false), true);
                    this.tanks[0].drain(drained, true);
                    update = true;
                }
            }
            ItemStack empty = getInventory()[9];
            if (empty != null && tanks[0].getFluidAmount() > 0) {
                ItemStack full = Utils.fillFluidContainer(tanks[0], empty, getInventory()[10], null);
                if (full != null) {
                    if (getInventory()[10] != null && OreDictionary.itemMatches(full, getInventory()[10], true))
                        getInventory()[10].stackSize += full.stackSize;
                    else
                        getInventory()[10] = full;
                    if (--inventory[9].stackSize <= 0)
                        inventory[9] = null;
                }
            }
        }
        if (inventory[8] != null && worldObj.getTotalWorldTime() % 8 == 0) {
            BlockPos outputPos = this.getPos().offset(fw);
            TileEntity outputTile = this.worldObj.getTileEntity(outputPos);
            if (outputTile != null) {
                ItemStack stack = Utils.copyStackWithAmount(inventory[8], 1);
                stack = Utils.insertStackIntoInventory(outputTile, stack, fw.getOpposite());
                if (stack == null)
                    if ((--this.inventory[8].stackSize) <= 0)
                        this.inventory[8] = null;
            }
        }
        if (update) {
            this.markDirty();
            this.markContainingBlockForUpdate(null);
        }
    }
}
Also used : FermenterRecipe(blusunrize.immersiveengineering.api.crafting.FermenterRecipe) FluidStack(net.minecraftforge.fluids.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 28 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.

the class TileEntityFluidPipe method getConnectedFluidHandlers.

public static Set<DirectionalFluidOutput> getConnectedFluidHandlers(BlockPos node, World world) {
    if (indirectConnections.containsKey(node))
        return indirectConnections.get(node);
    ArrayList<BlockPos> openList = new ArrayList();
    ArrayList<BlockPos> closedList = new ArrayList();
    Set<DirectionalFluidOutput> fluidHandlers = Collections.newSetFromMap(new ConcurrentHashMap<DirectionalFluidOutput, Boolean>());
    openList.add(node);
    while (!openList.isEmpty() && closedList.size() < 1024) {
        BlockPos next = openList.get(0);
        if (world.isBlockLoaded(next)) {
            TileEntity pipeTile = world.getTileEntity(next);
            if (!closedList.contains(next) && (pipeTile instanceof IFluidPipe)) {
                if (pipeTile instanceof TileEntityFluidPipe)
                    closedList.add(next);
                IFluidTankProperties[] tankInfo;
                for (int i = 0; i < 6; i++) {
                    //						boolean b = (te instanceof TileEntityFluidPipe)? (((TileEntityFluidPipe) te).sideConfig[i]==0): (((TileEntityFluidPump) te).sideConfig[i]==1);
                    EnumFacing fd = EnumFacing.getFront(i);
                    if (((IFluidPipe) pipeTile).hasOutputConnection(fd)) {
                        BlockPos nextPos = next.offset(fd);
                        if (world.isBlockLoaded(nextPos)) {
                            TileEntity adjacentTile = world.getTileEntity(nextPos);
                            if (adjacentTile != null)
                                if (adjacentTile instanceof TileEntityFluidPipe)
                                    openList.add(nextPos);
                                else if (adjacentTile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, fd.getOpposite())) {
                                    IFluidHandler handler = adjacentTile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, fd.getOpposite());
                                    tankInfo = handler.getTankProperties();
                                    if (tankInfo != null && tankInfo.length > 0)
                                        fluidHandlers.add(new DirectionalFluidOutput(handler, adjacentTile, fd));
                                }
                        }
                    }
                }
            }
        }
        openList.remove(0);
    }
    if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) {
        if (!indirectConnections.containsKey(node)) {
            indirectConnections.put(node, newSetFromMap(new ConcurrentHashMap<DirectionalFluidOutput, Boolean>()));
            indirectConnections.get(node).addAll(fluidHandlers);
        }
    }
    return fluidHandlers;
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) IFluidTankProperties(net.minecraftforge.fluids.capability.IFluidTankProperties) BlockPos(net.minecraft.util.math.BlockPos) IFluidPipe(blusunrize.immersiveengineering.api.fluid.IFluidPipe) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)28 FluidStack (net.minecraftforge.fluids.FluidStack)16 TileEntity (net.minecraft.tileentity.TileEntity)12 ItemStack (net.minecraft.item.ItemStack)11 EnumFacing (net.minecraft.util.EnumFacing)10 BlockPos (net.minecraft.util.math.BlockPos)7 IBlockState (net.minecraft.block.state.IBlockState)3 IFluidTankProperties (net.minecraftforge.fluids.capability.IFluidTankProperties)3 ArrayList (java.util.ArrayList)2 Nonnull (javax.annotation.Nonnull)2 AdvancedFluidHandler (mods.railcraft.common.fluids.AdvancedFluidHandler)2 Block (net.minecraft.block.Block)2 BlockLiquid (net.minecraft.block.BlockLiquid)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 Fluid (net.minecraftforge.fluids.Fluid)2 FluidActionResult (net.minecraftforge.fluids.FluidActionResult)2 FermenterRecipe (blusunrize.immersiveengineering.api.crafting.FermenterRecipe)1 MixerRecipe (blusunrize.immersiveengineering.api.crafting.MixerRecipe)1 RefineryRecipe (blusunrize.immersiveengineering.api.crafting.RefineryRecipe)1 SqueezerRecipe (blusunrize.immersiveengineering.api.crafting.SqueezerRecipe)1