Search in sources :

Example 41 with IFluidHandler

use of net.minecraftforge.fluids.IFluidHandler in project PneumaticCraft by MineMaarten.

the class FluidUtils method tryInsertingLiquid.

public static boolean tryInsertingLiquid(TileEntity te, ItemStack liquidContainer, boolean creative, List<ItemStack> returnedItems) {
    if (te instanceof IFluidHandler) {
        IFluidHandler fluidHandler = (IFluidHandler) te;
        if (liquidContainer != null) {
            FluidStack fluid = FluidContainerRegistry.getFluidForFilledItem(liquidContainer);
            if (fluid != null) {
                fluid.amount = 1000;
                if (fluidHandler.canFill(ForgeDirection.UNKNOWN, fluid.getFluid()) && fluidHandler.fill(ForgeDirection.UNKNOWN, fluid, false) == 1000) {
                    fluidHandler.fill(ForgeDirection.UNKNOWN, fluid, true);
                    if (!creative) {
                        liquidContainer.stackSize--;
                        ItemStack returnedItem = null;
                        FluidContainerData[] allFluidData = FluidContainerRegistry.getRegisteredFluidContainerData();
                        for (FluidContainerData fluidData : allFluidData) {
                            if (fluidData.filledContainer.isItemEqual(liquidContainer)) {
                                returnedItem = fluidData.emptyContainer;
                                break;
                            }
                        }
                        if (returnedItem != null) {
                            returnedItems.add(returnedItem.copy());
                        }
                    }
                    return true;
                }
            } else if (liquidContainer.getItem() instanceof IFluidContainerItem) {
                IFluidContainerItem container = (IFluidContainerItem) liquidContainer.getItem();
                fluid = container.getFluid(liquidContainer);
                if (fluid != null) {
                    fluid = fluid.copy();
                    if (fluidHandler.canFill(ForgeDirection.UNKNOWN, fluid.getFluid()) && fluidHandler.fill(ForgeDirection.UNKNOWN, fluid, false) == fluid.amount) {
                        ItemStack returnedItem = liquidContainer.copy();
                        returnedItem.stackSize = 1;
                        container.drain(returnedItem, fluid.amount, true);
                        fluidHandler.fill(ForgeDirection.UNKNOWN, fluid, true);
                        if (!creative) {
                            liquidContainer.stackSize--;
                            returnedItems.add(returnedItem.copy());
                        }
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : IFluidContainerItem(net.minecraftforge.fluids.IFluidContainerItem) FluidStack(net.minecraftforge.fluids.FluidStack) ItemStack(net.minecraft.item.ItemStack) FluidContainerData(net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData) IFluidHandler(net.minecraftforge.fluids.IFluidHandler)

Example 42 with IFluidHandler

use of net.minecraftforge.fluids.IFluidHandler in project PneumaticCraft by MineMaarten.

the class TileEntityBase method processFluidItem.

protected void processFluidItem(int inputSlot, int outputSlot) {
    IInventory inv = (IInventory) this;
    IFluidHandler fluidHandler = (IFluidHandler) this;
    FluidTankInfo tankInfo = fluidHandler.getTankInfo(ForgeDirection.UNKNOWN)[0];
    if (inv.getStackInSlot(inputSlot) != null) {
        ItemStack fluidContainer = inv.getStackInSlot(inputSlot);
        if (tankInfo.fluid == null || tankInfo.fluid.isFluidEqual(fluidContainer)) {
            FluidStack fluid = FluidContainerRegistry.getFluidForFilledItem(fluidContainer);
            int amount = FluidContainerRegistry.BUCKET_VOLUME;
            if (fluid == null) {
                if (fluidContainer.getItem() instanceof IFluidContainerItem) {
                    IFluidContainerItem containerItem = (IFluidContainerItem) fluidContainer.getItem();
                    fluid = containerItem.getFluid(fluidContainer);
                    if (fluid != null && fluidHandler.canFill(ForgeDirection.UNKNOWN, fluid.getFluid())) {
                        amount = fluid != null ? fluid.amount : 0;
                        int availableSpace = tankInfo.capacity - (tankInfo.fluid != null ? tankInfo.fluid.amount : 0);
                        if (availableSpace >= amount) {
                            ItemStack singleFuelItem = fluidContainer.copy();
                            singleFuelItem.stackSize = 1;
                            FluidStack drainedStack = containerItem.drain(singleFuelItem, availableSpace, true);
                            if (fluidContainer.stackSize == 1 || inv.getStackInSlot(outputSlot) == null || canStack(singleFuelItem, inv.getStackInSlot(outputSlot))) {
                                fluidHandler.fill(ForgeDirection.UNKNOWN, drainedStack, true);
                                if (fluidContainer.stackSize == 1) {
                                    inv.setInventorySlotContents(inputSlot, singleFuelItem);
                                } else {
                                    inv.getStackInSlot(inputSlot).stackSize--;
                                    if (inv.getStackInSlot(outputSlot) == null) {
                                        inv.setInventorySlotContents(outputSlot, singleFuelItem);
                                    } else {
                                        inv.getStackInSlot(outputSlot).stackSize++;
                                    }
                                }
                            }
                        }
                    }
                }
            } else if (fluidHandler.canFill(ForgeDirection.UNKNOWN, fluid.getFluid())) {
                if (tankInfo.capacity - (tankInfo.fluid != null ? tankInfo.fluid.amount : 0) >= amount) {
                    ItemStack returnedItem = null;
                    FluidContainerData[] allFluidData = FluidContainerRegistry.getRegisteredFluidContainerData();
                    for (FluidContainerData fluidData : allFluidData) {
                        if (fluidData.filledContainer.isItemEqual(fluidContainer)) {
                            returnedItem = fluidData.emptyContainer;
                            break;
                        }
                    }
                    if (returnedItem == null || inv.getStackInSlot(outputSlot) == null || canStack(returnedItem, inv.getStackInSlot(outputSlot))) {
                        if (returnedItem != null) {
                            if (inv.getStackInSlot(outputSlot) == null) {
                                inv.setInventorySlotContents(outputSlot, returnedItem.copy());
                            } else {
                                inv.getStackInSlot(outputSlot).stackSize += returnedItem.stackSize;
                            }
                        }
                        fluidHandler.fill(ForgeDirection.UNKNOWN, new FluidStack(fluid.getFluid(), amount, fluid.tag), true);
                        inv.getStackInSlot(inputSlot).stackSize--;
                        if (inv.getStackInSlot(inputSlot).stackSize <= 0)
                            inv.setInventorySlotContents(inputSlot, null);
                    }
                }
            }
        }
        if (fluidContainer.getItem() instanceof IFluidContainerItem) {
            if (((IFluidContainerItem) fluidContainer.getItem()).getFluid(fluidContainer) == null && (inv.getStackInSlot(outputSlot) == null || canStack(fluidContainer, inv.getStackInSlot(outputSlot)))) {
                if (inv.getStackInSlot(outputSlot) == null) {
                    inv.setInventorySlotContents(outputSlot, fluidContainer);
                } else {
                    inv.getStackInSlot(outputSlot).stackSize += fluidContainer.stackSize;
                }
                inv.setInventorySlotContents(inputSlot, null);
            }
        }
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) IFluidContainerItem(net.minecraftforge.fluids.IFluidContainerItem) FluidStack(net.minecraftforge.fluids.FluidStack) ItemStack(net.minecraft.item.ItemStack) FluidContainerData(net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) TargetPoint(cpw.mods.fml.common.network.NetworkRegistry.TargetPoint)

Example 43 with IFluidHandler

use of net.minecraftforge.fluids.IFluidHandler in project PneumaticCraft by MineMaarten.

the class ModuleLogistics method update.

@Override
public void update() {
    super.update();
    if (cachedFrame != null && cachedFrame.isInvalid())
        cachedFrame = null;
    if (!getTube().world().isRemote) {
        if (powered != getTube().getAirHandler().getPressure(null) >= MIN_PRESSURE) {
            powered = !powered;
            NetworkHandler.sendToAllAround(new PacketUpdateLogisticModule(this, 0), getTube().world());
        }
        if (--ticksUntilNextCycle <= 0) {
            LogisticsManager manager = new LogisticsManager();
            Map<SemiBlockLogistics, ModuleLogistics> frameToModuleMap = new HashMap<SemiBlockLogistics, ModuleLogistics>();
            for (TubeModule module : ModuleNetworkManager.getInstance().getConnectedModules(this)) {
                if (module instanceof ModuleLogistics) {
                    ModuleLogistics logistics = (ModuleLogistics) module;
                    if (logistics.getColorChannel() == getColorChannel()) {
                        // Make sure any connected module doesn't tick, set it to a 5 second timer. This is also a penalty value when no task is executed this tick.
                        logistics.ticksUntilNextCycle = 100;
                        if (logistics.hasPower() && logistics.getFrame() != null) {
                            frameToModuleMap.put(logistics.getFrame(), logistics);
                            manager.addLogisticFrame(logistics.getFrame());
                        }
                    }
                }
            }
            PriorityQueue<LogisticsTask> tasks = manager.getTasks(null);
            for (LogisticsTask task : tasks) {
                if (task.isStillValid(task.transportingItem != null ? task.transportingItem : task.transportingFluid.stack)) {
                    if (task.transportingItem != null) {
                        ItemStack remainder = IOHelper.insert(task.requester.getTileEntity(), task.transportingItem.copy(), true);
                        if (remainder == null || remainder.stackSize != task.transportingItem.stackSize) {
                            ItemStack toBeExtracted = task.transportingItem.copy();
                            if (remainder != null)
                                toBeExtracted.stackSize -= remainder.stackSize;
                            ItemStack extractedStack = IOHelper.extract(task.provider.getTileEntity(), toBeExtracted, true);
                            if (extractedStack != null) {
                                ModuleLogistics provider = frameToModuleMap.get(task.provider);
                                ModuleLogistics requester = frameToModuleMap.get(task.requester);
                                int airUsed = (int) (ITEM_TRANSPORT_COST * extractedStack.stackSize * Math.pow(PneumaticCraftUtils.distBetweenSq(provider.getTube().x(), provider.getTube().y(), provider.getTube().z(), requester.getTube().x(), requester.getTube().y(), requester.getTube().z()), 0.25));
                                if (requester.getTube().getAirHandler().getCurrentAir(null) > airUsed) {
                                    sendModuleUpdate(provider, true);
                                    sendModuleUpdate(requester, true);
                                    requester.getTube().getAirHandler().addAir(-airUsed, null);
                                    IOHelper.extract(task.provider.getTileEntity(), extractedStack, false);
                                    IOHelper.insert(task.requester.getTileEntity(), extractedStack, false);
                                    ticksUntilNextCycle = 20;
                                } else {
                                    sendModuleUpdate(provider, false);
                                    sendModuleUpdate(requester, false);
                                }
                            }
                        }
                    } else {
                        TileEntity providingTE = task.provider.getTileEntity();
                        TileEntity requestingTE = task.requester.getTileEntity();
                        if (providingTE instanceof IFluidHandler && requestingTE instanceof IFluidHandler) {
                            IFluidHandler provider = (IFluidHandler) task.provider.getTileEntity();
                            IFluidHandler requester = (IFluidHandler) task.requester.getTileEntity();
                            for (ForgeDirection di : ForgeDirection.VALID_DIRECTIONS) {
                                int amountFilled = requester.fill(di, task.transportingFluid.stack, false);
                                if (amountFilled > 0) {
                                    FluidStack drainingFluid = task.transportingFluid.stack.copy();
                                    drainingFluid.amount = amountFilled;
                                    FluidStack extractedFluid = null;
                                    ModuleLogistics p = frameToModuleMap.get(task.provider);
                                    ModuleLogistics r = frameToModuleMap.get(task.requester);
                                    int airUsed = 0;
                                    for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) {
                                        extractedFluid = provider.drain(d, drainingFluid, false);
                                        if (extractedFluid != null) {
                                            airUsed = (int) (FLUID_TRANSPORT_COST * extractedFluid.amount * PneumaticCraftUtils.distBetween(p.getTube().x(), p.getTube().y(), p.getTube().z(), r.getTube().x(), r.getTube().y(), r.getTube().z()));
                                            if (r.getTube().getAirHandler().getCurrentAir(null) > airUsed) {
                                                extractedFluid = provider.drain(d, drainingFluid, true);
                                                break;
                                            } else {
                                                sendModuleUpdate(p, false);
                                                sendModuleUpdate(r, false);
                                                extractedFluid = null;
                                                break;
                                            }
                                        }
                                    }
                                    if (extractedFluid != null) {
                                        sendModuleUpdate(p, true);
                                        sendModuleUpdate(r, true);
                                        r.getTube().getAirHandler().addAir(-airUsed, null);
                                        requester.fill(di, extractedFluid, true);
                                        ticksUntilNextCycle = 20;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    } else {
        if (ticksSinceAction >= 0) {
            ticksSinceAction++;
            if (ticksSinceAction > 3)
                ticksSinceAction = -1;
        }
        if (ticksSinceNotEnoughAir >= 0) {
            ticksSinceNotEnoughAir++;
            if (ticksSinceNotEnoughAir > 20)
                ticksSinceNotEnoughAir = -1;
        }
    }
}
Also used : LogisticsManager(pneumaticCraft.common.ai.LogisticsManager) SemiBlockLogistics(pneumaticCraft.common.semiblock.SemiBlockLogistics) HashMap(java.util.HashMap) FluidStack(net.minecraftforge.fluids.FluidStack) LogisticsTask(pneumaticCraft.common.ai.LogisticsManager.LogisticsTask) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) PacketUpdateLogisticModule(pneumaticCraft.common.network.PacketUpdateLogisticModule) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ItemStack(net.minecraft.item.ItemStack)

Example 44 with IFluidHandler

use of net.minecraftforge.fluids.IFluidHandler in project PneumaticCraft by MineMaarten.

the class ContainerAmadron method capShoppingAmount.

public int capShoppingAmount(int offerId, int wantedAmount, EntityPlayer player) {
    IInventory inv = ItemAmadronTablet.getItemProvider(player.getCurrentEquippedItem());
    IFluidHandler fluidHandler = ItemAmadronTablet.getLiquidProvider(player.getCurrentEquippedItem());
    return capShoppingAmount(offers.get(offerId), wantedAmount, inv, fluidHandler, this);
}
Also used : IInventory(net.minecraft.inventory.IInventory) IFluidHandler(net.minecraftforge.fluids.IFluidHandler)

Example 45 with IFluidHandler

use of net.minecraftforge.fluids.IFluidHandler in project ForestryMC by ForestryMC.

the class BlockBase method onBlockActivated.

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float par7, float par8, float par9) {
    if (getDefinition(world, x, y, z).onBlockActivated(world, x, y, z, player, side, par7, par8, par9)) {
        return true;
    }
    if (player.isSneaking()) {
        return false;
    }
    TileBase tile = (TileBase) world.getTileEntity(x, y, z);
    if (!Utils.isUseableByPlayer(player, tile)) {
        return false;
    }
    ItemStack current = player.getCurrentEquippedItem();
    if (current != null && current.getItem() != Items.bucket && tile instanceof IFluidHandler && tile.allowsAlteration(player)) {
        if (FluidHelper.handleRightClick((IFluidHandler) tile, ForgeDirection.getOrientation(side), player, true, tile.canDrainWithBucket())) {
            return true;
        }
    }
    if (!Proxies.common.isSimulating(world)) {
        return true;
    }
    if (tile.allowsViewing(player)) {
        tile.openGui(player, tile);
    } else {
        player.addChatMessage(new ChatComponentTranslation("for.chat.accesslocked", PlayerUtil.getOwnerName(tile)));
    }
    return true;
}
Also used : ChatComponentTranslation(net.minecraft.util.ChatComponentTranslation) ItemStack(net.minecraft.item.ItemStack) IFluidHandler(net.minecraftforge.fluids.IFluidHandler)

Aggregations

IFluidHandler (net.minecraftforge.fluids.IFluidHandler)46 TileEntity (net.minecraft.tileentity.TileEntity)35 FluidStack (net.minecraftforge.fluids.FluidStack)33 ItemStack (net.minecraft.item.ItemStack)13 EnumFacing (net.minecraft.util.EnumFacing)13 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)11 IInventory (net.minecraft.inventory.IInventory)9 FluidTankInfo (net.minecraftforge.fluids.FluidTankInfo)7 IPipeTile (buildcraft.api.transport.IPipeTile)5 ArrayList (java.util.ArrayList)5 Fluid (net.minecraftforge.fluids.Fluid)5 FluidIdentifier (logisticspipes.utils.FluidIdentifier)3 Block (net.minecraft.block.Block)3 IBlockState (net.minecraft.block.state.IBlockState)3 ChunkPosition (net.minecraft.world.ChunkPosition)3 IFluidContainerItem (net.minecraftforge.fluids.IFluidContainerItem)3 TargetPoint (cpw.mods.fml.common.network.NetworkRegistry.TargetPoint)2 HashMap (java.util.HashMap)2 IBufferTransmitter (micdoodle8.mods.galacticraft.api.transmission.tile.IBufferTransmitter)2 RadiationFluidTileEntity (minechem.radiation.RadiationFluidTileEntity)2