Search in sources :

Example 31 with TileEntity

use of net.minecraft.tileentity.TileEntity in project LogisticsPipes by RS485.

the class LogisticsTileGenericSubMultiBlock method getMainPipe.

public List<LogisticsTileGenericPipe> getMainPipe() {
    if (mainPipe == null) {
        mainPipe = new ArrayList<>();
        for (DoubleCoordinates pos : mainPipePos) {
            TileEntity tile = pos.getTileEntity(getWorldObj());
            if (tile instanceof LogisticsTileGenericPipe) {
                mainPipe.add((LogisticsTileGenericPipe) tile);
            }
        }
        mainPipe = Collections.unmodifiableList(mainPipe);
    }
    boolean allInvalid = true;
    for (LogisticsTileGenericPipe pipe : mainPipe) {
        if (!pipe.isInvalid()) {
            allInvalid = false;
            break;
        }
    }
    if (mainPipe.isEmpty() || allInvalid) {
        getWorldObj().setBlockToAir(xCoord, yCoord, zCoord);
    }
    if (mainPipe != null) {
        return mainPipe;
    }
    return Collections.emptyList();
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) DoubleCoordinates(network.rs485.logisticspipes.world.DoubleCoordinates)

Example 32 with TileEntity

use of net.minecraft.tileentity.TileEntity in project LogisticsPipes by RS485.

the class FluidRoutedPipe method endReached.

public boolean endReached(LPTravelingItemServer arrivingItem, TileEntity tile) {
    if (canInsertToTanks() && MainProxy.isServer(getWorld())) {
        getCacheHolder().trigger(CacheTypes.Inventory);
        if (arrivingItem.getItemIdentifierStack() == null || !(arrivingItem.getItemIdentifierStack().getItem().isFluidContainer())) {
            return false;
        }
        if (getRouter().getSimpleID() != arrivingItem.getDestination()) {
            return false;
        }
        int filled = 0;
        FluidStack liquid = SimpleServiceLocator.logisticsFluidManager.getFluidFromContainer(arrivingItem.getItemIdentifierStack());
        if (isConnectableTank(tile, arrivingItem.output, false)) {
            List<Pair<TileEntity, ForgeDirection>> adjTanks = getAdjacentTanks(false);
            //Try to put liquid into all adjacent tanks.
            for (Pair<TileEntity, ForgeDirection> pair : adjTanks) {
                IFluidHandler tank = (IFluidHandler) pair.getValue1();
                ForgeDirection dir = pair.getValue2();
                filled = tank.fill(dir.getOpposite(), liquid.copy(), true);
                liquid.amount -= filled;
                if (liquid.amount != 0) {
                    continue;
                }
                return true;
            }
            //Try inserting the liquid into the pipe side tank
            filled = ((PipeFluidTransportLogistics) transport).sideTanks[arrivingItem.output.ordinal()].fill(liquid, true);
            if (filled == liquid.amount) {
                return true;
            }
            liquid.amount -= filled;
        }
        //Try inserting the liquid into the pipe internal tank
        filled = ((PipeFluidTransportLogistics) transport).internalTank.fill(liquid, true);
        if (filled == liquid.amount) {
            return true;
        }
        //If liquids still exist,
        liquid.amount -= filled;
        //TODO: FIX THIS
        if (this instanceof IRequireReliableFluidTransport) {
            ((IRequireReliableFluidTransport) this).liquidNotInserted(FluidIdentifier.get(liquid), liquid.amount);
        }
        IRoutedItem routedItem = SimpleServiceLocator.routedItemHelper.createNewTravelItem(SimpleServiceLocator.logisticsFluidManager.getFluidContainer(liquid));
        Pair<Integer, Integer> replies = SimpleServiceLocator.logisticsFluidManager.getBestReply(liquid, getRouter(), routedItem.getJamList());
        int dest = replies.getValue1();
        routedItem.setDestination(dest);
        routedItem.setTransportMode(TransportMode.Passive);
        this.queueRoutedItem(routedItem, arrivingItem.output.getOpposite());
        return true;
    }
    return false;
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) PipeFluidTransportLogistics(logisticspipes.transport.PipeFluidTransportLogistics) IRequireReliableFluidTransport(logisticspipes.interfaces.routing.IRequireReliableFluidTransport) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) Pair(logisticspipes.utils.tuples.Pair)

Example 33 with TileEntity

use of net.minecraft.tileentity.TileEntity in project LogisticsPipes by RS485.

the class PipeFluidBasic method sinkAmount.

