Java Code Examples for net.minecraftforge.fluids.capability.IFluidHandler#fill()

The following examples show how to use net.minecraftforge.fluids.capability.IFluidHandler#fill() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FillerTileEntity.java    From EmergingTechnology with MIT License 6 votes vote down vote up
private void fillAdjacent() {
    for (EnumFacing facing : EnumFacing.VALUES) {
        TileEntity neighbour = this.world.getTileEntity(this.pos.offset(facing));

        // Return if no tile entity or another filler
        if (neighbour == null || neighbour instanceof FillerTileEntity) {
            continue;
        }

        IFluidHandler neighbourFluidHandler = neighbour
                .getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing.getOpposite());

        // Return if neighbour has no fluid tank
        if (neighbourFluidHandler == null) {
            continue;
        }

        // Fill the neighbour
        neighbourFluidHandler.fill(new FluidStack(FluidRegistry.WATER,
                EmergingTechnologyConfig.HYDROPONICS_MODULE.FILLER.fillerFluidTransferRate), true);
    }
}
 
Example 2
Source File: CoverPump.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static int moveHandlerFluids(IFluidHandler sourceHandler, IFluidHandler destHandler, int transferLimit, Predicate<FluidStack> fluidFilter) {
    int fluidLeftToTransfer = transferLimit;
    for (IFluidTankProperties tankProperties : sourceHandler.getTankProperties()) {
        FluidStack currentFluid = tankProperties.getContents();
        if (currentFluid == null || currentFluid.amount == 0 || !fluidFilter.test(currentFluid)) continue;
        currentFluid.amount = fluidLeftToTransfer;
        FluidStack fluidStack = sourceHandler.drain(currentFluid, false);
        if (fluidStack == null || fluidStack.amount == 0) continue;
        int canInsertAmount = destHandler.fill(fluidStack, false);
        if (canInsertAmount > 0) {
            fluidStack.amount = canInsertAmount;
            fluidStack = sourceHandler.drain(fluidStack, true);
            if (fluidStack != null && fluidStack.amount > 0) {
                destHandler.fill(fluidStack, true);

                fluidLeftToTransfer -= fluidStack.amount;
                if (fluidLeftToTransfer == 0) break;
            }
        }
    }
    return transferLimit - fluidLeftToTransfer;
}
 
Example 3
Source File: TileFluidTank.java    From AdvancedRocketry with MIT License 6 votes vote down vote up
@Override
public int fill(FluidStack resource, boolean doFill) {
	
	if(resource == null)
		return 0;
	
	IFluidHandler handler = this.getFluidTankInDirection(EnumFacing.DOWN);
	int amt = 0;

	if(handler != null) {
		amt = handler.fill(resource, doFill);
	}
	//Copy to avoid modifiying the passed one
	FluidStack resource2 = resource.copy();
	resource2.amount -= amt;
	if(resource2.amount > 0)
		amt += super.fill(resource2, doFill);
	
	if(amt > 0 && doFill)
		fluidChanged = true;	
	
	checkForUpdate();
	
	return amt;
}
 
Example 4
Source File: ItemBlockFluidTank.java    From AdvancedRocketry with MIT License 6 votes vote down vote up
@Override
public boolean placeBlockAt(ItemStack stack, EntityPlayer player,
		World world, BlockPos pos, EnumFacing side, float hitX, float hitY,
		float hitZ, IBlockState newState) {
	super.placeBlockAt(stack, player, world, pos, side, hitX, hitY, hitZ,
			newState);
	
	TileEntity tile = world.getTileEntity(pos);
	
	if(tile != null && tile instanceof TileFluidTank) {
		IFluidHandler handler = tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.DOWN);
		ItemStack stack2 = stack.copy();
		stack2.setCount(1);
		handler.fill(drain(stack2, Integer.MAX_VALUE), true);
	}
	
	return true;
}
 
Example 5
Source File: InjectorTileEntity.java    From EmergingTechnology with MIT License 5 votes vote down vote up
private void pushToFluidConsumers() {
    for (EnumFacing facing : EnumFacing.VALUES) {

        if (this.getNutrientFluid() < EmergingTechnologyConfig.HYDROPONICS_MODULE.INJECTOR.injectorFluidTransferRate) {
            return;
        }

        TileEntity neighbour = this.world.getTileEntity(this.pos.offset(facing));

        // Return if no tile entity
        if (neighbour == null) {
            continue;
        }

        IFluidHandler neighbourFluidHandler = neighbour
                .getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing.getOpposite());

        // Return if neighbour has no fluid tank
        if (neighbourFluidHandler == null) {
            continue;
        }

        // Fill the neighbour
        int filled = neighbourFluidHandler.fill(new FluidStack(ModFluids.NUTRIENT,
                EmergingTechnologyConfig.HYDROPONICS_MODULE.INJECTOR.injectorFluidTransferRate), true);

        this.nutrientFluidHandler.drain(filled, true);
    }
}
 
Example 6
Source File: MetaTileEntity.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean addFluidsToFluidHandler(IFluidHandler handler, boolean simulate, List<FluidStack> items) {
    boolean filledAll = true;
    for (FluidStack stack : items) {
        int filled = handler.fill(stack, !simulate);
        filledAll &= filled == stack.amount;
        if (!filledAll && simulate) return false;
    }
    return filledAll;
}
 
