Search in sources :

Example 16 with IFluidHandler

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

the class TileEntityBase method autoExportLiquid.

public void autoExportLiquid() {
    FluidStack extractedStack = ((IFluidHandler) this).drain(ForgeDirection.UNKNOWN, Integer.MAX_VALUE, false);
    if (extractedStack != null) {
        for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) {
            TileEntity te = getTileCache()[d.ordinal()].getTileEntity();
            if (te instanceof IFluidHandler) {
                if (((IFluidHandler) te).canFill(d.getOpposite(), extractedStack.getFluid())) {
                    int filledAmount = ((IFluidHandler) te).fill(d.getOpposite(), extractedStack, true);
                    ((IFluidHandler) this).drain(ForgeDirection.UNKNOWN, filledAmount, true);
                    extractedStack.amount -= filledAmount;
                    if (extractedStack.amount <= 0)
                        break;
                }
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) FluidStack(net.minecraftforge.fluids.FluidStack) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) TargetPoint(cpw.mods.fml.common.network.NetworkRegistry.TargetPoint)

Example 17 with IFluidHandler

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

the class TileEntityLiquidHopper method exportItem.

@Override
protected boolean exportItem(int maxItems) {
    ForgeDirection dir = ForgeDirection.getOrientation(getBlockMetadata());
    if (tank.getFluid() != null) {
        TileEntity neighbor = IOHelper.getNeighbor(this, dir);
        if (neighbor instanceof IFluidHandler) {
            IFluidHandler fluidHandler = (IFluidHandler) neighbor;
            if (fluidHandler.canFill(dir.getOpposite(), tank.getFluid().getFluid())) {
                FluidStack fluid = tank.getFluid().copy();
                fluid.amount = Math.min(maxItems * 100, tank.getFluid().amount - (leaveMaterial ? 1000 : 0));
                if (fluid.amount > 0) {
                    tank.getFluid().amount -= fluidHandler.fill(dir.getOpposite(), fluid, true);
                    if (tank.getFluidAmount() <= 0)
                        tank.setFluid(null);
                    return true;
                }
            }
        }
    }
    if (worldObj.isAirBlock(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ)) {
        for (EntityItem entity : getNeighborItems(this, dir)) {
            if (!entity.isDead) {
                List<ItemStack> returnedItems = new ArrayList<ItemStack>();
                if (FluidUtils.tryExtractingLiquid(this, entity.getEntityItem(), returnedItems)) {
                    if (entity.getEntityItem().stackSize <= 0)
                        entity.setDead();
                    for (ItemStack stack : returnedItems) {
                        EntityItem item = new EntityItem(worldObj, entity.posX, entity.posY, entity.posZ, stack);
                        item.motionX = entity.motionX;
                        item.motionY = entity.motionY;
                        item.motionZ = entity.motionZ;
                        worldObj.spawnEntityInWorld(item);
                    }
                    return true;
                }
            }
        }
    }
    if (getUpgrades(ItemMachineUpgrade.UPGRADE_DISPENSER_DAMAGE) > 0) {
        if (worldObj.isAirBlock(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ)) {
            FluidStack extractedFluid = drain(ForgeDirection.UNKNOWN, 1000, false);
            if (extractedFluid != null && extractedFluid.amount == 1000) {
                Block fluidBlock = extractedFluid.getFluid().getBlock();
                if (fluidBlock != null) {
                    drain(ForgeDirection.UNKNOWN, 1000, true);
                    worldObj.setBlock(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ, fluidBlock);
                }
            }
        }
    }
    return false;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) FluidStack(net.minecraftforge.fluids.FluidStack) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ArrayList(java.util.ArrayList) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) EntityItem(net.minecraft.entity.item.EntityItem)

Example 18 with IFluidHandler

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

the class FluidUtils method tryExtractingLiquid.