@Override
public int sinkAmount(FluidStack stack) {
    if (!guiOpenedBy.isEmpty()) {
        //Don't sink when the gui is open
        return 0;
    }
    FluidIdentifier ident = FluidIdentifier.get(stack);
    if (filterInv.getStackInSlot(0) == null) {
        return 0;
    }
    if (!ident.equals(FluidIdentifier.get(filterInv.getIDStackInSlot(0).getItem()))) {
        return 0;
    }
    int onTheWay = this.countOnRoute(ident);
    int freeSpace = -onTheWay;
    for (Pair<TileEntity, ForgeDirection> pair : getAdjacentTanks(true)) {
        FluidTank tank = ((PipeFluidTransportLogistics) transport).sideTanks[pair.getValue2().ordinal()];
        freeSpace += ident.getFreeSpaceInsideTank((IFluidHandler) pair.getValue1(), pair.getValue2().getOpposite());
        freeSpace += ident.getFreeSpaceInsideTank(tank);
        if (freeSpace >= stack.amount) {
            return stack.amount;
        }
    }
    return freeSpace;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) FluidTank(net.minecraftforge.fluids.FluidTank) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) FluidIdentifier(logisticspipes.utils.FluidIdentifier) IFluidHandler(net.minecraftforge.fluids.IFluidHandler)

Example 34 with TileEntity

use of net.minecraft.tileentity.TileEntity in project LogisticsPipes by RS485.

the class PipeFluidProvider method enabledUpdateEntity.

