Search in sources :

Example 1 with SidedConsumer

use of mcjty.xnet.api.keys.SidedConsumer in project RFTools by McJty.

the class StorageChannelSettings method tick.

@Override
public void tick(int channel, IControllerContext context) {
    if (updateCache(channel, context)) {
        // If cache was updated we send new state immediatelly
        delay = 0;
    }
    delay--;
    if (delay > 0) {
        return;
    }
    delay = 10;
    World world = context.getControllerWorld();
    for (Pair<SidedConsumer, StorageConnectorSettings> entry : storageControllers) {
        BlockPos extractorPos = context.findConsumerPosition(entry.getKey().getConsumerId());
        if (extractorPos != null) {
            EnumFacing side = entry.getKey().getSide();
            BlockPos pos = extractorPos.offset(side);
            if (!WorldTools.chunkLoaded(world, pos)) {
                continue;
            }
            TileEntity te = world.getTileEntity(pos);
            if (te instanceof StorageScannerTileEntity) {
                StorageScannerTileEntity scanner = (StorageScannerTileEntity) te;
                scanner.register(access);
            }
        }
    }
}
Also used : StorageScannerTileEntity(mcjty.rftools.blocks.storagemonitor.StorageScannerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) StorageScannerTileEntity(mcjty.rftools.blocks.storagemonitor.StorageScannerTileEntity)

Example 2 with SidedConsumer

use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.

the class TileEntityController method removeConnector.

private void removeConnector(int channel, SidedPos pos) {
    WorldBlob worldBlob = XNetBlobData.getBlobData(getWorld()).getWorldBlob(getWorld());
    ConsumerId consumerId = worldBlob.getConsumerAt(pos.getPos().offset(pos.getSide()));
    SidedConsumer toremove = null;
    for (Map.Entry<SidedConsumer, ConnectorInfo> entry : channels[channel].getConnectors().entrySet()) {
        SidedConsumer key = entry.getKey();
        if (key.getSide().getOpposite().equals(pos.getSide())) {
            if (key.getConsumerId().equals(consumerId)) {
                toremove = key;
                break;
            }
        }
    }
    if (toremove != null) {
        channels[channel].getConnectors().remove(toremove);
        networkDirty();
        markDirtyQuick();
    }
}
Also used : ConnectorInfo(mcjty.xnet.clientinfo.ConnectorInfo) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) ConsumerId(mcjty.xnet.api.keys.ConsumerId) WorldBlob(mcjty.xnet.multiblock.WorldBlob)

Example 3 with SidedConsumer

use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.

the class FluidChannelSettings method insertFluidSimulate.

// Returns what could not be filled
private int insertFluidSimulate(@Nonnull List<Pair<SidedConsumer, FluidConnectorSettings>> inserted, @Nonnull IControllerContext context, @Nonnull FluidStack stack) {
    World world = context.getControllerWorld();
    if (channelMode == ChannelMode.PRIORITY) {
        // Always start at 0
        roundRobinOffset = 0;
    }
    int amount = stack.amount;
    for (int j = 0; j < fluidConsumers.size(); j++) {
        int i = (j + roundRobinOffset) % fluidConsumers.size();
        Pair<SidedConsumer, FluidConnectorSettings> entry = fluidConsumers.get(i);
        FluidConnectorSettings settings = entry.getValue();
        if (settings.getMatcher() == null || settings.getMatcher().equals(stack)) {
            BlockPos consumerPos = context.findConsumerPosition(entry.getKey().getConsumerId());
            if (consumerPos != null) {
                if (!WorldTools.chunkLoaded(world, consumerPos)) {
                    continue;
                }
                if (checkRedstone(world, settings, consumerPos)) {
                    continue;
                }
                if (!context.matchColor(settings.getColorsMask())) {
                    continue;
                }
                EnumFacing side = entry.getKey().getSide();
                BlockPos pos = consumerPos.offset(side);
                TileEntity te = world.getTileEntity(pos);
                IFluidHandler handler = getFluidHandlerAt(te, settings.getFacing());
                // @todo report error somewhere?
                if (handler != null) {
                    int toinsert = Math.min(settings.getRate(), amount);
                    Integer count = settings.getMinmax();
                    if (count != null) {
                        int a = countFluid(handler, settings.getMatcher());
                        int caninsert = count - a;
                        if (caninsert <= 0) {
                            continue;
                        }
                        toinsert = Math.min(toinsert, caninsert);
                    }
                    FluidStack copy = stack.copy();
                    copy.amount = toinsert;
                    int filled = handler.fill(copy, false);
                    if (filled > 0) {
                        inserted.add(entry);
                        amount -= filled;
                        if (amount <= 0) {
                            return 0;
                        }
                    }
                }
            }
        }
    }
    return amount;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) FluidStack(net.minecraftforge.fluids.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 4 with SidedConsumer

use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.

the class FluidChannelSettings method tick.

@Override
public void tick(int channel, IControllerContext context) {
    delay--;
    if (delay <= 0) {
        // Multiply of the different speeds we have
        delay = 200 * 6;
    }
    if (delay % 10 != 0) {
        return;
    }
    int d = delay / 10;
    updateCache(channel, context);
    // @todo optimize
    World world = context.getControllerWorld();
    for (Map.Entry<SidedConsumer, FluidConnectorSettings> entry : fluidExtractors.entrySet()) {
        FluidConnectorSettings settings = entry.getValue();
        if (d % settings.getSpeed() != 0) {
            continue;
        }
        BlockPos extractorPos = context.findConsumerPosition(entry.getKey().getConsumerId());
        if (extractorPos != null) {
            EnumFacing side = entry.getKey().getSide();
            BlockPos pos = extractorPos.offset(side);
            if (!WorldTools.chunkLoaded(world, pos)) {
                continue;
            }
            TileEntity te = world.getTileEntity(pos);
            IFluidHandler handler = getFluidHandlerAt(te, settings.getFacing());
            // @todo report error somewhere?
            if (handler != null) {
                if (checkRedstone(world, settings, extractorPos)) {
                    continue;
                }
                if (!context.matchColor(settings.getColorsMask())) {
                    continue;
                }
                FluidStack extractMatcher = settings.getMatcher();
                int toextract = settings.getRate();
                Integer count = settings.getMinmax();
                if (count != null) {
                    int amount = countFluid(handler, extractMatcher);
                    int canextract = amount - count;
                    if (canextract <= 0) {
                        continue;
                    }
                    toextract = Math.min(toextract, canextract);
                }
                FluidStack stack = fetchFluid(handler, true, extractMatcher, toextract);
                if (stack != null) {
                    List<Pair<SidedConsumer, FluidConnectorSettings>> inserted = new ArrayList<>();
                    int remaining = insertFluidSimulate(inserted, context, stack);
                    if (!inserted.isEmpty()) {
                        if (context.checkAndConsumeRF(GeneralConfiguration.controllerOperationRFT)) {
                            insertFluidReal(context, inserted, fetchFluid(handler, false, extractMatcher, stack.amount - remaining));
                        }
                    }
                }
            }
        }
    }
}
Also used : SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) FluidStack(net.minecraftforge.fluids.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) World(net.minecraft.world.World) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) BlockPos(net.minecraft.util.math.BlockPos) HashMap(java.util.HashMap) Map(java.util.Map) Pair(org.apache.commons.lang3.tuple.Pair)

