Search in sources :

Example 6 with SidedConsumer

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

the class ItemChannelSettings method insertStackSimulate.

// Returns what could not be inserted
public int insertStackSimulate(@Nonnull List<Pair<SidedConsumer, ItemConnectorSettings>> inserted, @Nonnull IControllerContext context, @Nonnull ItemStack stack) {
    World world = context.getControllerWorld();
    if (channelMode == ChannelMode.PRIORITY) {
        // Always start at 0
        roundRobinOffset = 0;
    }
    int total = stack.getCount();
    for (int j = 0; j < itemConsumers.size(); j++) {
        int i = (j + roundRobinOffset) % itemConsumers.size();
        Pair<SidedConsumer, ItemConnectorSettings> entry = itemConsumers.get(i);
        ItemConnectorSettings settings = entry.getValue();
        if (settings.getMatcher().test(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);
                int actuallyinserted;
                int toinsert = total;
                ItemStack remaining;
                Integer count = settings.getCount();
                if (XNet.rftools && RFToolsSupport.isStorageScanner(te)) {
                    if (count != null) {
                        int amount = RFToolsSupport.countItems(te, settings.getMatcher(), count);
                        int caninsert = count - amount;
                        if (caninsert <= 0) {
                            continue;
                        }
                        toinsert = Math.min(toinsert, caninsert);
                        stack = stack.copy();
                        if (toinsert <= 0) {
                            stack.setCount(0);
                        } else {
                            stack.setCount(toinsert);
                        }
                    }
                    remaining = RFToolsSupport.insertItem(te, stack, true);
                } else {
                    IItemHandler handler = getItemHandlerAt(te, settings.getFacing());
                    if (handler != null) {
                        if (count != null) {
                            int amount = countItems(handler, settings.getMatcher());
                            int caninsert = count - amount;
                            if (caninsert <= 0) {
                                continue;
                            }
                            toinsert = Math.min(toinsert, caninsert);
                            stack = stack.copy();
                            if (toinsert <= 0) {
                                stack.setCount(0);
                            } else {
                                stack.setCount(toinsert);
                            }
                        }
                        remaining = ItemHandlerHelper.insertItem(handler, stack, true);
                    } else {
                        continue;
                    }
                }
                actuallyinserted = toinsert - remaining.getCount();
                if (count == null) {
                    // If we are not using a count then we restore 'stack' here as that is what
                    // we actually have to keep inserting until it is empty. If we are using a count
                    // then we don't do this as we don't want to risk stack getting null (on 1.10.2)
                    // from the insertItem() and then not being able to set stacksize a few lines
                    // above this
                    stack = remaining;
                }
                if (actuallyinserted > 0) {
                    inserted.add(entry);
                    total -= actuallyinserted;
                    if (total <= 0) {
                        return 0;
                    }
                }
            }
        }
    }
    return total;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IItemHandler(net.minecraftforge.items.IItemHandler) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack)

Example 7 with SidedConsumer

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

the class ItemChannelSettings method insertStackReal.