@Override
public void enabledUpdateEntity() {
    super.enabledUpdateEntity();
    if (!getFluidOrderManager().hasOrders(ResourceType.PROVIDER) || !isNthTick(6)) {
        return;
    }
    LogisticsFluidOrder order = getFluidOrderManager().peekAtTopRequest(ResourceType.PROVIDER);
    int amountToSend, attemptedAmount;
    amountToSend = attemptedAmount = Math.min(order.getAmount(), 5000);
    for (Pair<TileEntity, ForgeDirection> pair : getAdjacentTanks(false)) {
        if (amountToSend <= 0) {
            break;
        }
        boolean fallback = true;
        if (SimpleServiceLocator.specialTankHandler.hasHandlerFor(pair.getValue1())) {
            ISpecialTankHandler handler = SimpleServiceLocator.specialTankHandler.getTankHandlerFor(pair.getValue1());
            if (handler instanceof ISpecialTankAccessHandler) {
                fallback = false;
                FluidStack drained = ((ISpecialTankAccessHandler) handler).drainFrom(pair.getValue1(), order.getFluid(), amountToSend, false);
                if (drained != null && drained.amount > 0 && order.getFluid().equals(FluidIdentifier.get(drained))) {
                    drained = ((ISpecialTankAccessHandler) handler).drainFrom(pair.getValue1(), order.getFluid(), amountToSend, true);
                    int amount = drained.amount;
                    amountToSend -= amount;
                    ItemIdentifierStack stack = SimpleServiceLocator.logisticsFluidManager.getFluidContainer(drained);
                    IRoutedItem item = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stack);
                    item.setDestination(order.getRouter().getSimpleID());
                    item.setTransportMode(TransportMode.Active);
                    this.queueRoutedItem(item, pair.getValue2());
                    getFluidOrderManager().sendSuccessfull(amount, false, item);
                    if (amountToSend <= 0) {
                        break;
                    }
                }
            }
        }
        if (fallback) {
            FluidTankInfo[] tanks = ((IFluidHandler) pair.getValue1()).getTankInfo(pair.getValue2().getOpposite());
            if (tanks != null) {
                for (FluidTankInfo tank : tanks) {
                    if (tank == null) {
                        continue;
                    }
                    FluidStack liquid;
                    if ((liquid = tank.fluid) != null && liquid.getFluidID() != 0) {
                        if (order.getFluid().equals(FluidIdentifier.get(liquid))) {
                            int amount = Math.min(liquid.amount, amountToSend);
                            FluidStack drained = ((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), amount, false);
                            if (drained != null && drained.amount > 0 && order.getFluid().equals(FluidIdentifier.get(drained))) {
                                drained = ((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), amount, true);
                                while (drained.amount < amountToSend) {
                                    FluidStack addition = ((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), amountToSend - drained.amount, false);
                                    if (addition != null && addition.amount > 0 && order.getFluid().equals(FluidIdentifier.get(addition))) {
                                        addition = ((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), amountToSend - drained.amount, true);
                                        drained.amount += addition.amount;
                                    } else {
                                        break;
                                    }
                                }
                                amount = drained.amount;
                                amountToSend -= amount;
                                ItemIdentifierStack stack = SimpleServiceLocator.logisticsFluidManager.getFluidContainer(drained);
                                IRoutedItem item = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stack);
                                item.setDestination(order.getRouter().getSimpleID());
                                item.setTransportMode(TransportMode.Active);
                                this.queueRoutedItem(item, pair.getValue2());
                                getFluidOrderManager().sendSuccessfull(amount, false, item);
                                if (amountToSend <= 0) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (amountToSend >= attemptedAmount) {
        getFluidOrderManager().sendFailed();
    }
}
Also used : ISpecialTankHandler(logisticspipes.interfaces.ISpecialTankHandler) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) FluidStack(net.minecraftforge.fluids.FluidStack) LogisticsFluidOrder(logisticspipes.routing.order.LogisticsFluidOrder) ISpecialTankAccessHandler(logisticspipes.interfaces.ISpecialTankAccessHandler) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack)

Example 35 with TileEntity

use of net.minecraft.tileentity.TileEntity in project LogisticsPipes by RS485.

the class PipeFluidProvider method getAvailableFluids.

@Override
public Map<FluidIdentifier, Integer> getAvailableFluids() {
    Map<FluidIdentifier, Integer> map = new HashMap<>();
    for (Pair<TileEntity, ForgeDirection> pair : getAdjacentTanks(false)) {
        boolean fallback = true;
        if (SimpleServiceLocator.specialTankHandler.hasHandlerFor(pair.getValue1())) {
            ISpecialTankHandler handler = SimpleServiceLocator.specialTankHandler.getTankHandlerFor(pair.getValue1());
            if (handler instanceof ISpecialTankAccessHandler) {
                fallback = false;
                Map<FluidIdentifier, Long> tmp = ((ISpecialTankAccessHandler) handler).getAvailableLiquid(pair.getValue1());
                for (Entry<FluidIdentifier, Long> entry : tmp.entrySet()) {
                    if (map.containsKey(entry.getKey())) {
                        long addition = ((long) map.get(entry.getKey())) + entry.getValue();
                        map.put(entry.getKey(), addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition);
                    } else {
                        map.put(entry.getKey(), entry.getValue() > Integer.MAX_VALUE ? Integer.MAX_VALUE : entry.getValue().intValue());
                    }
                }
            }
        }
        if (fallback) {
            FluidTankInfo[] tanks = ((IFluidHandler) pair.getValue1()).getTankInfo(pair.getValue2().getOpposite());
            if (tanks != null) {
                for (FluidTankInfo tank : tanks) {
                    if (tank == null) {
                        continue;
                    }
                    FluidStack liquid;
                    if ((liquid = tank.fluid) != null && liquid.getFluidID() != 0) {
                        FluidIdentifier ident = FluidIdentifier.get(liquid);
                        if (((IFluidHandler) pair.getValue1()).canDrain(pair.getValue2().getOpposite(), liquid.getFluid())) {
                            if (((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), 1, false) != null) {
                                if (map.containsKey(ident)) {
                                    long addition = ((long) map.get(ident)) + tank.fluid.amount;
                                    map.put(ident, addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition);
                                } else {
                                    map.put(ident, tank.fluid.amount);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    Map<FluidIdentifier, Integer> result = new HashMap<>();
    //Reduce what has been reserved, add.
    for (Entry<FluidIdentifier, Integer> fluid : map.entrySet()) {
        int remaining = fluid.getValue() - getFluidOrderManager().totalFluidsCountInOrders(fluid.getKey());
        if (remaining < 1) {
            continue;
        }
        result.put(fluid.getKey(), remaining);
    }
    return result;
}
Also used : ISpecialTankHandler(logisticspipes.interfaces.ISpecialTankHandler) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) HashMap(java.util.HashMap) FluidStack(net.minecraftforge.fluids.FluidStack) ISpecialTankAccessHandler(logisticspipes.interfaces.ISpecialTankAccessHandler) FluidIdentifier(logisticspipes.utils.FluidIdentifier) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) ForgeDirection(net.minecraftforge.common.util.ForgeDirection)

Aggregations

TileEntity (net.minecraft.tileentity.TileEntity)822 ItemStack (net.minecraft.item.ItemStack)149 BlockPos (net.minecraft.util.math.BlockPos)130 Block (net.minecraft.block.Block)83 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)76 EnumFacing (net.minecraft.util.EnumFacing)62 ArrayList (java.util.ArrayList)61 IBlockState (net.minecraft.block.state.IBlockState)61 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)57 IInventory (net.minecraft.inventory.IInventory)49 DoubleCoordinates (network.rs485.logisticspipes.world.DoubleCoordinates)44 World (net.minecraft.world.World)43 EntityItem (net.minecraft.entity.item.EntityItem)39 LogisticsTileGenericPipe (logisticspipes.pipes.basic.LogisticsTileGenericPipe)35 FluidStack (net.minecraftforge.fluids.FluidStack)29 EntityPlayer (net.minecraft.entity.player.EntityPlayer)28 HashMap (java.util.HashMap)25 IFluidHandler (net.minecraftforge.fluids.IFluidHandler)23 AMVector3 (am2.api.math.AMVector3)21 Entity (net.minecraft.entity.Entity)21