public static boolean tryExtractingLiquid(TileEntity te, ItemStack liquidContainer, List<ItemStack> returnedItems) {
    if (te instanceof IFluidHandler) {
        IFluidHandler fluidHandler = (IFluidHandler) te;
        if (liquidContainer != null) {
            int containerCapacity = FluidContainerRegistry.getContainerCapacity(liquidContainer);
            if (containerCapacity > 0 || liquidContainer.getItem() == Items.bucket) {
                if (containerCapacity == 0)
                    containerCapacity = 1000;
                FluidStack extractedLiquid = fluidHandler.drain(ForgeDirection.UNKNOWN, containerCapacity, false);
                if (extractedLiquid != null && extractedLiquid.amount == containerCapacity) {
                    ItemStack filledContainer = FluidContainerRegistry.fillFluidContainer(extractedLiquid, liquidContainer);
                    if (filledContainer != null) {
                        fluidHandler.drain(ForgeDirection.UNKNOWN, containerCapacity, true);
                        liquidContainer.stackSize--;
                        returnedItems.add(filledContainer.copy());
                        return true;
                    }
                }
            } else if (liquidContainer.getItem() instanceof IFluidContainerItem) {
                IFluidContainerItem container = (IFluidContainerItem) liquidContainer.getItem();
                ItemStack singleItem = liquidContainer.copy();
                singleItem.stackSize = 1;
                FluidStack extractedLiquid = fluidHandler.drain(ForgeDirection.UNKNOWN, container.getCapacity(singleItem), false);
                if (extractedLiquid != null) {
                    int filledAmount = container.fill(singleItem, extractedLiquid, true);
                    if (filledAmount > 0) {
                        liquidContainer.stackSize--;
                        returnedItems.add(singleItem);
                        FluidStack fluid = extractedLiquid.copy();
                        fluid.amount = filledAmount;
                        fluidHandler.drain(ForgeDirection.UNKNOWN, fluid, true);
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : IFluidContainerItem(net.minecraftforge.fluids.IFluidContainerItem) FluidStack(net.minecraftforge.fluids.FluidStack) ItemStack(net.minecraft.item.ItemStack) IFluidHandler(net.minecraftforge.fluids.IFluidHandler)

Example 19 with IFluidHandler

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

the class DroneAILiquidExport method fillTank.

private boolean fillTank(ChunkPosition pos, boolean simulate) {
    if (drone.getTank().getFluidAmount() == 0) {
        drone.addDebugEntry("gui.progWidget.liquidExport.debug.emptyDroneTank");
        abort();
        return false;
    } else {
        TileEntity te = drone.getWorld().getTileEntity(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ);
        if (te instanceof IFluidHandler) {
            IFluidHandler tank = (IFluidHandler) te;
            FluidStack exportedFluid = drone.getTank().drain(Integer.MAX_VALUE, false);
            if (exportedFluid != null && ((ILiquidFiltered) widget).isFluidValid(exportedFluid.getFluid())) {
                for (int i = 0; i < 6; i++) {
                    if (((ISidedWidget) widget).getSides()[i]) {
                        int filledAmount = tank.fill(ForgeDirection.getOrientation(i), exportedFluid, false);
                        if (filledAmount > 0) {
                            if (((ICountWidget) widget).useCount())
                                filledAmount = Math.min(filledAmount, getRemainingCount());
                            if (!simulate) {
                                decreaseCount(tank.fill(ForgeDirection.getOrientation(i), drone.getTank().drain(filledAmount, true), true));
                            }
                            return true;
                        }
                    }
                }
                drone.addDebugEntry("gui.progWidget.liquidExport.debug.filledToMax", pos);
            } else {
                drone.addDebugEntry("gui.progWidget.liquidExport.debug.noValidFluid");
            }
        } else if (((ILiquidExport) widget).isPlacingFluidBlocks() && (!((ICountWidget) widget).useCount() || getRemainingCount() >= 1000)) {
            Block fluidBlock = drone.getTank().getFluid().getFluid().getBlock();
            if (drone.getTank().getFluidAmount() >= 1000 && fluidBlock != null && drone.getWorld().isAirBlock(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ)) {
                if (!simulate) {
                    decreaseCount(1000);
                    drone.getTank().drain(1000, true);
                    drone.getWorld().setBlock(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ, fluidBlock);
                }
                return true;
            }
        }
        return false;
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) FluidStack(net.minecraftforge.fluids.FluidStack) ICountWidget(pneumaticCraft.common.progwidgets.ICountWidget) ILiquidExport(pneumaticCraft.common.progwidgets.ILiquidExport) Block(net.minecraft.block.Block) IFluidHandler(net.minecraftforge.fluids.IFluidHandler)

Example 20 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)

Aggregations

IFluidHandler (net.minecraftforge.fluids.IFluidHandler)33 FluidStack (net.minecraftforge.fluids.FluidStack)26 TileEntity (net.minecraft.tileentity.TileEntity)24 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)19 ItemStack (net.minecraft.item.ItemStack)12 FluidTankInfo (net.minecraftforge.fluids.FluidTankInfo)11 IInventory (net.minecraft.inventory.IInventory)8 FluidIdentifier (logisticspipes.utils.FluidIdentifier)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 ISpecialTankAccessHandler (logisticspipes.interfaces.ISpecialTankAccessHandler)4 ISpecialTankHandler (logisticspipes.interfaces.ISpecialTankHandler)4 PipeFluidTransportLogistics (logisticspipes.transport.PipeFluidTransportLogistics)4 Item (net.minecraft.item.Item)4 SimpleServiceLocator (logisticspipes.proxy.SimpleServiceLocator)3 Textures (logisticspipes.textures.Textures)3 TextureType (logisticspipes.textures.Textures.TextureType)3 Pair (logisticspipes.utils.tuples.Pair)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 ChunkPosition (net.minecraft.world.ChunkPosition)3