public void insertStackReal(@Nonnull IControllerContext context, @Nonnull List<Pair<SidedConsumer, ItemConnectorSettings>> inserted, @Nonnull ItemStack stack) {
    int total = stack.getCount();
    for (Pair<SidedConsumer, ItemConnectorSettings> entry : inserted) {
        BlockPos consumerPosition = context.findConsumerPosition(entry.getKey().getConsumerId());
        EnumFacing side = entry.getKey().getSide();
        ItemConnectorSettings settings = entry.getValue();
        BlockPos pos = consumerPosition.offset(side);
        TileEntity te = context.getControllerWorld().getTileEntity(pos);
        if (XNet.rftools && RFToolsSupport.isStorageScanner(te)) {
            int toinsert = total;
            Integer count = settings.getCount();
            if (count != null) {
                int amount = RFToolsSupport.countItems(te, settings.getMatcher(), count);
                int caninsert = count - amount;
                if (caninsert <= 0) {
                    continue;
                }
                toinsert = Math.min(toinsert, caninsert);
                stack = stack.copy();
                if (toinsert <= 0) {
                    stack.setCount(0);
                } else {
                    stack.setCount(toinsert);
                }
            }
            ItemStack remaining = RFToolsSupport.insertItem(te, stack, false);
            int actuallyinserted = toinsert - remaining.getCount();
            if (count == null) {
                // If we are not using a count then we restore 'stack' here as that is what
                // we actually have to keep inserting until it is empty. If we are using a count
                // then we don't do this as we don't want to risk stack getting null (on 1.10.2)
                // from the insertItem() and then not being able to set stacksize a few lines
                // above this
                stack = remaining;
            }
            if (actuallyinserted > 0) {
                roundRobinOffset = (roundRobinOffset + 1) % itemConsumers.size();
                total -= actuallyinserted;
                if (total <= 0) {
                    return;
                }
            }
        } else {
            IItemHandler handler = getItemHandlerAt(te, settings.getFacing());
            int toinsert = total;
            Integer count = settings.getCount();
            if (count != null) {
                int amount = countItems(handler, settings.getMatcher());
                int caninsert = count - amount;
                if (caninsert <= 0) {
                    continue;
                }
                toinsert = Math.min(toinsert, caninsert);
                stack = stack.copy();
                if (toinsert <= 0) {
                    stack.setCount(0);
                } else {
                    stack.setCount(toinsert);
                }
            }
            ItemStack remaining = ItemHandlerHelper.insertItem(handler, stack, false);
            int actuallyinserted = toinsert - remaining.getCount();
            if (count == null) {
                // If we are not using a count then we restore 'stack' here as that is what
                // we actually have to keep inserting until it is empty. If we are using a count
                // then we don't do this as we don't want to risk stack getting null (on 1.10.2)
                // from the insertItem() and then not being able to set stacksize a few lines
                // above this
                stack = remaining;
            }
            if (actuallyinserted > 0) {
                roundRobinOffset = (roundRobinOffset + 1) % itemConsumers.size();
                total -= actuallyinserted;
                if (total <= 0) {
                    return;
                }
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IItemHandler(net.minecraftforge.items.IItemHandler) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 8 with SidedConsumer

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

the class EnergyChannelSettings method tick.

@Override
public void tick(int channel, IControllerContext context) {
    updateCache(channel, context);
    World world = context.getControllerWorld();
    // First find out how much energy we have to distribute in total
    int totalToDistribute = 0;
    // Keep track of the connectors we already got energy from and how much energy we
    // got from it
    Map<BlockPos, Integer> alreadyHandled = new HashMap<>();
    List<Pair<ConnectorTileEntity, Integer>> energyProducers = new ArrayList<>();
    for (Pair<SidedConsumer, EnergyConnectorSettings> entry : energyExtractors) {
        BlockPos connectorPos = context.findConsumerPosition(entry.getKey().getConsumerId());
        if (connectorPos != null) {
            EnumFacing side = entry.getKey().getSide();
            BlockPos energyPos = connectorPos.offset(side);
            if (!WorldTools.chunkLoaded(world, energyPos)) {
                continue;
            }
            TileEntity te = world.getTileEntity(energyPos);
            // @todo report error somewhere?
            if (isEnergyTE(te, side.getOpposite())) {
                EnergyConnectorSettings settings = entry.getValue();
                ConnectorTileEntity connectorTE = (ConnectorTileEntity) world.getTileEntity(connectorPos);
                if (checkRedstone(world, settings, connectorPos)) {
                    continue;
                }
                if (!context.matchColor(settings.getColorsMask())) {
                    continue;
                }
                Integer count = settings.getMinmax();
                if (count != null) {
                    int level = getEnergyLevel(te, side.getOpposite());
                    if (level < count) {
                        continue;
                    }
                }
                Integer rate = settings.getRate();
                if (rate == null) {
                    boolean advanced = ConnectorBlock.isAdvancedConnector(world, connectorPos);
                    rate = advanced ? GeneralConfiguration.maxRfRateAdvanced : GeneralConfiguration.maxRfRateNormal;
                }
                connectorTE.setEnergyInputFrom(side, rate);
                if (!alreadyHandled.containsKey(connectorPos)) {
                    // We did not handle this connector yet. Remember the amount of energy in it
                    alreadyHandled.put(connectorPos, connectorTE.getEnergy());
                }
                // Check how much energy we can still send from that connector
                int connectorEnergy = alreadyHandled.get(connectorPos);
                int tosend = Math.min(rate, connectorEnergy);
                if (tosend > 0) {
                    // Decrease the energy from our temporary datastructure
                    alreadyHandled.put(connectorPos, connectorEnergy - tosend);
                    totalToDistribute += tosend;
                    energyProducers.add(Pair.of(connectorTE, tosend));
                }
            }
        }
    }
    if (totalToDistribute <= 0) {
        // Nothing to do
        return;
    }
    if (!context.checkAndConsumeRF(GeneralConfiguration.controllerOperationRFT)) {
        // Not enough energy for this operation
        return;
    }
    int actuallyConsumed = insertEnergy(context, totalToDistribute);
    if (actuallyConsumed <= 0) {
        // Nothing was done
        return;
    }
    // Now we need to actually fetch the energy from the producers
    for (Pair<ConnectorTileEntity, Integer> entry : energyProducers) {
        ConnectorTileEntity connectorTE = entry.getKey();
        int amount = entry.getValue();
        int actuallySpent = Math.min(amount, actuallyConsumed);
        connectorTE.setEnergy(connectorTE.getEnergy() - actuallySpent);
        actuallyConsumed -= actuallySpent;
        if (actuallyConsumed <= 0) {
            break;
        }
    }
}
Also used : HashMap(java.util.HashMap) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) World(net.minecraft.world.World) ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BlockPos(net.minecraft.util.math.BlockPos) Pair(org.apache.commons.lang3.tuple.Pair)

Example 9 with SidedConsumer

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

the class ChannelInfo method writeToNBT.

public void writeToNBT(NBTTagCompound tag) {
    channelSettings.writeToNBT(tag);
    tag.setBoolean("enabled", enabled);
    if (channelName != null && !channelName.isEmpty()) {
        tag.setString("name", channelName);
    }
    NBTTagList conlist = new NBTTagList();
    for (Map.Entry<SidedConsumer, ConnectorInfo> entry : connectors.entrySet()) {
        NBTTagCompound tc = new NBTTagCompound();
        ConnectorInfo connectorInfo = entry.getValue();
        connectorInfo.writeToNBT(tc);
        tc.setInteger("consumerId", entry.getKey().getConsumerId().getId());
        tc.setInteger("side", entry.getKey().getSide().ordinal());
        tc.setString("type", connectorInfo.getType().getID());
        tc.setBoolean("advanced", connectorInfo.isAdvanced());
        conlist.appendTag(tc);
    }
    tag.setTag("connectors", conlist);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) ConnectorInfo(mcjty.xnet.clientinfo.ConnectorInfo) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with SidedConsumer

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

the class TileEntityRouter method addRoutedConnectors.

public void addRoutedConnectors(Map<SidedConsumer, IConnectorSettings> connectors, @Nonnull BlockPos controllerPos, int channel, IChannelType type) {
    if (inError()) {
        // We are in error. Don't do anything
        return;
    }
    LocalChannelId id = new LocalChannelId(controllerPos, channel);
    String publishedName = publishedChannels.get(id);
    if (publishedName != null && !publishedName.isEmpty()) {
        NetworkId networkId = findRoutingNetwork();
        if (networkId != null) {
            LogicTools.consumers(getWorld(), networkId).forEach(consumerPos -> LogicTools.routers(getWorld(), consumerPos).forEach(router -> router.addConnectorsFromConnectedNetworks(connectors, publishedName, type)));
        } else {
            // If there is no routing network that means we have a local network only
            addConnectorsFromConnectedNetworks(connectors, publishedName, type);
        }
    }
}
Also used : GeneralConfiguration(mcjty.xnet.config.GeneralConfiguration) java.util(java.util) SPacketUpdateTileEntity(net.minecraft.network.play.server.SPacketUpdateTileEntity) IConnectorSettings(mcjty.xnet.api.channels.IConnectorSettings) Constants(net.minecraftforge.common.util.Constants) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) ChannelInfo(mcjty.xnet.logic.ChannelInfo) NetworkManager(net.minecraft.network.NetworkManager) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) BlockPosTools(mcjty.lib.varia.BlockPosTools) NBTTagList(net.minecraft.nbt.NBTTagList) Type(mcjty.typed.Type) ControllerChannelClientInfo(mcjty.xnet.clientinfo.ControllerChannelClientInfo) GenericTileEntity(mcjty.lib.entity.GenericTileEntity) XNetBlobData(mcjty.xnet.multiblock.XNetBlobData) WorldBlob(mcjty.xnet.multiblock.WorldBlob) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) LogicTools(mcjty.xnet.logic.LogicTools) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) Argument(mcjty.lib.network.Argument) MAX_CHANNELS(mcjty.xnet.logic.ChannelInfo.MAX_CHANNELS) IChannelType(mcjty.xnet.api.channels.IChannelType) NetworkId(mcjty.xnet.api.keys.NetworkId) NetworkId(mcjty.xnet.api.keys.NetworkId)

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