Example 5 with SidedConsumer

use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.

the class ItemChannelSettings method tick.

@Override
public void tick(int channel, IControllerContext context) {
    delay--;
    if (delay <= 0) {
        // Multiply of the different speeds we have
        delay = 200 * 6;
    }
    if (delay % 5 != 0) {
        return;
    }
    int d = delay / 5;
    updateCache(channel, context);
    World world = context.getControllerWorld();
    for (Map.Entry<SidedConsumer, ItemConnectorSettings> entry : itemExtractors.entrySet()) {
        ItemConnectorSettings settings = entry.getValue();
        if (d % settings.getSpeed() != 0) {
            continue;
        }
        ConsumerId consumerId = entry.getKey().getConsumerId();
        BlockPos extractorPos = context.findConsumerPosition(consumerId);
        if (extractorPos != null) {
            EnumFacing side = entry.getKey().getSide();
            BlockPos pos = extractorPos.offset(side);
            if (!WorldTools.chunkLoaded(world, pos)) {
                continue;
            }
            if (checkRedstone(world, settings, extractorPos)) {
                return;
            }
            if (!context.matchColor(settings.getColorsMask())) {
                return;
            }
            TileEntity te = world.getTileEntity(pos);
            if (XNet.rftools && RFToolsSupport.isStorageScanner(te)) {
                RFToolsSupport.tickStorageScanner(context, settings, te, this);
            } else {
                IItemHandler handler = getItemHandlerAt(te, settings.getFacing());
                if (handler != null) {
                    int idx = getStartExtractIndex(settings, consumerId, handler);
                    idx = tickItemHandler(context, settings, handler, idx);
                    if (handler.getSlots() > 0) {
                        rememberExtractIndex(consumerId, (idx + 1) % handler.getSlots());
                    }
                }
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IItemHandler(net.minecraftforge.items.IItemHandler) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) EnumFacing(net.minecraft.util.EnumFacing) ConsumerId(mcjty.xnet.api.keys.ConsumerId) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World)

Aggregations

SidedConsumer (mcjty.xnet.api.keys.SidedConsumer)20 BlockPos (net.minecraft.util.math.BlockPos)14 EnumFacing (net.minecraft.util.EnumFacing)13 TileEntity (net.minecraft.tileentity.TileEntity)10 World (net.minecraft.world.World)8 ConnectorInfo (mcjty.xnet.clientinfo.ConnectorInfo)6 WorldBlob (mcjty.xnet.multiblock.WorldBlob)6 ConsumerId (mcjty.xnet.api.keys.ConsumerId)5 HashMap (java.util.HashMap)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 Map (java.util.Map)3 Nonnull (javax.annotation.Nonnull)3 ConnectorTileEntity (mcjty.xnet.blocks.cables.ConnectorTileEntity)3 NBTTagList (net.minecraft.nbt.NBTTagList)3 FluidStack (net.minecraftforge.fluids.FluidStack)3 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)3 IItemHandler (net.minecraftforge.items.IItemHandler)3 ArrayList (java.util.ArrayList)2 Argument (mcjty.lib.network.Argument)2 IChannelType (mcjty.xnet.api.channels.IChannelType)2