Example 7
Source File: TileFloader.java    From YouTubeModdingTutorial with MIT License 5 votes vote down vote up
private boolean findTankAndFill() {
    for (EnumFacing facing : EnumFacing.VALUES) {
        if (facing != EnumFacing.DOWN) {
            TileEntity te = world.getTileEntity(pos.offset(facing));
            if (te != null && te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing.getOpposite())) {
                IFluidHandler handler = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing.getOpposite());
                // Simulate filling
                if (handler != null && handler.fill(new FluidStack(ModLiquids.fload, 100), true) != 0) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 8
Source File: TileEnderTank.java    From EnderStorage with MIT License 5 votes vote down vote up
private void ejectLiquid() {
    IFluidHandler source = getStorage();
    for (Direction side : Direction.BY_INDEX) {
        IFluidHandler dest = capCache.getCapabilityOr(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side, EmptyFluidHandler.INSTANCE);
        FluidStack drain = source.drain(100, IFluidHandler.FluidAction.SIMULATE);
        if (!drain.isEmpty()) {
            int qty = dest.fill(drain, IFluidHandler.FluidAction.EXECUTE);
            if (qty > 0) {
                source.drain(qty, IFluidHandler.FluidAction.EXECUTE);
            }
        }
    }
}
 
Example 9
Source File: TileRocketFluidLoader.java    From AdvancedRocketry with MIT License 5 votes vote down vote up
@Override
public void update() {
	//Move a stack of items
	if(!world.isRemote && rocket != null) {

		boolean isAllowToOperate = (inputstate == RedstoneState.OFF || isStateActive(inputstate, getStrongPowerForSides(world, getPos())));

		List<TileEntity> tiles = rocket.storage.getFluidTiles();
		boolean rocketContainsItems = false;

		//Function returns if something can be moved
		for(TileEntity tile : tiles) {
			IFluidHandler handler = (IFluidHandler)tile;

			//See if we have anything to fill because redstone output
			FluidStack stack = handler.drain(1, false);
			if(stack == null || handler.fill(stack, false) > 0)
				rocketContainsItems = true;

			if(isAllowToOperate) {
				stack = fluidTank.drain(fluidTank.getCapacity(), false);
				if(stack != null && stack.amount > 0)
					fluidTank.drain(handler.fill(stack, true), true);
			}
		}

		//Update redstone state
		setRedstoneState(!rocketContainsItems);

	}
}
 
Example 10
Source File: GenericTank.java    From OpenModsLib with MIT License 5 votes vote down vote up
private static int tryFillNeighbour(FluidStack drainedFluid, EnumFacing side, TileEntity otherTank) {
	final FluidStack toFill = drainedFluid.copy();
	final EnumFacing fillSide = side.getOpposite();

	final IFluidHandler fluidHandler = CompatibilityUtils.getFluidHandler(otherTank, fillSide);
	return fluidHandler != null? fluidHandler.fill(toFill, true) : 0;
}
 
Example 11
Source File: ItemEnderBucket.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
public int getCapacityAvailable(ItemStack stack, FluidStack fluidStackIn, EntityPlayer player)
{
    FluidStack existingFluidStack;

    // Linked to a tank
    if (LinkMode.fromStack(stack) == LinkMode.ENABLED)
    {
        IFluidHandler handler = this.getLinkedTank(stack);

        if (handler != null)
        {
            IFluidTankProperties[] tank = handler.getTankProperties();

            if (tank == null || tank.length < 1 || tank[0] == null)
            {
                return 0;
            }

            existingFluidStack = tank[0].getContents();

            if (fluidStackIn == null)
            {
                return tank[0].getCapacity() - (existingFluidStack != null ? existingFluidStack.amount : 0);
            }
            else
            {
                fluidStackIn = fluidStackIn.copy();
                fluidStackIn.amount = Integer.MAX_VALUE;

                return handler.fill(fluidStackIn, false);
            }
        }

        return 0;
    }

    // Not linked to a tank, get the bucket's internal free capacity
    existingFluidStack = this.getFluid(stack, player);

    if (existingFluidStack != null)
    {
        return this.getCapacity(stack, player) - existingFluidStack.amount;
    }

    return this.getCapacity(stack, player);
}
 
Example 12
Source File: ItemEnderBucket.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
public int getCapacity(ItemStack stack, @Nullable EntityPlayer player)
{
    if (LinkMode.fromStack(stack) == LinkMode.DISABLED)
    {
        return Configs.enderBucketCapacity;
    }

    // Linked to a tank

    if (OwnerData.canAccessSelectedModule(stack, ModuleType.TYPE_LINKCRYSTAL, player) == false)
    {
        return 0;
    }

    TargetData targetData = this.getLinkedTankTargetData(stack);
    IFluidHandler handler = this.getLinkedTank(stack);

    if (targetData != null && handler != null)
    {
        IFluidTankProperties[] tank = handler.getTankProperties();

        // If we have tank info, it is the easiest and simplest way to get the tank capacity
        if (tank != null && tank.length > 0 && tank[0] != null)
        {
            return tank[0].getCapacity();
        }

        // No tank info available, get the capacity via simulating filling

        FluidStack fluidStack = handler.drain(Integer.MAX_VALUE, false);

        // Tank has fluid
        if (fluidStack != null)
        {
            FluidStack fs = fluidStack.copy();
            fs.amount = Integer.MAX_VALUE;

            // Filled amount plus existing amount
            return handler.fill(fs, false) + fluidStack.amount;
        }
        // Tank has no fluid
        else
        {
            // Since we have no fluid stored, get the capacity via simulating filling water into the tank
            return handler.fill(new FluidStack(FluidRegistry.WATER, Integer.MAX_VALUE), false);
        }
    }

    return 0;
}